| 1 |
let all ?(pos = 0) ?len re s : _ Seq.t = |
| 2 |
if pos < 0 then invalid_arg "Re.all"; |
| 3 |
(* index of the first position we do not consider. |
| 4 |
!pos < limit is an invariant *) |
| 5 |
let limit = |
| 6 |
match len with |
| 7 |
| None -> String.length s |
| 8 |
| Some l -> |
| 9 |
if l < 0 || pos + l > String.length s then invalid_arg "Re.all"; |
| 10 |
pos + l |
| 11 |
in |
| 12 |
(* iterate on matches. When a match is found, search for the next |
| 13 |
one just after its end *) |
| 14 |
let rec aux pos on_match () = |
| 15 |
if pos > limit |
| 16 |
then Seq.Nil (* no more matches *) |
| 17 |
else ( |
| 18 |
match |
| 19 |
Compile.match_str ~groups:true ~partial:false re s ~pos ~len:(limit - pos) |
| 20 |
with |
| 21 |
| Match substr -> |
| 22 |
let p1, p2 = Group.offset substr 0 in |
| 23 |
if on_match && p1 = pos && p1 = p2 |
| 24 |
then (* skip empty match right after a match *) |
| 25 |
aux (pos + 1) false () |
| 26 |
else ( |
| 27 |
let pos = if p1 = p2 then p2 + 1 else p2 in |
| 28 |
Seq.Cons (substr, aux pos (p1 <> p2))) |
| 29 |
| Running _ | Failed -> Seq.Nil) |
| 30 |
in |
| 31 |
aux pos false |
| 32 |
;; |
| 33 |
|
| 34 |
let matches ?pos ?len re s : _ Seq.t = |
| 35 |
all ?pos ?len re s |> Seq.map (fun sub -> Group.get sub 0) |
| 36 |
;; |
| 37 |
|
| 38 |
let split_full ?(pos = 0) ?len re s : _ Seq.t = |
| 39 |
if pos < 0 then invalid_arg "Re.split"; |
| 40 |
let limit = |
| 41 |
match len with |
| 42 |
| None -> String.length s |
| 43 |
| Some l -> |
| 44 |
if l < 0 || pos + l > String.length s then invalid_arg "Re.split"; |
| 45 |
pos + l |
| 46 |
in |
| 47 |
(* i: start of delimited string |
| 48 |
pos: first position after last match of [re] |
| 49 |
limit: first index we ignore (!pos < limit is an invariant) *) |
| 50 |
let pos0 = pos in |
| 51 |
let rec aux state i pos () = |
| 52 |
match state with |
| 53 |
| `Idle when pos > limit -> |
| 54 |
(* We had an empty match at the end of the string *) |
| 55 |
assert (i = limit); |
| 56 |
Seq.Nil |
| 57 |
| `Idle -> |
| 58 |
(match |
| 59 |
Compile.match_str ~groups:true ~partial:false re s ~pos ~len:(limit - pos) |
| 60 |
with |
| 61 |
| Match substr -> |
| 62 |
let p1, p2 = Group.offset substr 0 in |
| 63 |
let pos = if p1 = p2 then p2 + 1 else p2 in |
| 64 |
let old_i = i in |
| 65 |
let i = p2 in |
| 66 |
if old_i = p1 && p1 = p2 && p1 > pos0 |
| 67 |
then (* Skip empty match right after a delimiter *) |
| 68 |
aux state i pos () |
| 69 |
else if p1 > pos0 |
| 70 |
then ( |
| 71 |
(* string does not start by a delimiter *) |
| 72 |
let text = String.sub s old_i (p1 - old_i) in |
| 73 |
let state = `Yield (`Delim substr) in |
| 74 |
Seq.Cons (`Text text, aux state i pos)) |
| 75 |
else Seq.Cons (`Delim substr, aux state i pos) |
| 76 |
| Running _ -> Seq.Nil |
| 77 |
| Failed -> |
| 78 |
if i < limit |
| 79 |
then ( |
| 80 |
let text = String.sub s i (limit - i) in |
| 81 |
(* yield last string *) |
| 82 |
Seq.Cons (`Text text, aux state limit pos)) |
| 83 |
else Seq.Nil) |
| 84 |
| `Yield x -> Seq.Cons (x, aux `Idle i pos) |
| 85 |
in |
| 86 |
aux `Idle pos pos |
| 87 |
;; |
| 88 |
|
| 89 |
let split ?pos ?len re s : _ Seq.t = |
| 90 |
let seq = split_full ?pos ?len re s in |
| 91 |
let rec filter seq () = |
| 92 |
match seq () with |
| 93 |
| Seq.Nil -> Seq.Nil |
| 94 |
| Seq.Cons (`Delim _, tl) -> filter tl () |
| 95 |
| Seq.Cons (`Text s, tl) -> Seq.Cons (s, filter tl) |
| 96 |
in |
| 97 |
filter seq |
| 98 |
;; |
| 99 |
|
| 100 |
let split_delim ?pos ?len re s : _ Seq.t = |
| 101 |
let seq = split_full ?pos ?len re s in |
| 102 |
let rec filter ~delim seq () = |
| 103 |
match seq () with |
| 104 |
| Seq.Nil -> if delim then Seq.Cons ("", fun () -> Seq.Nil) else Seq.Nil |
| 105 |
| Seq.Cons (`Delim _, tl) -> |
| 106 |
if delim |
| 107 |
then Seq.Cons ("", fun () -> filter ~delim:true tl ()) |
| 108 |
else filter ~delim:true tl () |
| 109 |
| Seq.Cons (`Text s, tl) -> Seq.Cons (s, filter ~delim:false tl) |
| 110 |
in |
| 111 |
filter ~delim:true seq |
| 112 |
;; |