Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Normalization.cs
Go to the documentation of this file.
3using System.Text;
4
6
7internal static class Normalization
8{
9 internal static bool IsNormalized(string strInput, NormalizationForm normalizationForm)
10 {
12 {
13 return true;
14 }
16 {
17 return IcuIsNormalized(strInput, normalizationForm);
18 }
19 return NlsIsNormalized(strInput, normalizationForm);
20 }
21
22 internal static string Normalize(string strInput, NormalizationForm normalizationForm)
23 {
25 {
26 return strInput;
27 }
29 {
30 return IcuNormalize(strInput, normalizationForm);
31 }
32 return NlsNormalize(strInput, normalizationForm);
33 }
34
35 private unsafe static bool IcuIsNormalized(string strInput, NormalizationForm normalizationForm)
36 {
37 ValidateArguments(strInput, normalizationForm);
38 int num;
39 fixed (char* src = strInput)
40 {
41 num = Interop.Globalization.IsNormalized(normalizationForm, src, strInput.Length);
42 }
43 if (num == -1)
44 {
46 }
47 return num == 1;
48 }
49
50 private unsafe static string IcuNormalize(string strInput, NormalizationForm normalizationForm)
51 {
52 ValidateArguments(strInput, normalizationForm);
53 char[] array = null;
54 try
55 {
56 Span<char> span = ((strInput.Length > 512) ? ((Span<char>)(array = ArrayPool<char>.Shared.Rent(strInput.Length))) : stackalloc char[512]);
57 Span<char> span2 = span;
58 for (int i = 0; i < 2; i++)
59 {
60 int num;
61 fixed (char* src = strInput)
62 {
63 fixed (char* dstBuffer = &MemoryMarshal.GetReference(span2))
64 {
65 num = Interop.Globalization.NormalizeString(normalizationForm, src, strInput.Length, dstBuffer, span2.Length);
66 }
67 }
68 if (num == -1)
69 {
71 }
72 if (num <= span2.Length)
73 {
74 ReadOnlySpan<char> readOnlySpan = span2.Slice(0, num);
75 return readOnlySpan.SequenceEqual(strInput) ? strInput : new string(readOnlySpan);
76 }
77 if (i == 0)
78 {
79 if (array != null)
80 {
81 char[] array2 = array;
82 array = null;
83 ArrayPool<char>.Shared.Return(array2);
84 }
85 span2 = (array = ArrayPool<char>.Shared.Rent(num));
86 }
87 }
89 }
90 finally
91 {
92 if (array != null)
93 {
95 }
96 }
97 }
98
99 private static void ValidateArguments(string strInput, NormalizationForm normalizationForm)
100 {
102 {
103 }
104 if (normalizationForm != NormalizationForm.FormC && normalizationForm != NormalizationForm.FormD && normalizationForm != NormalizationForm.FormKC && normalizationForm != NormalizationForm.FormKD)
105 {
106 throw new ArgumentException(SR.Argument_InvalidNormalizationForm, "normalizationForm");
107 }
108 if (HasInvalidUnicodeSequence(strInput))
109 {
111 }
112 }
113
114 private static bool HasInvalidUnicodeSequence(string s)
115 {
116 for (int i = 0; i < s.Length; i++)
117 {
118 char c = s[i];
119 if (c < '\ud800')
120 {
121 continue;
122 }
123 if (c == '\ufffe')
124 {
125 return true;
126 }
127 if (char.IsLowSurrogate(c))
128 {
129 return true;
130 }
131 if (char.IsHighSurrogate(c))
132 {
133 if (i + 1 >= s.Length || !char.IsLowSurrogate(s[i + 1]))
134 {
135 return true;
136 }
137 i++;
138 }
139 }
140 return false;
141 }
142
143 private unsafe static bool NlsIsNormalized(string strInput, NormalizationForm normalizationForm)
144 {
145 Interop.BOOL bOOL;
146 fixed (char* source = strInput)
147 {
148 bOOL = Interop.Normaliz.IsNormalizedString(normalizationForm, source, strInput.Length);
149 }
150 int lastPInvokeError = Marshal.GetLastPInvokeError();
151 switch (lastPInvokeError)
152 {
153 case 87:
154 case 1113:
155 if (normalizationForm != NormalizationForm.FormC && normalizationForm != NormalizationForm.FormD && normalizationForm != NormalizationForm.FormKC && normalizationForm != NormalizationForm.FormKD)
156 {
157 throw new ArgumentException(SR.Argument_InvalidNormalizationForm, "normalizationForm");
158 }
160 case 8:
161 throw new OutOfMemoryException();
162 default:
163 throw new InvalidOperationException(SR.Format(SR.UnknownError_Num, lastPInvokeError));
164 case 0:
165 return bOOL != Interop.BOOL.FALSE;
166 }
167 }
168
169 private unsafe static string NlsNormalize(string strInput, NormalizationForm normalizationForm)
170 {
171 if (strInput.Length == 0)
172 {
173 return string.Empty;
174 }
175 char[] array = null;
176 try
177 {
178 Span<char> span = ((strInput.Length > 512) ? ((Span<char>)(array = ArrayPool<char>.Shared.Rent(strInput.Length))) : stackalloc char[512]);
179 Span<char> span2 = span;
180 while (true)
181 {
182 int num;
183 fixed (char* source = strInput)
184 {
185 fixed (char* destination = &MemoryMarshal.GetReference(span2))
186 {
187 num = Interop.Normaliz.NormalizeString(normalizationForm, source, strInput.Length, destination, span2.Length);
188 }
189 }
190 int lastPInvokeError = Marshal.GetLastPInvokeError();
191 switch (lastPInvokeError)
192 {
193 case 0:
194 {
195 ReadOnlySpan<char> readOnlySpan = span2.Slice(0, num);
196 return readOnlySpan.SequenceEqual(strInput) ? strInput : new string(readOnlySpan);
197 }
198 case 122:
199 num = Math.Abs(num);
200 if (array != null)
201 {
202 char[] array2 = array;
203 array = null;
204 ArrayPool<char>.Shared.Return(array2);
205 }
206 break;
207 case 87:
208 case 1113:
209 if (normalizationForm != NormalizationForm.FormC && normalizationForm != NormalizationForm.FormD && normalizationForm != NormalizationForm.FormKC && normalizationForm != NormalizationForm.FormKD)
210 {
211 throw new ArgumentException(SR.Argument_InvalidNormalizationForm, "normalizationForm");
212 }
214 case 8:
215 throw new OutOfMemoryException();
216 default:
217 throw new InvalidOperationException(SR.Format(SR.UnknownError_Num, lastPInvokeError));
218 }
219 span2 = (array = ArrayPool<char>.Shared.Rent(num));
220 }
221 }
222 finally
223 {
224 if (array != null)
225 {
227 }
228 }
229 }
230}
static unsafe int IsNormalized(NormalizationForm normalizationForm, char *src, int srcLen)
static unsafe int NormalizeString(NormalizationForm normalizationForm, char *src, int srcLen, char *dstBuffer, int dstBufferCapacity)
static unsafe int NormalizeString(NormalizationForm normForm, char *source, int sourceLength, char *destination, int destinationLength)
static unsafe BOOL IsNormalizedString(NormalizationForm normForm, char *source, int length)
static ArrayPool< T > Shared
Definition ArrayPool.cs:7
static unsafe string NlsNormalize(string strInput, NormalizationForm normalizationForm)
static unsafe bool NlsIsNormalized(string strInput, NormalizationForm normalizationForm)
static string Normalize(string strInput, NormalizationForm normalizationForm)
static unsafe bool IcuIsNormalized(string strInput, NormalizationForm normalizationForm)
static bool IsNormalized(string strInput, NormalizationForm normalizationForm)
static unsafe string IcuNormalize(string strInput, NormalizationForm normalizationForm)
static bool HasInvalidUnicodeSequence(string s)
static void ValidateArguments(string strInput, NormalizationForm normalizationForm)
static double Abs(double value)
static string Argument_InvalidCharSequenceNoIndex
Definition SR.cs:92
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Argument_InvalidNormalizationForm
Definition SR.cs:718
static string UnknownError_Num
Definition SR.cs:2028
Definition SR.cs:7
Span< T > Slice(int start)
Definition Span.cs:271
int Length
Definition Span.cs:70