Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit a7427d8

Browse files
authored
Move Nullable and string normalization to shared CoreLib partition (#15184)
1 parent 4c30649 commit a7427d8

File tree

9 files changed

+77
-95
lines changed

9 files changed

+77
-95
lines changed

src/mscorlib/System.Private.CoreLib.csproj

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,6 @@
562562
<Compile Include="$(BclSourcesRoot)\System\Resources\ResourceSet.cs" />
563563
</ItemGroup>
564564
<ItemGroup>
565-
<Compile Include="$(BclSourcesRoot)\System\Nullable.cs" />
566565
<Compile Include="$(BclSourcesRoot)\System\Collections\Generic\Comparer.cs" />
567566
<Compile Include="$(BclSourcesRoot)\System\Collections\Generic\ComparerHelpers.cs" />
568567
<Compile Include="$(BclSourcesRoot)\System\Collections\Generic\EqualityComparer.cs" />
@@ -598,16 +597,12 @@
598597
</ItemGroup>
599598
<ItemGroup Condition="'$(TargetsWindows)' == 'true'">
600599
<Compile Include="$(BclSourcesRoot)\Interop\Windows\Kernel32\Interop.GetSystemDirectoryW.cs" />
601-
<Compile Include="$(BclSourcesRoot)\Interop\Windows\Normaliz\Interop.Idna.cs" />
602-
<Compile Include="$(BclSourcesRoot)\Interop\Windows\Normaliz\Interop.Normalization.cs" />
603600
<Compile Include="$(BclSourcesRoot)\System\Diagnostics\Debug.Windows.cs" />
604601
<Compile Include="$(BclSourcesRoot)\System\Globalization\CompareInfo.Windows.cs" />
605602
<Compile Include="$(BclSourcesRoot)\System\Globalization\CultureData.Windows.cs" />
606603
<Compile Include="$(BclSourcesRoot)\System\Globalization\CultureInfo.Windows.cs" />
607604
<Compile Include="$(BclSourcesRoot)\System\Globalization\GlobalizationMode.Windows.cs" />
608-
<Compile Include="$(BclSourcesRoot)\System\Globalization\IdnMapping.Windows.cs" />
609605
<Compile Include="$(BclSourcesRoot)\System\Globalization\TextInfo.Windows.cs" />
610-
<Compile Include="$(BclSourcesRoot)\System\Text\Normalization.Windows.cs" />
611606
<Compile Include="$(BclSourcesRoot)\System\Threading\ClrThreadPoolBoundHandle.Windows.cs" />
612607
<Compile Include="$(BclSourcesRoot)\System\Environment.Windows.cs" />
613608
<Compile Include="$(BclSourcesRoot)\System\TimeZoneInfo.Win32.cs" />

src/mscorlib/src/Interop/Windows/Normaliz/Interop.Idna.cs renamed to src/mscorlib/shared/Interop/Windows/Normaliz/Interop.Idna.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,22 @@ internal partial class Normaliz
1414
//
1515

1616
[DllImport("Normaliz.dll", CharSet = CharSet.Unicode, SetLastError = true)]
17-
internal static extern int IdnToAscii(
17+
internal static extern unsafe int IdnToAscii(
1818
uint dwFlags,
19-
IntPtr lpUnicodeCharStr,
19+
char* lpUnicodeCharStr,
2020
int cchUnicodeChar,
21-
[System.Runtime.InteropServices.OutAttribute()]
22-
IntPtr lpASCIICharStr,
21+
char* lpASCIICharStr,
2322
int cchASCIIChar);
2423

2524
[DllImport("Normaliz.dll", CharSet = CharSet.Unicode, SetLastError = true)]
26-
internal static extern int IdnToUnicode(
25+
internal static extern unsafe int IdnToUnicode(
2726
uint dwFlags,
28-
IntPtr lpASCIICharStr,
27+
char* lpASCIICharStr,
2928
int cchASCIIChar,
30-
[System.Runtime.InteropServices.OutAttribute()]
31-
IntPtr lpUnicodeCharStr,
29+
char* lpUnicodeCharStr,
3230
int cchUnicodeChar);
3331

3432
internal const int IDN_ALLOW_UNASSIGNED = 0x1;
3533
internal const int IDN_USE_STD3_ASCII_RULES = 0x2;
36-
internal const int ERROR_INVALID_NAME = 123;
3734
}
3835
}

src/mscorlib/src/Interop/Windows/Normaliz/Interop.Normalization.cs renamed to src/mscorlib/shared/Interop/Windows/Normaliz/Interop.Normalization.cs

File renamed without changes.

src/mscorlib/shared/System.Private.CoreLib.Shared.projitems

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@
238238
<Compile Include="$(MSBuildThisFileDirectory)System\NotImplementedException.cs" />
239239
<Compile Include="$(MSBuildThisFileDirectory)System\NonSerializedAttribute.cs" />
240240
<Compile Include="$(MSBuildThisFileDirectory)System\NotSupportedException.cs" />
241+
<Compile Include="$(MSBuildThisFileDirectory)System\Nullable.cs" />
241242
<Compile Include="$(MSBuildThisFileDirectory)System\Number.Formatting.cs" />
242243
<Compile Include="$(MSBuildThisFileDirectory)System\Number.NumberBuffer.cs" />
243244
<Compile Include="$(MSBuildThisFileDirectory)System\Number.Parsing.cs" />
@@ -495,7 +496,7 @@
495496
<Compile Include="$(MSBuildThisFileDirectory)System\Text\EncodingNLS.cs" />
496497
<Compile Include="$(MSBuildThisFileDirectory)System\Text\EncodingProvider.cs" />
497498
<Compile Include="$(MSBuildThisFileDirectory)System\Text\Latin1Encoding.cs" />
498-
<Compile Include="$(MSBuildThisFileDirectory)System\Text\Normalization.cs" />
499+
<Compile Include="$(MSBuildThisFileDirectory)System\Text\NormalizationForm.cs" />
499500
<Compile Include="$(MSBuildThisFileDirectory)System\Text\StringBuilder.cs" />
500501
<Compile Include="$(MSBuildThisFileDirectory)System\Text\UnicodeEncoding.cs" />
501502
<Compile Include="$(MSBuildThisFileDirectory)System\Text\UTF32Encoding.cs" />
@@ -639,15 +640,19 @@
639640
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.WideCharToMultiByte.cs" />
640641
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.WriteFile_SafeHandle_IntPtr.cs" />
641642
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.WriteFile_SafeHandle_NativeOverlapped.cs" />
643+
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Normaliz\Interop.Idna.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" />
644+
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Normaliz\Interop.Normalization.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" />
642645
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\OleAut32\Interop.SysAllocStringLen.cs" />
643646
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\OleAut32\Interop.SysFreeString.cs" />
644647
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\OleAut32\Interop.SysStringLen.cs" />
645648
<Compile Include="$(MSBuildThisFileDirectory)Microsoft\Win32\SafeHandles\SafeFileHandle.Windows.cs" />
646649
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\CalendarData.Windows.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" />
647650
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\HijriCalendar.Win32.cs" Condition="'$(EnableWinRT)' != 'true' and '$(EnableDummyGlobalizationImplementation)' != 'true'" />
648651
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\HijriCalendar.WinRT.cs" Condition="'$(EnableWinRT)' == 'true'" />
652+
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\IdnMapping.Windows.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" />
649653
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\JapaneseCalendar.Win32.cs" Condition="'$(EnableWinRT)' != 'true' and '$(EnableDummyGlobalizationImplementation)' != 'true'" />
650654
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\JapaneseCalendar.WinRT.cs" Condition="'$(EnableWinRT)' == 'true'" />
655+
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\Normalization.Windows.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" />
651656
<Compile Include="$(MSBuildThisFileDirectory)System\IO\FileStream.Windows.cs" />
652657
<Compile Include="$(MSBuildThisFileDirectory)System\IO\FileStreamCompletionSource.Win32.cs" />
653658
<Compile Include="$(MSBuildThisFileDirectory)System\IO\Path.Windows.cs" />
@@ -719,12 +724,12 @@
719724
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\IdnMapping.Unix.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" />
720725
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\JapaneseCalendar.Unix.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" />
721726
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\LocaleData.Unix.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" />
727+
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\Normalization.Unix.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" />
722728
<Compile Include="$(MSBuildThisFileDirectory)System\IO\FileStream.OSX.cs" Condition="'$(TargetsOSX)' == 'true'" />
723729
<Compile Include="$(MSBuildThisFileDirectory)System\IO\FileStream.Linux.cs" Condition="'$(TargetsOSX)' != 'true'" />
724730
<Compile Include="$(MSBuildThisFileDirectory)System\IO\FileStream.Unix.cs" />
725731
<Compile Include="$(MSBuildThisFileDirectory)System\IO\Path.Unix.cs" />
726732
<Compile Include="$(MSBuildThisFileDirectory)System\IO\PathInternal.Unix.cs" />
727733
<Compile Include="$(MSBuildThisFileDirectory)System\Security\SecureString.Unix.cs" />
728-
<Compile Include="$(MSBuildThisFileDirectory)System\Text\Normalization.Unix.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" />
729734
</ItemGroup>
730735
</Project>

src/mscorlib/src/System/Globalization/IdnMapping.Windows.cs renamed to src/mscorlib/shared/System/Globalization/IdnMapping.Windows.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ private unsafe string GetAsciiCore(char* unicode, int count)
1616
uint flags = Flags;
1717

1818
// Determine the required length
19-
int length = Interop.Normaliz.IdnToAscii(flags, new IntPtr(unicode), count, IntPtr.Zero, 0);
19+
int length = Interop.Normaliz.IdnToAscii(flags, unicode, count, null, 0);
2020
if (length == 0)
2121
{
22-
ThrowForZeroLength(nameof(unicode), SR.Argument_IdnIllegalName, SR.Argument_InvalidCharSequenceNoIndex);
22+
ThrowForZeroLength(unicode: true);
2323
}
2424

2525
// Do the conversion
@@ -32,7 +32,7 @@ private unsafe string GetAsciiCore(char* unicode, int count)
3232
else
3333
{
3434
char[] output = new char[length];
35-
fixed (char* pOutput = output)
35+
fixed (char* pOutput = &output[0])
3636
{
3737
return GetAsciiCore(unicode, count, flags, pOutput, length);
3838
}
@@ -43,10 +43,10 @@ private unsafe string GetAsciiCore(char* unicode, int count, uint flags, char* o
4343
{
4444
Debug.Assert(!GlobalizationMode.Invariant);
4545

46-
int length = Interop.Normaliz.IdnToAscii(flags, new IntPtr(unicode), count, new IntPtr(output), outputLength);
46+
int length = Interop.Normaliz.IdnToAscii(flags, unicode, count, output, outputLength);
4747
if (length == 0)
4848
{
49-
ThrowForZeroLength(nameof(unicode), SR.Argument_IdnIllegalName, SR.Argument_InvalidCharSequenceNoIndex);
49+
ThrowForZeroLength(unicode: true);
5050
}
5151
Debug.Assert(length == outputLength);
5252
return new string(output, 0, length);
@@ -59,10 +59,10 @@ private unsafe string GetUnicodeCore(char* ascii, int count)
5959
uint flags = Flags;
6060

6161
// Determine the required length
62-
int length = Interop.Normaliz.IdnToUnicode(flags, new IntPtr(ascii), count, IntPtr.Zero, 0);
62+
int length = Interop.Normaliz.IdnToUnicode(flags, ascii, count, null, 0);
6363
if (length == 0)
6464
{
65-
ThrowForZeroLength(nameof(ascii), SR.Argument_IdnIllegalName, SR.Argument_IdnBadPunycode);
65+
ThrowForZeroLength(unicode: false);
6666
}
6767

6868
// Do the conversion
@@ -75,7 +75,7 @@ private unsafe string GetUnicodeCore(char* ascii, int count)
7575
else
7676
{
7777
char[] output = new char[length];
78-
fixed (char* pOutput = output)
78+
fixed (char* pOutput = &output[0])
7979
{
8080
return GetUnicodeCore(ascii, count, flags, pOutput, length);
8181
}
@@ -86,10 +86,10 @@ private unsafe string GetUnicodeCore(char* ascii, int count, uint flags, char* o
8686
{
8787
Debug.Assert(!GlobalizationMode.Invariant);
8888

89-
int length = Interop.Normaliz.IdnToUnicode(flags, new IntPtr(ascii), count, new IntPtr(output), outputLength);
89+
int length = Interop.Normaliz.IdnToUnicode(flags, ascii, count, output, outputLength);
9090
if (length == 0)
9191
{
92-
ThrowForZeroLength(nameof(ascii), SR.Argument_IdnIllegalName, SR.Argument_IdnBadPunycode);
92+
ThrowForZeroLength(unicode: false);
9393
}
9494
Debug.Assert(length == outputLength);
9595
return new string(output, 0, length);
@@ -110,11 +110,14 @@ private uint Flags
110110
}
111111
}
112112

113-
private static void ThrowForZeroLength(string paramName, string invalidNameString, string otherString)
113+
private static void ThrowForZeroLength(bool unicode)
114114
{
115+
int lastError = Marshal.GetLastWin32Error();
116+
115117
throw new ArgumentException(
116-
Marshal.GetLastWin32Error() == Interop.Normaliz.ERROR_INVALID_NAME ? invalidNameString : otherString,
117-
paramName);
118+
lastError == Interop.Errors.ERROR_INVALID_NAME ? SR.Argument_IdnIllegalName :
119+
(unicode ? SR.Argument_InvalidCharSequenceNoIndex : SR.Argument_IdnBadPunycode),
120+
unicode ? "unicode" : "ascii");
118121
}
119122
}
120123
}

src/mscorlib/shared/System/Text/Normalization.Unix.cs renamed to src/mscorlib/shared/System/Globalization/Normalization.Unix.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,26 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Diagnostics;
56
using System.Security;
67
using System.Text;
78
using System.Globalization;
89

910
namespace System.Text
1011
{
11-
static partial class Normalization
12+
internal static partial class Normalization
1213
{
13-
public static bool IsNormalized(this string strInput, NormalizationForm normalizationForm)
14+
internal static bool IsNormalized(string strInput, NormalizationForm normalizationForm)
1415
{
15-
ValidateArguments(strInput, normalizationForm);
16-
1716
if (GlobalizationMode.Invariant)
1817
{
1918
// In Invariant mode we assume all characters are normalized.
2019
// This is because we don't support any linguistic operation on the strings
2120
return true;
2221
}
2322

23+
ValidateArguments(strInput, normalizationForm);
24+
2425
int ret = Interop.GlobalizationInterop.IsNormalized(normalizationForm, strInput, strInput.Length);
2526

2627
if (ret == -1)
@@ -31,17 +32,17 @@ public static bool IsNormalized(this string strInput, NormalizationForm normaliz
3132
return ret == 1;
3233
}
3334

34-
public static string Normalize(this string strInput, NormalizationForm normalizationForm)
35+
internal static string Normalize(string strInput, NormalizationForm normalizationForm)
3536
{
36-
ValidateArguments(strInput, normalizationForm);
37-
3837
if (GlobalizationMode.Invariant)
3938
{
4039
// In Invariant mode we assume all characters are normalized.
4140
// This is because we don't support any linguistic operation on the strings
4241
return strInput;
4342
}
4443

44+
ValidateArguments(strInput, normalizationForm);
45+
4546
char[] buf = new char[strInput.Length];
4647

4748
for (int attempts = 2; attempts > 0; attempts--)
@@ -70,10 +71,7 @@ public static string Normalize(this string strInput, NormalizationForm normaliza
7071

7172
private static void ValidateArguments(string strInput, NormalizationForm normalizationForm)
7273
{
73-
if (strInput == null)
74-
{
75-
throw new ArgumentNullException(nameof(strInput));
76-
}
74+
Debug.Assert(strInput != null);
7775

7876
if (normalizationForm != NormalizationForm.FormC && normalizationForm != NormalizationForm.FormD &&
7977
normalizationForm != NormalizationForm.FormKC && normalizationForm != NormalizationForm.FormKD)

src/mscorlib/src/System/Text/Normalization.Windows.cs renamed to src/mscorlib/shared/System/Globalization/Normalization.Windows.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,16 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System;
6-
using System.Security;
5+
using System.Diagnostics;
76
using System.Globalization;
8-
using System.Text;
9-
using System.Runtime.CompilerServices;
107
using System.Runtime.InteropServices;
11-
using System.Runtime.Versioning;
12-
using System.Diagnostics;
8+
using System.Text;
139

1410
namespace System.Text
1511
{
16-
// This internal class wraps up our normalization behavior
17-
18-
internal class Normalization
12+
internal static partial class Normalization
1913
{
20-
internal static bool IsNormalized(String strInput, NormalizationForm normalizationForm)
14+
internal static bool IsNormalized(string strInput, NormalizationForm normalizationForm)
2115
{
2216
if (GlobalizationMode.Invariant)
2317
{
@@ -41,7 +35,10 @@ internal static bool IsNormalized(String strInput, NormalizationForm normalizati
4135

4236
case Interop.Errors.ERROR_INVALID_PARAMETER:
4337
case Interop.Errors.ERROR_NO_UNICODE_TRANSLATION:
44-
if (!Enum.IsDefined(typeof(NormalizationForm), normalizationForm))
38+
if (normalizationForm != NormalizationForm.FormC &&
39+
normalizationForm != NormalizationForm.FormD &&
40+
normalizationForm != NormalizationForm.FormKC &&
41+
normalizationForm != NormalizationForm.FormKD)
4542
{
4643
throw new ArgumentException(SR.Argument_InvalidNormalizationForm, nameof(normalizationForm));
4744
}
@@ -58,7 +55,7 @@ internal static bool IsNormalized(String strInput, NormalizationForm normalizati
5855
return result;
5956
}
6057

61-
internal static String Normalize(String strInput, NormalizationForm normalizationForm)
58+
internal static string Normalize(string strInput, NormalizationForm normalizationForm)
6259
{
6360
if (GlobalizationMode.Invariant)
6461
{
@@ -82,7 +79,10 @@ internal static String Normalize(String strInput, NormalizationForm normalizatio
8279
{
8380
if (lastError == Interop.Errors.ERROR_INVALID_PARAMETER)
8481
{
85-
if (!Enum.IsDefined(typeof(NormalizationForm), normalizationForm))
82+
if (normalizationForm != NormalizationForm.FormC &&
83+
normalizationForm != NormalizationForm.FormD &&
84+
normalizationForm != NormalizationForm.FormKC &&
85+
normalizationForm != NormalizationForm.FormKD)
8686
{
8787
throw new ArgumentException(SR.Argument_InvalidNormalizationForm, nameof(normalizationForm));
8888
}

0 commit comments

Comments
 (0)