-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[browser][non-icu]
HybridGlobalization
normalization. (#85510)
* Normalization. * Missing measurement. * Trying to fix trimming error. * Applied @pavelsavara's idea. * Code review improvement. * Feedback: standardize the exceptions. * Revert changes from #85516 to clean the CI. * feedback
- Loading branch information
1 parent
ce689d9
commit 7a11915
Showing
21 changed files
with
403 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/libraries/Common/src/Interop/Browser/Interop.Normalization.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Runtime.CompilerServices; | ||
using System.Text; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static unsafe partial class JsGlobalization | ||
{ | ||
[MethodImplAttribute(MethodImplOptions.InternalCall)] | ||
internal static extern unsafe int IsNormalized(NormalizationForm normalizationForm, in string source, out int exceptionalResult, out object result); | ||
|
||
[MethodImplAttribute(MethodImplOptions.InternalCall)] | ||
internal static extern unsafe int NormalizeString(NormalizationForm normalizationForm, in string source, char* dstBuffer, int dstBufferCapacity, out int exceptionalResult, out object result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...lization.Extensions/tests/Hybrid/System.Globalization.Extensions.Hybrid.WASM.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>$(NetCoreAppCurrent)-browser</TargetFrameworks> | ||
<TestRuntime>true</TestRuntime> | ||
<HybridGlobalization>true</HybridGlobalization> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="..\Normalization\NormalizationAll.cs" /> | ||
<Compile Include="..\Normalization\StringNormalizationTests.cs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<EmbeddedResource Include="..\Normalization\Data\win8.txt"> | ||
<LogicalName>NormalizationDataWin8</LogicalName> | ||
</EmbeddedResource> | ||
<EmbeddedResource Include="..\Normalization\Data\win7.txt"> | ||
<LogicalName>NormalizationDataWin7</LogicalName> | ||
</EmbeddedResource> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/libraries/System.Private.CoreLib/src/System/Globalization/Normalization.WebAssembly.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Buffers; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
|
||
namespace System.Globalization | ||
{ | ||
internal static partial class Normalization | ||
{ | ||
private static unsafe bool JsIsNormalized(string strInput, NormalizationForm normalizationForm) | ||
{ | ||
Debug.Assert(!GlobalizationMode.Invariant); | ||
Debug.Assert(!GlobalizationMode.UseNls); | ||
|
||
ValidateArguments(strInput, normalizationForm); | ||
|
||
int ret = Interop.JsGlobalization.IsNormalized(normalizationForm, strInput, out int exception, out object ex_result); | ||
if (exception != 0) | ||
throw new Exception((string)ex_result); | ||
|
||
return ret == 1; | ||
} | ||
|
||
private static unsafe string JsNormalize(string strInput, NormalizationForm normalizationForm) | ||
{ | ||
Debug.Assert(!GlobalizationMode.Invariant); | ||
Debug.Assert(!GlobalizationMode.UseNls); | ||
|
||
ValidateArguments(strInput, normalizationForm); | ||
|
||
char[]? toReturn = null; | ||
try | ||
{ | ||
const int StackallocThreshold = 512; | ||
|
||
Span<char> buffer = strInput.Length <= StackallocThreshold | ||
? stackalloc char[StackallocThreshold] | ||
: (toReturn = ArrayPool<char>.Shared.Rent(strInput.Length)); | ||
|
||
for (int attempt = 0; attempt < 2; attempt++) | ||
{ | ||
int realLen; | ||
fixed (char* pDest = &MemoryMarshal.GetReference(buffer)) | ||
{ | ||
realLen = Interop.JsGlobalization.NormalizeString(normalizationForm, strInput, pDest, buffer.Length, out int exception, out object ex_result); | ||
if (exception != 0) | ||
throw new Exception((string)ex_result); | ||
} | ||
|
||
if (realLen <= buffer.Length) | ||
{ | ||
ReadOnlySpan<char> result = buffer.Slice(0, realLen); | ||
return result.SequenceEqual(strInput) | ||
? strInput | ||
: new string(result); | ||
} | ||
|
||
Debug.Assert(realLen > StackallocThreshold); | ||
|
||
if (attempt == 0) | ||
{ | ||
if (toReturn != null) | ||
{ | ||
// Clear toReturn first to ensure we don't return the same buffer twice | ||
char[] temp = toReturn; | ||
toReturn = null; | ||
ArrayPool<char>.Shared.Return(temp); | ||
} | ||
|
||
buffer = toReturn = ArrayPool<char>.Shared.Rent(realLen); | ||
} | ||
} | ||
|
||
throw new ArgumentException(SR.Argument_InvalidCharSequenceNoIndex, nameof(strInput)); | ||
} | ||
finally | ||
{ | ||
if (toReturn != null) | ||
{ | ||
ArrayPool<char>.Shared.Return(toReturn); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.