public static void Trim(string input) { if (input is null) { return; // don't want to null ref } Utf8String utf8Input = u8(input); void RunTest(Func <Utf8String, Utf8String> utf8TrimAction, Func <string, string> utf16TrimAction) { Utf8String utf8Trimmed = utf8TrimAction(utf8Input); string utf16Trimmed = utf16TrimAction(input); if (utf16Trimmed.Length == input.Length) { Assert.Same(utf8Input, utf8Trimmed); // Trimming should no-op, return original input } else if (utf16Trimmed.Length == 0) { Assert.Same(Utf8String.Empty, utf8Trimmed); // Trimming an all-whitespace input, return Empty } else { Assert.True(Utf8String.AreEquivalent(utf8Trimmed, utf16Trimmed)); } } RunTest(ustr => ustr.Trim(), str => str.Trim()); RunTest(ustr => ustr.TrimStart(), str => str.TrimStart()); RunTest(ustr => ustr.TrimEnd(), str => str.TrimEnd()); }
public static void AreEquivalent_Utf8StringAndString_NullHandling() { Assert.True(Utf8String.AreEquivalent((Utf8String)null, (string)null)); Assert.False(Utf8String.AreEquivalent(Utf8String.Empty, (string)null)); Assert.False(Utf8String.AreEquivalent((Utf8String)null, string.Empty)); Assert.True(Utf8String.AreEquivalent(Utf8String.Empty, string.Empty)); }
public static void AreEquivalent_Tests(string utf8Input, string utf16Input, bool expected) { Utf8String asUtf8 = u8(utf8Input); // Call all three overloads Assert.Equal(expected, Utf8String.AreEquivalent(asUtf8, utf16Input)); Assert.Equal(expected, Utf8String.AreEquivalent(asUtf8.AsSpan(), utf16Input.AsSpan())); Assert.Equal(expected, Utf8String.AreEquivalent(asUtf8.AsBytes(), utf16Input.AsSpan())); }
public static void Normalize(string utf16Source, string utf16Expected, NormalizationForm normalizationForm) { Utf8String utf8Source = u8(utf16Source); // Quick IsNormalized tests Assert.Equal(utf16Source == utf16Expected, utf8Source.IsNormalized(normalizationForm)); // Normalize and return new Utf8String instances Utf8String utf8Normalized = utf8Source.Normalize(normalizationForm); Assert.True(Utf8String.AreEquivalent(utf8Normalized, utf16Expected)); }
[InlineData(new byte[] { 0xED }, new char[] { '\uD800' })] // don't support partials public static void AreEquivalent_IllFormedData_AlwaysReturnsFalse(byte[] asUtf8, char[] asUtf16) { Assert.False(Utf8String.AreEquivalent(asUtf8, asUtf16)); }