33// See the LICENSE file in the project root for more information.
44
55using System . Diagnostics ;
6- using System . Runtime . InteropServices ;
76
87namespace System . Globalization
98{
@@ -24,115 +23,33 @@ private unsafe void FinishInitialization()
2423 _sortHandle = ret > 0 ? handle : IntPtr . Zero ;
2524 }
2625
27- private unsafe string ChangeCase ( string s , bool toUpper )
26+ private unsafe void ChangeCase ( char * pSource , int pSourceLen , char * pResult , int pResultLen , bool toUpper )
2827 {
2928 Debug . Assert ( ! _invariantMode ) ;
30-
31- Debug . Assert ( s != null ) ;
32-
33- //
34- // Get the length of the string.
35- //
36- int nLengthInput = s . Length ;
37-
38- //
39- // Check if we have the empty string.
40- //
41- if ( nLengthInput == 0 )
42- {
43- return s ;
44- }
45-
46- int ret ;
29+ Debug . Assert ( pSource != null ) ;
30+ Debug . Assert ( pResult != null ) ;
31+ Debug . Assert ( pSourceLen >= 0 ) ;
32+ Debug . Assert ( pResultLen >= 0 ) ;
33+ Debug . Assert ( pSourceLen <= pResultLen ) ;
4734
4835 // Check for Invariant to avoid A/V in LCMapStringEx
4936 uint linguisticCasing = IsInvariantLocale ( _textInfoName ) ? 0 : LCMAP_LINGUISTIC_CASING ;
5037
51- //
52- // Create the result string.
53- //
54- string result = string . FastAllocateString ( nLengthInput ) ;
55-
56- fixed ( char * pSource = s )
57- fixed ( char * pResult = result )
58- {
59- ret = Interop . Kernel32 . LCMapStringEx ( _sortHandle != IntPtr . Zero ? null : _textInfoName ,
60- linguisticCasing | ( toUpper ? LCMAP_UPPERCASE : LCMAP_LOWERCASE ) ,
61- pSource ,
62- nLengthInput ,
63- pResult ,
64- nLengthInput ,
65- null ,
66- null ,
67- _sortHandle ) ;
68- }
69-
38+ int ret = Interop . Kernel32 . LCMapStringEx ( _sortHandle != IntPtr . Zero ? null : _textInfoName ,
39+ linguisticCasing | ( toUpper ? LCMAP_UPPERCASE : LCMAP_LOWERCASE ) ,
40+ pSource ,
41+ pSourceLen ,
42+ pResult ,
43+ pSourceLen ,
44+ null ,
45+ null ,
46+ _sortHandle ) ;
7047 if ( ret == 0 )
7148 {
7249 throw new InvalidOperationException ( SR . InvalidOperation_ReadOnly ) ;
7350 }
7451
75- Debug . Assert ( ret == nLengthInput , "Expected getting the same length of the original string" ) ;
76- return result ;
77- }
78-
79- internal unsafe void ChangeCase ( ReadOnlySpan < char > source , Span < char > destination , bool toUpper )
80- {
81- Debug . Assert ( ! _invariantMode ) ;
82- Debug . Assert ( destination . Length >= source . Length ) ;
83-
84- if ( source . IsEmpty )
85- {
86- return ;
87- }
88-
89- int ret ;
90-
91- // Check for Invariant to avoid A/V in LCMapStringEx
92- uint linguisticCasing = IsInvariantLocale ( _textInfoName ) ? 0 : LCMAP_LINGUISTIC_CASING ;
93-
94- fixed ( char * pSource = & MemoryMarshal . GetReference ( source ) )
95- fixed ( char * pResult = & MemoryMarshal . GetReference ( destination ) )
96- {
97- ret = Interop . Kernel32 . LCMapStringEx ( _sortHandle != IntPtr . Zero ? null : _textInfoName ,
98- linguisticCasing | ( toUpper ? LCMAP_UPPERCASE : LCMAP_LOWERCASE ) ,
99- pSource ,
100- source . Length ,
101- pResult ,
102- source . Length ,
103- null ,
104- null ,
105- _sortHandle ) ;
106- }
107-
108- if ( ret == 0 )
109- {
110- throw new InvalidOperationException ( SR . InvalidOperation_ReadOnly ) ;
111- }
112-
113- Debug . Assert ( ret == source . Length , "Expected getting the same length of the original span" ) ;
114- }
115-
116- private unsafe char ChangeCase ( char c , bool toUpper )
117- {
118- Debug . Assert ( ! _invariantMode ) ;
119-
120- char retVal = '\0 ' ;
121-
122- // Check for Invariant to avoid A/V in LCMapStringEx
123- uint linguisticCasing = IsInvariantLocale ( _textInfoName ) ? 0 : LCMAP_LINGUISTIC_CASING ;
124-
125- Interop . Kernel32 . LCMapStringEx ( _sortHandle != IntPtr . Zero ? null : _textInfoName ,
126- toUpper ? LCMAP_UPPERCASE | linguisticCasing : LCMAP_LOWERCASE | linguisticCasing ,
127- & c ,
128- 1 ,
129- & retVal ,
130- 1 ,
131- null ,
132- null ,
133- _sortHandle ) ;
134-
135- return retVal ;
52+ Debug . Assert ( ret == pSourceLen , "Expected getting the same length of the original string" ) ;
13653 }
13754
13855 // PAL Ends here
@@ -143,9 +60,6 @@ private unsafe char ChangeCase(char c, bool toUpper)
14360 private const uint LCMAP_LOWERCASE = 0x00000100 ;
14461 private const uint LCMAP_UPPERCASE = 0x00000200 ;
14562
146- private static bool IsInvariantLocale ( string localeName )
147- {
148- return localeName == "" ;
149- }
63+ private static bool IsInvariantLocale ( string localeName ) => localeName == "" ;
15064 }
15165}
0 commit comments