-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathgstring.cs
1218 lines (1041 loc) · 41.9 KB
/
gstring.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
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
//#define DBG
// gstring (gcfreestring) is a string wrapper that uses pointers to mutate the string when performing misc string operations
// the purpose is to be able to perform most the common operations we do no strings (concat, format, replace, etc)
// without any allocation.
// gstring is not meant to be stored as member variables, but to quickly declare them in a 'gstring block', use them
// for whatever string operation you want, then dispose of them.
// The nice thing is that you don't have to manually dispose of gstrings, once you're in a block all assignments are
// registered so that when the block/scope ends all used gstrings are disposed.
//
// But what if you wanted to keep/store the result you calculated and not dispose of them?
// Well this is where 'intern' comes in - basically there's a runtime intern (cache) table
// of strings (similar to .NET's string const intern table).
// string str = result.Intern();
// Which basically says, if the string is in the intern (cache) table, return it
// otherwise allocate new memory for it and store it in the table, next time we ask for it, it's there.
// The nice thing about interning is that you could pre-intern your strings via the static method gstring.Intern
//
// NOTES:
// 1- The class is not designed with concurrency/threading in mind, it's meant to be used in Unity
// 2- Cultural stuff I did not consider as well
// 3- Again, you shouldn't have gstring members in your class. All gstring instances are meant to be disposed.
// You just quickly open up a gstring.Block() and use gstrings in it, if you want to store a result you get
// back from a gstring operation use Intern
namespace System
{
public class gstring
{
static Dictionary<int, Stack<gstring>> g_cache;
static Stack<gstring_block> g_blocks;
static List<string> g_intern_table;
static gstring_block g_current_block;
static List<int> g_finds;
static gstring[] g_format_args;
const int INITIAL_BLOCK_CAPACITY = 32;
const int INITIAL_CACHE_CAPACITY = 128;
const int INITIAL_STACK_CAPACITY = 48;
const int INITIAL_INTERN_CAPACITY = 256;
const char NEW_ALLOC_CHAR = 'X';
[NonSerialized] string _value;
[NonSerialized] bool _disposed;
internal gstring()
{
throw new NotSupportedException();
}
internal gstring(int length)
{
_value = new string(NEW_ALLOC_CHAR, length);
}
static gstring()
{
Initialize(INITIAL_CACHE_CAPACITY,
INITIAL_STACK_CAPACITY,
INITIAL_BLOCK_CAPACITY,
INITIAL_INTERN_CAPACITY);
g_finds = new List<int>(10);
g_format_args = new gstring[10];
}
internal void dispose()
{
if (_disposed)
throw new ObjectDisposedException(this);
// At this point there *must* be a stack whose length is equal to ours
// otherwise we wouldn't exist
var stack = g_cache[Length];
stack.Push(this);
#if DBG
if (log != null)
log("Disposed: " + _value + " Length=" + Length + " Stack=" + stack.Count);
#endif
memcpy(_value, NEW_ALLOC_CHAR);
_disposed = true;
}
internal static gstring get(string value)
{
if (value == null)
return null;
#if DBG
if (log != null)
log("Getting: " + value);
#endif
var result = get(value.Length);
memcpy(dst: result, src: value);
return result;
}
internal static string __intern(string value)
{
int idx = g_intern_table.IndexOf(value);
if (idx != -1)
return g_intern_table[idx];
string interned = new string(NEW_ALLOC_CHAR, value.Length);
memcpy(interned, value);
g_intern_table.Add(interned);
#if DBG
if (log != null)
log("Interned: " + value);
#endif
return interned;
}
internal static gstring get(int length)
{
if (g_current_block == null)
throw new InvalidOperationException("Getting gstrings must be done in a gstring_block. Make sure you do a using(gstring.block())");
if (length <= 0)
throw new InvalidOperationException("Invalid length: " + length);
gstring result;
Stack<gstring> stack;
if (!g_cache.TryGetValue(length, out stack))
{
stack = new Stack<gstring>(INITIAL_STACK_CAPACITY);
for (int i = 0; i < INITIAL_STACK_CAPACITY; i++)
stack.Push(new gstring(length));
g_cache[length] = stack;
result = stack.Pop();
}
else
{
if (stack.Count == 0)
{
if (Log != null)
Log("Stack=0 Allocating new gstring Length=" + length);
result = new gstring(length);
}
else
{
result = stack.Pop();
#if DBG
if (log != null)
log("Popped Length=" + length + " Stack=" + stack.Count);
#endif
}
}
result._disposed = false;
g_current_block.push(result);
return result;
}
internal static int get_digit_count(int value)
{
int cnt;
for (cnt = 1; (value /= 10) > 0; cnt++);
return cnt;
}
internal static int internal_index_of(string input, char value, int start)
{
return internal_index_of(input, value, start, input.Length - start);
}
internal static int internal_index_of(string input, string value)
{
return internal_index_of(input, value, 0, input.Length);
}
internal static int internal_index_of(string input, string value, int start)
{
return internal_index_of(input, value, start, input.Length - start);
}
internal unsafe static gstring internal_format(string input, int num_args)
{
// "{0} {1}", "Hello", "World" ->
// "xxxxxxxxxxx"
// "Helloxxxxxx"
// "Hello xxxxx"
// "Hello World"
// "Player={0} Id={1}", "Jon", 10 ->
// "xxxxxxxxxxxxxxxx"
// "Player=xxxxxxxxx"
// "Player=Jonxxxxxx"
// "Player=Jon Id=xx"
// "Player=Jon Id=10"
if (input == null)
throw new ArgumentNullException("value");
int new_len = input.Length - 3 * num_args;
for (int i = 0; i < num_args; i++)
{
gstring arg = g_format_args[i];
new_len += arg.Length;
}
gstring result = get(new_len);
string res_value = result._value;
int brace_idx = -3;
for(int i = 0, j = 0, x = 0; x < num_args; x++)
{
string arg = g_format_args[x]._value;
brace_idx = internal_index_of(input, '{', brace_idx + 3);
if (brace_idx == -1)
throw new InvalidOperationException("Couldn't find open brace for argument " + arg);
if (brace_idx + 2 >= input.Length || input[brace_idx + 2] != '}')
throw new InvalidOperationException("Couldn't find close brace for argument " + arg);
fixed(char* ptr_input = input)
{
fixed(char* ptr_result = res_value)
{
for(int k = 0; i < new_len; )
{
if (j < brace_idx)
ptr_result[i++] = ptr_input[j++];
else
{
ptr_result[i++] = arg[k++];
if (k == arg.Length)
{
j += 3;
break;
}
}
}
}
}
}
return result;
}
internal unsafe static int internal_index_of(string input, char value, int start, int count)
{
if (start < 0 || start >= input.Length)
throw new ArgumentOutOfRangeException("start");
if (start + count > input.Length)
throw new ArgumentOutOfRangeException("count=" + count + " start+count=" + start + count);
fixed (char* ptr_this = input)
{
int end = start + count;
for(int i = start; i < end; i++)
if (ptr_this[i] == value)
return i;
return -1;
}
}
internal unsafe static int internal_index_of(string input, string value, int start, int count)
{
int input_len = input.Length;
if (start < 0 || start >= input_len)
throw new ArgumentOutOfRangeException("start");
if (count < 0 || start + count > input_len)
throw new ArgumentOutOfRangeException("count=" + count + " start+count=" + (start + count));
if (count == 0)
return -1;
fixed (char* ptr_input = input)
{
fixed (char* ptr_value = value)
{
int found = 0;
int end = start + count;
for(int i = start; i < end; i++)
{
for(int j = 0; j < value.Length && i + j < input_len; j++)
{
if (ptr_input[i + j] == ptr_value[j])
{
found++;
if (found == value.Length)
return i;
continue;
}
if (found > 0)
break;
}
}
return -1;
}
}
}
internal unsafe static gstring internal_remove(string input, int start, int count)
{
if (start < 0 || start >= input.Length)
throw new ArgumentOutOfRangeException("start=" + start + " Length=" + input.Length);
if (count < 0 || start + count > input.Length)
throw new ArgumentOutOfRangeException("count=" + count + " start+count=" + (start + count) + " Length=" + input.Length);
if (count == 0)
return input;
gstring result = get(input.Length - count);
internal_remove(result, input, start, count);
return result;
}
internal unsafe static void internal_remove(string dst, string src, int start, int count)
{
fixed(char* src_ptr = src)
{
fixed(char* dst_ptr = dst)
{
for(int i = 0, j = 0; i < dst.Length; i++)
{
if (i >= start && i < start + count) // within removal range
continue;
dst_ptr[j++] = src_ptr[i];
}
}
}
}
internal unsafe static gstring internal_replace(string value, string old_value, string new_value)
{
// "Hello, World. There World" | World->Jon =
// "000000000000000000000" (len = orig - 2 * (world-jon) = orig - 4
// "Hello, 00000000000000"
// "Hello, Jon00000000000"
// "Hello, Jon. There 000"
// "Hello, Jon. There Jon"
// "Hello, World. There World" | World->Alexander =
// "000000000000000000000000000000000" (len = orig + 2 * (alexander-world) = orig + 8
// "Hello, 00000000000000000000000000"
// "Hello, Alexander00000000000000000"
// "Hello, Alexander. There 000000000"
// "Hello, Alexander. There Alexander"
if (old_value == null)
throw new ArgumentNullException("old_value");
if (new_value == null)
throw new ArgumentNullException("new_value");
int idx = internal_index_of(value, old_value);
if (idx == -1)
return value;
g_finds.Clear();
g_finds.Add(idx);
// find all the indicies beforehand
while(idx + old_value.Length < value.Length)
{
idx = internal_index_of(value, old_value, idx + old_value.Length);
if (idx == -1)
break;
g_finds.Add(idx);
}
// calc the right new total length
int new_len;
int dif = old_value.Length - new_value.Length;
if (dif > 0)
new_len = value.Length - (g_finds.Count * dif);
else
new_len = value.Length + (g_finds.Count * -dif);
gstring result = get(new_len);
fixed(char* ptr_this = value)
{
fixed(char* ptr_result = result._value)
{
for (int i = 0, x = 0, j = 0; i < new_len;)
{
if (x == g_finds.Count || g_finds[x] != j)
{
ptr_result[i++] = ptr_this[j++];
}
else
{
for (int n = 0; n < new_value.Length; n++)
ptr_result[i + n] = new_value[n];
x++;
i += new_value.Length;
j += old_value.Length;
}
}
}
}
return result;
}
internal unsafe static gstring internal_insert(string value, char to_insert, int start, int count)
{
// "HelloWorld" (to_insert=x, start=5, count=3) -> "HelloxxxWorld"
if (start < 0 || start >= value.Length)
throw new ArgumentOutOfRangeException("start=" + start + " Length=" + value.Length);
if (count < 0)
throw new ArgumentOutOfRangeException("count=" + count);
if (count == 0)
return get(value);
int new_len = value.Length + count;
gstring result = get(new_len);
fixed(char* ptr_value = value)
{
fixed(char* ptr_result = result._value)
{
for(int i = 0, j = 0; i < new_len; i++)
{
if (i >= start && i < start + count)
ptr_result[i] = to_insert;
else
ptr_result[i] = ptr_value[j++];
}
}
}
return result;
}
internal unsafe static gstring internal_insert(string input, string to_insert, int start)
{
if (input == null)
throw new ArgumentNullException("input");
if (to_insert == null)
throw new ArgumentNullException("to_insert");
if (start < 0 || start >= input.Length)
throw new ArgumentOutOfRangeException("start=" + start + " Length=" + input.Length);
if (to_insert.Length == 0)
return get(input);
int new_len = input.Length + to_insert.Length;
gstring result = get(new_len);
internal_insert(result, input, to_insert, start);
return result;
}
internal unsafe static gstring internal_concat(string s1, string s2)
{
int total_length = s1.Length + s2.Length;
gstring result = get(total_length);
fixed(char* ptr_result = result._value)
{
fixed(char* ptr_s1 = s1)
{
fixed(char* ptr_s2 = s2)
{
memcpy(dst: ptr_result, src: ptr_s1, length: s1.Length, src_offset: 0);
memcpy(dst: ptr_result, src: ptr_s2, length: s2.Length, src_offset: s1.Length);
}
}
}
return result;
}
internal unsafe static void internal_insert(string dst, string src, string to_insert, int start)
{
fixed(char* ptr_src = src)
{
fixed(char* ptr_dst = dst)
{
fixed(char* ptr_to_insert = to_insert)
{
for(int i = 0, j = 0, k = 0; i < dst.Length; i++)
{
if (i >= start && i < start + to_insert.Length)
ptr_dst[i] = ptr_to_insert[k++];
else
ptr_dst[i] = ptr_src[j++];
}
}
}
}
}
internal unsafe static void intcpy(char* dst, int value, int start, int count)
{
int end = start + count;
for (int i = end - 1; i >= start; i--, value /= 10)
*(dst + i) = (char)(value % 10 + 48);
}
internal unsafe static void memcpy(char* dst, char* src, int count)
{
for(int i = 0; i < count; i++)
*(dst++) = *(src++);
}
internal unsafe static void memcpy(string dst, char src)
{
fixed (char* ptr_dst = dst)
{
int len = dst.Length;
for (int i = 0; i < len; i++)
ptr_dst[i] = src;
}
}
internal unsafe static void memcpy(string dst, char src, int index)
{
fixed (char* ptr = dst)
ptr[index] = src;
}
internal unsafe static void memcpy(string dst, string src)
{
if (dst.Length != src.Length)
throw new InvalidOperationException("Length mismatch");
memcpy(dst, src, dst.Length, 0);
}
internal unsafe static void memcpy(char* dst, char* src, int length, int src_offset)
{
for (int i = 0; i < length; i++)
*(dst + i + src_offset) = *(src + i);
}
internal unsafe static void memcpy(string dst, string src, int length, int src_offset)
{
fixed (char* ptr_dst = dst)
{
fixed (char* ptr_src = src)
{
for (int i = 0; i < length; i++)
ptr_dst[i + src_offset] = ptr_src[i];
}
}
}
internal class gstring_block : IDisposable
{
readonly Stack<gstring> stack;
internal gstring_block(int capacity)
{
stack = new Stack<gstring>(capacity);
}
internal void push(gstring str)
{
stack.Push(str);
}
internal IDisposable begin()
{
#if DBG
if (log != null)
log("Began block");
#endif
return this;
}
void IDisposable.Dispose()
{
#if DBG
if (log != null)
log("Disposing block");
#endif
while (stack.Count > 0)
{
var str = stack.Pop();
str.dispose();
}
gstring.g_blocks.Push(this);
}
}
// Public API
#region
public static Action<string> Log = null;
public static int DecimalAccuracy = 3; // digits after the decimal point
public int Length
{
get { return _value.Length; }
}
public static void Initialize(int cache_capacity, int stack_capacity, int block_capacity, int intern_capacity)
{
g_cache = new Dictionary<int, Stack<gstring>>(cache_capacity);
g_blocks = new Stack<gstring_block>(block_capacity);
g_intern_table = new List<string>(intern_capacity);
for (int c = 1; c < cache_capacity; c++)
{
var stack = new Stack<gstring>(stack_capacity);
for (int j = 0; j < stack_capacity; j++)
stack.Push(new gstring(c));
g_cache[c] = stack;
}
for (int i = 0; i < block_capacity; i++)
{
var block = new gstring_block(block_capacity * 2);
g_blocks.Push(block);
}
}
public static IDisposable Block()
{
if (g_blocks.Count == 0)
g_current_block = new gstring_block(INITIAL_BLOCK_CAPACITY * 2);
else
g_current_block = g_blocks.Pop();
return g_current_block.begin();
}
public string Intern()
{
return __intern(_value);
}
public static string Intern(string value)
{
return __intern(value);
}
public static void Intern(string[] values)
{
for(int i = 0; i < values.Length; i++)
__intern(values[i]);
}
public char this[int i]
{
get { return _value[i]; }
set { memcpy(this, value, i); }
}
public override int GetHashCode()
{
return RuntimeHelpers.GetHashCode(_value);
}
public override bool Equals(object obj)
{
if (obj == null)
return ReferenceEquals(this, null);
var gstr = obj as gstring;
if (gstr != null)
return gstr._value == this._value;
var str = obj as string;
if (str != null)
return str == this._value;
return false;
}
public override string ToString()
{
return _value;
}
public static implicit operator gstring(bool value)
{
return get(value ? "True" : "False");
}
public unsafe static implicit operator gstring(int value)
{
// e.g. 125
// first pass: count the number of digits
// then: get a gstring with length = num digits
// finally: iterate again, get the char of each digit, memcpy char to result
bool negative = value < 0;
value = Math.Abs(value);
int num_digits = get_digit_count(value);
gstring result;
if (negative)
{
result = get(num_digits + 1);
fixed(char* ptr = result._value)
{
*ptr = '-';
intcpy(ptr, value, 1, num_digits);
}
}
else
{
result = get(num_digits);
fixed(char* ptr = result._value)
intcpy(ptr, value, 0, num_digits);
}
return result;
}
public unsafe static implicit operator gstring(float value)
{
// e.g. 3.148
bool negative = value < 0;
if (negative) value = -value;
int mul = (int)Math.Pow(10, DecimalAccuracy);
int number = (int)(value * mul); // gets the number as a whole, e.g. 3148
int left_num = number / mul; // left part of the decimal point, e.g. 3
int right_num = number % mul; // right part of the decimal pnt, e.g. 148
int left_digit_count = get_digit_count(left_num); // e.g. 1
int right_digit_count = get_digit_count(right_num); // e.g. 3
int total = left_digit_count + right_digit_count + 1; // +1 for '.'
gstring result;
if (negative)
{
result = get(total + 1); // +1 for '-'
fixed(char* ptr = result._value)
{
*ptr = '-';
intcpy(ptr, left_num, 1, left_digit_count);
*(ptr + left_digit_count + 1) = '.';
intcpy(ptr, right_num, left_digit_count + 2, right_digit_count);
}
}
else
{
result = get(total);
fixed(char* ptr = result._value)
{
intcpy(ptr, left_num, 0, left_digit_count);
*(ptr + left_digit_count) = '.';
intcpy(ptr, right_num, left_digit_count + 1, right_digit_count);
}
}
return result;
}
public static implicit operator gstring(string value)
{
return get(value);
}
public static implicit operator string(gstring value)
{
return value._value;
}
public static gstring operator +(gstring left, gstring right)
{
return internal_concat(left, right);
}
public static bool operator ==(gstring left, gstring right)
{
if (ReferenceEquals(left, null))
return ReferenceEquals(right, null);
if (ReferenceEquals(right, null))
return false;
return left._value == right._value;
}
public static bool operator !=(gstring left, gstring right)
{
return !(left._value == right._value);
}
public unsafe gstring ToUpper()
{
var result = get(Length);
fixed(char* ptr_this = this._value)
{
fixed(char* ptr_result = result._value)
{
for(int i = 0; i < _value.Length; i++)
{
var ch = ptr_this[i];
if (char.IsLower(ch))
ptr_result[i] = char.ToUpper(ch);
else
ptr_result[i] = ptr_this[i];
}
}
}
return result;
}
public unsafe gstring ToLower()
{
var result = get(Length);
fixed(char* ptr_this = this._value)
{
fixed(char* ptr_result = result._value)
{
for(int i = 0;i < _value.Length; i++)
{
var ch = ptr_this[i];
if (char.IsUpper(ch))
ptr_result[i] = char.ToLower(ch);
else
ptr_result[i] = ptr_this[i];
}
}
}
return result;
}
public gstring Remove(int start)
{
return Remove(start, Length - start);
}
public gstring Remove(int start, int count)
{
return internal_remove(this._value, start, count);
}
public gstring Insert(char value, int start, int count)
{
return internal_insert(this._value, value, start, count);
}
public gstring Insert(string value, int start)
{
return internal_insert(this._value, value, start);
}
public unsafe gstring Replace(char old_value, char new_value)
{
gstring result = get(Length);
fixed(char* ptr_this = this._value)
{
fixed(char* ptr_result = result._value)
{
for(int i = 0; i < Length; i++)
{
if (ptr_this[i] == old_value)
ptr_result[i] = new_value;
else
ptr_result[i] = ptr_this[i];
}
}
}
return result;
}
public gstring Replace(string old_value, string new_value)
{
return internal_replace(this._value, old_value, new_value);
}
public gstring Substring(int start)
{
return Substring(start, Length - start);
}
public unsafe gstring Substring(int start, int count)
{
if (start < 0 || start >= Length)
throw new ArgumentOutOfRangeException("start");
if (count > Length)
throw new ArgumentOutOfRangeException("count");
gstring result = get(count);
fixed(char* src = this._value)
fixed(char* dst = result._value)
memcpy(dst, src + start, count);
return result;
}
public bool Contains(string value)
{
return IndexOf(value) != -1;
}
public bool Contains(char value)
{
return IndexOf(value) != -1;
}
public int LastIndexOf(string value)
{
int idx = -1;
int last_find = -1;
while(true)
{
idx = internal_index_of(this._value, value, idx + value.Length);
last_find = idx;
if (idx == -1 || idx + value.Length >= this._value.Length)
break;
}
return last_find;
}
public int LastIndexOf(char value)
{
int idx = -1;
int last_find = -1;
while(true)
{
idx = internal_index_of(this._value, value, idx + 1);
last_find = idx;
if (idx == -1 || idx + 1 >= this._value.Length)
break;
}
return last_find;
}
public int IndexOf(char value)
{
return IndexOf(value, 0, Length);
}
public int IndexOf(char value, int start)
{
return internal_index_of(this._value, value, start);
}
public int IndexOf(char value, int start, int count)
{
return internal_index_of(this._value, value, start, count);
}
public int IndexOf(string value)
{
return IndexOf(value, 0, Length);
}
public int IndexOf(string value, int start)
{
return IndexOf(value, start, Length - start);
}
public int IndexOf(string value, int start, int count)
{
return internal_index_of(this._value, value, start, count);
}
public unsafe bool EndsWith(string postfix)
{
if (postfix == null)
throw new ArgumentNullException("postfix");
if (this.Length < postfix.Length)
return false;
fixed(char* ptr_this = this._value)
{
fixed(char* ptr_postfix = postfix)
{
for (int i = this._value.Length - 1, j = postfix.Length - 1; j >= 0; i--, j--)
if (ptr_this[i] != ptr_postfix[j])
return false;
}
}
return true;
}
public unsafe bool StartsWith(string prefix)
{
if (prefix == null)
throw new ArgumentNullException("prefix");
if (this.Length < prefix.Length)
return false;
fixed(char* ptr_this = this._value)
{
fixed(char* ptr_prefix = prefix)
{
for (int i = 0; i < prefix.Length; i++)
if (ptr_this[i] != ptr_prefix[i])
return false;
}
}
return true;
}
public static int GetCacheCount(int length)
{
Stack<gstring> stack;
if (!g_cache.TryGetValue(length, out stack))
return -1;