diff --git a/src/core/CCString.cppo.ml b/src/core/CCString.cppo.ml index 37b29b65..71a01709 100644 --- a/src/core/CCString.cppo.ml +++ b/src/core/CCString.cppo.ml @@ -391,17 +391,6 @@ end let split_on_char c s: _ list = Split.list_cpy ~by:(String.make 1 c) s -(*$= & ~printer:Q.Print.(list string) - ["a"; "few"; "words"; "from"; "our"; "sponsors"] \ - (split_on_char ' ' "a few words from our sponsors") -*) - -(*$Q - Q.(printable_string) (fun s -> \ - let s = split_on_char ' ' s |> String.concat " " in \ - s = split_on_char ' ' s |> String.concat " ") -*) - let split = Split.list_cpy let compare_versions a b = @@ -721,7 +710,16 @@ let lowercase_ascii = map CCChar.lowercase_ascii #endif - +let equal_caseless s1 s2: bool = + let char_lower c = + if c >= 'A' && c <= 'Z' + then Char.unsafe_chr (Char. code c + 32) + else c + in + String.length s1 = String.length s2 && + for_all2 + (fun c1 c2 -> Char.equal (char_lower c1) (char_lower c2)) + s1 s2 let pp buf s = Buffer.add_char buf '"'; diff --git a/src/core/CCString.mli b/src/core/CCString.mli index bbf59ddc..b167e824 100644 --- a/src/core/CCString.mli +++ b/src/core/CCString.mli @@ -399,6 +399,22 @@ val uppercase_ascii : string -> string val lowercase_ascii : string -> string (** See {!String}. @since 0.18 *) +val equal_caseless : string -> string -> bool +(** Comparison without respect to {b ascii} lowercase. + @since NEXT_RELEASE *) + +(*$T + equal_caseless "foo" "FoO" + equal_caseless "helLo" "HEllO" +*) + +(*$Q + Q.(pair printable_string printable_string) (fun (s1,s2) -> \ + equal_caseless s1 s2 = equal (lowercase_ascii s1)(lowercase_ascii s2)) + Q.(printable_string) (fun s -> equal_caseless s s) + Q.(printable_string) (fun s -> equal_caseless (uppercase_ascii s) s) +*) + (** {2 Finding} A relatively efficient algorithm for finding sub-strings @@ -499,6 +515,17 @@ val split_on_char : char -> string -> string list (** Split the string along the given char @since NEXT_RELEASE *) +(*$= & ~printer:Q.Print.(list string) + ["a"; "few"; "words"; "from"; "our"; "sponsors"] \ + (split_on_char ' ' "a few words from our sponsors") +*) + +(*$Q + Q.(printable_string) (fun s -> \ + let s = split_on_char ' ' s |> String.concat " " in \ + s = (split_on_char ' ' s |> String.concat " ")) +*) + val split : by:string -> string -> string list (** Alias to {!Split.list_cpy} @since NEXT_RELEASE *)