File: netstring_str.ml

package info (click to toggle)
netstring 0.10.1-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,000 kB
  • ctags: 895
  • sloc: ml: 8,389; xml: 416; makefile: 188; sh: 103
file content (238 lines) | stat: -rw-r--r-- 4,813 bytes parent folder | download
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
(* $Id: netstring_str.ml,v 1.2 2000/06/25 21:15:48 gerd Exp $
 * ----------------------------------------------------------------------
 *
 *)

let lock   = ref (fun () -> ());;
let unlock = ref (fun () -> ());;

let init_mt new_lock new_unlock =
  lock   := new_lock;
  unlock := new_unlock
;;

let protect f =
  !lock();
  try
    let r = f() in
    !unlock();
    r
  with
      x ->
	!unlock();
	raise x
;;

type regexp = Str.regexp;;
type split_result = Str.split_result = Text of string | Delim of string;;

type result =
    { pos : int;
      match_beg : int;
      match_end : int;
      group_beg : int array;
      group_end : int array;
    }
;;

let regexp s =
  protect
    (fun () -> Str.regexp s)
;;

let regexp_case_fold s =
  protect
    (fun () -> Str.regexp_case_fold s)
;;

let quote s =
  protect
    (fun () -> Str.quote s)
;;

let regexp_string s =
  protect
    (fun () -> Str.regexp_string s)
;;

let regexp_string_case_fold s =
  protect
    (fun () -> Str.regexp_string_case_fold s)
;;

let return_result pos n_groups =
  let r =
    { pos = pos;
      match_beg = (try Str.match_beginning() with Not_found -> -1);
      match_end = (try Str.match_end()       with Not_found -> -1);
      group_beg = Array.create n_groups (-1);
      group_end = Array.create n_groups (-1);
    }
  in
  for g = 0 to n_groups - 1 do
    r.group_beg.(g) <- (try Str.group_beginning (g+1) with Not_found -> -1);
    r.group_end.(g) <- (try Str.group_end (g+1)       with Not_found -> -1);
  done;
  r
;;

let string_match ?(groups = 9) pat s pos =
  protect
    (fun () ->
       if Str.string_match pat s pos then
	 Some (return_result pos groups)
       else
	 None
    )
;;

let string_partial_match ?(groups = 9) pat s pos =
  protect
    (fun () ->
       if Str.string_partial_match pat s pos then
	 Some (return_result pos groups)
       else
	 None
    )
;;

let search_forward ?(groups = 9) pat s pos =
  protect
    (fun () ->
       let i = Str.search_forward pat s pos in
       i, return_result pos groups
    )
;;

let search_backward ?(groups = 9) pat s pos =
  protect
    (fun () ->
       let i = Str.search_backward pat s pos in
       i, return_result pos groups
    )
;;

let matched_string result s =
  if result.match_beg < 0 or result.match_end < 0 then raise Not_found;
  String.sub s result.match_beg (result.match_end - result.match_beg)
;;

let match_beginning result =
  if result.match_beg < 0 then raise Not_found;
  result.match_beg
;;

let match_end result =
  if result.match_end < 0 then raise Not_found;
  result.match_end
;;

let matched_group result n s =
  if n < 0 || n >= Array.length result.group_beg then raise Not_found;
  let gbeg = result.group_beg.(n-1) in
  let gend = result.group_end.(n-1) in
  if gbeg < 0 or gend < 0 then raise Not_found;
  String.sub s gbeg (gend - gbeg)
;;

let group_beginning result n =
  if n < 0 || n >= Array.length result.group_beg then raise Not_found;
  let gbeg = result.group_beg.(n-1) in
  if gbeg < 0 then raise Not_found else 
    gbeg
;;

let group_end result n =
  if n < 0 || n >= Array.length result.group_end then raise Not_found;
  let gend = result.group_end.(n-1) in
  if gend < 0 then raise Not_found else 
    gend
;;

let global_replace pat templ s =
  protect
    (fun () ->
       Str.global_replace pat templ s)
;;

let replace_first pat templ s =
  protect
    (fun () ->
       Str.replace_first pat templ s)
;;

let global_substitute ?(groups = 9) pat subst s =
  protect
    (fun () ->
       let xsubst s =
	 let r = return_result 0 groups in
	 subst r s
       in
       Str.global_substitute pat xsubst s)
;;

let substitute_first ?(groups = 9) pat subst s =
  protect
    (fun () ->
       let xsubst s =
	 let r = return_result 0 groups in
	 subst r s
       in
       Str.substitute_first pat xsubst s)
;;

(* replace_matched: n/a *)

let split sep s =
  protect
    (fun () ->
       Str.split sep s)
;;

let bounded_split sep s max =
  protect
    (fun () ->
       Str.bounded_split sep s max)
;;

let split_delim sep s =
  protect
    (fun () ->
       Str.split_delim sep s)
;;

let bounded_split_delim sep s max =
  protect
    (fun () ->
       Str.bounded_split_delim sep s max)
;;

let full_split sep s =
  protect
    (fun () ->
       Str.full_split sep s)
;;

let bounded_full_split sep s max =
  protect
    (fun () ->
       Str.bounded_full_split sep s max)
;;

let string_before = Str.string_before;;
let string_after = Str.string_after;;
let first_chars = Str.first_chars;;
let last_chars = Str.last_chars;;

(* ======================================================================
 * History:
 * 
 * $Log: netstring_str.ml,v $
 * Revision 1.2  2000/06/25 21:15:48  gerd
 * 	Checked thread-safety.
 *
 * Revision 1.1  2000/06/25 20:48:19  gerd
 * 	Initial revision.
 *
 * 
 *)