Skip to content

Commit

Permalink
Remove unecessary line ending computation
Browse files Browse the repository at this point in the history
  • Loading branch information
Julow authored and jonludlam committed Oct 22, 2020
1 parent 6a2b91d commit 8342a24
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/parser/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ let trim_trailing_blank_lines : string -> string = fun s ->

let trim_leading_whitespace : first_line_offset:int -> string -> string =
fun ~first_line_offset s ->
(** Returns [None] for an empty, [Some ident] for an indented line. *)
let count_leading_whitespace line =
let rec count_leading_whitespace' index len =
if index = len then None
Expand All @@ -83,21 +84,17 @@ let trim_leading_whitespace : first_line_offset:int -> string -> string =
let len = String.length line in
(* '\r' may remain because we only split on '\n' below. This is important
for the first line, which would be considered not empty without this check. *)
let len, ending =
if len > 0 && line.[len - 1] = '\r' then (len - 1, "\r\n") else (len, "\n")
in
match count_leading_whitespace' 0 len with
| Some index -> `Leading_whitespace index
| None -> `Blank_line ending
let len = if len > 0 && line.[len - 1] = '\r' then len - 1 else len in
count_leading_whitespace' 0 len
in

let lines = Astring.String.cuts ~sep:"\n" s in

let least_amount_of_whitespace =
List.fold_left (fun least_so_far line ->
match (count_leading_whitespace line, least_so_far) with
| (`Leading_whitespace n, None) -> Some n
| (`Leading_whitespace n, Some least) when n < least -> Some n
| (Some _ as n', None) -> n'
| (Some n as n', Some least) when n < least -> n'
| _ -> least_so_far)
in

Expand All @@ -106,9 +103,9 @@ let trim_leading_whitespace : first_line_offset:int -> string -> string =
| [] -> 0, None
| first_line :: tl ->
begin match count_leading_whitespace first_line with
| `Leading_whitespace n ->
| Some n ->
n, least_amount_of_whitespace (Some (first_line_offset + n)) tl
| `Blank_line _ ->
| None ->
0, least_amount_of_whitespace None tl
end
in
Expand Down

0 comments on commit 8342a24

Please sign in to comment.