CCstring.suffix

This commit is contained in:
Simon Cruanes 2014-12-18 14:09:00 +01:00
parent b8b0cc3645
commit 15aecd2435
2 changed files with 28 additions and 2 deletions

View file

@ -181,7 +181,17 @@ let prefix ~pre s =
String.length pre <= String.length s &&
(let i = ref 0 in
while !i < String.length pre && s.[!i] = pre.[!i] do incr i done;
!i = String.length pre)
!i = String.length pre
)
let suffix ~suf s =
String.length suf <= String.length s &&
let off = String.length s - String.length suf in
(let i = ref 0 in
while !i < String.length suf && s.[off + !i] = suf.[!i] do incr i done;
!i = String.length suf
)
let blit = String.blit

View file

@ -91,7 +91,23 @@ val repeat : string -> int -> string
(** The same string, repeated n times *)
val prefix : pre:string -> string -> bool
(** [str_prefix ~pre s] returns [true] iff [pre] is a prefix of [s] *)
(** [prefix ~pre s] returns [true] iff [pre] is a prefix of [s] *)
(*$T
prefix ~pre:"aab" "aabcd"
not (prefix ~pre:"ab" "aabcd")
not (prefix ~pre:"abcd" "abc")
*)
val suffix : suf:string -> string -> bool
(** [suffix ~suf s] returns [true] iff [suf] is a suffix of [s]
@since NEXT_RELEASE *)
(*$T
suffix ~suf:"cd" "abcd"
not (suffix ~suf:"cd" "abcde")
not (suffix ~suf:"abcd" "cd")
*)
include S with type t := string