About half way through switching cocanwiki to using the new PG interface.
[cocanwiki.git] / scripts / lib / cocanwiki_strings.ml
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.3 2006/03/27 16:43:44 rich Exp $
5  *
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.
10  *
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.
15  *
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.
20  *)
21
22 open ExtString
23
24 let string_contains substr str =
25   try ignore (String.find str substr); true
26   with Invalid_string -> false
27
28 let string_of_char = String.make 1
29
30 let truncate n str =
31   if String.length str < n then str else String.sub str 0 (n-1)
32
33 (* These versions only work in the C locale for 7-bit characters. *)
34 let isspace c =
35   c = ' '
36   (* || c = '\f' *) || c = '\n' || c = '\r' || c = '\t' (* || c = '\v' *)
37
38 let isalpha c =
39   c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'
40
41 let isdigit c =
42   c >= '0' && c <= '9'
43
44 let isalnum c =
45   c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'
46
47 let islower c =
48   c >= 'a' && c <= 'z'
49
50 let isupper c =
51   c >= 'A' && c <= 'Z'
52
53 let isxdigit c =
54   c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F'
55
56 let triml ?(test = isspace) str =
57   let i = ref 0 in
58   let n = ref (String.length str) in
59   while !n > 0 && test str.[!i]; do
60     decr n;
61     incr i
62   done;
63   if !i = 0 then str
64   else String.sub str !i !n
65
66 let trimr ?(test = isspace) str =
67   let n = ref (String.length str) in
68   while !n > 0 && test str.[!n-1]; do
69     decr n
70   done;
71   if !n = String.length str then str
72   else String.sub str 0 !n
73
74 let trim ?(test = isspace) str =
75   trimr (triml str)
76
77 let string_for_all f str =
78   let len = String.length str in
79   let rec loop i =
80     if i = len then true
81     else (
82       let c = str.[i] in
83       if not (f c) then false
84       else loop (i+1)
85     )
86   in
87   loop 0
88
89 let string_exists f str =
90   let len = String.length str in
91   let rec loop i =
92     if i = len then false
93     else (
94       let c = str.[i] in
95       if f c then true
96       else loop (i+1)
97     )
98   in
99   loop 0
100
101 let string_is_whitespace = string_for_all isspace