Add Misc.delete_eol_spaces

master
Jeremie Dimino 2016-02-24 17:29:47 +00:00
parent 86daba0e90
commit 68feb5f286
2 changed files with 36 additions and 0 deletions

View File

@ -652,3 +652,35 @@ let normalise_eol s =
if s.[i] <> '\r' then Buffer.add_char b s.[i]
done;
Buffer.contents b
let delete_eol_spaces src =
let len_src = String.length src in
let dst = Bytes.create len_src in
let rec loop i_src i_dst =
if i_src = len_src then
i_dst
else
match src.[i_src] with
| ' ' | '\t' ->
loop_spaces 1 (i_src + 1) i_dst
| c ->
Bytes.set dst i_dst c;
loop (i_src + 1) (i_dst + 1)
and loop_spaces spaces i_src i_dst =
if i_src = len_src then
i_dst
else
match src.[i_src] with
| ' ' | '\t' ->
loop_spaces (spaces + 1) (i_src + 1) i_dst
| '\n' ->
Bytes.set dst i_dst '\n';
loop (i_src + 1) (i_dst + 1)
| c ->
for n = 0 to spaces do
Bytes.set dst (i_dst + n) src.[i_src - spaces + n]
done;
loop (i_src + 1) (i_dst + spaces + 1)
in
let stop = loop 0 0 in
Bytes.sub_string dst 0 stop

View File

@ -310,3 +310,7 @@ val normalise_eol : string -> string
(** [normalise_eol s] returns a fresh copy of [s] with any '\r' characters
removed. Intended for pre-processing text which will subsequently be printed
on a channel which performs EOL transformations (i.e. Windows) *)
val delete_eol_spaces : string -> string
(** [delete_eol_spaces s] returns a fresh copy of [s] with any end of line spaces
removed. Intended to normalize the output of the toplevel for tests. *)