style update for some string functions

This commit is contained in:
Simon Cruanes 2017-09-07 10:10:34 +02:00
parent 14d701f84c
commit 54e12b7f62
2 changed files with 17 additions and 9 deletions

View file

@ -492,18 +492,20 @@ let repeat s n =
let prefix ~pre s = let prefix ~pre s =
String.length pre <= String.length s && String.length pre <= String.length s &&
(let i = ref 0 in begin
let i = ref 0 in
while !i < String.length pre && s.[!i] = pre.[!i] do incr i done; while !i < String.length pre && s.[!i] = pre.[!i] do incr i done;
!i = String.length pre !i = String.length pre
) end
let suffix ~suf s = let suffix ~suf s =
String.length suf <= String.length s && String.length suf <= String.length s &&
begin
let off = String.length s - String.length suf in let off = String.length s - String.length suf in
(let i = ref 0 in let i = ref 0 in
while !i < String.length suf && s.[off + !i] = suf.[!i] do incr i done; while !i < String.length suf && s.[off + !i] = suf.[!i] do incr i done;
!i = String.length suf !i = String.length suf
) end
let take n s = let take n s =
if n < String.length s if n < String.length s

View file

@ -212,6 +212,10 @@ val prefix : pre:string -> string -> bool
prefix ~pre:"aab" "aabcd" prefix ~pre:"aab" "aabcd"
not (prefix ~pre:"ab" "aabcd") not (prefix ~pre:"ab" "aabcd")
not (prefix ~pre:"abcd" "abc") not (prefix ~pre:"abcd" "abc")
prefix ~pre:"abc" "abcde"
prefix ~pre:"" ""
prefix ~pre:"" "abc"
prefix ~pre:"abc" "abc"
*) *)
val suffix : suf:string -> string -> bool val suffix : suf:string -> string -> bool
@ -220,6 +224,8 @@ val suffix : suf:string -> string -> bool
(*$T (*$T
suffix ~suf:"cd" "abcd" suffix ~suf:"cd" "abcd"
suffix ~suf:"" ""
suffix ~suf:"" "abc"
not (suffix ~suf:"cd" "abcde") not (suffix ~suf:"cd" "abcde")
not (suffix ~suf:"abcd" "cd") not (suffix ~suf:"abcd" "cd")
*) *)