From 40260a31cd197f655e6d3e0570a88d96fc1a9cac Mon Sep 17 00:00:00 2001 From: Hugo Herbelin Date: Wed, 4 Oct 2017 15:37:36 +0200 Subject: Fixing BZ#5769 (variable of type "_something" was named after invalid "_"). --- lib/unicode.ml | 28 ++++++++++++++++++++++++++++ lib/unicode.mli | 4 ++++ 2 files changed, 32 insertions(+) (limited to 'lib') diff --git a/lib/unicode.ml b/lib/unicode.ml index 528d6434c..7f3743900 100644 --- a/lib/unicode.ml +++ b/lib/unicode.ml @@ -194,6 +194,14 @@ let is_unknown = function | Unknown -> true | Letter | IdentSep | IdentPart | Symbol -> false +let is_ident_part = function + | IdentPart -> true + | Letter | IdentSep | Symbol | Unknown -> false + +let is_ident_sep = function + | IdentSep -> true + | Letter | IdentPart | Symbol | Unknown -> false + let ident_refutation s = if s = ".." then None else try let j, n = next_utf8 s 0 in @@ -227,6 +235,26 @@ let lowercase_first_char s = let j, n = next_utf8 s 0 in utf8_of_unicode (lowercase_unicode n) +let split_at_first_letter s = + let n, v = next_utf8 s 0 in + if ((* optim *) n = 1 && s.[0] != '_') || not (is_ident_sep (classify v)) then None + else begin + let n = ref n in + let p = ref 0 in + while !n < String.length s && + let n', v = next_utf8 s !n in + p := n'; + (* Test if not letter *) + ((* optim *) n' = 1 && (s.[!n] = '_' || s.[!n] = '\'')) + || let st = classify v in + is_ident_sep st || is_ident_part st + do n := !n + !p + done; + let s1 = String.sub s 0 !n in + let s2 = String.sub s !n (String.length s - !n) in + Some (s1,s2) + end + (** For extraction, we need to encode unicode character into ascii ones *) let is_basic_ascii s = diff --git a/lib/unicode.mli b/lib/unicode.mli index a608d5f02..49b844493 100644 --- a/lib/unicode.mli +++ b/lib/unicode.mli @@ -30,6 +30,10 @@ val is_unknown : status -> bool @raise Assert_failure if the input string is empty. *) val lowercase_first_char : string -> string +(** Split a string supposed to be an ident at the first letter; + as an optimization, return None if the first character is a letter *) +val split_at_first_letter : string -> (string * string) option + (** Return [true] if all UTF-8 characters in the input string are just plain ASCII characters. Returns [false] otherwise. *) val is_basic_ascii : string -> bool -- cgit v1.2.3