mirrored from git://git.sv.gnu.org/emacs.git
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathfns.c
6931 lines (6009 loc) · 198 KB
/
fns.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Random utility Lisp functions.
Copyright (C) 1985-2025 Free Software Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h>
#include <stdlib.h>
#include <sys/random.h>
#include <unistd.h>
#include <filevercmp.h>
#include <intprops.h>
#include <vla.h>
#include <errno.h>
#include <math.h>
#include "lisp.h"
#include "bignum.h"
#include "character.h"
#include "coding.h"
#include "composite.h"
#include "buffer.h"
#include "intervals.h"
#include "window.h"
#include "gnutls.h"
#ifdef HAVE_TREE_SITTER
#include "treesit.h"
#endif
enum equal_kind { EQUAL_NO_QUIT, EQUAL_PLAIN, EQUAL_INCLUDING_PROPERTIES };
static bool internal_equal (Lisp_Object, Lisp_Object,
enum equal_kind, int, Lisp_Object);
static EMACS_UINT sxhash_obj (Lisp_Object, int);
DEFUN ("identity", Fidentity, Sidentity, 1, 1, 0,
doc: /* Return the ARGUMENT unchanged. */
attributes: const)
(Lisp_Object argument)
{
return argument;
}
/* Return a random Lisp fixnum I in the range 0 <= I < LIM,
where LIM is taken from a positive fixnum. */
static Lisp_Object
get_random_fixnum (EMACS_INT lim)
{
/* Return the remainder of a random integer R (in range 0..INTMASK)
divided by LIM, except reject the rare case where R is so close
to INTMASK that the remainder isn't random. */
EMACS_INT difflim = INTMASK - lim + 1, diff, remainder;
do
{
EMACS_INT r = get_random ();
remainder = r % lim;
diff = r - remainder;
}
while (difflim < diff);
return make_fixnum (remainder);
}
DEFUN ("random", Frandom, Srandom, 0, 1, 0,
doc: /* Return a pseudo-random integer.
By default, return a fixnum; all fixnums are equally likely.
With positive integer LIMIT, return random integer in interval [0,LIMIT).
With argument t, set the random number seed from the system's entropy
pool if available, otherwise from less-random volatile data such as the time.
With a string argument, set the seed based on the string's contents.
See Info node `(elisp)Random Numbers' for more details. */)
(Lisp_Object limit)
{
if (EQ (limit, Qt))
init_random ();
else if (STRINGP (limit))
seed_random (SSDATA (limit), SBYTES (limit));
else if (FIXNUMP (limit))
{
EMACS_INT lim = XFIXNUM (limit);
if (lim <= 0)
xsignal1 (Qargs_out_of_range, limit);
return get_random_fixnum (lim);
}
else if (BIGNUMP (limit))
{
struct Lisp_Bignum *lim = XBIGNUM (limit);
if (mpz_sgn (*bignum_val (lim)) <= 0)
xsignal1 (Qargs_out_of_range, limit);
return get_random_bignum (lim);
}
return make_ufixnum (get_random ());
}
/* Random data-structure functions. */
/* Return LIST's length. Signal an error if LIST is not a proper list. */
ptrdiff_t
list_length (Lisp_Object list)
{
ptrdiff_t i = 0;
FOR_EACH_TAIL (list)
i++;
CHECK_LIST_END (list, list);
return i;
}
DEFUN ("length", Flength, Slength, 1, 1, 0,
doc: /* Return the length of vector, list or string SEQUENCE.
A byte-code function object is also allowed.
If the string contains multibyte characters, this is not necessarily
the number of bytes in the string; it is the number of characters.
To get the number of bytes, use `string-bytes'.
If the length of a list is being computed to compare to a (small)
number, the `length<', `length>' and `length=' functions may be more
efficient. */)
(Lisp_Object sequence)
{
EMACS_INT val;
if (STRINGP (sequence))
val = SCHARS (sequence);
else if (CONSP (sequence))
val = list_length (sequence);
else if (NILP (sequence))
val = 0;
else if (VECTORP (sequence))
val = ASIZE (sequence);
else if (CHAR_TABLE_P (sequence))
val = MAX_CHAR;
else if (BOOL_VECTOR_P (sequence))
val = bool_vector_size (sequence);
else if (CLOSUREP (sequence) || RECORDP (sequence))
val = PVSIZE (sequence);
else
wrong_type_argument (Qsequencep, sequence);
return make_fixnum (val);
}
DEFUN ("safe-length", Fsafe_length, Ssafe_length, 1, 1, 0,
doc: /* Return the length of a list, but avoid error or infinite loop.
This function never gets an error. If LIST is not really a list,
it returns 0. If LIST is circular, it returns an integer that is at
least the number of distinct elements. */)
(Lisp_Object list)
{
ptrdiff_t len = 0;
FOR_EACH_TAIL_SAFE (list)
len++;
return make_fixnum (len);
}
static inline
EMACS_INT length_internal (Lisp_Object sequence, int len)
{
/* If LENGTH is short (arbitrarily chosen cut-off point), use a
fast loop that doesn't care about whether SEQUENCE is
circular or not. */
if (len < 0xffff)
while (CONSP (sequence))
{
if (--len <= 0)
return -1;
sequence = XCDR (sequence);
}
/* Signal an error on circular lists. */
else
FOR_EACH_TAIL (sequence)
if (--len <= 0)
return -1;
return len;
}
DEFUN ("length<", Flength_less, Slength_less, 2, 2, 0,
doc: /* Return non-nil if SEQUENCE is shorter than LENGTH.
See `length' for allowed values of SEQUENCE and how elements are
counted. */)
(Lisp_Object sequence, Lisp_Object length)
{
CHECK_FIXNUM (length);
EMACS_INT len = XFIXNUM (length);
if (CONSP (sequence))
return length_internal (sequence, len) == -1? Qnil: Qt;
else
return XFIXNUM (Flength (sequence)) < len? Qt: Qnil;
}
DEFUN ("length>", Flength_greater, Slength_greater, 2, 2, 0,
doc: /* Return non-nil if SEQUENCE is longer than LENGTH.
See `length' for allowed values of SEQUENCE and how elements are
counted. */)
(Lisp_Object sequence, Lisp_Object length)
{
CHECK_FIXNUM (length);
EMACS_INT len = XFIXNUM (length);
if (CONSP (sequence))
return length_internal (sequence, len + 1) == -1? Qt: Qnil;
else
return XFIXNUM (Flength (sequence)) > len? Qt: Qnil;
}
DEFUN ("length=", Flength_equal, Slength_equal, 2, 2, 0,
doc: /* Return non-nil if SEQUENCE has length equal to LENGTH.
See `length' for allowed values of SEQUENCE and how elements are
counted. */)
(Lisp_Object sequence, Lisp_Object length)
{
CHECK_FIXNUM (length);
EMACS_INT len = XFIXNUM (length);
if (len < 0)
return Qnil;
if (CONSP (sequence))
return length_internal (sequence, len + 1) == 1? Qt: Qnil;
else
return XFIXNUM (Flength (sequence)) == len? Qt: Qnil;
}
DEFUN ("proper-list-p", Fproper_list_p, Sproper_list_p, 1, 1, 0,
doc: /* Return OBJECT's length if it is a proper list, nil otherwise.
A proper list is neither circular nor dotted (i.e., its last cdr is nil). */
attributes: const)
(Lisp_Object object)
{
ptrdiff_t len = 0;
Lisp_Object last_tail = object;
Lisp_Object tail = object;
FOR_EACH_TAIL_SAFE (tail)
{
len++;
rarely_quit (len);
last_tail = XCDR (tail);
}
if (!NILP (last_tail))
return Qnil;
return make_fixnum (len);
}
DEFUN ("string-bytes", Fstring_bytes, Sstring_bytes, 1, 1, 0,
doc: /* Return the number of bytes in STRING.
If STRING is multibyte, this may be greater than the length of STRING. */)
(Lisp_Object string)
{
CHECK_STRING (string);
return make_fixnum (SBYTES (string));
}
DEFUN ("string-distance", Fstring_distance, Sstring_distance, 2, 3, 0,
doc: /* Return Levenshtein distance between STRING1 and STRING2.
The distance is the number of deletions, insertions, and substitutions
required to transform STRING1 into STRING2.
If BYTECOMPARE is nil or omitted, compute distance in terms of characters.
If BYTECOMPARE is non-nil, compute distance in terms of bytes.
Letter-case is significant, but text properties are ignored. */)
(Lisp_Object string1, Lisp_Object string2, Lisp_Object bytecompare)
{
CHECK_STRING (string1);
CHECK_STRING (string2);
bool use_byte_compare =
!NILP (bytecompare)
|| (!STRING_MULTIBYTE (string1) && !STRING_MULTIBYTE (string2));
ptrdiff_t len1 = use_byte_compare ? SBYTES (string1) : SCHARS (string1);
ptrdiff_t len2 = use_byte_compare ? SBYTES (string2) : SCHARS (string2);
ptrdiff_t x, y, lastdiag, olddiag;
USE_SAFE_ALLOCA;
ptrdiff_t *column;
SAFE_NALLOCA (column, 1, len1 + 1);
for (y = 0; y <= len1; y++)
column[y] = y;
if (use_byte_compare)
{
char *s1 = SSDATA (string1);
char *s2 = SSDATA (string2);
for (x = 1; x <= len2; x++)
{
column[0] = x;
for (y = 1, lastdiag = x - 1; y <= len1; y++)
{
olddiag = column[y];
column[y] = min (min (column[y] + 1, column[y-1] + 1),
lastdiag + (s1[y-1] == s2[x-1] ? 0 : 1));
lastdiag = olddiag;
}
}
}
else
{
int c1, c2;
ptrdiff_t i1, i1_byte, i2 = 0, i2_byte = 0;
for (x = 1; x <= len2; x++)
{
column[0] = x;
c2 = fetch_string_char_advance (string2, &i2, &i2_byte);
i1 = i1_byte = 0;
for (y = 1, lastdiag = x - 1; y <= len1; y++)
{
olddiag = column[y];
c1 = fetch_string_char_advance (string1, &i1, &i1_byte);
column[y] = min (min (column[y] + 1, column[y-1] + 1),
lastdiag + (c1 == c2 ? 0 : 1));
lastdiag = olddiag;
}
}
}
SAFE_FREE ();
return make_fixnum (column[len1]);
}
DEFUN ("string-equal", Fstring_equal, Sstring_equal, 2, 2, 0,
doc: /* Return t if two strings have identical contents.
Case is significant, but text properties are ignored.
Symbols are also allowed; their print names are used instead.
See also `string-equal-ignore-case'. */)
(register Lisp_Object s1, Lisp_Object s2)
{
if (SYMBOLP (s1))
s1 = SYMBOL_NAME (s1);
if (SYMBOLP (s2))
s2 = SYMBOL_NAME (s2);
CHECK_STRING (s1);
CHECK_STRING (s2);
if (SCHARS (s1) != SCHARS (s2)
|| SBYTES (s1) != SBYTES (s2)
|| memcmp (SDATA (s1), SDATA (s2), SBYTES (s1)))
return Qnil;
return Qt;
}
DEFUN ("compare-strings", Fcompare_strings, Scompare_strings, 6, 7, 0,
doc: /* Compare the contents of two strings, converting to multibyte if needed.
The arguments START1, END1, START2, and END2, if non-nil, are
positions specifying which parts of STR1 or STR2 to compare. In
string STR1, compare the part between START1 (inclusive) and END1
\(exclusive). If START1 is nil, it defaults to 0, the beginning of
the string; if END1 is nil, it defaults to the length of the string.
Likewise, in string STR2, compare the part between START2 and END2.
Like in `substring', negative values are counted from the end.
The strings are compared by the numeric values of their characters.
For instance, STR1 is "less than" STR2 if its first differing
character has a smaller numeric value. If IGNORE-CASE is non-nil,
characters are converted to upper-case before comparing them. Unibyte
strings are converted to multibyte for comparison.
The value is t if the strings (or specified portions) match.
If string STR1 is less, the value is a negative number N;
- 1 - N is the number of characters that match at the beginning.
If string STR1 is greater, the value is a positive number N;
N - 1 is the number of characters that match at the beginning. */)
(Lisp_Object str1, Lisp_Object start1, Lisp_Object end1, Lisp_Object str2,
Lisp_Object start2, Lisp_Object end2, Lisp_Object ignore_case)
{
ptrdiff_t from1, to1, from2, to2, i1, i1_byte, i2, i2_byte;
CHECK_STRING (str1);
CHECK_STRING (str2);
/* For backward compatibility, silently bring too-large positive end
values into range. */
if (FIXNUMP (end1) && SCHARS (str1) < XFIXNUM (end1))
end1 = make_fixnum (SCHARS (str1));
if (FIXNUMP (end2) && SCHARS (str2) < XFIXNUM (end2))
end2 = make_fixnum (SCHARS (str2));
validate_subarray (str1, start1, end1, SCHARS (str1), &from1, &to1);
validate_subarray (str2, start2, end2, SCHARS (str2), &from2, &to2);
i1 = from1;
i2 = from2;
i1_byte = string_char_to_byte (str1, i1);
i2_byte = string_char_to_byte (str2, i2);
while (i1 < to1 && i2 < to2)
{
/* When we find a mismatch, we must compare the
characters, not just the bytes. */
int c1 = fetch_string_char_as_multibyte_advance (str1, &i1, &i1_byte);
int c2 = fetch_string_char_as_multibyte_advance (str2, &i2, &i2_byte);
if (c1 == c2)
continue;
if (! NILP (ignore_case))
{
c1 = XFIXNUM (Fupcase (make_fixnum (c1)));
c2 = XFIXNUM (Fupcase (make_fixnum (c2)));
}
if (c1 == c2)
continue;
/* Note that I1 has already been incremented
past the character that we are comparing;
hence we don't add or subtract 1 here. */
if (c1 < c2)
return make_fixnum (- i1 + from1);
else
return make_fixnum (i1 - from1);
}
if (i1 < to1)
return make_fixnum (i1 - from1 + 1);
if (i2 < to2)
return make_fixnum (- i1 + from1 - 1);
return Qt;
}
/* Check whether the platform allows access to unaligned addresses for
size_t integers without trapping or undue penalty (a few cycles is OK),
and that a word-sized memcpy can be used to generate such an access.
This whitelist is incomplete but since it is only used to improve
performance, omitting cases is safe. */
#if (defined __x86_64__|| defined __amd64__ \
|| defined __i386__ || defined __i386 \
|| defined __arm64__ || defined __aarch64__ \
|| defined __powerpc__ || defined __powerpc \
|| defined __ppc__ || defined __ppc \
|| defined __s390__ || defined __s390x__) \
&& defined __OPTIMIZE__
#define HAVE_FAST_UNALIGNED_ACCESS 1
#else
#define HAVE_FAST_UNALIGNED_ACCESS 0
#endif
/* Load a word from a possibly unaligned address. */
static inline size_t
load_unaligned_size_t (const void *p)
{
size_t x;
memcpy (&x, p, sizeof x);
return x;
}
/* Return -1/0/1 to indicate the relation </=/> between string1 and string2. */
static int
string_cmp (Lisp_Object string1, Lisp_Object string2)
{
ptrdiff_t n = min (SCHARS (string1), SCHARS (string2));
if ((!STRING_MULTIBYTE (string1) || SCHARS (string1) == SBYTES (string1))
&& (!STRING_MULTIBYTE (string2) || SCHARS (string2) == SBYTES (string2)))
{
/* Each argument is either unibyte or all-ASCII multibyte:
we can compare bytewise. */
int d = memcmp (SSDATA (string1), SSDATA (string2), n);
if (d)
return d;
return n < SCHARS (string2) ? -1 : n < SCHARS (string1);
}
else if (STRING_MULTIBYTE (string1) && STRING_MULTIBYTE (string2))
{
/* Two arbitrary multibyte strings: we cannot use memcmp because
the encoding for raw bytes would sort those between U+007F and U+0080
which isn't where we want them.
Instead, we skip the longest common prefix and look at
what follows. */
ptrdiff_t nb1 = SBYTES (string1);
ptrdiff_t nb2 = SBYTES (string2);
ptrdiff_t nb = min (nb1, nb2);
ptrdiff_t b = 0;
/* String data is normally allocated with word alignment, but
there are exceptions (notably pure strings) so we restrict the
wordwise skipping to safe architectures. */
if (HAVE_FAST_UNALIGNED_ACCESS)
{
/* First compare entire machine words. */
int ws = sizeof (size_t);
const char *w1 = SSDATA (string1);
const char *w2 = SSDATA (string2);
while (b < nb - ws + 1 && load_unaligned_size_t (w1 + b)
== load_unaligned_size_t (w2 + b))
b += ws;
}
/* Scan forward to the differing byte. */
while (b < nb && SREF (string1, b) == SREF (string2, b))
b++;
if (b >= nb)
/* One string is a prefix of the other. */
return b < nb2 ? -1 : b < nb1;
/* Now back up to the start of the differing characters:
it's the last byte not having the bit pattern 10xxxxxx. */
while ((SREF (string1, b) & 0xc0) == 0x80)
b--;
/* Compare the differing characters. */
ptrdiff_t i1 = 0, i2 = 0;
ptrdiff_t i1_byte = b, i2_byte = b;
int c1 = fetch_string_char_advance_no_check (string1, &i1, &i1_byte);
int c2 = fetch_string_char_advance_no_check (string2, &i2, &i2_byte);
return c1 < c2 ? -1 : c1 > c2;
}
else if (STRING_MULTIBYTE (string1))
{
/* string1 multibyte, string2 unibyte */
ptrdiff_t i1 = 0, i1_byte = 0, i2 = 0;
while (i1 < n)
{
int c1 = fetch_string_char_advance_no_check (string1, &i1, &i1_byte);
int c2 = SREF (string2, i2++);
if (c1 != c2)
return c1 < c2 ? -1 : 1;
}
return i1 < SCHARS (string2) ? -1 : i1 < SCHARS (string1);
}
else
{
/* string1 unibyte, string2 multibyte */
ptrdiff_t i1 = 0, i2 = 0, i2_byte = 0;
while (i1 < n)
{
int c1 = SREF (string1, i1++);
int c2 = fetch_string_char_advance_no_check (string2, &i2, &i2_byte);
if (c1 != c2)
return c1 < c2 ? -1 : 1;
}
return i1 < SCHARS (string2) ? -1 : i1 < SCHARS (string1);
}
}
DEFUN ("string-lessp", Fstring_lessp, Sstring_lessp, 2, 2, 0,
doc: /* Return non-nil if STRING1 is less than STRING2 in lexicographic order.
Case is significant.
Symbols are also allowed; their print names are used instead. */)
(Lisp_Object string1, Lisp_Object string2)
{
if (SYMBOLP (string1))
string1 = SYMBOL_NAME (string1);
else
CHECK_STRING (string1);
if (SYMBOLP (string2))
string2 = SYMBOL_NAME (string2);
else
CHECK_STRING (string2);
return string_cmp (string1, string2) < 0 ? Qt : Qnil;
}
DEFUN ("string-version-lessp", Fstring_version_lessp,
Sstring_version_lessp, 2, 2, 0,
doc: /* Return non-nil if S1 is less than S2, as version strings.
This function compares version strings S1 and S2:
1) By prefix lexicographically.
2) Then by version (similarly to version comparison of Debian's dpkg).
Leading zeros in version numbers are ignored.
3) If both prefix and version are equal, compare as ordinary strings.
For example, \"foo2.png\" compares less than \"foo12.png\".
Case is significant.
Symbols are also allowed; their print names are used instead. */)
(Lisp_Object string1, Lisp_Object string2)
{
if (SYMBOLP (string1))
string1 = SYMBOL_NAME (string1);
if (SYMBOLP (string2))
string2 = SYMBOL_NAME (string2);
CHECK_STRING (string1);
CHECK_STRING (string2);
int cmp = filenvercmp (SSDATA (string1), SBYTES (string1),
SSDATA (string2), SBYTES (string2));
return cmp < 0 ? Qt : Qnil;
}
DEFUN ("string-collate-lessp", Fstring_collate_lessp, Sstring_collate_lessp, 2, 4, 0,
doc: /* Return t if first arg string is less than second in collation order.
Symbols are also allowed; their print names are used instead.
This function obeys the conventions for collation order in your
locale settings. For example, punctuation and whitespace characters
might be considered less significant for sorting:
\(sort \\='("11" "12" "1 1" "1 2" "1.1" "1.2") \\='string-collate-lessp)
=> ("11" "1 1" "1.1" "12" "1 2" "1.2")
The optional argument LOCALE, a string, overrides the setting of your
current locale identifier for collation. The value is system
dependent; a LOCALE \"en_US.UTF-8\" is applicable on POSIX systems,
while it would be, e.g., \"enu_USA.1252\" on MS-Windows systems.
If IGNORE-CASE is non-nil, characters are converted to lower-case
before comparing them.
To emulate Unicode-compliant collation on MS-Windows systems,
bind `w32-collate-ignore-punctuation' to a non-nil value, since
the codeset part of the locale cannot be \"UTF-8\" on MS-Windows.
Some operating systems do not implement correct collation (in specific
locale environments or at all). Then, this functions falls back to
case-sensitive `string-lessp' and IGNORE-CASE argument is ignored. */)
(Lisp_Object s1, Lisp_Object s2, Lisp_Object locale, Lisp_Object ignore_case)
{
#if defined __STDC_ISO_10646__ || defined WINDOWSNT
/* Check parameters. */
if (SYMBOLP (s1))
s1 = SYMBOL_NAME (s1);
if (SYMBOLP (s2))
s2 = SYMBOL_NAME (s2);
CHECK_STRING (s1);
CHECK_STRING (s2);
if (!NILP (locale))
CHECK_STRING (locale);
return (str_collate (s1, s2, locale, ignore_case) < 0) ? Qt : Qnil;
#else /* !__STDC_ISO_10646__, !WINDOWSNT */
return Fstring_lessp (s1, s2);
#endif /* !__STDC_ISO_10646__, !WINDOWSNT */
}
DEFUN ("string-collate-equalp", Fstring_collate_equalp, Sstring_collate_equalp, 2, 4, 0,
doc: /* Return t if two strings have identical contents.
Symbols are also allowed; their print names are used instead.
This function obeys the conventions for collation order in your locale
settings. For example, characters with different coding points but
the same meaning might be considered as equal, like different grave
accent Unicode characters:
\(string-collate-equalp (string ?\\uFF40) (string ?\\u1FEF))
=> t
The optional argument LOCALE, a string, overrides the setting of your
current locale identifier for collation. The value is system
dependent; a LOCALE \"en_US.UTF-8\" is applicable on POSIX systems,
while it would be \"enu_USA.1252\" on MS Windows systems.
If IGNORE-CASE is non-nil, characters are converted to lower-case
before comparing them.
To emulate Unicode-compliant collation on MS-Windows systems,
bind `w32-collate-ignore-punctuation' to a non-nil value, since
the codeset part of the locale cannot be \"UTF-8\" on MS-Windows.
If your system does not support a locale environment, this function
behaves like `string-equal', and in that case the IGNORE-CASE argument
is ignored.
Do NOT use this function to compare file names for equality. */)
(Lisp_Object s1, Lisp_Object s2, Lisp_Object locale, Lisp_Object ignore_case)
{
#if defined __STDC_ISO_10646__ || defined WINDOWSNT
/* Check parameters. */
if (SYMBOLP (s1))
s1 = SYMBOL_NAME (s1);
if (SYMBOLP (s2))
s2 = SYMBOL_NAME (s2);
CHECK_STRING (s1);
CHECK_STRING (s2);
if (!NILP (locale))
CHECK_STRING (locale);
return (str_collate (s1, s2, locale, ignore_case) == 0) ? Qt : Qnil;
#else /* !__STDC_ISO_10646__, !WINDOWSNT */
return Fstring_equal (s1, s2);
#endif /* !__STDC_ISO_10646__, !WINDOWSNT */
}
static Lisp_Object concat_to_list (ptrdiff_t nargs, Lisp_Object *args,
Lisp_Object last_tail);
static Lisp_Object concat_to_vector (ptrdiff_t nargs, Lisp_Object *args);
static Lisp_Object concat_to_string (ptrdiff_t nargs, Lisp_Object *args);
Lisp_Object
concat2 (Lisp_Object s1, Lisp_Object s2)
{
return concat_to_string (2, ((Lisp_Object []) {s1, s2}));
}
Lisp_Object
concat3 (Lisp_Object s1, Lisp_Object s2, Lisp_Object s3)
{
return concat_to_string (3, ((Lisp_Object []) {s1, s2, s3}));
}
DEFUN ("append", Fappend, Sappend, 0, MANY, 0,
doc: /* Concatenate all the arguments and make the result a list.
The result is a list whose elements are the elements of all the arguments.
Each argument may be a list, vector or string.
All arguments except the last argument are copied. The last argument
is just used as the tail of the new list. If the last argument is not
a list, this results in a dotted list.
As an exception, if all the arguments except the last are nil, and the
last argument is not a list, the return value is that last argument
unaltered, not a list.
usage: (append &rest SEQUENCES) */)
(ptrdiff_t nargs, Lisp_Object *args)
{
if (nargs == 0)
return Qnil;
return concat_to_list (nargs - 1, args, args[nargs - 1]);
}
DEFUN ("concat", Fconcat, Sconcat, 0, MANY, 0,
doc: /* Concatenate all the arguments and make the result a string.
The result is a string whose elements are the elements of all the arguments.
Each argument may be a string or a list or vector of characters (integers).
Values of the `composition' property of the result are not guaranteed
to be `eq'.
usage: (concat &rest SEQUENCES) */)
(ptrdiff_t nargs, Lisp_Object *args)
{
return concat_to_string (nargs, args);
}
DEFUN ("vconcat", Fvconcat, Svconcat, 0, MANY, 0,
doc: /* Concatenate all the arguments and make the result a vector.
The result is a vector whose elements are the elements of all the arguments.
Each argument may be a list, vector or string.
usage: (vconcat &rest SEQUENCES) */)
(ptrdiff_t nargs, Lisp_Object *args)
{
return concat_to_vector (nargs, args);
}
DEFUN ("copy-sequence", Fcopy_sequence, Scopy_sequence, 1, 1, 0,
doc: /* Return a copy of a list, vector, string, char-table or record.
The elements of a list, vector or record are not copied; they are
shared with the original. See Info node `(elisp) Sequence Functions'
for more details about this sharing and its effects.
If the original sequence is empty, this function may return
the same empty object instead of its copy. */)
(Lisp_Object arg)
{
if (NILP (arg)) return arg;
if (CONSP (arg))
{
Lisp_Object val = Fcons (XCAR (arg), Qnil);
Lisp_Object prev = val;
Lisp_Object tail = XCDR (arg);
FOR_EACH_TAIL (tail)
{
Lisp_Object c = Fcons (XCAR (tail), Qnil);
XSETCDR (prev, c);
prev = c;
}
CHECK_LIST_END (tail, tail);
return val;
}
if (STRINGP (arg))
{
ptrdiff_t bytes = SBYTES (arg);
ptrdiff_t chars = SCHARS (arg);
Lisp_Object val = STRING_MULTIBYTE (arg)
? make_uninit_multibyte_string (chars, bytes)
: make_uninit_string (bytes);
memcpy (SDATA (val), SDATA (arg), bytes);
INTERVAL ivs = string_intervals (arg);
if (ivs)
{
INTERVAL copy = copy_intervals (ivs, 0, chars);
set_interval_object (copy, val);
set_string_intervals (val, copy);
}
return val;
}
if (VECTORP (arg))
return Fvector (ASIZE (arg), XVECTOR (arg)->contents);
if (RECORDP (arg))
return Frecord (PVSIZE (arg), XVECTOR (arg)->contents);
if (CHAR_TABLE_P (arg))
return copy_char_table (arg);
if (BOOL_VECTOR_P (arg))
{
EMACS_INT nbits = bool_vector_size (arg);
ptrdiff_t nbytes = bool_vector_bytes (nbits);
Lisp_Object val = make_uninit_bool_vector (nbits);
memcpy (bool_vector_data (val), bool_vector_data (arg), nbytes);
return val;
}
wrong_type_argument (Qsequencep, arg);
}
/* This structure holds information of an argument of `concat_to_string'
that is a string and has text properties to be copied. */
struct textprop_rec
{
ptrdiff_t argnum; /* refer to ARGS (arguments of `concat') */
ptrdiff_t to; /* refer to VAL (the target string) */
};
static Lisp_Object
concat_to_string (ptrdiff_t nargs, Lisp_Object *args)
{
USE_SAFE_ALLOCA;
/* Check types and compute total length in chars of arguments in RESULT_LEN,
length in bytes in RESULT_LEN_BYTE, and determine in DEST_MULTIBYTE
whether the result should be a multibyte string. */
EMACS_INT result_len = 0;
EMACS_INT result_len_byte = 0;
bool dest_multibyte = false;
bool some_unibyte = false;
for (ptrdiff_t i = 0; i < nargs; i++)
{
Lisp_Object arg = args[i];
EMACS_INT len;
/* We must count the number of bytes needed in the string
as well as the number of characters. */
if (STRINGP (arg))
{
ptrdiff_t arg_len_byte = SBYTES (arg);
len = SCHARS (arg);
if (STRING_MULTIBYTE (arg))
dest_multibyte = true;
else
some_unibyte = true;
if (STRING_BYTES_BOUND - result_len_byte < arg_len_byte)
string_overflow ();
result_len_byte += arg_len_byte;
}
else if (VECTORP (arg))
{
len = ASIZE (arg);
ptrdiff_t arg_len_byte = 0;
for (ptrdiff_t j = 0; j < len; j++)
{
Lisp_Object ch = AREF (arg, j);
CHECK_CHARACTER (ch);
int c = XFIXNAT (ch);
arg_len_byte += CHAR_BYTES (c);
if (!ASCII_CHAR_P (c) && !CHAR_BYTE8_P (c))
dest_multibyte = true;
}
if (STRING_BYTES_BOUND - result_len_byte < arg_len_byte)
string_overflow ();
result_len_byte += arg_len_byte;
}
else if (NILP (arg))
continue;
else if (CONSP (arg))
{
len = XFIXNAT (Flength (arg));
ptrdiff_t arg_len_byte = 0;
for (; CONSP (arg); arg = XCDR (arg))
{
Lisp_Object ch = XCAR (arg);
CHECK_CHARACTER (ch);
int c = XFIXNAT (ch);
arg_len_byte += CHAR_BYTES (c);
if (!ASCII_CHAR_P (c) && !CHAR_BYTE8_P (c))
dest_multibyte = true;
}
if (STRING_BYTES_BOUND - result_len_byte < arg_len_byte)
string_overflow ();
result_len_byte += arg_len_byte;
}
else
wrong_type_argument (Qsequencep, arg);
result_len += len;
if (MOST_POSITIVE_FIXNUM < result_len)
memory_full (SIZE_MAX);
}
if (dest_multibyte && some_unibyte)
{
/* Non-ASCII characters in unibyte strings take two bytes when
converted to multibyte -- count them and adjust the total. */
for (ptrdiff_t i = 0; i < nargs; i++)
{
Lisp_Object arg = args[i];
if (STRINGP (arg) && !STRING_MULTIBYTE (arg))
{
ptrdiff_t bytes = SCHARS (arg);
const unsigned char *s = SDATA (arg);
ptrdiff_t nonascii = 0;
for (ptrdiff_t j = 0; j < bytes; j++)
nonascii += s[j] >> 7;
if (STRING_BYTES_BOUND - result_len_byte < nonascii)
string_overflow ();
result_len_byte += nonascii;
}
}
}
if (!dest_multibyte)
result_len_byte = result_len;
/* Create the output object. */
Lisp_Object result = dest_multibyte
? make_uninit_multibyte_string (result_len, result_len_byte)
: make_uninit_string (result_len);
/* Copy the contents of the args into the result. */
ptrdiff_t toindex = 0;
ptrdiff_t toindex_byte = 0;
/* When we make a multibyte string, we can't copy text properties
while concatenating each string because the length of resulting
string can't be decided until we finish the whole concatenation.
So, we record strings that have text properties to be copied
here, and copy the text properties after the concatenation. */
struct textprop_rec *textprops;
/* Number of elements in textprops. */
ptrdiff_t num_textprops = 0;
SAFE_NALLOCA (textprops, 1, nargs);
for (ptrdiff_t i = 0; i < nargs; i++)
{
Lisp_Object arg = args[i];
if (STRINGP (arg))
{
if (string_intervals (arg))
{
textprops[num_textprops].argnum = i;
textprops[num_textprops].to = toindex;
num_textprops++;
}
ptrdiff_t nchars = SCHARS (arg);
if (STRING_MULTIBYTE (arg) == dest_multibyte)
{
/* Between strings of the same kind, copy fast. */
ptrdiff_t arg_len_byte = SBYTES (arg);
memcpy (SDATA (result) + toindex_byte, SDATA (arg), arg_len_byte);
toindex_byte += arg_len_byte;
}
else
{
/* Copy a single-byte string to a multibyte string. */
toindex_byte += str_to_multibyte (SDATA (result) + toindex_byte,
SDATA (arg), nchars);
}
toindex += nchars;
}
else if (VECTORP (arg))
{
ptrdiff_t len = ASIZE (arg);
for (ptrdiff_t j = 0; j < len; j++)
{
int c = XFIXNAT (AREF (arg, j));
if (dest_multibyte)
toindex_byte += CHAR_STRING (c, SDATA (result) + toindex_byte);
else
SSET (result, toindex_byte++, c);
toindex++;
}
}
else
for (Lisp_Object tail = arg; !NILP (tail); tail = XCDR (tail))
{
int c = XFIXNAT (XCAR (tail));
if (dest_multibyte)
toindex_byte += CHAR_STRING (c, SDATA (result) + toindex_byte);
else
SSET (result, toindex_byte++, c);
toindex++;