add CCstring.equal_caseless (close #112)

This commit is contained in:
Simon Cruanes 2017-04-18 21:41:44 +02:00
parent c725543faa
commit 1b200ff695
2 changed files with 37 additions and 12 deletions

View file

@ -391,17 +391,6 @@ end
let split_on_char c s: _ list = let split_on_char c s: _ list =
Split.list_cpy ~by:(String.make 1 c) s 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 split = Split.list_cpy
let compare_versions a b = let compare_versions a b =
@ -721,7 +710,16 @@ let lowercase_ascii = map CCChar.lowercase_ascii
#endif #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 = let pp buf s =
Buffer.add_char buf '"'; Buffer.add_char buf '"';

View file

@ -399,6 +399,22 @@ val uppercase_ascii : string -> string
val lowercase_ascii : string -> string val lowercase_ascii : string -> string
(** See {!String}. @since 0.18 *) (** 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} (** {2 Finding}
A relatively efficient algorithm for finding sub-strings 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 (** Split the string along the given char
@since NEXT_RELEASE *) @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 val split : by:string -> string -> string list
(** Alias to {!Split.list_cpy} (** Alias to {!Split.list_cpy}
@since NEXT_RELEASE *) @since NEXT_RELEASE *)