diff --git a/src/core/CCString.cppo.ml b/src/core/CCString.cppo.ml index 0ac4cca7..2fd5639d 100644 --- a/src/core/CCString.cppo.ml +++ b/src/core/CCString.cppo.ml @@ -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 diff --git a/src/core/CCString.mli b/src/core/CCString.mli index 06dd50e8..9e6f7e98 100644 --- a/src/core/CCString.mli +++ b/src/core/CCString.mli @@ -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