(* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. * $Id: cocanwiki_strings.ml,v 1.2 2004/09/09 12:21:22 rich Exp $ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; see the file COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. *) 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