1 (* COCANWIKI - a wiki written in Objective CAML.
2 * Written by Richard W.M. Jones <rich@merjis.com>.
3 * Copyright (C) 2004 Merjis Ltd.
4 * $Id: cocanwiki_strings.ml,v 1.1 2004/10/21 11:42:05 rich Exp $
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 let string_contains substr str =
25 try String.find str substr; true
26 with String.Invalid_string -> false
28 let string_of_char = String.make 1
31 if String.length str < n then str else String.sub str 0 (n-1)
33 (* These versions only work in the C locale for 7-bit characters. *)
36 (* || c = '\f' *) || c = '\n' || c = '\r' || c = '\t' (* || c = '\v' *)
39 c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'
45 c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'
54 c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F'
56 let triml ?(test = isspace) str =
58 let n = ref (String.length str) in
59 while !n > 0 && test str.[!i]; do
64 else String.sub str !i !n
66 let trimr ?(test = isspace) str =
67 let n = ref (String.length str) in
68 while !n > 0 && test str.[!n-1]; do
71 if !n = String.length str then str
72 else String.sub str 0 !n
74 let trim ?(test = isspace) str =
77 let string_for_all f str =
78 let len = String.length str in
83 if not (f c) then false
89 let string_exists f str =
90 let len = String.length str in
101 let string_is_whitespace = string_for_all isspace