package devkit

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file netstring_str.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
open ExtLib

let explode s =
  let l = String.length s in
  let rec loop k = if k < l then s.[k] :: loop (k + 1) else [] in
  loop 0

let implode l =
  let n = List.length l in
  let s = Bytes.create n in
  let k = ref 0 in
  List.iter
    (fun c ->
      Bytes.set s !k c;
      incr k)
    l;
  Bytes.to_string s

let quote_set s =
  let l = explode s in
  let have_circum = List.mem '^' l in
  let have_minus = List.mem '-' l in
  let have_rbracket = List.mem ']' l in
  let l1 = List.filter (fun c -> c <> '^' && c <> '-' && c <> ']') l in
  let l2 = if have_rbracket then ']' :: l1 else l1 in
  let l3 = if have_circum then l2 @ [ '^' ] else l2 in
  let l4 = if have_minus then l3 @ [ '-' ] else l3 in
  let s4 = implode l4 in
  match s4 with
  | "" -> failwith "Netstring_str.quote_set: empty"
  | "^" -> "^"
  | "^-" -> "[-^]"
  | _ -> "[" ^ s4 ^ "]"

type setatom = Schar of char | Srange of (char * char)
and set = setatom list

type re_term =
  | Texact of string (* literal characters (except NUL) *)
  | Tnullchar (* NUL characer *)
  | Tany (* . but no newline *)
  | Tnull (* emptiness *)
  | Tconcat of re_term list
  | Tstar of re_term (* x* *)
  | Tplus of re_term (* x+ *)
  | Toption of re_term (* x? *)
  | Tset of set (* [...] *)
  | Tnegset of set (* [^...] *)
  | Tbegline (* ^ *)
  | Tendline (* $ *)
  | Talt of re_term list (* x\|y *)
  | Tgroup of (int * re_term) (* \(...\) *)
  | Trefer of int (* \i *)
  | Twordbound (* \b *)

(**********************************************************************)
(* Final types *)

type regexp = Pcre.regexp
type split_result = Str.split_result = Text of string | Delim of string
type result = Pcre.substrings

(**********************************************************************)
(* Parse Str-style regexps, and convert to Pcre-style regexps *)

let scan_str_regexp re_string =
  let l = String.length re_string in
  let k = ref (-1) in
  let c = ref ' ' in
  let esc = ref false in
  let group = ref 1 in
  let n_open_groups = ref 0 in
  let closed_groups = Array.create 10 false in

  let next () =
    incr k;
    if !k < l then
      let c1 = re_string.[!k] in
      if c1 = '\\' then
        if !k < l then (
          incr k;
          c := re_string.[!k];
          esc := true)
        else failwith "Web.Url.Netstring_str regexp: bad backslash"
      else (
        esc := false;
        c := c1)
  in

  let next_noesc () =
    incr k;
    if !k < l then (
      c := re_string.[!k];
      esc := false)
  in

  let rec scan_alternative () =
    let t1 = scan_concatenation () in
    if !k < l then
      if !esc && !c = '|' then (
        next ();
        match scan_alternative () with
        | Talt alist -> Talt (t1 :: alist)
        | t -> Talt [ t1; t ])
      else t1
    else t1
  and scan_concatenation () =
    let t1 = scan_repetition () in
    if t1 = Tnull then t1
    else
      let t2 = scan_concatenation () in
      match t2 with
      | Tnull -> t1
      | Texact s2 -> (
          match t1 with
          | Texact s1 -> Texact (s1 ^ s2)
          | _ -> Tconcat [ t1; t2 ])
      | Tconcat clist -> Tconcat (t1 :: clist)
      | _ -> Tconcat [ t1; t2 ]
  and scan_repetition () =
    let t1 = ref (scan_literal_or_group ()) in
    let continue = ref true in
    while !continue do
      if !k < l && not !esc then
        match !c with
        | '*' ->
            next ();
            t1 := Tstar !t1
        | '+' ->
            next ();
            t1 := Tplus !t1
        | '?' ->
            next ();
            t1 := Toption !t1
        (* {...} is not implemented in Str *)
        | _ -> continue := false
      else continue := false
    done;
    !t1
  and scan_literal_or_group () =
    if !k >= l then Tnull
    else if !esc then (
      match !c with
      | '(' ->
          next ();
          let n = !group in
          incr group;
          incr n_open_groups;
          let t = scan_alternative () in
          decr n_open_groups;
          if !k < l && !esc && !c = ')' then (
            next ();
            closed_groups.(n) <- true;
            Tgroup (n, t))
          else failwith "regexp: closing paranthesis \\) not found"
      | '1' .. '9' ->
          let n = Char.code !c - Char.code '0' in
          if closed_groups.(n) then (
            next ();
            Trefer n)
          else failwith "regexp: bad reference to group"
      (*
      |	'w' -> next(); Twordchar
      |	'W' -> next(); Tnowordchar
      *)
      | 'b' ->
          next ();
          Twordbound
      (*
      |	'B' -> next(); Tnowordbound
      |	'<' -> next(); Twordbeg
      |	'>' -> next(); Twordend
      |	'`' -> next(); Tbegbuf
      |	'\'' -> next(); Tendbuf
      *)
      | '\\' ->
          next ();
          Texact (String.make 1 '\\')
      | '|' -> Tnull
      | ')' ->
          if !n_open_groups > 0 then Tnull
          else failwith "regexp: unmatched closing parenthesis"
      | ch ->
          next ();
          Texact (String.make 1 ch))
    else
      match !c with
      | '*' -> Tnull
      | '+' -> Tnull
      | '?' -> Tnull
      | '{' -> Tnull
      | '^' ->
          next ();
          Tbegline
      | '$' ->
          next ();
          Tendline
      | '.' ->
          next ();
          Tany
      | '\000' ->
          next ();
          Tnullchar
      | '[' ->
          next_noesc ();
          if !k < l then (
            let negated = ref false in
            let set = ref [] in

            let add_char c = set := Schar c :: !set in

            let add_range c1 c2 = set := Srange (c1, c2) :: !set in

            if !c = '^' then (
              next_noesc ();
              negated := true);

            let continue = ref true in
            let first = ref true in

            (* the character after [ or [^ ? *)
            while !continue && !k < l do
              match () with
              | () when !c = '[' && !k + 1 < l && re_string.[!k + 1] = ':' ->
                  failwith
                    "regexp: Character classes such as [[:digit:]] not \
                     implemented"
              (* TODO: check for predefined sets *)
              | () when !c = ']' && not !first ->
                  next ();
                  continue := false
              | ()
                when !k + 2 < l
                     && re_string.[!k + 1] = '-'
                     && re_string.[!k + 2] <> ']' ->
                  (* range *)
                  add_range !c re_string.[!k + 2];
                  next_noesc ();
                  next_noesc ();
                  next_noesc ();
                  first := false
              | () ->
                  add_char !c;
                  next_noesc ();
                  first := false
            done;

            if !continue then failwith "regexp: closing bracket ] not found";

            if !negated then Tnegset !set else Tset !set)
          else failwith "regexp: closing bracket ] not found"
      | ch ->
          next ();
          Texact (String.make 1 ch)
  in

  try
    next ();
    scan_alternative ()
  with Failure msg -> failwith (msg ^ " - regexp: " ^ re_string)

let pcre_safe_quote c =
  (* for print_set *)
  match c with
  | 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '_' -> String.make 1 c
  | '\000' -> "\\000"
  | _ -> "\\" ^ String.make 1 c

let rec print_pcre_regexp ret =
  match ret with
  | Texact s -> Pcre.quote s
  | Tnullchar ->
      (* Pcre.quote "\000" returns nonsense *)
      "[\\000]"
  | Tany -> "."
  | Tnull -> "(?:)"
  | Tconcat l -> String.concat "" (List.map print_pcre_regexp l)
  | Tstar ret' -> print_pcre_subregexp ret' ^ "*"
  | Tplus ret' -> print_pcre_subregexp ret' ^ "+"
  | Toption ret' -> print_pcre_subregexp ret' ^ "?"
  | Tset s -> "[" ^ print_set s ^ "]"
  | Tnegset s -> "[^" ^ print_set s ^ "]"
  | Talt l -> String.concat "|" (List.map print_pcre_subregexp l)
  | Tgroup (_, ret') -> "(" ^ print_pcre_regexp ret' ^ ")"
  | Trefer n ->
      (* Put parentheses around \n to disambiguate from \nn *)
      "(?:\\" ^ string_of_int n ^ ")"
  | Tbegline -> "^"
  | Tendline -> "(?:$)"
  | Twordbound -> "\\b"

and print_pcre_subregexp ret =
  (* Print ret, but put parentheses around ret *)
  match ret with
  | Tset _ | Tnegset _ | Tgroup (_, _) ->
      (* No additional parentheses needed *)
      print_pcre_regexp ret
  | _ ->
      (* Print (?:ret). This is the "neutral" form of grouping that only
         * changes precedence
      *)
      "(?:" ^ print_pcre_regexp ret ^ ")"

and print_set s =
  String.concat ""
    (List.map
       (function
         | Schar c -> pcre_safe_quote c
         | Srange (c1, c2) -> pcre_safe_quote c1 ^ "-" ^ pcre_safe_quote c2)
       s)

(**********************************************************************)
(* Emulation *)

let regexp s =
  let ret = scan_str_regexp s in
  let s' = print_pcre_regexp ret in
  Pcre.regexp ~flags:[ `MULTILINE ] s'

let search_forward pat s pos =
  let result = Pcre.exec ~rex:pat ~pos s in
  (fst (Pcre.get_substring_ofs result 0), result)

let matched_string result _ =
  (* Unfortunately, Pcre.get_substring will not raise Not_found if there is
   * no matched string. Instead, it returns "", but this value cannot be
   * distinguished from an empty match.
   * The workaround is to call Pcre.get_substring_ofs first. This function
   * will raise Not_found if there is not any matched string.
   *
   * NOTE: Current versions of Pcre do return Not_found!
   *)
  ignore (Pcre.get_substring_ofs result 0);
  Pcre.get_substring result 0

let match_beginning result = fst (Pcre.get_substring_ofs result 0)
let match_end result = snd (Pcre.get_substring_ofs result 0)

let matched_group result n _ =
  (* See also the comment for [matched_string] *)
  if n < 0 || n >= Pcre.num_of_subs result then raise Not_found;
  ignore (Pcre.get_substring_ofs result n);
  Pcre.get_substring result n

let global_substitute pat subst s =
  Pcre.substitute_substrings ~rex:pat ~subst:(fun r -> subst r s) s

let tr_split_result r =
  List.map
    (function
      | Pcre.Text t -> Text t | Pcre.Delim d -> Delim d | _ -> assert false)
    (List.filter
       (function Pcre.Group (_, _) | Pcre.NoGroup -> false | _ -> true)
       r)

let full_split sep s = tr_split_result (Pcre.full_split ~rex:sep ~max:(-1) s)
OCaml

Innovation. Community. Security.