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

Commit 8497ffe

Browse files
jkotassafern
authored andcommitted
Move TextInfo to shared CoreLib partition (dotnet/coreclr#15195)
Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
1 parent 55185c5 commit 8497ffe

File tree

4 files changed

+991
-0
lines changed

4 files changed

+991
-0
lines changed

src/Common/src/CoreLib/System.Private.CoreLib.Shared.projitems

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@
167167
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\TaiwanCalendar.cs" />
168168
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\TaiwanLunisolarCalendar.cs" />
169169
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\TextElementEnumerator.cs" />
170+
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\TextInfo.cs" />
170171
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\ThaiBuddhistCalendar.cs" />
171172
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\TimeSpanFormat.cs" />
172173
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\TimeSpanParse.cs" />
@@ -653,6 +654,7 @@
653654
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\JapaneseCalendar.Win32.cs" Condition="'$(EnableWinRT)' != 'true' and '$(EnableDummyGlobalizationImplementation)' != 'true'" />
654655
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\JapaneseCalendar.WinRT.cs" Condition="'$(EnableWinRT)' == 'true'" />
655656
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\Normalization.Windows.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" />
657+
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\TextInfo.Windows.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" />
656658
<Compile Include="$(MSBuildThisFileDirectory)System\IO\FileStream.Windows.cs" />
657659
<Compile Include="$(MSBuildThisFileDirectory)System\IO\FileStreamCompletionSource.Win32.cs" />
658660
<Compile Include="$(MSBuildThisFileDirectory)System\IO\Path.Windows.cs" />
@@ -725,6 +727,7 @@
725727
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\JapaneseCalendar.Unix.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" />
726728
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\LocaleData.Unix.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" />
727729
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\Normalization.Unix.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" />
730+
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\TextInfo.Unix.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" />
728731
<Compile Include="$(MSBuildThisFileDirectory)System\IO\FileStream.OSX.cs" Condition="'$(TargetsOSX)' == 'true'" />
729732
<Compile Include="$(MSBuildThisFileDirectory)System\IO\FileStream.Linux.cs" Condition="'$(TargetsOSX)' != 'true'" />
730733
<Compile Include="$(MSBuildThisFileDirectory)System\IO\FileStream.Unix.cs" />
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Diagnostics;
6+
using System.Security;
7+
using System.Text;
8+
9+
namespace System.Globalization
10+
{
11+
public partial class TextInfo
12+
{
13+
private Tristate _needsTurkishCasing = Tristate.NotInitialized;
14+
15+
private void FinishInitialization()
16+
{
17+
}
18+
19+
private unsafe string ChangeCase(string s, bool toUpper)
20+
{
21+
Debug.Assert(!_invariantMode);
22+
23+
Debug.Assert(s != null);
24+
25+
if (s.Length == 0)
26+
{
27+
return string.Empty;
28+
}
29+
30+
string result = string.FastAllocateString(s.Length);
31+
32+
fixed (char* pSource = s)
33+
{
34+
fixed (char* pResult = result)
35+
{
36+
#if CORECLR
37+
if (IsAsciiCasingSameAsInvariant && s.IsAscii())
38+
{
39+
int length = s.Length;
40+
char* a = pSource, b = pResult;
41+
if (toUpper)
42+
{
43+
while (length-- != 0)
44+
{
45+
*b++ = ToUpperAsciiInvariant(*a++);
46+
}
47+
}
48+
else
49+
{
50+
while (length-- != 0)
51+
{
52+
*b++ = ToLowerAsciiInvariant(*a++);
53+
}
54+
}
55+
}
56+
else
57+
#endif
58+
{
59+
ChangeCase(pSource, s.Length, pResult, result.Length, toUpper);
60+
}
61+
}
62+
}
63+
64+
return result;
65+
}
66+
67+
private unsafe char ChangeCase(char c, bool toUpper)
68+
{
69+
Debug.Assert(!_invariantMode);
70+
71+
char dst = default(char);
72+
73+
ChangeCase(&c, 1, &dst, 1, toUpper);
74+
75+
return dst;
76+
}
77+
78+
// -----------------------------
79+
// ---- PAL layer ends here ----
80+
// -----------------------------
81+
82+
private bool NeedsTurkishCasing(string localeName)
83+
{
84+
Debug.Assert(localeName != null);
85+
86+
return CultureInfo.GetCultureInfo(localeName).CompareInfo.Compare("\u0131", "I", CompareOptions.IgnoreCase) == 0;
87+
}
88+
89+
private bool IsInvariant { get { return _cultureName.Length == 0; } }
90+
91+
internal unsafe void ChangeCase(char* src, int srcLen, char* dstBuffer, int dstBufferCapacity, bool bToUpper)
92+
{
93+
Debug.Assert(!_invariantMode);
94+
95+
if (IsInvariant)
96+
{
97+
Interop.GlobalizationInterop.ChangeCaseInvariant(src, srcLen, dstBuffer, dstBufferCapacity, bToUpper);
98+
}
99+
else
100+
{
101+
if (_needsTurkishCasing == Tristate.NotInitialized)
102+
{
103+
_needsTurkishCasing = NeedsTurkishCasing(_textInfoName) ? Tristate.True : Tristate.False;
104+
}
105+
if (_needsTurkishCasing == Tristate.True)
106+
{
107+
Interop.GlobalizationInterop.ChangeCaseTurkish(src, srcLen, dstBuffer, dstBufferCapacity, bToUpper);
108+
}
109+
else
110+
{
111+
Interop.GlobalizationInterop.ChangeCase(src, srcLen, dstBuffer, dstBufferCapacity, bToUpper);
112+
}
113+
}
114+
}
115+
116+
}
117+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Diagnostics;
6+
7+
namespace System.Globalization
8+
{
9+
public partial class TextInfo
10+
{
11+
private unsafe void FinishInitialization()
12+
{
13+
if (_invariantMode)
14+
{
15+
_sortHandle = IntPtr.Zero;
16+
return;
17+
}
18+
19+
const uint LCMAP_SORTHANDLE = 0x20000000;
20+
21+
IntPtr handle;
22+
int ret = Interop.Kernel32.LCMapStringEx(_textInfoName, LCMAP_SORTHANDLE, null, 0, &handle, IntPtr.Size, null, null, IntPtr.Zero);
23+
_sortHandle = ret > 0 ? handle : IntPtr.Zero;
24+
}
25+
26+
private unsafe string ChangeCase(string s, bool toUpper)
27+
{
28+
Debug.Assert(!_invariantMode);
29+
30+
Debug.Assert(s != null);
31+
32+
//
33+
// Get the length of the string.
34+
//
35+
int nLengthInput = s.Length;
36+
37+
//
38+
// Check if we have the empty string.
39+
//
40+
if (nLengthInput == 0)
41+
{
42+
return s;
43+
}
44+
45+
int ret;
46+
47+
// Check for Invariant to avoid A/V in LCMapStringEx
48+
uint linguisticCasing = IsInvariantLocale(_textInfoName) ? 0 : LCMAP_LINGUISTIC_CASING;
49+
50+
//
51+
// Create the result string.
52+
//
53+
string result = string.FastAllocateString(nLengthInput);
54+
55+
fixed (char* pSource = s)
56+
fixed (char* pResult = result)
57+
{
58+
ret = Interop.Kernel32.LCMapStringEx(_sortHandle != IntPtr.Zero ? null : _textInfoName,
59+
linguisticCasing | (toUpper ? LCMAP_UPPERCASE : LCMAP_LOWERCASE),
60+
pSource,
61+
nLengthInput,
62+
pResult,
63+
nLengthInput,
64+
null,
65+
null,
66+
_sortHandle);
67+
}
68+
69+
if (ret == 0)
70+
{
71+
throw new InvalidOperationException(SR.InvalidOperation_ReadOnly);
72+
}
73+
74+
Debug.Assert(ret == nLengthInput, "Expected getting the same length of the original string");
75+
return result;
76+
}
77+
78+
private unsafe char ChangeCase(char c, bool toUpper)
79+
{
80+
Debug.Assert(!_invariantMode);
81+
82+
char retVal = '\0';
83+
84+
// Check for Invariant to avoid A/V in LCMapStringEx
85+
uint linguisticCasing = IsInvariantLocale(_textInfoName) ? 0 : LCMAP_LINGUISTIC_CASING;
86+
87+
Interop.Kernel32.LCMapStringEx(_sortHandle != IntPtr.Zero ? null : _textInfoName,
88+
toUpper ? LCMAP_UPPERCASE | linguisticCasing : LCMAP_LOWERCASE | linguisticCasing,
89+
&c,
90+
1,
91+
&retVal,
92+
1,
93+
null,
94+
null,
95+
_sortHandle);
96+
97+
return retVal;
98+
}
99+
100+
// PAL Ends here
101+
102+
private IntPtr _sortHandle;
103+
104+
private const uint LCMAP_LINGUISTIC_CASING = 0x01000000;
105+
private const uint LCMAP_LOWERCASE = 0x00000100;
106+
private const uint LCMAP_UPPERCASE = 0x00000200;
107+
108+
private static bool IsInvariantLocale(string localeName)
109+
{
110+
return localeName == "";
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)