Skip to content

Remove duplicate IsAscii check in string.IsNormalized #110576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;

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

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

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

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

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

if (source.IsEmpty)
{
charsWritten = 0;
return true;
}

// In Invariant mode we assume all characters are normalized because we don't support any linguistic operations on strings.
// If it's ASCII && one of the 4 main forms, then it's already normalized.
if (GlobalizationMode.Invariant || Ascii.IsValid(source))
{
// In Invariant mode we assume all characters are normalized.
// This is because we don't support any linguistic operation on the strings

if (source.TryCopyTo(destination))
{
charsWritten = source.Length;
Expand All @@ -75,10 +67,10 @@ internal static int GetNormalizedLength(this ReadOnlySpan<char> source, Normaliz
{
CheckNormalizationForm(normalizationForm);

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

Expand Down
19 changes: 0 additions & 19 deletions src/libraries/System.Private.CoreLib/src/System/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers;
using System.Buffers.Text;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
Expand Down Expand Up @@ -709,15 +708,6 @@ public bool IsNormalized()

public bool IsNormalized(NormalizationForm normalizationForm)
{
if (Ascii.IsValid(this))
{
// If its ASCII && one of the 4 main forms, then its already normalized
if (normalizationForm == NormalizationForm.FormC ||
normalizationForm == NormalizationForm.FormKC ||
normalizationForm == NormalizationForm.FormD ||
normalizationForm == NormalizationForm.FormKD)
return true;
}
return Normalization.IsNormalized(this, normalizationForm);
}

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

public string Normalize(NormalizationForm normalizationForm)
{
if (Ascii.IsValid(this))
{
// If its ASCII && one of the 4 main forms, then its already normalized
if (normalizationForm == NormalizationForm.FormC ||
normalizationForm == NormalizationForm.FormKC ||
normalizationForm == NormalizationForm.FormD ||
normalizationForm == NormalizationForm.FormKD)
return this;
}
return Normalization.Normalize(this, normalizationForm);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Globalization;
using System.Text;

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

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

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