fan-1.0 /src/sys/dotnet/fan/sys/FanStr.cs

Language C# Lines 990
MD5 Hash a8ca26f6b6c1321d7ef591a6917d43d3 Estimated Cost $21,486 (why?)
Repository https://bitbucket.org/bedlaczech/fan-1.0 View Raw File View Project SPDX
  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
//
// Copyright (c) 2006, Brian Frank and Andy Frank
// Licensed under the Academic Free License version 3.0
//
// History:
//   13 Sep 06  Andy Frank  Creation
//   21 Oct 08  Andy Frank  Refactor String into FanStr
//

using System;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using Fanx.Serial;
using Fanx.Util;

namespace Fan.Sys
{
  /// <summary>
  /// FanStr defines the methods for sys::string.  The actual
  /// class used for representation is F.lang.String.
  /// </summary>
  public sealed class FanStr
  {

  //////////////////////////////////////////////////////////////////////////
  // Construction
  //////////////////////////////////////////////////////////////////////////

    public static string fromChars(List chars)
    {
      if (chars.sz() == 0) return "";
      StringBuilder s = new StringBuilder(chars.sz());
      for (int i=0; i<chars.sz(); ++i)
        s.Append((char)((Long)chars.get(i)).longValue());
      return s.ToString();
    }

    public static string makeTrim(StringBuilder s)
    {
      int start = 0;
      int end = s.Length;
      while (start < end) if (FanInt.isSpace(s[start])) start++; else break;
      while (end > start) if (FanInt.isSpace(s[end-1])) end--; else break;
      return s.ToString(start, end-start);
    }

  //////////////////////////////////////////////////////////////////////////
  // Identity
  //////////////////////////////////////////////////////////////////////////

    public static bool equals(string self, object obj)
    {
      if (obj is string)
        return self == (string)obj;
      else
        return false;
    }

    public static bool equalsIgnoreCase(string a, string b)
    {
      if (a == b) return true;

      int an   = a.Length;
      int bn   = b.Length;
      if (an != bn) return false;

      for (int i=0; i<an; i++)
      {
        int ac = a[i];
        int bc = b[i];
        if ('A' <= ac && ac <= 'Z') ac |= 0x20;
        if ('A' <= bc && bc <= 'Z') bc |= 0x20;
        if (ac != bc) return false;
      }
      return true;
    }

    public static long compare(string self, object obj)
    {
      int cmp = String.CompareOrdinal(self, (string)obj);
      if (cmp < 0) return -1;
      return cmp == 0 ? 0 : +1;
    }

    public static long compareIgnoreCase(string a, string b)
    {
      if (a == b) return 0;

      int an   = a.Length;
      int bn   = b.Length;

      for (int i=0; i<an && i<bn; i++)
      {
        int ac = a[i];
        int bc = b[i];
        if ('A' <= ac && ac <= 'Z') ac |= 0x20;
        if ('A' <= bc && bc <= 'Z') bc |= 0x20;
        if (ac != bc) return ac < bc ? -1 : +1;
      }

      if (an == bn) return 0;
      return an < bn ? -1 : +1;
    }

    public static long hash(string self)
    {
      return self.GetHashCode();
    }

    public static int caseInsensitiveHash(string self)
    {
      int n = self.Length;
      int hash = 0;

      for (int i=0; i<n; i++)
      {
        int c = self[i];
        if ('A' <= c && c <= 'Z') c |= 0x20;
        hash = 31*hash + c;
      }

      return hash;
    }

    public static string toStr(string self)
    {
      return self;
    }

    public static string toLocale(string self)
    {
      return self;
    }

    public static Type type(string self)
    {
      return Sys.StrType;
    }

  //////////////////////////////////////////////////////////////////////////
  // Operators
  //////////////////////////////////////////////////////////////////////////

    public static long get(string self, long index)
    {
      try
      {
        int i = (int)index;
        if (i < 0) i = self.Length+i;
        return self[i];
      }
      catch (System.IndexOutOfRangeException)
      {
        throw IndexErr.make(index).val;
      }
    }

    public static long getSafe(String self, long index) { return getSafe(self, index, 0); }
    public static long getSafe(String self, long index, long def)
    {
      try
      {
        int i = (int)index;
        if (i < 0) i = self.Length+i;
        return self[i];
      }
      catch (System.IndexOutOfRangeException)
      {
        return def;
      }
    }

    public static string getRange(string self, Range r)
    {
      int size = self.Length;

      int s = r.start(size);
      int e = r.end(size);
      if (e+1 < s) throw IndexErr.make(r).val;

      return self.Substring(s, (e-s)+1);
    }

    public static string plus(string self, object obj)
    {
      if (obj == null) return String.Concat(self, "null");
      string x = FanObj.toStr(obj);
      if (x == "") return self;
      return String.Concat(self, x);
    }

  //////////////////////////////////////////////////////////////////////////
  // Identity
  //////////////////////////////////////////////////////////////////////////

    public static string intern(string self)
    {
      return String.Intern(self);
    }

    public static bool isEmpty(string self)
    {
      return self.Length == 0;
    }

    public static long size(string self)
    {
      return self.Length;
    }

    public static bool startsWith(string self, string s)
    {
      return self.StartsWith(s);
    }

    public static bool endsWith(string self, string s)
    {
      return self.EndsWith(s);
    }

    public static bool contains(string self, string s)
    {
      return index(self, s, 0) != null;
    }

    public static bool containsChar(string self, long ch)
    {
      return self.IndexOf((char)ch) >= 0;
    }

    public static Long index(string self, string s) { return index(self, s, 0); }
    public static Long index(string self, string s, long off)
    {
      int i = (int)off;
      if (i < 0) i = self.Length+i;

      int r;
      if (s.Length == 1)
        r = self.IndexOf(s[0], i);
      else
        r = self.IndexOf(s, i);

      if (r < 0) return null;
      return Long.valueOf(r);
    }

    public static Long indexr(string self, string s) { return indexr(self, s, -1); }
    public static Long indexr(string self, string s, long off)
    {
      int i = (int)off;
      if (i < 0) i = self.Length+i;

      int r;
      if (s.Length == 1)
        r = self.LastIndexOf(s[0], i);
      else
      {
        // this doesn't match Java impl - so we have to roll
        // our own - prob alot of room for improvement...
        //r = val.LastIndexOf(sval, i, StringComparison.InvariantCulture);

        int len = self.Length;
        int slen = s.Length;
        if (len < slen) return null;
        r = -1;
        for (; i>=0; i--)
          if (nStartsWith(self, s, i))
            { r = i; break; }
      }

      if (r < 0) return null;
      return Long.valueOf(r);
    }

    public static Long indexIgnoreCase(string self, string s) { return indexIgnoreCase(self, s, 0); }
    public static Long indexIgnoreCase(string self, string s, long off)
    {
      int vlen = self.Length, slen = s.Length;
      int r = -1;

      int i = (int)off;
      if (i < 0) i = vlen+i;

      int first = s[0];
      for (; i<=vlen-slen; ++i)
      {
        // test first char
        if (neic(first, self[i])) continue;

        // test remainder of chars
        r = i;
        for (int si=1, vi=i+1; si<slen; ++si, ++vi)
          if (neic(s[si], self[vi]))
            { r = -1; break; }
        if (r >= 0) break;
      }

      if (r < 0) return null;
      return Long.valueOf(r);
    }

    public static Long indexrIgnoreCase(string self, string s) { return indexrIgnoreCase(self, s, -1); }
    public static Long indexrIgnoreCase(string self, string s, long off)
    {
      int vlen = self.Length, slen = s.Length;
      int r = -1;

      int i = (int)off;
      if (i < 0) i = vlen+i;
      if (i+slen >= vlen) i = vlen-slen;

      int first = s[0];
      for (; i>=0; --i)
      {
        // test first char
        if (neic(first, self[i])) continue;

        // test remainder of chars
        r = i;
        for (int si=1, vi=i+1; si<slen; ++si, ++vi)
          if (neic(s[si], self[vi]))
            { r = -1; break; }
        if (r >= 0) break;
      }

      if (r < 0) return null;
      return Long.valueOf(r);
    }

    private static bool neic(int a, int b)
    {
      if (a == b) return false;
      if ((a | 0x20) == (b | 0x20)) return FanInt.lower(a) != FanInt.lower(b);
      return true;
    }

    private static bool nStartsWith(string s, string pre, int off)
    {
      if (off >= s.Length) return false;

      int slen = s.Length;
      int plen = pre.Length;
      if (off+plen > s.Length) return false;

      for (int i=0; i<plen && i+off<slen; i++)
        if (s[i+off] != pre[i]) return false;

      return true;
    }

  //////////////////////////////////////////////////////////////////////////
  // Iterators
  //////////////////////////////////////////////////////////////////////////

    public static List chars(string self)
    {
      int len = self.Length;
      if (len == 0) return Sys.IntType.emptyList();
      Long[] chars = new Long[len];
      for (int i=0; i<len; ++i) chars[i] = Long.valueOf(self[i]);
      return new List(Sys.IntType, chars);
    }

    public static void each(string self, Func f)
    {
      int len = self.Length;
      for (int i=0; i<len ; i++)
        f.call(self[i], i);
    }

    public static void eachr(string self, Func f)
    {
      for (int i=self.Length-1; i>=0; --i)
        f.call(self[i], i);
    }

    public static bool any(string self, Func f)
    {
      int len = self.Length;
      for (int i=0; i<len ; i++)
        if (f.call(self[i], i) == Boolean.True)
          return true;
      return false;
    }

    public static bool all(string self, Func f)
    {
      int len = self.Length;
      for (int i=0; i<len ; i++)
        if (f.call(self[i], i) == Boolean.False)
          return false;
      return true;
    }

  //////////////////////////////////////////////////////////////////////////
  // Utils
  //////////////////////////////////////////////////////////////////////////

    public static string spaces(long n)
    {
      // do an array lookup for reasonable length
      // strings since that is the common case
      int count = (int)n;
      try { return m_spaces[count]; } catch (IndexOutOfRangeException) {}

      // otherwise we build a new one
      StringBuilder s = new StringBuilder(m_spaces[m_spaces.Length-1]);
      for (int i=m_spaces.Length-1; i<count; i++)
        s.Append(' ');
      return s.ToString();
    }

    public static string lower(string self)
    {
      StringBuilder s = new StringBuilder(self.Length);
      for (int i=0; i<self.Length; i++)
      {
        int ch = self[i];
        if ('A' <= ch && ch <= 'Z') ch |= 0x20;
        s.Append((char)ch);
      }
      return s.ToString();
    }

    public static string upper(string self)
    {
      StringBuilder s = new StringBuilder(self.Length);
      for (int i=0; i<self.Length; i++)
      {
        int ch = self[i];
        if ('a' <= ch && ch <= 'z') ch &= ~0x20;
        s.Append((char)ch);
      }
      return s.ToString();
    }

    public static string capitalize(string self)
    {
      if (self.Length > 0)
      {
        int ch = self[0];
        if ('a' <= ch && ch <= 'z')
        {
          StringBuilder s = new StringBuilder(self.Length);
          s.Append((char)(ch & ~0x20));
          s.Append(self, 1, self.Length-1);
          return s.ToString();
        }
      }
      return self;
    }

    public static string decapitalize(string self)
    {
      if (self.Length > 0)
      {
        int ch = self[0];
        if ('A' <= ch && ch <= 'Z')
        {
          StringBuilder s = new StringBuilder(self.Length);
          s.Append((char)(ch | 0x20));
          s.Append(self, 1, self.Length-1);
          return s.ToString();
        }
      }
      return self;
    }

    public static string toDisplayName(string self)
    {
      if (self.Length == 0) return "";
      StringBuilder s = new StringBuilder(self.Length+4);

      // capitalize first word
      int c = self[0];
      if ('a' <= c && c <= 'z') c &= ~0x20;
      s.Append((char)c);

      // insert spaces before every capital
      int last = c;
      for (int i=1; i<self.Length; ++i)
      {
        c = self[i];
        if ('A' <= c && c <= 'Z' && last != '_')
        {
          int next = i+1 < self.Length ? self[i+1] : 'Q';
          if (!('A' <= last && last <= 'Z') || !('A' <= next && next <= 'Z'))
            s.Append(' ');
        }
        else if ('a' <= c && c <= 'z')
        {
          if ('0' <= last && last <= '9') { s.Append(' '); c &= ~0x20; }
          else if (last == '_') c &= ~0x20;
        }
        else if ('0' <= c && c <= '9')
        {
          if (!('0' <= last && last <= '9')) s.Append(' ');
        }
        else if (c == '_')
        {
          s.Append(' ');
          last = c;
          continue;
        }
        s.Append((char)c);
        last = c;
      }
      return s.ToString();
    }

    public static string fromDisplayName(string self)
    {
      if (self.Length == 0) return "";
      StringBuilder s = new StringBuilder(self.Length);
      int c = self[0];
      int c2 = self.Length == 1 ? 0 : self[1];
      if ('A' <= c && c <= 'Z' && !('A' <= c2 && c2 <= 'Z')) c |= 0x20;
      s.Append((char)c);
      int last = c;
      for (int i=1; i<self.Length; ++i)
      {
        c = self[i];
        if (c != ' ')
        {
          if (last == ' ' && 'a' <= c && c <= 'z') c &= ~0x20;
          s.Append((char)c);
        }
        last = c;
      }
      return s.ToString();
    }

    public static string justl(string self, long width)
    {
      return padr(self, width, ' ');
    }

    public static string justr(string self, long width)
    {
      return padl(self, width, ' ');
    }

    public static string padl(string self, long width) { return padl(self, width, ' '); }
    public static string padl(string self, long width, long ch)
    {
      int w = (int)width;
      if (self.Length >= w) return self;
      char c = (char)ch;
      StringBuilder s = new StringBuilder(w);
      for (int i=self.Length; i<w; i++) s.Append(c);
      s.Append(self);
      return s.ToString();
    }

    public static string padr(string self, long width) { return padr(self, width, ' '); }
    public static string padr(string self, long width, long ch)
    {
      int w = (int)width;
      if (self.Length >= w) return self;
      char c = (char)ch;
      StringBuilder s = new StringBuilder(w);
      s.Append(self);
      for (int i=self.Length; i<w; i++) s.Append(c);
      return s.ToString();
    }

    public static string reverse(string self)
    {
      if (self.Length < 2) return self;
      StringBuilder s = new StringBuilder(self.Length);
      for (int i=self.Length-1; i>=0; i--)
        s.Append(self[i]);
      return s.ToString();
    }

    public static string trim(string self)
    {
      int len = self.Length;
      if (len == 0) return self;
      if (self[0] > ' ' && self[len-1] > ' ') return self;
      return self.Trim(m_trimChars);
    }

    public static string trimStart(string self)
    {
      int len = self.Length;
      if (len == 0) return self;
      if (self[0] > ' ') return self;
      int pos = 1;
      while (pos < len && self[pos] <= ' ') pos++;
      return self.Substring(pos, len-pos);
    }

    public static string trimEnd(string self)
    {
      int len = self.Length;
      if (len == 0) return self;
      int pos = len-1;
      if (self[pos] > ' ') return self;
      while (pos >= 0 && self[pos] <= ' ') pos--;
      return self.Substring(0, pos+1);
    }

    public static List split(string self) { return split(self, null, true); }
    public static List split(string self, Long separator) { return split(self, separator, true); }
    public static List split(string self, Long separator, bool trim)
    {
      if (separator == null) return splitws(self);
      int sep = separator.intValue();
      List toks = new List(Sys.StrType, 16);
      int len = self.Length;
      int x = 0;
      for (int i=0; i<len; ++i)
      {
        if (self[i] != sep) continue;
        if (x <= i) toks.add(splitStr(self, x, i, trim));
        x = i+1;
      }
      if (x <= len) toks.add(splitStr(self, x, len, trim));
      return toks;
    }

    private static string splitStr(String val, int s, int e, bool trim)
    {
      if (trim)
      {
        while (s < e && val[s] <= ' ') ++s;
        while (e > s && val[e-1] <= ' ') --e;
      }
      return val.Substring(s, e-s);
    }

    public static List splitws(String val)
    {
      List toks = new List(Sys.StrType, 16);
      int len = val.Length;
      while (len > 0 && val[len-1] <= ' ') --len;
      int x = 0;
      while (x < len && val[x] <= ' ') ++x;
      for (int i=x; i<len; ++i)
      {
        if (val[i] > ' ') continue;
        toks.add(val.Substring(x, i-x));
        x = i + 1;
        while (x < len && val[x] <= ' ') ++x;
        i = x;
      }
      if (x <= len) toks.add(val.Substring(x, len-x));
      if (toks.sz() == 0) toks.add("");
      return toks;
    }

    public static List splitLines(string self)
    {
      List lines = new List(Sys.StrType, 16);
      int len = self.Length;
      int s = 0;
      for (int i=0; i<len; ++i)
      {
        int c = self[i];
        if (c == '\n' || c == '\r')
        {
          lines.add(self.Substring(s, i-s));
          s = i+1;
          if (c == '\r' && s < len && self[s] == '\n') { i++; s++; }
        }
      }
      lines.add(self.Substring(s, len-s));
      return lines;
    }

    public static string replace(string self, string from, string to)
    {
      if (self.Length == 0) return self;
      return StrUtil.Replace(self, from, to);
    }

    public static long numNewlines(string self)
    {
      int numLines = 0;
      int len = self.Length;
      for (int i=0; i<len; ++i)
      {
        int c = self[i];
        if (c == '\n') numLines++;
        else if (c == '\r')
        {
          numLines++;
          if (i+1<len && self[i+1] == '\n') i++;
        }
      }
      return numLines;
    }

    public static bool isAscii(string self)
    {
      int len = self.Length;
      for (int i=0; i<len; ++i)
        if (self[i] >= 128) return false;
      return true;
    }

    public static bool isSpace(string self)
    {
      int len = self.Length;
      for (int i=0; i<len; i++)
      {
        int ch = self[i];
        if (ch >= 128 || (FanInt.charMap[ch] & FanInt.SPACE) == 0)
          return false;
      }
      return true;
    }

    public static bool isUpper(string self)
    {
      int len = self.Length;
      for (int i=0; i<len; ++i)
      {
        int ch = self[i];
        if (ch >= 128 || (FanInt.charMap[ch] & FanInt.UPPER) == 0)
          return false;
      }
      return true;
    }

    public static bool isLower(string self)
    {
      int len = self.Length;
      for (int i=0; i<len; ++i)
      {
        int ch = self[i];
        if (ch >= 128 || (FanInt.charMap[ch] & FanInt.LOWER) == 0)
          return false;
      }
      return true;
    }

    public static bool isAlpha(string self)
    {
      int len = self.Length;
      for (int i=0; i<len; ++i)
      {
        int ch = self[i];
        if (ch >= 128 || (FanInt.charMap[ch] & FanInt.ALPHA) == 0)
          return false;
      }
      return true;
    }

    public static bool isAlphaNum(string self)
    {
      int len = self.Length;
      for (int i=0; i<len; ++i)
      {
        int ch = self[i];
        if (ch >= 128 || (FanInt.charMap[ch] & FanInt.ALPHANUM) == 0)
          return false;
      }
      return true;
    }

    public static bool isEveryChar(string self, int ch)
    {
      int len = self.Length;
      for (int i=0; i<len; ++i)
        if (self[i] != ch) return false;
      return true;
    }

    public static InStream @in(string self)
    {
      return new StrInStream(self);
    }

    public static Buf toBuf(string self) { return toBuf(self, Charset.m_utf8); }
    public static Buf toBuf(string self, Charset charset)
    {
      MemBuf buf = new MemBuf(self.Length*2);
      buf.charset(charset);
      buf.print(self);
      return buf.flip();
    }

  //////////////////////////////////////////////////////////////////////////
  // Locale
  //////////////////////////////////////////////////////////////////////////

    public static long localeCompare(string self, string x)
    {
      int cmp = String.Compare(self, x, true, Locale.cur().dotnet());
      if (cmp < 0) return -1;
      return cmp == 0 ? 0 : +1;
    }

    public static string localeLower(string self)
    {
      return self.ToLower(Locale.cur().dotnet());
    }

    public static string localeUpper(string self)
    {
      return self.ToUpper(Locale.cur().dotnet());
    }

    public static string localeCapitalize(string self)
    {
      if (self.Length > 0)
      {
        int ch = self[0];
        if (Char.IsLower((char)ch))
        {
          StringBuilder s = new StringBuilder(self.Length);
          s.Append(Char.ToUpper((char)ch, Locale.cur().dotnet()));
          s.Append(self, 1, self.Length-1);
          return s.ToString();
        }
      }
      return self;
    }

    public static string localeDecapitalize(string self)
    {
      if (self.Length > 0)
      {
        int ch = self[0];
        if (Char.IsUpper((char)ch))
        {
          StringBuilder s = new StringBuilder(self.Length);
          s.Append(Char.ToLower((char)ch, Locale.cur().dotnet()));
          s.Append(self, 1, self.Length-1);
          return s.ToString();
        }
      }
      return self;
    }

  //////////////////////////////////////////////////////////////////////////
  // Conversion
  //////////////////////////////////////////////////////////////////////////

    public static Boolean toBool(string self) { return FanBool.fromStr(self, true); }
    public static Boolean toBool(string self, bool check) { return FanBool.fromStr(self, check); }

    public static Long toInt(string self) { return FanInt.fromStr(self, 10, true); }
    public static Long toInt(string self, long radix) { return FanInt.fromStr(self, radix, true); }
    public static Long toInt(string self, long radix, bool check) { return FanInt.fromStr(self, radix, check); }

    public static Double toFloat(string self) { return FanFloat.fromStr(self, true); }
    public static Double toFloat(string self, bool check) { return FanFloat.fromStr(self, check); }

    public static BigDecimal toDecimal(string self) { return FanDecimal.fromStr(self, true); }
    public static BigDecimal toDecimal(string self, bool check) { return FanDecimal.fromStr(self, check); }

    public static Uri toUri(string self) { return Uri.fromStr(self); }

    public static Regex toRegex(string self) { return Regex.fromStr(self); }

    public static string toCode(string self) { return toCode(self, Long.valueOf('"'), false); }
    public static string toCode(string self, Long quote) { return toCode(self, quote, false); }
    public static string toCode(string self, Long quote, bool escapeUnicode)
    {
      StringBuilder s = new StringBuilder(self.Length+10);

      // opening quote
      bool escu = escapeUnicode;
      int q = 0;
      if (quote != null)
      {
        q = quote.intValue();
        s.Append((char)q);
      }

      // NOTE: these escape sequences are duplicated in ObjEncoder
      int len = self.Length;
      for (int i=0; i<len; ++i)
      {
        int c = self[i];
        switch (c)
        {
          case '\n': s.Append('\\').Append('n'); break;
          case '\r': s.Append('\\').Append('r'); break;
          case '\f': s.Append('\\').Append('f'); break;
          case '\t': s.Append('\\').Append('t'); break;
          case '\\': s.Append('\\').Append('\\'); break;
          case '"':  if (q == '"')  s.Append('\\').Append('"');  else s.Append((char)c); break;
          case '`':  if (q == '`')  s.Append('\\').Append('`');  else s.Append((char)c); break;
          case '\'': if (q == '\'') s.Append('\\').Append('\''); else s.Append((char)c); break;
          case '$':  s.Append('\\').Append('$'); break;
          default:
            if (c < ' ' || (escu && c > 127))
            {
              s.Append('\\').Append('u')
               .Append((char)hex((c>>12)&0xf))
               .Append((char)hex((c>>8)&0xf))
               .Append((char)hex((c>>4)&0xf))
               .Append((char)hex(c&0xf));
            }
            else
            {
              s.Append((char)c);
            }
            break;
        }
      }

      // closing quote
      if (q != 0) s.Append((char)q);

      return s.ToString();
    }

    private static int hex(int nib) { return "0123456789abcdef"[nib]; }

    public static string toXml(string self)
    {
      StringBuilder s = null;
      int len = self.Length;
      for (int i=0; i<len; ++i)
      {
        int c = self[i];
        if (c > '>')
        {
          if (s != null) s.Append((char)c);
        }
        else
        {
          string esc = m_xmlEsc[c];
          if (esc != null && (c != '>' || i==0 || self[i-1] == ']'))
          {
            if (s == null)
            {
              s = new StringBuilder(len+12);
              s.Append(self, 0, i);
            }
            s.Append(esc);
          }
          else if (s != null)
          {
            s.Append((char)c);
          }
        }
      }
      if (s == null) return self;
      return s.ToString();
    }

  //////////////////////////////////////////////////////////////////////////
  // Fields
  //////////////////////////////////////////////////////////////////////////

    private static readonly Hashtable interns = new Hashtable();

    public static readonly string m_defVal = "";

    internal static readonly string[] m_ascii = new string[128];
    internal static string[] m_spaces = new string[20];
    internal static readonly char[] m_trimChars;
    internal static string[] m_xmlEsc = new string['>'+1];

    static FanStr()
    {
      // ascii
      for (int i=0; i<m_ascii.Length; i++)
        m_ascii[i] = String.Intern(""+(char)i);

      // spaces
      StringBuilder s = new StringBuilder();
      for (int i=0; i<m_spaces.Length; i++)
      {
        m_spaces[i] = s.ToString();
        s.Append(' ');
      }

      // trim chars
      m_trimChars = new char[0x20+1];
      for (int i=0; i<=0x20; i++)
        m_trimChars[i] = (char)i;

      // xml
      m_xmlEsc['&']  = "&amp;";
      m_xmlEsc['<']  = "&lt;";
      m_xmlEsc['>']  = "&gt;";
      m_xmlEsc['\''] = "&#39;";
      m_xmlEsc['"']  = "&quot;";
    }

  }
}
Back to Top