Changed license terms to the plain LGPL thru-out.
[bpt/guile.git] / libguile / strorder.c
CommitLineData
f2c9fcb0 1/* Copyright (C) 1995, 1996, 1999, 2000 Free Software Foundation, Inc.
0f2d19dd 2 *
73be1d9e
MV
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
0f2d19dd 7 *
73be1d9e
MV
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
0f2d19dd 12 *
73be1d9e
MV
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
6e8d25a6 17
0f2d19dd
JB
18\f
19
a0599745
MD
20#include "libguile/_scm.h"
21#include "libguile/chars.h"
a002f1a2
DH
22#include "libguile/strings.h"
23#include "libguile/symbols.h"
0f2d19dd 24
a0599745
MD
25#include "libguile/validate.h"
26#include "libguile/strorder.h"
0f2d19dd
JB
27\f
28
c3ee7520 29SCM_DEFINE1 (scm_string_equal_p, "string=?", scm_tc7_rpsubr,
6e8d25a6 30 (SCM s1, SCM s2),
1e6808ea
MG
31 "Lexicographic equality predicate; return @code{#t} if the two\n"
32 "strings are the same length and contain the same characters in\n"
33 "the same positions, otherwise return @code{#f}.\n"
34 "\n"
35 "The procedure @code{string-ci=?} treats upper and lower case\n"
36 "letters as though they were the same character, but\n"
37 "@code{string=?} treats upper and lower case as distinct\n"
38 "characters.")
6e8d25a6 39#define FUNC_NAME s_scm_string_equal_p
0f2d19dd 40{
1be6b49c 41 size_t length;
0f2d19dd 42
e9bfab50
DH
43 SCM_VALIDATE_STRING (1, s1);
44 SCM_VALIDATE_STRING (2, s2);
45
46 length = SCM_STRING_LENGTH (s2);
47 if (SCM_STRING_LENGTH (s1) == length)
0f2d19dd 48 {
34f0f2b8
DH
49 unsigned char *c1 = SCM_STRING_UCHARS (s1) + length - 1;
50 unsigned char *c2 = SCM_STRING_UCHARS (s2) + length - 1;
1be6b49c 51 size_t i;
e9bfab50
DH
52
53 /* comparing from back to front typically finds mismatches faster */
54 for (i = 0; i != length; ++i, --c1, --c2)
55 if (*c1 != *c2)
56 return SCM_BOOL_F;
57
58 return SCM_BOOL_T;
0f2d19dd 59 }
e9bfab50
DH
60 else
61 {
0f2d19dd 62 return SCM_BOOL_F;
e9bfab50 63 }
0f2d19dd 64}
6e8d25a6 65#undef FUNC_NAME
0f2d19dd 66
e9bfab50 67
c3ee7520 68SCM_DEFINE1 (scm_string_ci_equal_p, "string-ci=?", scm_tc7_rpsubr,
6e8d25a6 69 (SCM s1, SCM s2),
1e6808ea
MG
70 "Case-insensitive string equality predicate; return @code{#t} if\n"
71 "the two strings are the same length and their component\n"
72 "characters match (ignoring case) at each position; otherwise\n"
73 "return @code{#f}.")
6e8d25a6 74#define FUNC_NAME s_scm_string_ci_equal_p
0f2d19dd 75{
1be6b49c 76 size_t length;
6e8d25a6 77
e9bfab50
DH
78 SCM_VALIDATE_STRING (1, s1);
79 SCM_VALIDATE_STRING (2, s2);
80
81 length = SCM_STRING_LENGTH (s2);
82 if (SCM_STRING_LENGTH (s1) == length)
0f2d19dd 83 {
34f0f2b8
DH
84 unsigned char *c1 = SCM_STRING_UCHARS (s1) + length - 1;
85 unsigned char *c2 = SCM_STRING_UCHARS (s2) + length - 1;
1be6b49c 86 size_t i;
e9bfab50
DH
87
88 /* comparing from back to front typically finds mismatches faster */
89 for (i = 0; i != length; ++i, --c1, --c2)
90 if (scm_upcase (*c1) != scm_upcase (*c2))
91 return SCM_BOOL_F;
92
93 return SCM_BOOL_T;
0f2d19dd 94 }
e9bfab50
DH
95 else
96 {
0f2d19dd 97 return SCM_BOOL_F;
e9bfab50 98 }
0f2d19dd 99}
6e8d25a6 100#undef FUNC_NAME
0f2d19dd 101
e9bfab50 102
3ba5a6c2
DH
103/* Helper function for the lexicographic ordering predicates.
104 * No argument checking is performed. */
105static SCM
106string_less_p (SCM s1, SCM s2)
0f2d19dd 107{
1be6b49c 108 size_t i, length1, length2, lengthm;
e9bfab50
DH
109 unsigned char *c1, *c2;
110
e9bfab50
DH
111 length1 = SCM_STRING_LENGTH (s1);
112 length2 = SCM_STRING_LENGTH (s2);
113 lengthm = min (length1, length2);
34f0f2b8
DH
114 c1 = SCM_STRING_UCHARS (s1);
115 c2 = SCM_STRING_UCHARS (s2);
0f2d19dd 116
e9bfab50
DH
117 for (i = 0; i != lengthm; ++i, ++c1, ++c2) {
118 int c = *c1 - *c2;
119 if (c < 0) return SCM_BOOL_T;
120 if (c > 0) return SCM_BOOL_F;
0f2d19dd 121 }
e9bfab50
DH
122
123 return SCM_BOOL (length1 < length2);
0f2d19dd 124}
3ba5a6c2
DH
125
126
127SCM_DEFINE1 (scm_string_less_p, "string<?", scm_tc7_rpsubr,
128 (SCM s1, SCM s2),
1e6808ea
MG
129 "Lexicographic ordering predicate; return @code{#t} if @var{s1}\n"
130 "is lexicographically less than @var{s2}.")
3ba5a6c2
DH
131#define FUNC_NAME s_scm_string_less_p
132{
133 SCM_VALIDATE_STRING (1, s1);
134 SCM_VALIDATE_STRING (2, s2);
135
136 return string_less_p (s1, s2);
137}
6e8d25a6 138#undef FUNC_NAME
0f2d19dd 139
e9bfab50 140
c3ee7520 141SCM_DEFINE1 (scm_string_leq_p, "string<=?", scm_tc7_rpsubr,
6e8d25a6 142 (SCM s1, SCM s2),
1e6808ea
MG
143 "Lexicographic ordering predicate; return @code{#t} if @var{s1}\n"
144 "is lexicographically less than or equal to @var{s2}.")
6e8d25a6 145#define FUNC_NAME s_scm_string_leq_p
0f2d19dd 146{
3ba5a6c2
DH
147 SCM_VALIDATE_STRING (1, s1);
148 SCM_VALIDATE_STRING (2, s2);
149
150 return SCM_BOOL_NOT (string_less_p (s2, s1));
0f2d19dd 151}
6e8d25a6 152#undef FUNC_NAME
0f2d19dd 153
e9bfab50 154
c3ee7520 155SCM_DEFINE1 (scm_string_gr_p, "string>?", scm_tc7_rpsubr,
6e8d25a6 156 (SCM s1, SCM s2),
1e6808ea
MG
157 "Lexicographic ordering predicate; return @code{#t} if @var{s1}\n"
158 "is lexicographically greater than @var{s2}.")
6e8d25a6 159#define FUNC_NAME s_scm_string_gr_p
0f2d19dd 160{
3ba5a6c2
DH
161 SCM_VALIDATE_STRING (1, s1);
162 SCM_VALIDATE_STRING (2, s2);
163
164 return string_less_p (s2, s1);
0f2d19dd 165}
6e8d25a6 166#undef FUNC_NAME
0f2d19dd 167
e9bfab50 168
c3ee7520 169SCM_DEFINE1 (scm_string_geq_p, "string>=?", scm_tc7_rpsubr,
6e8d25a6 170 (SCM s1, SCM s2),
1e6808ea
MG
171 "Lexicographic ordering predicate; return @code{#t} if @var{s1}\n"
172 "is lexicographically greater than or equal to @var{s2}.")
6e8d25a6 173#define FUNC_NAME s_scm_string_geq_p
0f2d19dd 174{
3ba5a6c2
DH
175 SCM_VALIDATE_STRING (1, s1);
176 SCM_VALIDATE_STRING (2, s2);
177
178 return SCM_BOOL_NOT (string_less_p (s1, s2));
0f2d19dd 179}
6e8d25a6 180#undef FUNC_NAME
0f2d19dd 181
e9bfab50 182
3ba5a6c2
DH
183/* Helper function for the case insensitive lexicographic ordering
184 * predicates. No argument checking is performed. */
185static SCM
186string_ci_less_p (SCM s1, SCM s2)
0f2d19dd 187{
1be6b49c 188 size_t i, length1, length2, lengthm;
e9bfab50
DH
189 unsigned char *c1, *c2;
190
e9bfab50
DH
191 length1 = SCM_STRING_LENGTH (s1);
192 length2 = SCM_STRING_LENGTH (s2);
193 lengthm = min (length1, length2);
34f0f2b8
DH
194 c1 = SCM_STRING_UCHARS (s1);
195 c2 = SCM_STRING_UCHARS (s2);
e9bfab50
DH
196
197 for (i = 0; i != lengthm; ++i, ++c1, ++c2) {
198 int c = scm_upcase (*c1) - scm_upcase (*c2);
199 if (c < 0) return SCM_BOOL_T;
200 if (c > 0) return SCM_BOOL_F;
0f2d19dd 201 }
e9bfab50
DH
202
203 return SCM_BOOL (length1 < length2);
0f2d19dd 204}
3ba5a6c2
DH
205
206
207SCM_DEFINE1 (scm_string_ci_less_p, "string-ci<?", scm_tc7_rpsubr,
208 (SCM s1, SCM s2),
1e6808ea
MG
209 "Case insensitive lexicographic ordering predicate; return\n"
210 "@code{#t} if @var{s1} is lexicographically less than @var{s2}\n"
211 "regardless of case.")
3ba5a6c2
DH
212#define FUNC_NAME s_scm_string_ci_less_p
213{
214 SCM_VALIDATE_STRING (1, s1);
215 SCM_VALIDATE_STRING (2, s2);
216
217 return string_ci_less_p (s1, s2);
218}
6e8d25a6 219#undef FUNC_NAME
0f2d19dd 220
e9bfab50 221
c3ee7520 222SCM_DEFINE1 (scm_string_ci_leq_p, "string-ci<=?", scm_tc7_rpsubr,
6e8d25a6 223 (SCM s1, SCM s2),
1e6808ea
MG
224 "Case insensitive lexicographic ordering predicate; return\n"
225 "@code{#t} if @var{s1} is lexicographically less than or equal\n"
226 "to @var{s2} regardless of case.")
6e8d25a6 227#define FUNC_NAME s_scm_string_ci_leq_p
0f2d19dd 228{
3ba5a6c2
DH
229 SCM_VALIDATE_STRING (1, s1);
230 SCM_VALIDATE_STRING (2, s2);
231
232 return SCM_BOOL_NOT (string_ci_less_p (s2, s1));
0f2d19dd 233}
6e8d25a6 234#undef FUNC_NAME
0f2d19dd 235
e9bfab50 236
c3ee7520 237SCM_DEFINE1 (scm_string_ci_gr_p, "string-ci>?", scm_tc7_rpsubr,
6e8d25a6 238 (SCM s1, SCM s2),
1e6808ea
MG
239 "Case insensitive lexicographic ordering predicate; return\n"
240 "@code{#t} if @var{s1} is lexicographically greater than\n"
241 "@var{s2} regardless of case.")
6e8d25a6 242#define FUNC_NAME s_scm_string_ci_gr_p
0f2d19dd 243{
3ba5a6c2
DH
244 SCM_VALIDATE_STRING (1, s1);
245 SCM_VALIDATE_STRING (2, s2);
246
247 return string_ci_less_p (s2, s1);
0f2d19dd 248}
6e8d25a6 249#undef FUNC_NAME
0f2d19dd 250
e9bfab50 251
c3ee7520 252SCM_DEFINE1 (scm_string_ci_geq_p, "string-ci>=?", scm_tc7_rpsubr,
6e8d25a6 253 (SCM s1, SCM s2),
1e6808ea
MG
254 "Case insensitive lexicographic ordering predicate; return\n"
255 "@code{#t} if @var{s1} is lexicographically greater than or\n"
256 "equal to @var{s2} regardless of case.")
6e8d25a6 257#define FUNC_NAME s_scm_string_ci_geq_p
0f2d19dd 258{
3ba5a6c2
DH
259 SCM_VALIDATE_STRING (1, s1);
260 SCM_VALIDATE_STRING (2, s2);
261
262 return SCM_BOOL_NOT (string_ci_less_p (s1, s2));
0f2d19dd 263}
6e8d25a6 264#undef FUNC_NAME
0f2d19dd
JB
265
266\f
1cc91f1b 267
0f2d19dd
JB
268void
269scm_init_strorder ()
0f2d19dd 270{
a0599745 271#include "libguile/strorder.x"
0f2d19dd
JB
272}
273
89e00824
ML
274
275/*
276 Local Variables:
277 c-file-style: "gnu"
278 End:
279*/