-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
StringUtil.cs
483 lines (419 loc) · 15.8 KB
/
StringUtil.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//------------------------------------------------------------------------------
// <copyright file="StringUtil.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
/*
* StringUtil class
*
* Copyright (c) 1999 Microsoft Corporation
*/
namespace System.Web.Util {
using System.Text;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Web.Hosting;
using System.Diagnostics.CodeAnalysis;
/*
* Various string handling utilities
*/
internal static class StringUtil {
internal static string CheckAndTrimString(string paramValue, string paramName) {
return CheckAndTrimString(paramValue, paramName, true);
}
internal static string CheckAndTrimString(string paramValue, string paramName, bool throwIfNull) {
return CheckAndTrimString(paramValue, paramName, throwIfNull, -1);
}
internal static string CheckAndTrimString(string paramValue, string paramName,
bool throwIfNull, int lengthToCheck) {
if (paramValue == null) {
if (throwIfNull) {
throw new ArgumentNullException(paramName);
}
return null;
}
string trimmedValue = paramValue.Trim();
if (trimmedValue.Length == 0) {
throw new ArgumentException(
SR.GetString(SR.PersonalizationProviderHelper_TrimmedEmptyString,
paramName));
}
if (lengthToCheck > -1 && trimmedValue.Length > lengthToCheck) {
throw new ArgumentException(
SR.GetString(SR.StringUtil_Trimmed_String_Exceed_Maximum_Length,
paramValue, paramName, lengthToCheck.ToString(CultureInfo.InvariantCulture)));
}
return trimmedValue;
}
internal static bool Equals(string s1, string s2) {
if (s1 == s2) {
return true;
}
if (String.IsNullOrEmpty(s1) && String.IsNullOrEmpty(s2)) {
return true;
}
return false;
}
unsafe internal static bool Equals(string s1, int offset1, string s2, int offset2, int length) {
if (offset1 < 0)
throw new ArgumentOutOfRangeException("offset1");
if (offset2 < 0)
throw new ArgumentOutOfRangeException("offset2");
if (length < 0)
throw new ArgumentOutOfRangeException("length");
if ((s1 == null ? 0 : s1.Length) - offset1 < length)
throw new ArgumentOutOfRangeException(SR.GetString(SR.InvalidOffsetOrCount, "offset1", "length"));
if ((s2 == null ? 0 : s2.Length) - offset2 < length)
throw new ArgumentOutOfRangeException(SR.GetString(SR.InvalidOffsetOrCount, "offset2", "length"));
if (String.IsNullOrEmpty(s1) || String.IsNullOrEmpty(s2))
return true;
fixed (char * pch1 = s1, pch2 = s2) {
char * p1 = pch1 + offset1, p2 = pch2 + offset2;
int c = length;
while (c-- > 0) {
if (*p1++ != *p2++)
return false;
}
}
return true;
}
internal static bool EqualsIgnoreCase(string s1, string s2) {
if (String.IsNullOrEmpty(s1) && String.IsNullOrEmpty(s2)) {
return true;
}
if (String.IsNullOrEmpty(s1) || String.IsNullOrEmpty(s2)) {
return false;
}
if(s2.Length != s1.Length) {
return false;
}
return 0 == string.Compare(s1, 0, s2, 0, s2.Length, StringComparison.OrdinalIgnoreCase);
}
internal static bool EqualsIgnoreCase(string s1, int index1, string s2, int index2, int length) {
return String.Compare(s1, index1, s2, index2, length, StringComparison.OrdinalIgnoreCase) == 0;
}
internal static string StringFromWCharPtr(IntPtr ip, int length) {
unsafe {
return new string((char *) ip, 0, length);
}
}
internal static string StringFromCharPtr(IntPtr ip, int length) {
return Marshal.PtrToStringAnsi(ip, length);
}
/*
* Determines if the string ends with the specified character.
* Fast, non-culture aware.
*/
internal static bool StringEndsWith(string s, char c) {
int len = s.Length;
return len != 0 && s[len - 1] == c;
}
/*
* Determines if the first string ends with the second string.
* Fast, non-culture aware.
*/
unsafe internal static bool StringEndsWith(string s1, string s2) {
int offset = s1.Length - s2.Length;
if (offset < 0) {
return false;
}
fixed (char * pch1=s1, pch2=s2) {
char * p1 = pch1 + offset, p2=pch2;
int c = s2.Length;
while (c-- > 0) {
if (*p1++ != *p2++)
return false;
}
}
return true;
}
/*
* Determines if the first string ends with the second string, ignoring case.
* Fast, non-culture aware.
*/
internal static bool StringEndsWithIgnoreCase(string s1, string s2) {
int offset = s1.Length - s2.Length;
if (offset < 0) {
return false;
}
return 0 == string.Compare(s1, offset, s2, 0, s2.Length, StringComparison.OrdinalIgnoreCase);
}
/*
* Determines if the string starts with the specified character.
* Fast, non-culture aware.
*/
internal static bool StringStartsWith(string s, char c) {
return s.Length != 0 && (s[0] == c);
}
/*
* Determines if the first string starts with the second string.
* Fast, non-culture aware.
*/
unsafe internal static bool StringStartsWith(string s1, string s2) {
if (s2.Length > s1.Length) {
return false;
}
fixed (char * pch1=s1, pch2=s2) {
char * p1 = pch1, p2=pch2;
int c = s2.Length;
while (c-- > 0) {
if (*p1++ != *p2++)
return false;
}
}
return true;
}
/*
* Determines if the first string starts with the second string, ignoring case.
* Fast, non-culture aware.
*/
internal static bool StringStartsWithIgnoreCase(string s1, string s2) {
if (String.IsNullOrEmpty(s1) || String.IsNullOrEmpty(s2)) {
return false;
}
if (s2.Length > s1.Length) {
return false;
}
return 0 == string.Compare(s1, 0, s2, 0, s2.Length, StringComparison.OrdinalIgnoreCase);
}
internal unsafe static void UnsafeStringCopy(string src, int srcIndex, char[] dest, int destIndex, int len) {
// We do not verify the parameters in this function, as the callers are assumed
// to have done so. This is important for users such as HttpWriter that already
// do the argument checking, and are called several times per request.
Debug.Assert(len >= 0, "len >= 0");
Debug.Assert(src != null, "src != null");
Debug.Assert(srcIndex >= 0, "srcIndex >= 0");
Debug.Assert(srcIndex + len <= src.Length, "src");
Debug.Assert(dest != null, "dest != null");
Debug.Assert(destIndex >= 0, "destIndex >= 0");
Debug.Assert(destIndex + len <= dest.Length, "dest");
int cb = len * sizeof(char);
fixed (char * pchSrc = src, pchDest = dest) {
byte* pbSrc = (byte *) (pchSrc + srcIndex);
byte* pbDest = (byte*) (pchDest + destIndex);
memcpyimpl(pbSrc, pbDest, cb);
}
}
internal static bool StringArrayEquals(string[] a, string [] b) {
if ((a == null) != (b == null)) {
return false;
}
if (a == null) {
return true;
}
int n = a.Length;
if (n != b.Length) {
return false;
}
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}
// This is copied from String.GetHashCode. We want our own copy, because the result of
// String.GetHashCode is not guaranteed to be the same from build to build. But we want a
// stable hash, since we persist things to disk (e.g. precomp scenario). VSWhidbey 399279.
internal static int GetStringHashCode(string s) {
unsafe {
fixed (char* src = s) {
int hash1 = (5381 << 16) + 5381;
int hash2 = hash1;
// 32bit machines.
int* pint = (int*)src;
int len = s.Length;
while (len > 0) {
hash1 = ((hash1 << 5) + hash1 + (hash1 >> 27)) ^ pint[0];
if (len <= 2) {
break;
}
hash2 = ((hash2 << 5) + hash2 + (hash2 >> 27)) ^ pint[1];
pint += 2;
len -= 4;
}
return hash1 + (hash2 * 1566083941);
}
}
}
internal static int GetNonRandomizedHashCode(string s, bool ignoreCase = false) {
// Preserve the default behavior when string hash randomization is off
if (!AppSettings.UseRandomizedStringHashAlgorithm) {
return ignoreCase ? StringComparer.InvariantCultureIgnoreCase.GetHashCode(s) : s.GetHashCode();
}
if (ignoreCase) {
s = s.ToLower(CultureInfo.InvariantCulture);
}
// Use our stable hash algorithm implementation
return GetStringHashCode(s);
}
// Need StringComparer implementation. It's very expensive to port the actual BCL code here
// Instead use the default AppDomain, because it doesn't have string hash randomization enabled.
// Marshal the call to reuse the default StringComparer behavior.
// PERF isn't optimal, so apply consideration!
[SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Justification = "We carefully control the callers.")]
internal static int GetNonRandomizedStringComparerHashCode(string s) {
// Preserve the default behavior when string hash randomization is off
if (!AppSettings.UseRandomizedStringHashAlgorithm) {
return StringComparer.InvariantCultureIgnoreCase.GetHashCode(s);
}
ApplicationManager appManager = HostingEnvironment.GetApplicationManager();
if (appManager != null) {
// Cross AppDomain call may cause marshaling of IPrincipal to fail. So strip out the exectuion context
int hashCode = 0;
ExecutionContextUtil.RunInNullExecutionContext(delegate {
hashCode = appManager.GetNonRandomizedStringComparerHashCode(s, ignoreCase:true);
});
return hashCode;
}
// Fall back to non-compat result
return GetStringHashCode(s.ToLower(CultureInfo.InvariantCulture));
}
internal static int GetNullTerminatedByteArray(Encoding enc, string s, out byte[] bytes)
{
bytes = null;
if (s == null)
return 0;
// Encoding.GetMaxByteCount is faster than GetByteCount, but will probably allocate more
// memory than needed. Working with small short-lived strings here, so that's probably ok.
bytes = new byte[enc.GetMaxByteCount(s.Length) + 1];
return enc.GetBytes(s, 0, s.Length, bytes, 0);
}
internal unsafe static void memcpyimpl(byte* src, byte* dest, int len) {
Debug.Assert(len >= 0, "Negative length in memcpyimpl!");
#if FEATURE_PAL
// Portable naive implementation
while (len-- > 0)
*dest++ = *src++;
#else
#if IA64
long dstAlign = (7 & (8 - (((long)dest) & 7))); // number of bytes to copy before dest is 8-byte aligned
while ((dstAlign > 0) && (len > 0))
{
// <
*dest++ = *src++;
len--;
dstAlign--;
}
if (len > 0)
{
long srcAlign = 8 - (((long)src) & 7);
if (srcAlign != 8)
{
if (4 == srcAlign)
{
while (len >= 4)
{
((int*)dest)[0] = ((int*)src)[0];
dest += 4;
src += 4;
len -= 4;
}
srcAlign = 2; // fall through to 2-byte copies
}
if ((2 == srcAlign) || (6 == srcAlign))
{
while (len >= 2)
{
((short*)dest)[0] = ((short*)src)[0];
dest += 2;
src += 2;
len -= 2;
}
}
while (len-- > 0)
{
*dest++ = *src++;
}
}
else
{
if (len >= 16)
{
do
{
((long*)dest)[0] = ((long*)src)[0];
((long*)dest)[1] = ((long*)src)[1];
dest += 16;
src += 16;
} while ((len -= 16) >= 16);
}
if(len > 0) // protection against negative len and optimization for len==16*N
{
if ((len & 8) != 0)
{
((long*)dest)[0] = ((long*)src)[0];
dest += 8;
src += 8;
}
if ((len & 4) != 0)
{
((int*)dest)[0] = ((int*)src)[0];
dest += 4;
src += 4;
}
if ((len & 2) != 0)
{
((short*)dest)[0] = ((short*)src)[0];
dest += 2;
src += 2;
}
if ((len & 1) != 0)
{
*dest++ = *src++;
}
}
}
}
#else
// <
if (len >= 16) {
do {
#if AMD64
((long*)dest)[0] = ((long*)src)[0];
((long*)dest)[1] = ((long*)src)[1];
#else
((int*)dest)[0] = ((int*)src)[0];
((int*)dest)[1] = ((int*)src)[1];
((int*)dest)[2] = ((int*)src)[2];
((int*)dest)[3] = ((int*)src)[3];
#endif
dest += 16;
src += 16;
} while ((len -= 16) >= 16);
}
if(len > 0) // protection against negative len and optimization for len==16*N
{
if ((len & 8) != 0) {
#if AMD64
((long*)dest)[0] = ((long*)src)[0];
#else
((int*)dest)[0] = ((int*)src)[0];
((int*)dest)[1] = ((int*)src)[1];
#endif
dest += 8;
src += 8;
}
if ((len & 4) != 0) {
((int*)dest)[0] = ((int*)src)[0];
dest += 4;
src += 4;
}
if ((len & 2) != 0) {
((short*)dest)[0] = ((short*)src)[0];
dest += 2;
src += 2;
}
if ((len & 1) != 0)
*dest++ = *src++;
}
#endif
#endif // FEATURE_PAL
}
internal static string[] ObjectArrayToStringArray(object[] objectArray) {
String[] stringKeys = new String[objectArray.Length];
objectArray.CopyTo(stringKeys, 0);
return stringKeys;
}
}
}