aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRichard M. Stallman1998-04-20 03:52:46 +0000
committerRichard M. Stallman1998-04-20 03:52:46 +0000
commit0e1e9f8da50e654891b498d5f3b601857d2093cc (patch)
treefe7916b80a52ae315f5528a6f60a87fffca8f9e0 /src
parentf9e9ac1ddebce30fd644f9c854edfbc40a93d4d5 (diff)
downloademacs-0e1e9f8da50e654891b498d5f3b601857d2093cc.tar.gz
emacs-0e1e9f8da50e654891b498d5f3b601857d2093cc.zip
(Fcompare_strings): New function.
(syms_of_fns): defsubr it.
Diffstat (limited to 'src')
-rw-r--r--src/fns.c102
1 files changed, 102 insertions, 0 deletions
diff --git a/src/fns.c b/src/fns.c
index 5d5f3fc12d6..97548e70f30 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -218,6 +218,107 @@ Symbols are also allowed; their print names are used instead.")
218 return Qt; 218 return Qt;
219} 219}
220 220
221DEFUN ("compare-strings", Fcompare_strings,
222 Scompare_strings, 2, 7, 0,
223 "Compare the contents of two strings, converting to multibyte if needed.\n\
224In string STR1, skip the first START1 characters and stop at END1.\n\
225In string STR2, skip the first START2 characters and stop at END2.\n\
226Case is significant in this comparison if IGNORE-CASE is nil.\n\
227Unibyte strings are converted to multibyte for comparison.\n\
228\n\
229The value is t if the strings (or specified portions) match.\n\
230If string STR1 is less, the value is a negative number N;\n\
231 - 1 - N is the number of characters that match at the beginning.\n\
232If string STR1 is greater, the value is a positive number N;\n\
233 N - 1 is the number of characters that match at the beginning.")
234 (str1, start1, end1, str2, start2, end2, ignore_case)
235 Lisp_Object str1, start1, end1, start2, str2, end2, ignore_case;
236{
237 register int end1_char, end2_char;
238 register int i1, i1_byte, i2, i2_byte;
239
240 CHECK_STRING (str1, 0);
241 CHECK_STRING (str2, 1);
242 if (NILP (start1))
243 start1 = make_number (0);
244 if (NILP (start2))
245 start2 = make_number (0);
246 CHECK_NATNUM (start1, 2);
247 CHECK_NATNUM (start2, 3);
248 if (! NILP (end1))
249 CHECK_NATNUM (end1, 4);
250 if (! NILP (end2))
251 CHECK_NATNUM (end2, 4);
252
253 i1 = XINT (start1);
254 i2 = XINT (start2);
255
256 i1_byte = string_char_to_byte (str1, i1);
257 i2_byte = string_char_to_byte (str2, i2);
258
259 end1_char = XSTRING (str1)->size;
260 if (! NILP (end1) && end1_char > XINT (end1))
261 end1_char = XINT (end1);
262
263 end2_char = XSTRING (str2)->size;
264 if (! NILP (end2) && end2_char > XINT (end2))
265 end2_char = XINT (end2);
266
267 while (i1 < end1_char && i2 < end2_char)
268 {
269 /* When we find a mismatch, we must compare the
270 characters, not just the bytes. */
271 int c1, c2;
272
273 if (STRING_MULTIBYTE (str1))
274 FETCH_STRING_CHAR_ADVANCE (c1, str1, i1, i1_byte);
275 else
276 {
277 c1 = XSTRING (str1)->data[i1++];
278 c1 = unibyte_char_to_multibyte (c1);
279 }
280
281 if (STRING_MULTIBYTE (str2))
282 FETCH_STRING_CHAR_ADVANCE (c2, str2, i2, i2_byte);
283 else
284 {
285 c2 = XSTRING (str2)->data[i2++];
286 c2 = unibyte_char_to_multibyte (c2);
287 }
288
289 if (c1 == c2)
290 continue;
291
292 if (! NILP (ignore_case))
293 {
294 Lisp_Object tem;
295
296 tem = Fupcase (make_number (c1));
297 c1 = XINT (tem);
298 tem = Fupcase (make_number (c2));
299 c2 = XINT (tem);
300 }
301
302 if (c1 == c2)
303 continue;
304
305 /* Note that I1 has already been incremented
306 past the character that we are comparing;
307 hence we don't add or subtract 1 here. */
308 if (c1 < c2)
309 return make_number (- i1);
310 else
311 return make_number (i1);
312 }
313
314 if (i1 < end1_char)
315 return make_number (i1 - XINT (start1) + 1);
316 if (i2 < end2_char)
317 return make_number (- i1 + XINT (start1) - 1);
318
319 return Qt;
320}
321
221DEFUN ("string-lessp", Fstring_lessp, Sstring_lessp, 2, 2, 0, 322DEFUN ("string-lessp", Fstring_lessp, Sstring_lessp, 2, 2, 0,
222 "Return t if first arg string is less than second in lexicographic order.\n\ 323 "Return t if first arg string is less than second in lexicographic order.\n\
223Case is significant.\n\ 324Case is significant.\n\
@@ -2600,6 +2701,7 @@ invoked by mouse clicks and mouse menu items.");
2600 defsubr (&Ssafe_length); 2701 defsubr (&Ssafe_length);
2601 defsubr (&Sstring_bytes); 2702 defsubr (&Sstring_bytes);
2602 defsubr (&Sstring_equal); 2703 defsubr (&Sstring_equal);
2704 defsubr (&Scompare_strings);
2603 defsubr (&Sstring_lessp); 2705 defsubr (&Sstring_lessp);
2604 defsubr (&Sappend); 2706 defsubr (&Sappend);
2605 defsubr (&Sconcat); 2707 defsubr (&Sconcat);