Skip to content

Commit cc66c77

Browse files
authored
Remove duplicate IsAscii check in string.IsNormalized (#110576)
1 parent 202d0e4 commit cc66c77

File tree

3 files changed

+15
-41
lines changed

3 files changed

+15
-41
lines changed

src/libraries/System.Private.CoreLib/src/System/Globalization/Normalization.cs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Diagnostics;
54
using System.Runtime.CompilerServices;
65
using System.Text;
76

@@ -13,10 +12,10 @@ internal static bool IsNormalized(ReadOnlySpan<char> source, NormalizationForm n
1312
{
1413
CheckNormalizationForm(normalizationForm);
1514

16-
// In Invariant mode we assume all characters are normalized.
17-
if (GlobalizationMode.Invariant || source.IsEmpty || Ascii.IsValid(source))
15+
// In Invariant mode we assume all characters are normalized because we don't support any linguistic operations on strings.
16+
// If it's ASCII && one of the 4 main forms, then it's already normalized.
17+
if (GlobalizationMode.Invariant || Ascii.IsValid(source))
1818
{
19-
// This is because we don't support any linguistic operation on the strings
2019
return true;
2120
}
2221

@@ -29,10 +28,10 @@ internal static string Normalize(string strInput, NormalizationForm normalizatio
2928
{
3029
CheckNormalizationForm(normalizationForm);
3130

32-
if (GlobalizationMode.Invariant)
31+
// In Invariant mode we assume all characters are normalized because we don't support any linguistic operations on strings.
32+
// If it's ASCII && one of the 4 main forms, then it's already normalized.
33+
if (GlobalizationMode.Invariant || Ascii.IsValid(strInput))
3334
{
34-
// In Invariant mode we assume all characters are normalized.
35-
// This is because we don't support any linguistic operation on the strings
3635
return strInput;
3736
}
3837

@@ -45,17 +44,10 @@ internal static bool TryNormalize(ReadOnlySpan<char> source, Span<char> destinat
4544
{
4645
CheckNormalizationForm(normalizationForm);
4746

48-
if (source.IsEmpty)
49-
{
50-
charsWritten = 0;
51-
return true;
52-
}
53-
47+
// In Invariant mode we assume all characters are normalized because we don't support any linguistic operations on strings.
48+
// If it's ASCII && one of the 4 main forms, then it's already normalized.
5449
if (GlobalizationMode.Invariant || Ascii.IsValid(source))
5550
{
56-
// In Invariant mode we assume all characters are normalized.
57-
// This is because we don't support any linguistic operation on the strings
58-
5951
if (source.TryCopyTo(destination))
6052
{
6153
charsWritten = source.Length;
@@ -75,10 +67,10 @@ internal static int GetNormalizedLength(this ReadOnlySpan<char> source, Normaliz
7567
{
7668
CheckNormalizationForm(normalizationForm);
7769

78-
if (GlobalizationMode.Invariant || source.IsEmpty || Ascii.IsValid(source))
70+
// In Invariant mode we assume all characters are normalized because we don't support any linguistic operations on strings.
71+
// If it's ASCII && one of the 4 main forms, then it's already normalized.
72+
if (GlobalizationMode.Invariant || Ascii.IsValid(source))
7973
{
80-
// In Invariant mode we assume all characters are normalized.
81-
// This is because we don't support any linguistic operation on the strings
8274
return source.Length;
8375
}
8476

src/libraries/System.Private.CoreLib/src/System/String.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Buffers;
5-
using System.Buffers.Text;
65
using System.Collections;
76
using System.Collections.Generic;
87
using System.ComponentModel;
@@ -709,15 +708,6 @@ public bool IsNormalized()
709708

710709
public bool IsNormalized(NormalizationForm normalizationForm)
711710
{
712-
if (Ascii.IsValid(this))
713-
{
714-
// If its ASCII && one of the 4 main forms, then its already normalized
715-
if (normalizationForm == NormalizationForm.FormC ||
716-
normalizationForm == NormalizationForm.FormKC ||
717-
normalizationForm == NormalizationForm.FormD ||
718-
normalizationForm == NormalizationForm.FormKD)
719-
return true;
720-
}
721711
return Normalization.IsNormalized(this, normalizationForm);
722712
}
723713

@@ -728,15 +718,6 @@ public string Normalize()
728718

729719
public string Normalize(NormalizationForm normalizationForm)
730720
{
731-
if (Ascii.IsValid(this))
732-
{
733-
// If its ASCII && one of the 4 main forms, then its already normalized
734-
if (normalizationForm == NormalizationForm.FormC ||
735-
normalizationForm == NormalizationForm.FormKC ||
736-
normalizationForm == NormalizationForm.FormD ||
737-
normalizationForm == NormalizationForm.FormKD)
738-
return this;
739-
}
740721
return Normalization.Normalize(this, normalizationForm);
741722
}
742723

src/libraries/System.Private.CoreLib/src/System/StringNormalizationExtensions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Globalization;
45
using System.Text;
56

67
namespace System
@@ -41,7 +42,7 @@ public static bool IsNormalized(this string strInput, NormalizationForm normaliz
4142
/// <returns><see langword="true"/> if the specified span of characters is in a normalized form; otherwise, <see langword="false"/>.</returns>
4243
/// <exception cref="ArgumentException">The specified character span contains an invalid code point or the normalization form is invalid.</exception>
4344
public static bool IsNormalized(this ReadOnlySpan<char> source, NormalizationForm normalizationForm = NormalizationForm.FormC) =>
44-
System.Globalization.Normalization.IsNormalized(source, normalizationForm);
45+
Normalization.IsNormalized(source, normalizationForm);
4546

4647
/// <summary>
4748
/// Normalizes the specified string to the <see cref="NormalizationForm.FormC" />.
@@ -77,7 +78,7 @@ public static string Normalize(this string strInput, NormalizationForm normaliza
7778
/// <returns><see langword="true"/> if the specified span of characters was successfully normalized; otherwise, <see langword="false"/>.</returns>
7879
/// <exception cref="ArgumentException">The specified character span contains an invalid code point or the normalization form is invalid.</exception>
7980
public static bool TryNormalize(this ReadOnlySpan<char> source, Span<char> destination, out int charsWritten, NormalizationForm normalizationForm = NormalizationForm.FormC) =>
80-
System.Globalization.Normalization.TryNormalize(source, destination, out charsWritten, normalizationForm);
81+
Normalization.TryNormalize(source, destination, out charsWritten, normalizationForm);
8182

8283
/// <summary>
8384
/// Gets the estimated length of the normalized form of the specified string in the <see cref="NormalizationForm.FormC" />.
@@ -87,6 +88,6 @@ public static bool TryNormalize(this ReadOnlySpan<char> source, Span<char> desti
8788
/// <returns>The estimated length of the normalized form of the specified string.</returns>
8889
/// <exception cref="ArgumentException">The specified character span contains an invalid code point or the normalization form is invalid.</exception>
8990
public static int GetNormalizedLength(this ReadOnlySpan<char> source, NormalizationForm normalizationForm = NormalizationForm.FormC) =>
90-
System.Globalization.Normalization.GetNormalizedLength(source, normalizationForm);
91+
Normalization.GetNormalizedLength(source, normalizationForm);
9192
}
9293
}

0 commit comments

Comments
 (0)