Commit | Line | Data |
---|---|---|
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 | 29 | SCM_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 | ||
cc95e00a MV |
46 | length = scm_i_string_length (s2); |
47 | if (scm_i_string_length (s1) == length) | |
0f2d19dd | 48 | { |
cc95e00a MV |
49 | const unsigned char *c1 = scm_i_string_chars (s1) + length - 1; |
50 | const unsigned char *c2 = scm_i_string_chars (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) | |
8824ac88 MV |
56 | { |
57 | scm_remember_upto_here_2 (s1, s2); | |
58 | return SCM_BOOL_F; | |
59 | } | |
e9bfab50 | 60 | |
8824ac88 | 61 | scm_remember_upto_here_2 (s1, s2); |
e9bfab50 | 62 | return SCM_BOOL_T; |
0f2d19dd | 63 | } |
e9bfab50 DH |
64 | else |
65 | { | |
0f2d19dd | 66 | return SCM_BOOL_F; |
e9bfab50 | 67 | } |
0f2d19dd | 68 | } |
6e8d25a6 | 69 | #undef FUNC_NAME |
0f2d19dd | 70 | |
e9bfab50 | 71 | |
c3ee7520 | 72 | SCM_DEFINE1 (scm_string_ci_equal_p, "string-ci=?", scm_tc7_rpsubr, |
6e8d25a6 | 73 | (SCM s1, SCM s2), |
1e6808ea MG |
74 | "Case-insensitive string equality predicate; return @code{#t} if\n" |
75 | "the two strings are the same length and their component\n" | |
76 | "characters match (ignoring case) at each position; otherwise\n" | |
77 | "return @code{#f}.") | |
6e8d25a6 | 78 | #define FUNC_NAME s_scm_string_ci_equal_p |
0f2d19dd | 79 | { |
1be6b49c | 80 | size_t length; |
6e8d25a6 | 81 | |
e9bfab50 DH |
82 | SCM_VALIDATE_STRING (1, s1); |
83 | SCM_VALIDATE_STRING (2, s2); | |
84 | ||
cc95e00a MV |
85 | length = scm_i_string_length (s2); |
86 | if (scm_i_string_length (s1) == length) | |
0f2d19dd | 87 | { |
cc95e00a MV |
88 | const unsigned char *c1 = scm_i_string_chars (s1) + length - 1; |
89 | const unsigned char *c2 = scm_i_string_chars (s2) + length - 1; | |
1be6b49c | 90 | size_t i; |
e9bfab50 DH |
91 | |
92 | /* comparing from back to front typically finds mismatches faster */ | |
93 | for (i = 0; i != length; ++i, --c1, --c2) | |
84fad130 | 94 | if (scm_c_upcase (*c1) != scm_c_upcase (*c2)) |
8824ac88 MV |
95 | { |
96 | scm_remember_upto_here_2 (s1, s2); | |
97 | return SCM_BOOL_F; | |
98 | } | |
e9bfab50 | 99 | |
8824ac88 | 100 | scm_remember_upto_here_2 (s1, s2); |
e9bfab50 | 101 | return SCM_BOOL_T; |
0f2d19dd | 102 | } |
e9bfab50 DH |
103 | else |
104 | { | |
0f2d19dd | 105 | return SCM_BOOL_F; |
e9bfab50 | 106 | } |
0f2d19dd | 107 | } |
6e8d25a6 | 108 | #undef FUNC_NAME |
0f2d19dd | 109 | |
e9bfab50 | 110 | |
3ba5a6c2 DH |
111 | /* Helper function for the lexicographic ordering predicates. |
112 | * No argument checking is performed. */ | |
113 | static SCM | |
114 | string_less_p (SCM s1, SCM s2) | |
0f2d19dd | 115 | { |
1be6b49c | 116 | size_t i, length1, length2, lengthm; |
cc95e00a | 117 | const unsigned char *c1, *c2; |
e9bfab50 | 118 | |
cc95e00a MV |
119 | length1 = scm_i_string_length (s1); |
120 | length2 = scm_i_string_length (s2); | |
e9bfab50 | 121 | lengthm = min (length1, length2); |
cc95e00a MV |
122 | c1 = scm_i_string_chars (s1); |
123 | c2 = scm_i_string_chars (s2); | |
0f2d19dd | 124 | |
e9bfab50 DH |
125 | for (i = 0; i != lengthm; ++i, ++c1, ++c2) { |
126 | int c = *c1 - *c2; | |
8824ac88 MV |
127 | if (c == 0) |
128 | continue; | |
129 | scm_remember_upto_here_2 (s1, s2); | |
130 | return scm_from_bool (c < 0); | |
0f2d19dd | 131 | } |
e9bfab50 | 132 | |
7888309b | 133 | return scm_from_bool (length1 < length2); |
0f2d19dd | 134 | } |
3ba5a6c2 DH |
135 | |
136 | ||
137 | SCM_DEFINE1 (scm_string_less_p, "string<?", scm_tc7_rpsubr, | |
138 | (SCM s1, SCM s2), | |
1e6808ea MG |
139 | "Lexicographic ordering predicate; return @code{#t} if @var{s1}\n" |
140 | "is lexicographically less than @var{s2}.") | |
3ba5a6c2 DH |
141 | #define FUNC_NAME s_scm_string_less_p |
142 | { | |
143 | SCM_VALIDATE_STRING (1, s1); | |
144 | SCM_VALIDATE_STRING (2, s2); | |
145 | ||
146 | return string_less_p (s1, s2); | |
147 | } | |
6e8d25a6 | 148 | #undef FUNC_NAME |
0f2d19dd | 149 | |
e9bfab50 | 150 | |
c3ee7520 | 151 | SCM_DEFINE1 (scm_string_leq_p, "string<=?", scm_tc7_rpsubr, |
6e8d25a6 | 152 | (SCM s1, SCM s2), |
1e6808ea MG |
153 | "Lexicographic ordering predicate; return @code{#t} if @var{s1}\n" |
154 | "is lexicographically less than or equal to @var{s2}.") | |
6e8d25a6 | 155 | #define FUNC_NAME s_scm_string_leq_p |
0f2d19dd | 156 | { |
3ba5a6c2 DH |
157 | SCM_VALIDATE_STRING (1, s1); |
158 | SCM_VALIDATE_STRING (2, s2); | |
159 | ||
7888309b | 160 | return scm_not (string_less_p (s2, s1)); |
0f2d19dd | 161 | } |
6e8d25a6 | 162 | #undef FUNC_NAME |
0f2d19dd | 163 | |
e9bfab50 | 164 | |
c3ee7520 | 165 | SCM_DEFINE1 (scm_string_gr_p, "string>?", scm_tc7_rpsubr, |
6e8d25a6 | 166 | (SCM s1, SCM s2), |
1e6808ea MG |
167 | "Lexicographic ordering predicate; return @code{#t} if @var{s1}\n" |
168 | "is lexicographically greater than @var{s2}.") | |
6e8d25a6 | 169 | #define FUNC_NAME s_scm_string_gr_p |
0f2d19dd | 170 | { |
3ba5a6c2 DH |
171 | SCM_VALIDATE_STRING (1, s1); |
172 | SCM_VALIDATE_STRING (2, s2); | |
173 | ||
174 | return string_less_p (s2, s1); | |
0f2d19dd | 175 | } |
6e8d25a6 | 176 | #undef FUNC_NAME |
0f2d19dd | 177 | |
e9bfab50 | 178 | |
c3ee7520 | 179 | SCM_DEFINE1 (scm_string_geq_p, "string>=?", scm_tc7_rpsubr, |
6e8d25a6 | 180 | (SCM s1, SCM s2), |
1e6808ea MG |
181 | "Lexicographic ordering predicate; return @code{#t} if @var{s1}\n" |
182 | "is lexicographically greater than or equal to @var{s2}.") | |
6e8d25a6 | 183 | #define FUNC_NAME s_scm_string_geq_p |
0f2d19dd | 184 | { |
3ba5a6c2 DH |
185 | SCM_VALIDATE_STRING (1, s1); |
186 | SCM_VALIDATE_STRING (2, s2); | |
187 | ||
7888309b | 188 | return scm_not (string_less_p (s1, s2)); |
0f2d19dd | 189 | } |
6e8d25a6 | 190 | #undef FUNC_NAME |
0f2d19dd | 191 | |
e9bfab50 | 192 | |
3ba5a6c2 DH |
193 | /* Helper function for the case insensitive lexicographic ordering |
194 | * predicates. No argument checking is performed. */ | |
195 | static SCM | |
196 | string_ci_less_p (SCM s1, SCM s2) | |
0f2d19dd | 197 | { |
1be6b49c | 198 | size_t i, length1, length2, lengthm; |
cc95e00a | 199 | const unsigned char *c1, *c2; |
e9bfab50 | 200 | |
cc95e00a MV |
201 | length1 = scm_i_string_length (s1); |
202 | length2 = scm_i_string_length (s2); | |
e9bfab50 | 203 | lengthm = min (length1, length2); |
cc95e00a MV |
204 | c1 = scm_i_string_chars (s1); |
205 | c2 = scm_i_string_chars (s2); | |
e9bfab50 DH |
206 | |
207 | for (i = 0; i != lengthm; ++i, ++c1, ++c2) { | |
84fad130 | 208 | int c = scm_c_upcase (*c1) - scm_c_upcase (*c2); |
8824ac88 MV |
209 | if (c == 0) |
210 | continue; | |
211 | scm_remember_upto_here_2 (s1, s2); | |
212 | return scm_from_bool (c < 0); | |
0f2d19dd | 213 | } |
e9bfab50 | 214 | |
7888309b | 215 | return scm_from_bool (length1 < length2); |
0f2d19dd | 216 | } |
3ba5a6c2 DH |
217 | |
218 | ||
219 | SCM_DEFINE1 (scm_string_ci_less_p, "string-ci<?", scm_tc7_rpsubr, | |
220 | (SCM s1, SCM s2), | |
1e6808ea MG |
221 | "Case insensitive lexicographic ordering predicate; return\n" |
222 | "@code{#t} if @var{s1} is lexicographically less than @var{s2}\n" | |
223 | "regardless of case.") | |
3ba5a6c2 DH |
224 | #define FUNC_NAME s_scm_string_ci_less_p |
225 | { | |
226 | SCM_VALIDATE_STRING (1, s1); | |
227 | SCM_VALIDATE_STRING (2, s2); | |
228 | ||
229 | return string_ci_less_p (s1, s2); | |
230 | } | |
6e8d25a6 | 231 | #undef FUNC_NAME |
0f2d19dd | 232 | |
e9bfab50 | 233 | |
c3ee7520 | 234 | SCM_DEFINE1 (scm_string_ci_leq_p, "string-ci<=?", scm_tc7_rpsubr, |
6e8d25a6 | 235 | (SCM s1, SCM s2), |
1e6808ea MG |
236 | "Case insensitive lexicographic ordering predicate; return\n" |
237 | "@code{#t} if @var{s1} is lexicographically less than or equal\n" | |
238 | "to @var{s2} regardless of case.") | |
6e8d25a6 | 239 | #define FUNC_NAME s_scm_string_ci_leq_p |
0f2d19dd | 240 | { |
3ba5a6c2 DH |
241 | SCM_VALIDATE_STRING (1, s1); |
242 | SCM_VALIDATE_STRING (2, s2); | |
243 | ||
7888309b | 244 | return scm_not (string_ci_less_p (s2, s1)); |
0f2d19dd | 245 | } |
6e8d25a6 | 246 | #undef FUNC_NAME |
0f2d19dd | 247 | |
e9bfab50 | 248 | |
c3ee7520 | 249 | SCM_DEFINE1 (scm_string_ci_gr_p, "string-ci>?", scm_tc7_rpsubr, |
6e8d25a6 | 250 | (SCM s1, SCM s2), |
1e6808ea MG |
251 | "Case insensitive lexicographic ordering predicate; return\n" |
252 | "@code{#t} if @var{s1} is lexicographically greater than\n" | |
253 | "@var{s2} regardless of case.") | |
6e8d25a6 | 254 | #define FUNC_NAME s_scm_string_ci_gr_p |
0f2d19dd | 255 | { |
3ba5a6c2 DH |
256 | SCM_VALIDATE_STRING (1, s1); |
257 | SCM_VALIDATE_STRING (2, s2); | |
258 | ||
259 | return string_ci_less_p (s2, s1); | |
0f2d19dd | 260 | } |
6e8d25a6 | 261 | #undef FUNC_NAME |
0f2d19dd | 262 | |
e9bfab50 | 263 | |
c3ee7520 | 264 | SCM_DEFINE1 (scm_string_ci_geq_p, "string-ci>=?", scm_tc7_rpsubr, |
6e8d25a6 | 265 | (SCM s1, SCM s2), |
1e6808ea MG |
266 | "Case insensitive lexicographic ordering predicate; return\n" |
267 | "@code{#t} if @var{s1} is lexicographically greater than or\n" | |
268 | "equal to @var{s2} regardless of case.") | |
6e8d25a6 | 269 | #define FUNC_NAME s_scm_string_ci_geq_p |
0f2d19dd | 270 | { |
3ba5a6c2 DH |
271 | SCM_VALIDATE_STRING (1, s1); |
272 | SCM_VALIDATE_STRING (2, s2); | |
273 | ||
7888309b | 274 | return scm_not (string_ci_less_p (s1, s2)); |
0f2d19dd | 275 | } |
6e8d25a6 | 276 | #undef FUNC_NAME |
0f2d19dd JB |
277 | |
278 | \f | |
1cc91f1b | 279 | |
0f2d19dd JB |
280 | void |
281 | scm_init_strorder () | |
0f2d19dd | 282 | { |
a0599745 | 283 | #include "libguile/strorder.x" |
0f2d19dd JB |
284 | } |
285 | ||
89e00824 ML |
286 | |
287 | /* | |
288 | Local Variables: | |
289 | c-file-style: "gnu" | |
290 | End: | |
291 | */ |