示例#1
0
        public static void Normalize(string utf16Source, string utf16Expected, NormalizationForm normalizationForm)
        {
            using BoundedUtf8Span boundedSpan = new BoundedUtf8Span(utf16Source);
            Utf8Span utf8Source = boundedSpan.Span;

            // Quick IsNormalized tests

            Assert.Equal(utf16Source == utf16Expected, utf8Source.IsNormalized(normalizationForm));

            // Normalize and return new Utf8String instances

            ustring utf8Normalized = utf8Source.Normalize(normalizationForm);

            Assert.True(ustring.AreEquivalent(utf8Normalized, utf16Expected));

            // Normalize to byte arrays which are too small, expect -1 (failure)

            Assert.Equal(-1, utf8Source.Normalize(new byte[utf8Normalized.GetByteLength() - 1], normalizationForm));

            // Normalize to byte arrays which are the correct length, expect success,
            // then compare buffer contents for ordinal equality.

            foreach (int bufferLength in new int[] { utf8Normalized.GetByteLength() /* just right */, utf8Normalized.GetByteLength() + 1 /* with extra room */ })
            {
                byte[] dest = new byte[bufferLength];
                Assert.Equal(utf8Normalized.GetByteLength(), utf8Source.Normalize(dest, normalizationForm));
                Utf8Span normalizedSpan = Utf8Span.UnsafeCreateWithoutValidation(dest[..utf8Normalized.GetByteLength()]);