(* Basic string functions. * Copyright (C) 2004 Merjis Ltd. * Written By Richard W.M. Jones (rich@merjis.com) * $Id: cocanwiki_strings.ml,v 1.1 2004/09/07 14:58:34 rich Exp $ *) open ExtString let string_contains substr str = try String.find str substr; true with String.Invalid_string -> false let string_of_char = String.make 1 let truncate n str = if String.length str < n then str else String.sub str 0 (n-1) (* These versions only work in the C locale for 7-bit characters. *) let isspace c = c = ' ' (* || c = '\f' *) || c = '\n' || c = '\r' || c = '\t' (* || c = '\v' *) let isalpha c = c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' let isdigit c = c >= '0' && c <= '9' let isalnum c = c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' let islower c = c >= 'a' && c <= 'z' let isupper c = c >= 'A' && c <= 'Z' let isxdigit c = c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F' let triml ?(test = isspace) str = let i = ref 0 in let n = ref (String.length str) in while !n > 0 && test str.[!i]; do decr n; incr i done; if !i = 0 then str else String.sub str !i !n let trimr ?(test = isspace) str = let n = ref (String.length str) in while !n > 0 && test str.[!n-1]; do decr n done; if !n = String.length str then str else String.sub str 0 !n let trim ?(test = isspace) str = trimr (triml str) let string_for_all f str = let len = String.length str in let rec loop i = if i = len then true else ( let c = str.[i] in if not (f c) then false else loop (i+1) ) in loop 0 let string_exists f str = let len = String.length str in let rec loop i = if i = len then false else ( let c = str.[i] in if f c then true else loop (i+1) ) in loop 0 let string_is_whitespace = string_for_all isspace