Source

cadastr / src / cd_Strings.ml

  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
module Strings
 =
  struct

    open Cd_Typeinfo;
    open Cdt;

    value monoid_concat_gen = new Monoid.t "" ( ^ );

    value perv_append = ( ^ );

    type perv_char = char;

    value ti_string : #tifull string = Cdt.ti_string
    ;

    module type TFUNCS
     =
      sig
        value cmp : string -> string -> cmp_res;
        value eq : string -> string -> bool;
        value hash : string -> int;
        value show : string -> string;
        value opt_cmp : string -> string -> cmp_res;
        value opt_eq : string -> string -> bool;
        value opt_hash : string -> int;
        value opt_show : string -> string;
        value ti : Cdt.ti string;
      end
    ;

    module TFuncs_
     =
      struct

        value cmp (x : string) (y : string) =
          match  Pervasives.compare x y  with
          [ 0 -> Cdt.EQ
          | x when x < 0 -> Cdt.LT
          | _ (* when _ > 0 *) -> Cdt.GT
          ]
        ;
        value eq (x : string) (y : string) = (x = y);
        value hash (x : string) = Hashtbl.hash x;
        value show (x : string) = x;

        value opt_cmp = cmp
          and opt_eq = eq
          and opt_hash = hash
          and opt_show = show
        ;

        value ti = (ti_string :> ti _)
        ;

      end
    ;

    module TFuncs
     =
      struct

        include TFuncs_;

      end
    ;

    module TCommon : Cdt.TIFULL with type t = string
     =
      struct

        include TFuncs;
        type t = string;

        value ti = (ti_string :> ti string)
        ;

      end
    ;


    module type ONEBYTE
     =
      sig

        (* from amall { *)
        value is_prefix : ~string:string -> ~prefix:string -> bool;
        value chop_prefix : ~string:string -> ~prefix:string -> string;
        value is_suffix : ~string:string -> ~suffix:string -> bool;
        value chop_suffix : ~string:string -> ~suffix:string -> string;
        value trim_count : (char -> bool) -> string -> (int * string * int);
        value trim : (char -> bool) -> string -> string;
        value split_by_first :
          (char -> bool) -> string -> (string * string * string);
        value split_exact : (char -> bool) -> string -> list string;
        value split : (char -> bool) -> string -> list string;
        value decode_hex_opt : char -> option int;
        value urldecode : string -> string;
        value urlencode : string -> string;
        value concat_array : string -> array string -> string;
        value explode : string -> list char;
        value compare_nocase_latin1 : string -> string -> int;
        (* } from amall *)

        value of_bytes : Cd_Bytes.Bytes.t -> string;
        value of_char : char -> string;
        value length_bytes : string -> int;

        value map_reduce : (char -> 'a) -> Monoid.t 'a -> string -> 'a;
        value monoid_concat : Monoid.t string;

        value append : string -> string -> string;

        value eq_nocase_latin1 : string -> string -> bool;
        value cmp_nocase_latin1 : string -> string -> cmp_res;
        value hash_nocase_latin1 : string -> int;

        include TFUNCS;

      end
    ;


    module Onebyte : ONEBYTE
     =
      struct

        type char = perv_char;

        include TFuncs;

        (* from amall { *)

        value is_prefix ~string ~prefix =
          let str_len = String.length string
          and pref_len = String.length prefix in
          pref_len <= str_len
          && String.sub string 0 pref_len = prefix
        ;

        value chop_prefix ~string ~prefix =
          let pref_len = String.length prefix in
          if is_prefix ~string ~prefix
          then String.sub string pref_len (String.length string - pref_len)
          else string
        ;

        value is_suffix ~string ~suffix =
          let str_len = String.length string
          and suf_len = String.length suffix in
          suf_len <= str_len &&
          String.sub string (str_len - suf_len) suf_len = suffix
        ;

        value chop_suffix ~string ~suffix =
          let suf_len = String.length suffix in
          if is_suffix ~string ~suffix
          then String.sub string 0 (String.length string - suf_len)
          else string
        ;

        value trim_count pred s =
          let len = String.length s in
          let first_skipping =
            inner 0
            where rec inner i =
              if i = len || not (pred s.[i])
              then i
              else inner (i + 1)
          in
            if first_skipping = len
            then (len, "", 0)
            else
              let last_leaving =
                inner (len - 1)
                where rec inner i =
                  let () = assert (i >= first_skipping) in
                  if pred s.[i]
                  then inner (i - 1)
                  else i
              in
                let new_len = last_leaving - first_skipping + 1 in
                ( first_skipping
                , if new_len = len
                  then s
                  else String.sub s first_skipping new_len
                , len - last_leaving - 1
                )
        ;

        value trim pred s =
          let (_firsts, res, _lasts) = trim_count pred s in
          res
        ;

        value split_by_first pred s =
          let len = String.length s in
          let i =
            inner 0
            where rec inner i =
              if i = len || pred s.[i]
              then i
              else inner (i + 1)
          in
            if i = len
            then (s, "", "")
            else
              ( String.sub s 0 i
              , String.sub s i 1
              , String.sub s (i + 1) (len - i - 1)
              )
        ;


        (* [split_exact ((=) '.') ".asd..sdf." = ["";"asd";"";"sdf";""]] *)
        value split_exact pred s =
          let len = String.length s in

          let rec rev_delimiters rev_acc i =  (* including -1 and len *)
            if i = len
            then [len :: rev_acc]
            else
            if pred s.[i]
            then
              rev_delimiters [i :: rev_acc] (i + 1)
            else
              rev_delimiters rev_acc (i + 1)
          in
          let rev_dels = rev_delimiters [-1] 0 in
          let rec inner acc rev_dels =
            match rev_dels with
            [ [] -> assert False
            | [ _ :: [] ] -> acc
            | [next :: ([this :: _] as tl)] ->
                inner
                  [(String.sub s (this+1) (next - this - 1)) :: acc]
                  tl
            ]
          in
            inner [] rev_dels
        ;


        (* todo: more effective implementation. *)

        value split pred s =
          List.filter ((<>) "") (split_exact pred s)
        ;


        value decode_hex_opt ch =
          let c = Char.code ch in
          if ch >= '0' && ch <= '9'
          then Some (c - (Char.code '0'))
          else if ch >= 'A' && ch <= 'F'
          then Some (c - (Char.code 'A') + 10)
          else if ch >= 'a' && ch <= 'f'
          then Some (c - (Char.code 'a') + 10)
          else None
        ;


        value urldecode s =
          let len = String.length s in
          let buf = Buffer.create (len / 3) in
          loop 0
          where rec loop i =
            if i = len
            then Buffer.contents buf
            else
              let out ch adv =
                (Buffer.add_char buf ch; loop (i + adv))
              in
              match s.[i] with
              [ '+' -> out '\x20' 1
              | '%' as c0 ->
                  if i + 2 >= len
                  then out c0 1
                  else
                    let c1 = s.[i + 1]
                    and c2 = s.[i + 2] in
                    match (decode_hex_opt c1, decode_hex_opt c2) with
                    [ (Some d1, Some d2) ->
                        out (Char.chr (16*d1 + d2)) 3
                    | _ -> out c0 1
                    ]
              | c -> out c 1
              ]
        ;


        value hex_digits = "0123456789ABCDEF"
        ;

        value urlencode s =
          let len = String.length s in
          let buf = Buffer.create len in
          let out ch =
            Buffer.add_char buf ch
          in
          let out_esc ch =
            let c = Char.code ch in
            ( out '%'
            ; out hex_digits.[c lsr 4]
            ; out hex_digits.[c land 0x0F]
            )
          in
          let rec loop i =
            if i = len
            then Buffer.contents buf
            else
              (
                match s.[i] with
                [   'a' .. 'z'
                  | 'A' .. 'Z'
                  | '-' | '.' | '_' | '~'
                  as ch
                    ->
                      out ch
                | ch ->
                      out_esc ch
                ]
              ;
                loop (i + 1)
              )
          in
            loop 0
        ;


        (* todo: more effective implementation *)

        value concat_array sep str_arr =
          String.concat sep (Array.to_list str_arr)
        ;


        value explode str =
          inner [] (String.length str - 1)
          where rec inner acc i =
            if i < 0
            then acc
            else inner [str.[i] :: acc] (i - 1)
        ;


        value compare_nocase_latin1 s1 s2 =
          let len1 = String.length s1 in
          let len2 = String.length s2 in
          let bound = min len1 len2 in
          loop 0
          where rec loop i =
            if i = bound
            then
              Pervasives.compare len1 len2
            else
              match Pervasives.compare
                (Char.uppercase s1.[i]) (Char.uppercase s2.[i])
              with
              [ 0 -> loop (i + 1)
              | r -> r
              ]
        ;

        value eq a b =
          let open String in
          let len = length a in
          if len <> length b
          then False
          else
            loop 0
            where rec loop i =
              if i = len
              then True
              else
                if (unsafe_get a i) = (unsafe_get b i)
                then loop (i + 1)
                else False
        ;


        (* } from amall *)


        value cmp_nocase_latin1 s1 s2 =
          match compare_nocase_latin1 s1 s2 with
          [ 0 -> EQ
          | x when x < 0 -> LT
          | _ (* when _ > 0 *) -> GT
          ]
        ;

        value eq_nocase_latin1 a b =
          let open String in
          let len = length a in
          if len <> length b
          then False
          else
            loop 0
            where rec loop i =
              if i = len
              then True
              else
                if (Char.uppercase (unsafe_get a i))
                 = (Char.uppercase (unsafe_get b i))
                then loop (i + 1)
                else False
        ;

        value hash_nocase_latin1 s =
          TCommon.hash (String.uppercase s)
        ;


        value of_bytes = String.copy;

        value of_char =
          let open Cd_Ops in
          let arr = Array.init 256 (Char.chr @> String.make 1) in
          fun c ->
            arr.(Char.code c)
        ;

        value length_bytes = String.length;

        value map_reduce : (char -> 'a) -> Monoid.t 'a -> string -> 'a
         = fun f mon str ->
             let plus = mon#plus
             and len = String.length str in
             inner mon#zero 0
             where rec inner acc i =
               if i = len
               then acc
                else inner (plus acc (f str.[i])) (i + 1)
        ;

        value monoid_concat = monoid_concat_gen;

        value append = perv_append;

        include TCommon;

      end
    ;


    value latin1_tstring_nocase =
      let show = ti_string#show in
      new tifull_on_tti
        ~cmp:Onebyte.cmp_nocase_latin1
        ~eq:Onebyte.eq_nocase_latin1
        ~hash:Onebyte.hash_nocase_latin1
        ~show
        ti_string__base
    ;

    module Perv
     =
      struct
        module String
         =
          struct
            include String;
            include Onebyte;
          end
        ;

      end
    ;


    module Latin1
     =
      struct
        module String
         :
          sig
            type t = (* why I needed 'private'?.. *) string;
            type char = Cd_Chars.Chars.Latin1.Char.t;

            external length : t -> int = "%string_length";
            external get : t -> int -> char = "%string_safe_get";
            value make : int -> char -> t;
            value copy : t -> t;
            value sub : t -> int -> int -> t;
            value concat : t -> list t -> t;
            value iter : (char -> unit) -> t -> unit;
            value escaped : t -> t;
            value index : t -> char -> int;
            value rindex : t -> char -> int;
            value index_from : t -> int -> char -> int;
            value rindex_from : t -> int -> char -> int;
            value contains : t -> char -> bool;
            value contains_from : t -> int -> char -> bool;
            value rcontains_from : t -> int -> char -> bool;
            value uppercase : t -> t;
            value lowercase : t -> t;
            value capitalize : t -> t;
            value uncapitalize : t -> t;
            value compare : t -> t -> int;
            external unsafe_get : t -> int -> char = "%string_unsafe_get";

            include ONEBYTE;
            include TFUNCS;

          end
         =
          struct
            include String;

            type char = Cd_Chars.Chars.Latin1.Char.t;

            include Onebyte;
            include TFuncs;

          end
        ;

        value ( ^ ) = String.append;
        type char = String.char;

        include TCommon;

      end  (* Latin1 *)
    ;


    module Utf8
     =
      struct

        module String
         :
          sig

            type t = (* why I needed 'private'?.. *) string;

            type char = Cd_Chars.Chars.Unicode.Char.t;

            value copy : t -> t;
            value concat : t -> list t -> t;
            value compare : t -> t -> int;
            value of_bytes : Cd_Bytes.Bytes.t -> t;

            value monoid_concat : Monoid.t string;

            value append : t -> t -> t;

            value length_bytes : t -> int;

            include TFUNCS;

            value is_prefix : ~string:string -> ~prefix:string -> bool;

            value chop_prefix : ~string:string -> ~prefix:string -> string;

            (* follows the conventions of stdlib's String.escaped,
               but doesn't escape unicode characters. *)
            value escaped : string -> string;

          end
         =
          struct

            type t = string;

            type char = Cd_Chars.Chars.Unicode.Char.t;

            module S = Perv.String;

            value copy = S.copy;
            value concat = S.concat;
            value compare = S.compare;
            value of_bytes = S.copy;

            value monoid_concat = monoid_concat_gen;

            value append = perv_append;

            value length_bytes = S.length;

            include TFuncs;


            (* some real utf8 work *)

            module U = Cd_Utf8;
            module Scom = Cd_StringsCommon;

            value prefix_bytes ~string ~prefix =
              let sb = length_bytes string
              and pb = length_bytes prefix in
              loop 0
              where rec loop i =
                if i = pb || i = sb
                then i
                else
                  let sc = U.char_length string i
                  and pc = U.char_length prefix i in
                  if sc <> pc
                  then
                    i
                  else
                    let i' = i + sc in
                    if i' > pb || i' > sb
                    then raise
                      (U.Bad_utf8 "end of string, incomplete character")
                    else
                      if Scom.substring_eq
                           ~s1:string ~ofs1:i
                           ~s2:prefix ~ofs2:i
                           ~len:sc
                      then
                        loop i'
                      else
                        i
            ;

            value is_prefix ~string ~prefix =
              prefix_bytes ~string ~prefix = length_bytes prefix
            ;

            value chop_prefix ~string ~prefix =
              let pb = prefix_bytes ~string ~prefix in
              if pb = length_bytes prefix
              then S.sub string pb (String.length string - pb)
              else string
            ;


            module Com = Cd_Buffer.Buffer.Copy_on_modify;

            value escaped s =
              let r = Com.on_string s in
              let len = length_bytes s in
              let esc_dec c =
                ( Com.add_char r '\\'
                ; Com.add_string r (string_of_int (Char.code c))
                )
              in
              ( loop 0
              ; Com.contents r
              )
              where rec loop i =
                if i = len
                then
                  ()
                else
                  let advance =
                    try
                      let cl = U.char_length s i in
                      if i + cl > len
                      then
                        (* incomplete utf8 char *)
                        let left = len - i in
                        let () =
                          for j = 0 to left - 1 do
                            ( esc_dec s.[i + j] )
                          done
                        in
                          left
                      else
                        if cl = 1
                        then
                          ( match s.[i] with
                            [ '\x08' -> Com.add_string r "\\b"
                            | '\x09' -> Com.add_string r "\\t"
                            | '\x0A' -> Com.add_string r "\\n"
                            | '\x0D' -> Com.add_string r "\\r"
                            | '\"' -> Com.add_string r "\\\""
                            | '\\' -> Com.add_string r "\\\\"
                            | c when c < '\x20' -> esc_dec c
                            | c -> Com.add_char r c
                            ]
                          ; 1
                          )
                        else
                          ( Com.add_substring r s i cl
                          ; cl
                          )
                    with
                    [ U.Bad_utf8 _ -> ( esc_dec s.[i] ; 1 )
                    ]
                  in
                    loop (i + advance)
            ;


          end  (* Utf8.String *)
        ;

        value ( ^ ) = String.append;
        type char = String.char;

      end  (* Utf8 *)
    ;


  end
;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.