2 * ----------------------------------------------------------------------
6 let lock = ref (fun () -> ());;
7 let unlock = ref (fun () -> ());;
9 let init_mt new_lock new_unlock =
26 type regexp = Str.regexp;;
27 type split_result = Str.split_result = Text of string | Delim of string;;
33 group_beg : int array;
34 group_end : int array;
40 (fun () -> Str.regexp s)
43 let regexp_case_fold s =
45 (fun () -> Str.regexp_case_fold s)
50 (fun () -> Str.quote s)
55 (fun () -> Str.regexp_string s)
58 let regexp_string_case_fold s =
60 (fun () -> Str.regexp_string_case_fold s)
63 let return_result pos n_groups =
66 match_beg = (try Str.match_beginning() with Not_found -> -1);
67 match_end = (try Str.match_end() with Not_found -> -1);
68 group_beg = Array.create n_groups (-1);
69 group_end = Array.create n_groups (-1);
72 for g = 0 to n_groups - 1 do
73 r.group_beg.(g) <- (try Str.group_beginning (g+1) with Not_found -> -1);
74 r.group_end.(g) <- (try Str.group_end (g+1) with Not_found -> -1);
79 let string_match ?(groups = 9) ~pat s ~pos =
82 if Str.string_match pat s pos then
83 Some (return_result pos groups)
89 let string_partial_match ?(groups = 9) ~pat s ~pos =
92 if Str.string_partial_match pat s pos then
93 Some (return_result pos groups)
99 let search_forward ?(groups = 9) ~pat s ~pos =
102 let i = Str.search_forward pat s pos in
103 i, return_result pos groups
107 let search_backward ?(groups = 9) ~pat s ~pos =
110 let i = Str.search_backward pat s pos in
111 i, return_result pos groups
115 let matched_string result s =
116 if result.match_beg < 0 or result.match_end < 0 then raise Not_found;
117 String.sub s result.match_beg (result.match_end - result.match_beg)
120 let match_beginning result =
121 if result.match_beg < 0 then raise Not_found;
125 let match_end result =
126 if result.match_end < 0 then raise Not_found;
130 let matched_group result n s =
131 if n < 0 || n >= Array.length result.group_beg then raise Not_found;
132 let gbeg = result.group_beg.(n-1) in
133 let gend = result.group_end.(n-1) in
134 if gbeg < 0 or gend < 0 then raise Not_found;
135 String.sub s gbeg (gend - gbeg)
138 let group_beginning result n =
139 if n < 0 || n >= Array.length result.group_beg then raise Not_found;
140 let gbeg = result.group_beg.(n-1) in
141 if gbeg < 0 then raise Not_found else
145 let group_end result n =
146 if n < 0 || n >= Array.length result.group_end then raise Not_found;
147 let gend = result.group_end.(n-1) in
148 if gend < 0 then raise Not_found else
152 let global_replace ~pat ~templ s =
155 Str.global_replace pat templ s)
158 let replace_first ~pat ~templ s =
161 Str.replace_first pat templ s)
164 let global_substitute ?(groups = 9) ~pat ~subst s =
168 let r = return_result 0 groups in
171 Str.global_substitute pat xsubst s)
174 let substitute_first ?(groups = 9) ~pat ~subst s =
178 let r = return_result 0 groups in
181 Str.substitute_first pat xsubst s)
184 (* replace_matched: n/a *)
192 let bounded_split ~sep s ~max =
195 Str.bounded_split sep s max)
198 let split_delim ~sep s =
201 Str.split_delim sep s)
204 let bounded_split_delim ~sep s ~max =
207 Str.bounded_split_delim sep s max)
210 let full_split ~sep s =
213 Str.full_split sep s)
216 let bounded_full_split ~sep s ~max =
219 Str.bounded_full_split sep s max)
222 let string_before = Str.string_before;;
223 let string_after = Str.string_after;;
224 let first_chars = Str.first_chars;;
225 let last_chars = Str.last_chars;;
227 (* ======================================================================
231 * Revision 1.1 2000/11/17 09:57:28 lpadovan
234 * Revision 1.2 2000/06/25 21:15:48 gerd
235 * Checked thread-safety.
237 * Revision 1.1 2000/06/25 20:48:19 gerd