From a1d714f9d4ed73c1e38f3ed8ec34df438112b8b8 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Wed, 20 Jun 2018 15:24:29 +0630 Subject: [PATCH] Fix definition of is_valid_jbuild Signed-off-by: Rudi Grinberg --- src/usexp/atom.ml | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/usexp/atom.ml b/src/usexp/atom.ml index 3034ff73..0935d0b8 100644 --- a/src/usexp/atom.ml +++ b/src/usexp/atom.ml @@ -4,28 +4,29 @@ let invalid_argf fmt = Printf.ksprintf invalid_arg fmt type syntax = Jbuild | Dune -let (is_valid_jbuild, is_valid_dune) = - let rec jbuild s i len = - i = len || - match String.unsafe_get s i with - | '#' -> disallow_next '|' s (i + 1) len - | '|' -> disallow_next '#' s (i + 1) len - | '"' | '(' | ')' | ';' | '\000'..'\032' | '\127'..'\255' -> false - | _ -> jbuild s (i + 1) len - and disallow_next c s i len = - i = len || String.unsafe_get s i <> c && jbuild s i len - in - let rec dune s i len = +let is_valid_dune = + let rec loop s i len = i = len || match String.unsafe_get s i with | '%' | '"' | '(' | ')' | ';' | '\000'..'\032' | '\127'..'\255' -> false - | _ -> dune s (i + 1) len + | _ -> loop s (i + 1) len in - let make looper s = + fun s -> let len = String.length s in - len > 0 && looper s 0 len + len > 0 && loop s 0 len + +let is_valid_jbuild str = + let len = String.length str in + len > 0 && + let rec loop ix = + match str.[ix] with + | '"' | '(' | ')' | ';' -> true + | '|' -> ix > 0 && let next = ix - 1 in str.[next] = '#' || loop next + | '#' -> ix > 0 && let next = ix - 1 in str.[next] = '|' || loop next + | ' ' | '\t' | '\n' | '\012' | '\r' -> true + | _ -> ix > 0 && loop (ix - 1) in - (make jbuild, make dune) + not (loop (len - 1)) let of_string s = A s