Skip to content

Commit 0032bc0

Browse files
Merge pull request #386 from Leonidas-from-XIV/semisemi-dedup
Deduplicate implementation of `;;` detection
2 parents cdcc344 + e6667dc commit 0032bc0

File tree

3 files changed

+9
-16
lines changed

3 files changed

+9
-16
lines changed

lib/block.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,7 @@ let guess_ocaml_kind contents =
217217

218218
let ends_by_semi_semi c =
219219
match List.rev c with
220-
| h :: _ ->
221-
let len = String.length h in
222-
len > 2 && h.[len - 1] = ';' && h.[len - 2] = ';'
220+
| h :: _ -> Astring.String.is_suffix ~affix:";;" h
223221
| _ -> false
224222

225223
let pp_line_directive ppf (file, line) = Fmt.pf ppf "#%d %S" line file

lib/block.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,7 @@ val executable_contents : syntax:Syntax.t -> t -> string list
159159
(e.g. the phrase result is discarded). *)
160160

161161
val is_active : ?section:string -> t -> bool
162+
163+
(** {2 Helpers} *)
164+
165+
val ends_by_semi_semi : string list -> bool

lib/top/mdx_top.ml

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,10 @@ module Phrase = struct
141141
let endpos = lexbuf.Lexing.lex_curr_p in
142142
{ doc = { lexbuf; contents }; startpos; endpos; parsed }
143143

144-
let ends_by_semi_semi c =
145-
match List.rev c with
146-
| h :: _ ->
147-
let len = String.length h in
148-
len > 2 && h.[len - 1] = ';' && h.[len - 2] = ';'
149-
| _ -> false
150-
151144
let parse lines =
152-
let lines = if ends_by_semi_semi lines then lines else lines @ [ ";;" ] in
145+
let lines =
146+
if Mdx.Block.ends_by_semi_semi lines then lines else lines @ [ ";;" ]
147+
in
153148
match parse lines with exception End_of_file -> None | t -> Some t
154149

155150
(** Returns the name of the toplevel directive or [None] if the given phrase
@@ -394,13 +389,9 @@ let rtrim l = List.rev (ltrim (List.rev l))
394389
let trim l = ltrim (rtrim (List.map trim_line l))
395390

396391
let cut_into_sentences l =
397-
let ends_by_semi_semi h =
398-
let len = String.length h in
399-
len > 2 && h.[len - 1] = ';' && h.[len - 2] = ';'
400-
in
401392
let rec aux acc sentence = function
402393
| [] -> List.rev (List.rev sentence :: acc)
403-
| h :: t when ends_by_semi_semi h ->
394+
| h :: t when Mdx.Block.ends_by_semi_semi [ h ] ->
404395
aux (List.rev (h :: sentence) :: acc) [] t
405396
| h :: t -> aux acc (h :: sentence) t
406397
in

0 commit comments

Comments
 (0)