Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added [index] to UTF8 and rope (with a few changes in rope's iteri fu…

…nctions)
  • Loading branch information...
commit ece3cb97b3badb2b7ea2dbe01bfdd9b0b6533c6d 1 parent 7b98984
Edgar Friendly authored
6  trunk/batteries/ChangeLog
... ...
@@ -1,3 +1,9 @@
  1
+2008-10-25  Edgar Friendly  <thelema314@gmail.com>
  2
+
  3
+	* src/core/extlib/extUTF8.ml*: [index] added
  4
+
  5
+	* src/core/extlib/rope.ml*: [iteri] extra argument, [bulk_iteri] added, [index] added (uses [bulk_iteri])
  6
+
1 7
 2008-10-24  Edgar Friendly  <thelema314@gmail.com>
2 8
 
3 9
 	* src/core/extlib/enum.ml*: [clump] added - helper for char enum -> string enum conversion
12  trunk/batteries/src/core/extlib/extUTF8.ml
@@ -221,6 +221,18 @@ module UTF8 = struct
221 221
     iter (fun c -> match f c with None -> () | Some c -> Buf.add_char b c) us;
222 222
     Buf.contents b
223 223
 
  224
+  let rec index_aux us c char_idx byte_idx =
  225
+    if look us byte_idx = c then char_idx
  226
+    else index_aux us c (char_idx+1) (next us byte_idx)
  227
+
  228
+  let index us c = (* relies on exception at end of string *)
  229
+    try 
  230
+      index_aux us c 0 0
  231
+    with 
  232
+	Invalid_argument "UTF8.next" -> raise Not_found
  233
+
  234
+
  235
+
224 236
   let compare s1 s2 = Pervasives.compare s1 s2
225 237
     
226 238
   let copy = String.copy
4  trunk/batteries/src/core/extlib/extUTF8.mli
@@ -200,6 +200,10 @@ val map : (UChar.t -> UChar.t) -> t -> t
200 200
 (** As [String.map] *)
201 201
 
202 202
 val filter_map : (UChar.t -> UChar.t option) -> t -> t
  203
+(** As [String.filter_map] *)
  204
+
  205
+val index : t -> UChar.t -> int
  206
+(** As [String.index] *)
203 207
 
204 208
 (** Buffer module for UTF-8 strings *)
205 209
 module Buf : sig
34  trunk/batteries/src/core/extlib/rope.ml
@@ -291,21 +291,25 @@ let rec iter f = function
291 291
   | Leaf (_,s) -> UTF8.iter f s
292 292
   | Concat(l,_,r,_,_) -> iter f l; iter f r
293 293
  
294  
-let iteri f r =
295  
-  let rec aux f i = function
  294
+let rec iteri ?(base=0) f = function
296 295
     Empty -> ()
297 296
   | Leaf (_,s) ->
298 297
       let e = UTF8.enum s in
299  
-      Enum.iteri (fun j c -> f (i+j) c) e
300  
-  | Concat(l,cl,r,_,_) -> aux f i l; aux f (i + cl) r
301  
-  in
302  
-    aux f 0 r
  298
+      Enum.iteri (fun j c -> f (base+j) c) e
  299
+  | Concat(l,cl,r,_,_) -> iteri ~base f l; iteri ~base:(base + cl) f r
303 300
  
304 301
 let rec bulk_iter f = function
305 302
     Empty -> ()
306 303
   | Leaf (_,s) -> f s
307 304
   | Concat(l,_,r,_,_) -> bulk_iter f l; bulk_iter f r
308  
- 
  305
+
  306
+let rec bulk_iteri ?(base=0) f = function
  307
+    Empty -> ()
  308
+  | Leaf (_,s) -> f base s
  309
+  | Concat(l,cl,r,_,_) -> 
  310
+      bulk_iteri ~base f l; 
  311
+      bulk_iteri ~base:(base+cl) f r
  312
+
309 313
 let rec range_iter f start len = function
310 314
     Empty -> if start <> 0 || len <> 0 then raise Out_of_bounds
311 315
   | Leaf (lens, s) ->
@@ -480,4 +484,20 @@ let map f r = bulk_map (fun s -> UTF8.map f s) r
480 484
 
481 485
 let bulk_filter_map f r = bulk_fold (fun acc s -> match f s with None -> acc | Some r -> append acc r) Empty r
482 486
 let filter_map f r = bulk_map (UTF8.filter_map f) r
  487
+
  488
+open Labels
  489
+
  490
+let index r item = 
  491
+  label (fun return ->
  492
+	   let index_aux i us =
  493
+	     try 
  494
+	       let p = UTF8.index us item in
  495
+	       recall return (p+i)
  496
+	     with Not_found -> ()
  497
+	   in
  498
+	   bulk_iteri index_aux r;
  499
+	   raise Not_found)
  500
+
  501
+
  502
+
483 503
 (* =end *)
10  trunk/batteries/src/core/extlib/rope.mli
@@ -196,7 +196,7 @@ val iter : (UChar.t -> unit) -> t -> unit
196 196
   (** [iter f r] applies [f] to all the characters in the [r] rope,
197 197
       in order. *)
198 198
   
199  
-val iteri : (int -> UChar.t -> unit) -> t -> unit
  199
+val iteri : ?base:int -> (int -> UChar.t -> unit) -> t -> unit
200 200
   (** Operates like iter, but also passes the index of the character
201 201
       to the given function. *)
202 202
   
@@ -211,6 +211,9 @@ val range_iter : (UChar.t -> unit) -> int -> int -> t -> unit
211 211
   
212 212
 val bulk_iter : (UTF8.t -> unit) -> t -> unit
213 213
   (** as iter but over larger chunks of data *)
  214
+
  215
+val bulk_iteri : ?base:int -> (int -> UTF8.t -> unit) -> t -> unit
  216
+  (** as iteri but over larger chunks of data. *)
214 217
   
215 218
 val fold : ('a -> UChar.t -> 'a ) -> 'a -> t -> 'a
216 219
   (** [Rope.fold f a r] computes [ f (... (f (f a r0) r1)...) rN-1 ]
@@ -247,6 +250,11 @@ val bulk_filter_map : (UTF8.t -> UTF8.t option) -> t -> t
247 250
 val filter_map : (UChar.t -> UChar.t option) -> t -> t
248 251
 (** As [String.filter_map] *)
249 252
 
  253
+val index : t -> UChar.t -> int
  254
+(** As [String.index] *)
  255
+
  256
+
  257
+
250 258
 (** {6 Boilerplate code}*)
251 259
 (** {7 S-Expressions}*)
252 260
 

0 notes on commit ece3cb9

Please sign in to comment.
Something went wrong with that request. Please try again.