dotnetanywhere-wb /corlib/System/String.cs

Language C# Lines 677
MD5 Hash c311cffec731f522cd6dccaf3770c677 Estimated Cost $13,570 (why?)
Repository https://bitbucket.org/cosi2/dotnetanywhere-wb.git View Raw File
  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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
#if !LOCALTEST

using System.Runtime.CompilerServices;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;

namespace System {
	public class String : ICloneable, IEnumerable, IEnumerable<char>,
		IComparable, IComparable<string>, IEquatable<string> {

		public static readonly string Empty = "";

		public static bool IsNullOrEmpty(string value) {
			return (value == null) || (value.length == 0);
		}

		// This field must be the only field, to tie up with C code
		private int length;

		//public int Length{get{return length;}}

		[MethodImpl(MethodImplOptions.InternalCall)]
		extern public String(char c, int count);

		[MethodImpl(MethodImplOptions.InternalCall)]
		extern public String(char[] chars);

		[MethodImpl(MethodImplOptions.InternalCall)]
		extern public String(char[] chars, int startIndex, int length);
		
		#region Private Internal Calls

		[MethodImpl(MethodImplOptions.InternalCall)]
		extern private String(string str, int startIndex, int length);

		[MethodImpl(MethodImplOptions.InternalCall)]
		extern private static string InternalConcat(string str0, string str1);

		[MethodImpl(MethodImplOptions.InternalCall)]
		extern private string InternalReplace(string oldValue, string newValue);

		// trimType: bit 0 = start; bit 1 = end
		[MethodImpl(MethodImplOptions.InternalCall)]
		extern private string InternalTrim(char[] trimChars, int trimType);

		[MethodImpl(MethodImplOptions.InternalCall)]
		extern private int InternalIndexOf(char value, int startIndex, int count, bool forwards);

		[MethodImpl(MethodImplOptions.InternalCall)]
		extern private int InternalIndexOfAny(char[] anyOf, int startIndex, int count, bool forward);

		#endregion

		public int Length {
			get {
				return this.length;
			}
		}

		[IndexerName("Chars")]
		extern public char this[int index] {
			[MethodImpl(MethodImplOptions.InternalCall)]
			get;
		}

		#region Misc Methods

		public static string Join(string separator, string[] value) {
			return Join(separator, value, 0, value.Length);
		}

		//public override int GetHashCode(){
		//	if(Length==0) return 0;
		//	return (int)Chars[0] + 13*(int)Chars[Length-1];
		//}

		public static string Join(string separator, string[] value, int startIndex, int count) {
			StringBuilder sb = new StringBuilder();
			for (int i = startIndex; i < count; i++) {
				sb.Append(value[i]);
				sb.Append(separator);
			}
			return sb.ToString(0, sb.Length - separator.Length);
		}

		public string[] Split(params char[] separator) {
			return this.Split(separator, int.MaxValue);
		}

		public string[] Split(char[] separator, int count) {
			if (count < 0) {
				throw new ArgumentException("count");
			}
			if (count == 0) {
				return new string[0];
			}
			if (separator == null || separator.Length == 0) {
				separator = char.WhiteChars;
			}

			List<string> ret = new List<string>();

			int pos = 0;
			for (; count > 0; count--) {
				int sepPos = this.IndexOfAny(separator, pos);
				if (sepPos < 0) {
					ret.Add(new string(this, pos, this.length - pos));
					break;
				}
				ret.Add(new string(this, pos, sepPos - pos));
				pos = sepPos + 1;
			}

			return ret.ToArray();
		}

		public bool StartsWith(string str) {
			if(str.length > this.length) return false;
			return this.Substring(0, str.length) == str;
		}

		public bool EndsWith(string str) {
			if(str.length > this.length) return false;
 			return this.Substring(this.length - str.length, str.length) == str;
		}

		#endregion

		#region Concat Methods

		public static string Concat(string str0, string str1) {
			if (str0 == null) {
				return str1 ?? string.Empty;
			}
			if (str1 == null) {
				return str0;
			}
			return InternalConcat(str0, str1);
		}

		public static string Concat(string str0, string str1, string str2) {
			return Concat(Concat(str0, str1), str2);
		}

		public static string Concat(string str0, string str1, string str2, string str3) {
			return Concat(Concat(str0, str1), Concat(str2, str3));
		}

		public static string Concat(params string[] values) {
			if (values == null) {
				throw new ArgumentNullException("args");
			}
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < values.Length; i++) {
				sb.Append(values[i]);
			}
			return sb.ToString();
		}

		public static string Concat(object obj0) {
			return obj0.ToString();
		}

		public static string Concat(object obj0, object obj1) {
			string str0 = (obj0 == null) ? null : obj0.ToString();
			string str1 = (obj1 == null) ? null : obj1.ToString();
			if (str0 == null) {
				return str1 ?? string.Empty;
			}
			if (str1 == null) {
				return str0;
			}
			return InternalConcat(str0, str1);
		}

		public static string Concat(object obj0, object obj1, object obj2) {
			return Concat(new object[] { obj0, obj1, obj2 });
		}

		public static string Concat(object obj0, object obj1, object obj2, object obj3) {
			return Concat(new object[] { obj0, obj1, obj2, obj3 });
		}

		public static string Concat(params object[] objs) {
			if (objs == null) {
				throw new ArgumentNullException("args");
			}
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < objs.Length; i++) {
				sb.Append(objs[i]);
			}
			return sb.ToString();
		}

		#endregion

		#region Trim Methods

		public string Trim(params char[] trimChars) {
			if (trimChars == null || trimChars.Length == 0) {
				trimChars = char.WhiteChars;
			}
			return InternalTrim(trimChars, 3);
		}

		public string Trim() {
			return InternalTrim(char.WhiteChars, 3);
		}

		public string TrimStart(params char[] trimChars) {
			if (trimChars == null || trimChars.Length == 0) {
				trimChars = char.WhiteChars;
			}
			return InternalTrim(trimChars, 1);
		}

		public string TrimEnd(params char[] trimChars) {
			if (trimChars == null || trimChars.Length == 0) {
				trimChars = char.WhiteChars;
			}
			return InternalTrim(trimChars, 2);
		}

		#endregion

		#region Substring Methods

		public string Substring(int startIndex) {
			if (startIndex < 0 || startIndex > this.length) {
				throw new ArgumentOutOfRangeException();
			}

			return new string(this, startIndex, this.length - startIndex);
		}

		public string Substring(int startIndex, int length) {
			if (startIndex < 0 || length < 0 || startIndex + length > this.length) {
				throw new ArgumentOutOfRangeException();
			}

			return new string(this, startIndex, length);
		}

		#endregion

		#region Format Methods

		public static string Format(string format, object obj0) {
			return Format(null, format, new object[] { obj0 });
		}

		public static string Format(string format, object obj0, object obj1) {
			return Format(null, format, new object[] { obj0, obj1 });
		}

		public static string Format(string format, object obj0, object obj1, object obj2) {
			return Format(null, format, new object[] { obj0, obj1, obj2 });
		}

		public static string Format(string format, params object[] args) {
			return Format(null, format, args);
		}

		public static string Format(IFormatProvider provider, string format, params object[] args) {
			StringBuilder sb = new StringBuilder();
			StringHelper.FormatHelper(sb, provider, format, args);
			return sb.ToString();
		}

		#endregion

		#region Replace & Remove Methods

		public string Replace(char oldChar, char newChar) {
			StringBuilder sb = new StringBuilder(this);
			return sb.Replace(oldChar, newChar).ToString();
		}

		public string Replace(string oldValue, string newValue) {
			if (oldValue == null) {
				throw new ArgumentNullException("oldValue");
			}
			if (oldValue.Length == 0) {
				throw new ArgumentException("oldValue is an empty string.");
			}
			if (this.length == 0) {
				return this;
			}
			if (newValue == null) {
				newValue = string.Empty;
			}
			return InternalReplace(oldValue, newValue);
		}

		public string Remove(int startIndex) {
			if (startIndex < 0 || startIndex >= this.length) {
				throw new ArgumentOutOfRangeException("startIndex");
			}
			return new string(this, 0, startIndex);
		}

		public string Remove(int startIndex, int count) {
			if (startIndex < 0 || count < 0 || startIndex + count >= this.length) {
				throw new ArgumentOutOfRangeException();
			}
			int pos2 = startIndex+count;
			return (new string(this, 0, startIndex)) + (new string(this, pos2, this.length - pos2));
		}

		#endregion

		#region Compare and CompareOrdinal Methods

		public static int Compare(string strA, string strB) {
			return CompareOrdinal(strA, strB);
		}

		public static int Compare(string strA, string strB, bool IgnoreCase) {
			if(IgnoreCase)
			{//HACK: Horribly inefficient.
				strA = strA.ToLower();
				strB = strB.ToLower();
			}
			return CompareOrdinal(strA, strB);
		}

		public static int Compare(string strA, string strB, StringComparison type) {
			return Compare(strA, 0, strB, 0, Math.Max(strA.Length,strB.Length), type);
		}
		public static int Compare(string strA, int indexA, string strB, int indexB, int length) {
			return CompareOrdinal(strA.Substring(indexA, length), strB.Substring(indexB, length));
		}

		public static int CompareOrdinal(string strA, string strB) {
			if (strA == null) {
				if (strB == null) {
					return 0;
				}
				return -1;
			}
			if (strB == null) {
				return 1;
			}
			int top = Math.Min(strA.Length, strB.Length);
			for (int i = 0; i < top; i++) {
				if (strA[i] != strB[i]) {
					return (strA[i] - strB[i]);
				}
			}
			return strA.Length - strB.Length;
		}

		public static int CompareOrdinal (String strA, int indexA, String strB, int indexB, int length)
		{
			if ((indexA > strA.Length) || (indexB > strB.Length) || (indexA < 0) || (indexB < 0) || (length < 0))
				throw new ArgumentOutOfRangeException ();

			return CompareOrdinalUnchecked (strA, indexA, length, strB, indexB, length);
		}

		public static int CompareOrdinalCaseInsensitive (String strA, int indexA, String strB, int indexB, int length)
		{
			//HACK:Horribly inefficient
			strA = strA.ToLower();
			strB = strB.ToLower();
			return CompareOrdinal (strA, indexA, strB, indexB, length);
		}

		public static int Compare (string strA, int indexA, string strB, int indexB, int length, StringComparison comparisonType)
		{
			switch (comparisonType) {
			case StringComparison.CurrentCulture:
			case StringComparison.InvariantCulture:
			case StringComparison.Ordinal:
				return CompareOrdinal (strA, indexA, strB, indexB, length);
			case StringComparison.CurrentCultureIgnoreCase:
			case StringComparison.InvariantCultureIgnoreCase:
			case StringComparison.OrdinalIgnoreCase:
				return CompareOrdinalCaseInsensitive (strA, indexA, strB, indexB, length);
			default:
				string msg = String.Format ("Invalid value '{0}' for StringComparison", comparisonType);
				throw new ArgumentException (msg, "comparisonType");
			}
		}

		internal static /*unsafe*/ int CompareOrdinalUnchecked (String strA, int indexA, int lenA, String strB, int indexB, int lenB)
		{
			if (strA == null) {
				if (strB == null)
					return 0;
				else
					return -1;
			} else if (strB == null) {
				return 1;
			}
			int lengthA = Math.Min (lenA, strA.Length - indexA);
			int lengthB = Math.Min (lenB, strB.Length - indexB);

			if (lengthA == lengthB && Object.ReferenceEquals (strA, strB))
				return 0;

			//fixed (char* aptr = strA, bptr = strB) {
				int ap = 0/*aptr*/ + indexA;
				int end = ap + Math.Min (lengthA, lengthB);
				int bp = 0/*bptr*/ + indexB;
				while (ap < end) {
					if (strA[ap] != strB[bp])
						return strA[ap] - strB[bp];
					ap++;
					bp++;
				}
				return lengthA - lengthB;
			//}
		}

 

 


		#endregion

		#region IndexOf... Methods

		public int IndexOf(string value) {
			return IndexOf(value, 0, this.length);
		}

		public int IndexOf(string value, int startIndex) {
			return IndexOf(value, startIndex, this.length - startIndex);
		}

		public int IndexOf(string value, int startIndex, int count) {
			if (value == null) {
				throw new ArgumentNullException("value");
			}
			if (startIndex < 0 || count < 0 || startIndex + count > this.length) {
				throw new ArgumentOutOfRangeException();
			}
			if (value.length == 0) {
				return startIndex;
			}
			int valueLen = value.length;
			int finalIndex = startIndex + count - valueLen + 1;
			char char0 = value[0];
			for (int i = startIndex; i < finalIndex; i++) {
				if (this[i] == char0) {
					bool ok = true;
					for (int j = 1; j < valueLen; j++) {
						if (this[i + j] != value[j]) {
							ok = false;
							break;
						}
					}
					if (ok) {
						return i;
					}
				}
			}
			return -1;
		}

		public int IndexOf(char value) {
			return this.IndexOf(value, 0, this.length, true);
		}

		public int IndexOf(char value, int startIndex) {
			return this.IndexOf(value, startIndex, this.length - startIndex, true);
		}

		public int IndexOf(char value, int startIndex, int count) {
			return this.IndexOf(value, startIndex, count, true);
		}

		public int LastIndexOf(char value) {
			return this.IndexOf(value, 0, this.length, false);
		}

		public int LastIndexOf(char value, int startIndex) {
			return this.IndexOf(value, startIndex, this.length - startIndex, false);
		}

		public int LastIndexOf(char value, int startIndex, int count) {
			return this.IndexOf(value, startIndex, count, false);
		}

		private int IndexOf(char value, int startIndex, int count, bool forwards) {
			if (startIndex < 0 || count < 0 || startIndex + count > this.length) {
				throw new ArgumentOutOfRangeException();
			}
			return this.InternalIndexOf(value, startIndex, count, forwards);
		}

		public int IndexOfAny(char[] anyOf) {
			return this.IndexOfAny(anyOf, 0, this.length, true);
		}

		public int IndexOfAny(char[] anyOf, int startIndex) {
			return this.IndexOfAny(anyOf, startIndex, this.length - startIndex, true);
		}

		public int IndexOfAny(char[] anyOf, int startIndex, int count) {
			return this.IndexOfAny(anyOf, startIndex, count, true);
		}

		public int LastIndexOfAny(char[] anyOf) {
			return this.IndexOfAny(anyOf, 0, this.length, false);
		}

		public int LastIndexOfAny(char[] anyOf, int startIndex) {
			return this.IndexOfAny(anyOf, startIndex, this.length - startIndex, false);
		}

		public int LastIndexOfAny(char[] anyOf, int startIndex, int count) {
			return this.IndexOfAny(anyOf, startIndex, count, false);
		}

		private int IndexOfAny(char[] anyOf, int startIndex, int count, bool forward) {
			if (startIndex < 0 || count < 0 || startIndex + count > this.length) {
				throw new ArgumentOutOfRangeException();
			}
			/*int anyOfLen = anyOf.Length;
			int finIndex = (forward) ? (startIndex + count) : (startIndex - 1);
			int inc = (forward) ? 1 : -1;
			for (int i = (forward) ? startIndex : (startIndex + count - 1); i != finIndex; i += inc) {
				char c = this[i];
				for (int j = 0; j < anyOfLen; j++) {
					if (c == anyOf[j]) {
						return i;
					}
				}
			}
			return -1;*/
			return this.InternalIndexOfAny(anyOf, startIndex, count, forward);
		}

		#endregion

		#region Case methods

		public string ToLower() {
			return ToLower(CultureInfo.CurrentCulture);
		}

		public string ToLower(CultureInfo culture) {
			if (culture == null) {
				throw new ArgumentNullException("culture");
			}
			if (culture.LCID == 0x007f) {
				return ToLowerInvariant();
			}
			return culture.TextInfo.ToLower(this);
		}

		public string ToLowerInvariant() {
			int len = this.length;
			StringBuilder sb = new StringBuilder(len);
			for (int i = 0; i < len; i++) {
				sb.Append(char.ToLowerInvariant(this[i]));
			}
			return sb.ToString();
		}

		public string ToUpper() {
			return ToLower(CultureInfo.CurrentCulture);
		}

		public string ToUpper(CultureInfo culture) {
			if (culture == null) {
				throw new ArgumentNullException("culture");
			}
			if (culture.LCID == 0x007f) {
				return ToUpperInvariant();
			}
			return culture.TextInfo.ToUpper(this);
		}

		public string ToUpperInvariant() {
			int len = this.length;
			StringBuilder sb = new StringBuilder(len);
			for (int i = 0; i < len; i++) {
				sb.Append(char.ToUpperInvariant(this[i]));
			}
			return sb.ToString();
		}

		#endregion

		#region Overrides and Operators

		public override string ToString() {
			return this;
		}

		public override bool Equals(object obj) {
			return Equals(this, obj as string);
		}

		public static bool operator ==(string a, string b) {
			return Equals(a, b);
		}

		public static bool operator !=(string a, string b) {
			return !Equals(a, b);
		}

		[MethodImpl(MethodImplOptions.InternalCall)]
		extern public static bool Equals(string a, string b);

		[MethodImpl(MethodImplOptions.InternalCall)]
		extern public override int GetHashCode();

		#endregion

		#region IClonable Members

		public object Clone() {
			return this;
		}

		#endregion

		#region IComparable Members

		public int CompareTo(object value) {
			if (value == null) {
				return 1;
			}
			if (!(value is string)) {
				throw new ArgumentException();
			}
			return string.Compare(this, (string)value);
		}

		public int CompareTo(string value) {
			if (value == null)
				return 1;

			return string.Compare(this, value);
		}

		#endregion

		#region IEquatable<string> Members

		public bool Equals(string other) {
			return Equals(this, other);
		}

		#endregion

		#region IEnumerable Members

		public IEnumerator GetEnumerator() {
			return new CharEnumerator(this);
		}

		IEnumerator<char> IEnumerable<char>.GetEnumerator() {
			return new CharEnumerator(this);
		}

		#endregion
		public char [] ToCharArray()
		{
			char[] chars = new char[Length];
			for(int i=0;i<Length;i++)
			{
				chars[i] = this[i];
			}
			return chars;
		}
	}
}
#endif
Back to Top