1 /* Copyright (C) 1995, 1996, 1999, 2000 Free Software Foundation, Inc.
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.
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.
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
20 #include "libguile/_scm.h"
21 #include "libguile/chars.h"
22 #include "libguile/strings.h"
23 #include "libguile/symbols.h"
25 #include "libguile/validate.h"
26 #include "libguile/strorder.h"
29 SCM_DEFINE1 (scm_string_equal_p
, "string=?", scm_tc7_rpsubr
,
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"
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"
39 #define FUNC_NAME s_scm_string_equal_p
43 SCM_VALIDATE_STRING (1, s1
);
44 SCM_VALIDATE_STRING (2, s2
);
46 length
= SCM_STRING_LENGTH (s2
);
47 if (SCM_STRING_LENGTH (s1
) == length
)
49 unsigned char *c1
= SCM_STRING_UCHARS (s1
) + length
- 1;
50 unsigned char *c2
= SCM_STRING_UCHARS (s2
) + length
- 1;
53 /* comparing from back to front typically finds mismatches faster */
54 for (i
= 0; i
!= length
; ++i
, --c1
, --c2
)
68 SCM_DEFINE1 (scm_string_ci_equal_p
, "string-ci=?", scm_tc7_rpsubr
,
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"
74 #define FUNC_NAME s_scm_string_ci_equal_p
78 SCM_VALIDATE_STRING (1, s1
);
79 SCM_VALIDATE_STRING (2, s2
);
81 length
= SCM_STRING_LENGTH (s2
);
82 if (SCM_STRING_LENGTH (s1
) == length
)
84 unsigned char *c1
= SCM_STRING_UCHARS (s1
) + length
- 1;
85 unsigned char *c2
= SCM_STRING_UCHARS (s2
) + length
- 1;
88 /* comparing from back to front typically finds mismatches faster */
89 for (i
= 0; i
!= length
; ++i
, --c1
, --c2
)
90 if (scm_c_upcase (*c1
) != scm_c_upcase (*c2
))
103 /* Helper function for the lexicographic ordering predicates.
104 * No argument checking is performed. */
106 string_less_p (SCM s1
, SCM s2
)
108 size_t i
, length1
, length2
, lengthm
;
109 unsigned char *c1
, *c2
;
111 length1
= SCM_STRING_LENGTH (s1
);
112 length2
= SCM_STRING_LENGTH (s2
);
113 lengthm
= min (length1
, length2
);
114 c1
= SCM_STRING_UCHARS (s1
);
115 c2
= SCM_STRING_UCHARS (s2
);
117 for (i
= 0; i
!= lengthm
; ++i
, ++c1
, ++c2
) {
119 if (c
< 0) return SCM_BOOL_T
;
120 if (c
> 0) return SCM_BOOL_F
;
123 return SCM_BOOL (length1
< length2
);
127 SCM_DEFINE1 (scm_string_less_p
, "string<?", scm_tc7_rpsubr
,
129 "Lexicographic ordering predicate; return @code{#t} if @var{s1}\n"
130 "is lexicographically less than @var{s2}.")
131 #define FUNC_NAME s_scm_string_less_p
133 SCM_VALIDATE_STRING (1, s1
);
134 SCM_VALIDATE_STRING (2, s2
);
136 return string_less_p (s1
, s2
);
141 SCM_DEFINE1 (scm_string_leq_p
, "string<=?", scm_tc7_rpsubr
,
143 "Lexicographic ordering predicate; return @code{#t} if @var{s1}\n"
144 "is lexicographically less than or equal to @var{s2}.")
145 #define FUNC_NAME s_scm_string_leq_p
147 SCM_VALIDATE_STRING (1, s1
);
148 SCM_VALIDATE_STRING (2, s2
);
150 return SCM_BOOL_NOT (string_less_p (s2
, s1
));
155 SCM_DEFINE1 (scm_string_gr_p
, "string>?", scm_tc7_rpsubr
,
157 "Lexicographic ordering predicate; return @code{#t} if @var{s1}\n"
158 "is lexicographically greater than @var{s2}.")
159 #define FUNC_NAME s_scm_string_gr_p
161 SCM_VALIDATE_STRING (1, s1
);
162 SCM_VALIDATE_STRING (2, s2
);
164 return string_less_p (s2
, s1
);
169 SCM_DEFINE1 (scm_string_geq_p
, "string>=?", scm_tc7_rpsubr
,
171 "Lexicographic ordering predicate; return @code{#t} if @var{s1}\n"
172 "is lexicographically greater than or equal to @var{s2}.")
173 #define FUNC_NAME s_scm_string_geq_p
175 SCM_VALIDATE_STRING (1, s1
);
176 SCM_VALIDATE_STRING (2, s2
);
178 return SCM_BOOL_NOT (string_less_p (s1
, s2
));
183 /* Helper function for the case insensitive lexicographic ordering
184 * predicates. No argument checking is performed. */
186 string_ci_less_p (SCM s1
, SCM s2
)
188 size_t i
, length1
, length2
, lengthm
;
189 unsigned char *c1
, *c2
;
191 length1
= SCM_STRING_LENGTH (s1
);
192 length2
= SCM_STRING_LENGTH (s2
);
193 lengthm
= min (length1
, length2
);
194 c1
= SCM_STRING_UCHARS (s1
);
195 c2
= SCM_STRING_UCHARS (s2
);
197 for (i
= 0; i
!= lengthm
; ++i
, ++c1
, ++c2
) {
198 int c
= scm_c_upcase (*c1
) - scm_c_upcase (*c2
);
199 if (c
< 0) return SCM_BOOL_T
;
200 if (c
> 0) return SCM_BOOL_F
;
203 return SCM_BOOL (length1
< length2
);
207 SCM_DEFINE1 (scm_string_ci_less_p
, "string-ci<?", scm_tc7_rpsubr
,
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.")
212 #define FUNC_NAME s_scm_string_ci_less_p
214 SCM_VALIDATE_STRING (1, s1
);
215 SCM_VALIDATE_STRING (2, s2
);
217 return string_ci_less_p (s1
, s2
);
222 SCM_DEFINE1 (scm_string_ci_leq_p
, "string-ci<=?", scm_tc7_rpsubr
,
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.")
227 #define FUNC_NAME s_scm_string_ci_leq_p
229 SCM_VALIDATE_STRING (1, s1
);
230 SCM_VALIDATE_STRING (2, s2
);
232 return SCM_BOOL_NOT (string_ci_less_p (s2
, s1
));
237 SCM_DEFINE1 (scm_string_ci_gr_p
, "string-ci>?", scm_tc7_rpsubr
,
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.")
242 #define FUNC_NAME s_scm_string_ci_gr_p
244 SCM_VALIDATE_STRING (1, s1
);
245 SCM_VALIDATE_STRING (2, s2
);
247 return string_ci_less_p (s2
, s1
);
252 SCM_DEFINE1 (scm_string_ci_geq_p
, "string-ci>=?", scm_tc7_rpsubr
,
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.")
257 #define FUNC_NAME s_scm_string_ci_geq_p
259 SCM_VALIDATE_STRING (1, s1
);
260 SCM_VALIDATE_STRING (2, s2
);
262 return SCM_BOOL_NOT (string_ci_less_p (s1
, s2
));
271 #include "libguile/strorder.x"