1 (* Basic string functions.
2 * Copyright (C) 2004 Merjis Ltd.
3 * Written By Richard W.M. Jones (rich@merjis.com)
4 * $Id: cocanwiki_strings.ml,v 1.1 2004/09/07 14:58:34 rich Exp $
9 let string_contains substr str =
10 try String.find str substr; true
11 with String.Invalid_string -> false
13 let string_of_char = String.make 1
16 if String.length str < n then str else String.sub str 0 (n-1)
18 (* These versions only work in the C locale for 7-bit characters. *)
21 (* || c = '\f' *) || c = '\n' || c = '\r' || c = '\t' (* || c = '\v' *)
24 c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'
30 c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'
39 c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F'
41 let triml ?(test = isspace) str =
43 let n = ref (String.length str) in
44 while !n > 0 && test str.[!i]; do
49 else String.sub str !i !n
51 let trimr ?(test = isspace) str =
52 let n = ref (String.length str) in
53 while !n > 0 && test str.[!n-1]; do
56 if !n = String.length str then str
57 else String.sub str 0 !n
59 let trim ?(test = isspace) str =
62 let string_for_all f str =
63 let len = String.length str in
68 if not (f c) then false
74 let string_exists f str =
75 let len = String.length str in
86 let string_is_whitespace = string_for_all isspace