@@ -100,47 +100,44 @@ let make_cryptographic_safe_string =
100100*)
101101
102102module Netstring_pcre = struct
103+ module Pcre = Re. Pcre
104+
103105 let regexp s = Pcre. regexp ~flags: [`MULTILINE ] s
104106 let templ_re = Pcre. regexp " (?:\\\\\\ d)|[\\ $\\\\ ]"
105107
106- let tr_templ s =
107- (* Convert \n to $n etc. *)
108- (* Unfortunately we cannot just replace \ by $. *)
108+ let tr_templ s g =
109+ (* Instantiate backreferences in s based on match g *)
110+ let b = Buffer. create ( String. length s) in
109111 let rec tr l =
110112 match l with
111- | Pcre. Delim "$" :: l' -> " $$" :: tr l'
112- | Pcre. Delim "\\ " :: Pcre. Delim "$" :: l' -> " $$" :: tr l'
113- | Pcre. Delim "\\ " :: Pcre. Delim s :: l' -> s :: tr l'
114- | Pcre. Delim "\\ " :: Pcre. Text s :: l' -> s :: tr l'
113+ | Pcre. Delim "$" :: l' -> Buffer. add_char b '$' ; tr l'
114+ | Pcre. Delim "\\ " :: Pcre. Delim s :: l' -> Buffer. add_string b s; tr l'
115+ | Pcre. Delim "\\ " :: Pcre. Text s :: l' -> Buffer. add_string b s; tr l'
115116 | [Pcre. Delim " \\ " ] -> failwith " trailing backslash"
116117 | Pcre. Delim d :: l' ->
117118 assert (d.[0 ] = '\\' );
118119 let n = Char. code d.[1 ] - Char. code '0' in
119- if n = 0
120- then " $&" :: tr l'
121- else (" $" ^ string_of_int n ^ " $!" ) :: tr l'
122- | Pcre. Text t :: l' -> t :: tr l'
120+ Buffer. add_string b (Re.Group. get g n);
121+ tr l'
122+ | Pcre. Text t :: l' -> Buffer. add_string b t; tr l'
123123 | Pcre. Group (_ , _ ) :: _ -> assert false
124124 | Pcre. NoGroup :: _ -> assert false
125- | [] -> []
125+ | [] -> ()
126126 in
127127 let l = Pcre. full_split ~rex: templ_re ~max: (- 1 ) s in
128- String. concat " " ( tr l)
128+ tr l; Buffer. contents b
129129
130130 let matched_group result n _ =
131- if n < 0 || n > = Pcre. num_of_subs result then raise Not_found ;
131+ if n < 0 || n > = Re.Group. nb_groups result then raise Not_found ;
132132 ignore (Pcre. get_substring_ofs result n);
133133 Pcre. get_substring result n
134134
135135 let matched_string result _ =
136136 ignore (Pcre. get_substring_ofs result 0 );
137137 Pcre. get_substring result 0
138138
139- let global_replace pat templ s =
140- Pcre. replace ~rex: pat ~itempl: (Pcre. subst (tr_templ templ)) s
141-
142- let global_substitute pat subst s =
143- Pcre. substitute_substrings ~rex: pat ~subst: (fun r -> subst r s) s
139+ let global_replace pat templ s = Re. replace pat ~f: (tr_templ templ) s
140+ let global_substitute pat subst s = Re. replace pat ~f: (fun r -> subst r s) s
144141
145142 let search_forward pat s pos =
146143 let result = Pcre. exec ~rex: pat ~pos s in
@@ -149,14 +146,6 @@ module Netstring_pcre = struct
149146 let string_after s n = String. sub s n (String. length s - n)
150147
151148 let bounded_split expr text num =
152- let start =
153- try
154- let start_substrs = Pcre. exec ~rex: expr ~flags: [`ANCHORED ] text in
155- (* or Not_found *)
156- let _, match_end = Pcre. get_substring_ofs start_substrs 0 in
157- match_end
158- with Not_found -> 0
159- in
160149 let rec split start n =
161150 if start > = String. length text
162151 then []
@@ -167,21 +156,24 @@ module Netstring_pcre = struct
167156 let next_substrs = Pcre. exec ~rex: expr ~pos: start text in
168157 (* or Not_found *)
169158 let pos, match_end = Pcre. get_substring_ofs next_substrs 0 in
170- String. sub text start (pos - start) :: split match_end (n - 1 )
159+ if pos = 0
160+ then split match_end n (* a leading match is ignored *)
161+ else String. sub text start (pos - start) :: split match_end (n - 1 )
171162 with Not_found -> [string_after text start]
172163 in
173- split start num
164+ split 0 num
174165
175166 let split sep s = bounded_split sep s 0
176167
177168 let string_match pat s pos =
178169 try
179- let result = Pcre. exec ~rex: pat ~flags: [ `ANCHORED ] ~ pos s in
180- Some result
170+ let result = Pcre. exec ~rex: pat ~pos s in
171+ if Re.Group. start result 0 = pos then Some result else None
181172 with Not_found -> None
182173end
183174
184175module Url = struct
176+ module Pcre = Re. Pcre
185177 include Url_base
186178
187179 (* Taken from Neturl version 1.1.2 *)
0 commit comments