| 1 |
module Re = Core |
| 2 |
|
| 3 |
exception Parse_error = Perl.Parse_error |
| 4 |
exception Not_supported = Perl.Not_supported |
| 5 |
|
| 6 |
type regexp = Re.re |
| 7 |
|
| 8 |
type flag = |
| 9 |
[ `CASELESS |
| 10 |
| `MULTILINE |
| 11 |
| `ANCHORED |
| 12 |
| `DOTALL |
| 13 |
] |
| 14 |
|
| 15 |
type split_result = |
| 16 |
| Text of string |
| 17 |
| Delim of string |
| 18 |
| Group of int * string |
| 19 |
| NoGroup |
| 20 |
|
| 21 |
type groups = Core.Group.t |
| 22 |
|
| 23 |
let re ?(flags = []) pat = |
| 24 |
let opts = |
| 25 |
List.map |
| 26 |
(function |
| 27 |
| `CASELESS -> `Caseless |
| 28 |
| `MULTILINE -> `Multiline |
| 29 |
| `ANCHORED -> `Anchored |
| 30 |
| `DOTALL -> `Dotall) |
| 31 |
flags |
| 32 |
in |
| 33 |
Perl.re ~opts pat |
| 34 |
;; |
| 35 |
|
| 36 |
let regexp ?flags pat = Re.compile (re ?flags pat) |
| 37 |
let extract ~rex s = Re.Group.all (Re.exec rex s) |
| 38 |
let exec ~rex ?pos s = Re.exec rex ?pos s |
| 39 |
let get_substring s i = Re.Group.get s i |
| 40 |
let names rex = Re.group_names rex |> List.map fst |> Array.of_list |
| 41 |
|
| 42 |
let get_named_substring rex name s = |
| 43 |
let rec loop = function |
| 44 |
| [] -> raise Not_found |
| 45 |
| (n, i) :: rem when n = name -> |
| 46 |
(try get_substring s i with |
| 47 |
| Not_found -> loop rem) |
| 48 |
| _ :: rem -> loop rem |
| 49 |
in |
| 50 |
loop (Re.group_names rex) |
| 51 |
;; |
| 52 |
|
| 53 |
let get_substring_ofs s i = Re.Group.offset s i |
| 54 |
let pmatch ~rex s = Re.execp rex s |
| 55 |
|
| 56 |
let substitute ~rex ~subst str = |
| 57 |
let b = Buffer.create 1024 in |
| 58 |
let rec loop pos on_match = |
| 59 |
if Re.execp ~pos rex str |
| 60 |
then ( |
| 61 |
let ss = Re.exec ~pos rex str in |
| 62 |
let start, fin = Re.Group.offset ss 0 in |
| 63 |
if on_match && start = pos && start = fin |
| 64 |
then ( |
| 65 |
if (* Empty match following a match *) |
| 66 |
pos < String.length str |
| 67 |
then ( |
| 68 |
Buffer.add_char b str.[pos]; |
| 69 |
loop (pos + 1) false)) |
| 70 |
else ( |
| 71 |
let pat = Re.Group.get ss 0 in |
| 72 |
Buffer.add_substring b str pos (start - pos); |
| 73 |
Buffer.add_string b (subst pat); |
| 74 |
if start = fin |
| 75 |
then ( |
| 76 |
if (* Manually advance by one after an empty match *) |
| 77 |
fin < String.length str |
| 78 |
then ( |
| 79 |
Buffer.add_char b str.[fin]; |
| 80 |
loop (fin + 1) false)) |
| 81 |
else loop fin true)) |
| 82 |
else Buffer.add_substring b str pos (String.length str - pos) |
| 83 |
in |
| 84 |
loop 0 false; |
| 85 |
Buffer.contents b |
| 86 |
;; |
| 87 |
|
| 88 |
let split ~rex str = |
| 89 |
let finish str last accu = |
| 90 |
let accu = String.sub str last (String.length str - last) :: accu in |
| 91 |
List.rev accu |
| 92 |
in |
| 93 |
let rec loop accu last pos on_match = |
| 94 |
if Re.execp ~pos rex str |
| 95 |
then ( |
| 96 |
let ss = Re.exec ~pos rex str in |
| 97 |
let start, fin = Re.Group.offset ss 0 in |
| 98 |
if on_match && start = pos && start = fin |
| 99 |
then |
| 100 |
if (* Empty match following a match *) |
| 101 |
pos = String.length str |
| 102 |
then finish str last accu |
| 103 |
else loop accu last (pos + 1) false |
| 104 |
else ( |
| 105 |
let accu = String.sub str last (start - last) :: accu in |
| 106 |
if start = fin |
| 107 |
then |
| 108 |
if (* Manually advance by one after an empty match *) |
| 109 |
fin = String.length str |
| 110 |
then finish str fin accu |
| 111 |
else loop accu fin (fin + 1) false |
| 112 |
else loop accu fin fin true)) |
| 113 |
else finish str last accu |
| 114 |
in |
| 115 |
loop [] 0 0 false |
| 116 |
;; |
| 117 |
|
| 118 |
(* From PCRE *) |
| 119 |
let string_unsafe_sub s ofs len = |
| 120 |
let r = Bytes.create len in |
| 121 |
Bytes.unsafe_blit s ofs r 0 len; |
| 122 |
Bytes.unsafe_to_string r |
| 123 |
;; |
| 124 |
|
| 125 |
let quote s = |
| 126 |
let len = String.length s in |
| 127 |
let buf = Bytes.create (len lsl 1) in |
| 128 |
let pos = ref 0 in |
| 129 |
for i = 0 to len - 1 do |
| 130 |
match String.unsafe_get s i with |
| 131 |
| ('\\' | '^' | '$' | '.' | '[' | '|' | '(' | ')' | '?' | '*' | '+' | '{') as c -> |
| 132 |
Bytes.unsafe_set buf !pos '\\'; |
| 133 |
incr pos; |
| 134 |
Bytes.unsafe_set buf !pos c; |
| 135 |
incr pos |
| 136 |
| c -> |
| 137 |
Bytes.unsafe_set buf !pos c; |
| 138 |
incr pos |
| 139 |
done; |
| 140 |
string_unsafe_sub buf 0 !pos |
| 141 |
;; |
| 142 |
|
| 143 |
let full_split ?(max = 0) ~rex s = |
| 144 |
if String.length s = 0 |
| 145 |
then [] |
| 146 |
else if max = 1 |
| 147 |
then [ Text s ] |
| 148 |
else ( |
| 149 |
let results = Re.split_full rex s in |
| 150 |
let matches = |
| 151 |
List.map |
| 152 |
(function |
| 153 |
| `Text s -> [ Text s ] |
| 154 |
| `Delim d -> |
| 155 |
let matches = Re.Group.all_offset d in |
| 156 |
let delim = Re.Group.get d 0 in |
| 157 |
Delim delim |
| 158 |
:: |
| 159 |
(let l = ref [] in |
| 160 |
for i = 1 to Array.length matches - 1 do |
| 161 |
l |
| 162 |
:= (if matches.(i) = (-1, -1) then NoGroup else Group (i, Re.Group.get d i)) |
| 163 |
:: !l |
| 164 |
done; |
| 165 |
List.rev !l)) |
| 166 |
results |
| 167 |
in |
| 168 |
List.concat matches) |
| 169 |
;; |
| 170 |
|
| 171 |
type substrings = Group.t |