| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open BenchUtil |
| 12 |
|
| 13 |
let source_substring source start stop = |
| 14 |
|
| 15 |
|
| 16 |
let start = if start = 0 then 1 else start in |
| 17 |
let len = stop - start + 1 in |
| 18 |
String.sub source (start-1) len |
| 19 |
|
| 20 |
let count_newlines s = String.fold_left (fun n c -> if c = '\n' then n+1 else n) 0 s |
| 21 |
|
| 22 |
let is_white_char = function ' '|'\n'|'\t' -> true | _ -> false |
| 23 |
|
| 24 |
let rec join_loop ~dummy ~source ~last_end ~lines acc = function |
| 25 |
| [] -> |
| 26 |
let sourcelen = String.length source in |
| 27 |
let acc = if last_end + 1 <= sourcelen then |
| 28 |
let text = source_substring source (last_end+1) sourcelen in |
| 29 |
if String.for_all is_white_char text then acc |
| 30 |
else |
| 31 |
({ chars = { start_char = last_end+1; stop_char = sourcelen; }; |
| 32 |
line = lines+1; text}, dummy) |
| 33 |
:: acc |
| 34 |
else |
| 35 |
acc |
| 36 |
in |
| 37 |
List.rev acc |
| 38 |
| (loc,v) :: rest -> |
| 39 |
let acc, lines, last_end = |
| 40 |
if loc.start_char > last_end + 1 then |
| 41 |
let text = source_substring source (last_end + 1) (loc.start_char - 1) in |
| 42 |
|
| 43 |
|
| 44 |
if not (String.for_all is_white_char text) then |
| 45 |
let n = count_newlines text in |
| 46 |
let acc = |
| 47 |
({ chars = { start_char = last_end + 1; stop_char = loc.start_char - 1; }; |
| 48 |
line = lines; text }, dummy) |
| 49 |
:: acc |
| 50 |
in |
| 51 |
acc, (lines+n), loc.start_char - 1 |
| 52 |
else acc, lines, last_end |
| 53 |
else acc, lines, last_end |
| 54 |
in |
| 55 |
let text = source_substring source (last_end+1) loc.stop_char in |
| 56 |
let lines, n = if text <> "" && text.[0] = '\n' then lines+1, 1 else lines, 0 in |
| 57 |
let n = count_newlines text - n in |
| 58 |
let acc = |
| 59 |
({ chars = { start_char = last_end + 1; stop_char = loc.stop_char; }; |
| 60 |
line = lines; text }, v) |
| 61 |
:: acc |
| 62 |
in |
| 63 |
join_loop ~dummy ~source ~last_end:loc.stop_char ~lines:(lines + n) acc rest |
| 64 |
|
| 65 |
let join_to_source ~dummy ~source vals = |
| 66 |
join_loop ~dummy ~source ~last_end:(-1) ~lines:1 [] vals |