Repository navigation
Expand file tree
/
Copy pathstring.c
More file actions
4669 lines (4268 loc) · 142 KB
/
Copy pathstring.c
File metadata and controls
4669 lines (4268 loc) · 142 KB
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
/*
** string.c - String class
**
** See Copyright Notice in mruby.h
*/
#ifdef _MSC_VER
# define _CRT_NONSTDC_NO_DEPRECATE
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
#endif
#include <mruby.h>
#include <mruby/array.h>
#include <mruby/class.h>
#include <mruby/range.h>
#include <mruby/string.h>
#include <mruby/numeric.h>
#include <mruby/internal.h>
#include <string.h>
typedef struct mrb_shared_string {
int refcnt;
mrb_int capa;
/* Offset past the last byte any sharer can see. Bytes at or above it are
dead to every sharer, so a writer may use them in place (str_modify_cat).
Only grows, as sharers are added. */
mrb_int reserved;
char *ptr;
} mrb_shared_string;
const char mrb_digitmap[] = "0123456789abcdefghijklmnopqrstuvwxyz";
#define mrb_obj_alloc_string(mrb) MRB_OBJ_ALLOC((mrb), MRB_TT_STRING, (mrb)->string_class)
#ifndef MRB_STR_LENGTH_MAX
#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__)
#define MRB_STR_LENGTH_MAX 0
#else
#define MRB_STR_LENGTH_MAX 1048576
#endif
#endif
static void
str_check_length(mrb_state *mrb, mrb_int len)
{
if (len < 0 || len == MRB_INT_MAX) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "negative (or overflowed) string size");
}
#if MRB_STR_LENGTH_MAX != 0
if (len > MRB_STR_LENGTH_MAX-1) {
mrb_raisef(mrb, E_ARGUMENT_ERROR, "string too long (len=%i max=" MRB_STRINGIZE(MRB_STR_LENGTH_MAX) ")", len);
}
#endif
}
mrb_bool
mrb_strcasecmp_p(const char *s1, mrb_int len1, const char *s2, mrb_int len2)
{
if (len1 != len2) return FALSE;
const char *e1 = s1 + len1;
while (s1 < e1) {
if (*s1 != *s2 && TOUPPER(*s1) != TOUPPER(*s2)) return FALSE;
s1++;
s2++;
}
return TRUE;
}
static struct RString*
str_init_normal_capa(mrb_state *mrb, struct RString *s,
const char *p, mrb_int len, mrb_int capa)
{
str_check_length(mrb, capa);
char *dst = (char*)mrb_malloc(mrb, capa + 1);
if (p) memcpy(dst, p, len);
dst[len] = '\0';
s->as.heap.ptr = dst;
s->as.heap.len = len;
s->as.heap.aux.capa = capa;
RSTR_SET_TYPE(s, NORMAL);
return s;
}
static struct RString*
str_init_normal(mrb_state *mrb, struct RString *s, const char *p, mrb_int len)
{
return str_init_normal_capa(mrb, s, p, len, len);
}
static struct RString*
str_init_embed(struct RString *s, const char *p, mrb_int len)
{
mrb_assert(len >= 0);
if (p) memcpy(RSTR_EMBED_PTR(s), p, len);
RSTR_EMBED_PTR(s)[len] = '\0';
RSTR_SET_TYPE(s, EMBED);
RSTR_SET_EMBED_LEN(s, len);
return s;
}
static struct RString*
str_init_nofree(struct RString *s, const char *p, mrb_int len)
{
s->as.heap.ptr = (char*)p;
s->as.heap.len = len;
s->as.heap.aux.capa = 0; /* nofree */
RSTR_SET_TYPE(s, NOFREE);
return s;
}
static struct RString*
str_init_shared(mrb_state *mrb, const struct RString *orig, struct RString *s, mrb_shared_string *shared)
{
if (shared) {
mrb_int end = (mrb_int)(orig->as.heap.ptr - shared->ptr) + orig->as.heap.len;
if (shared->reserved < end) shared->reserved = end;
shared->refcnt++;
}
else {
shared = (mrb_shared_string*)mrb_malloc(mrb, sizeof(mrb_shared_string));
shared->refcnt = 1;
shared->ptr = orig->as.heap.ptr;
shared->capa = orig->as.heap.aux.capa;
shared->reserved = orig->as.heap.len;
}
s->as.heap.ptr = orig->as.heap.ptr;
s->as.heap.len = orig->as.heap.len;
s->as.heap.aux.shared = shared;
RSTR_SET_TYPE(s, SHARED);
return s;
}
static struct RString*
str_init_fshared(const struct RString *orig, struct RString *s, struct RString *fshared)
{
s->as.heap.ptr = orig->as.heap.ptr;
s->as.heap.len = orig->as.heap.len;
s->as.heap.aux.fshared = fshared;
RSTR_SET_TYPE(s, FSHARED);
return s;
}
static struct RString*
str_init_modifiable(mrb_state *mrb, struct RString *s, const char *p, mrb_int len)
{
if (RSTR_EMBEDDABLE_P(len)) {
return str_init_embed(s, p, len);
}
return str_init_normal(mrb, s, p, len);
}
static struct RString*
str_new_static(mrb_state *mrb, const char *p, mrb_int len)
{
if (RSTR_EMBEDDABLE_P(len)) {
return str_init_embed(mrb_obj_alloc_string(mrb), p, len);
}
return str_init_nofree(mrb_obj_alloc_string(mrb), p, len);
}
static struct RString*
str_new(mrb_state *mrb, const char *p, mrb_int len)
{
str_check_length(mrb, len);
if (RSTR_EMBEDDABLE_P(len)) {
return str_init_embed(mrb_obj_alloc_string(mrb), p, len);
}
if (p && mrb_ro_data_p(p)) {
return str_init_nofree(mrb_obj_alloc_string(mrb), p, len);
}
return str_init_normal(mrb, mrb_obj_alloc_string(mrb), p, len);
}
/*
* @param mrb The mruby state.
* @param capa The desired capacity of the new string.
* @return A new mruby string with the specified capacity.
*
* Creates a new mruby string with a given initial capacity.
* The string is initially empty.
*/
MRB_API mrb_value
mrb_str_new_capa(mrb_state *mrb, mrb_int capa)
{
struct RString *s = mrb_obj_alloc_string(mrb);
if (RSTR_EMBEDDABLE_P(capa)) {
s = str_init_embed(s, NULL, 0);
}
else {
s = str_init_normal_capa(mrb, s, NULL, 0, capa);
}
return mrb_obj_value(s);
}
static void
resize_capa(mrb_state *mrb, struct RString *s, mrb_int capacity)
{
if (RSTR_EMBED_P(s)) {
if (!RSTR_EMBEDDABLE_P(capacity)) {
str_init_normal_capa(mrb, s, RSTR_EMBED_PTR(s), RSTR_EMBED_LEN(s), capacity);
}
}
else {
str_check_length(mrb, capacity);
s->as.heap.ptr = (char*)mrb_realloc(mrb, RSTR_PTR(s), capacity+1);
s->as.heap.aux.capa = (mrb_ssize)capacity;
}
}
/*
* @param mrb The mruby state.
* @param p A pointer to the C string to copy.
* @param len The length of the C string.
* @return A new mruby string containing the copied C string.
*
* Creates a new mruby string from a C string and a specified length.
* If `p` is NULL, an empty string is created.
*/
MRB_API mrb_value
mrb_str_new(mrb_state *mrb, const char *p, mrb_int len)
{
return mrb_obj_value(str_new(mrb, p, len));
}
/*
* @param mrb The mruby state.
* @param p A pointer to the null-terminated C string to copy.
* @return A new mruby string containing the copied C string.
*
* Creates a new mruby string from a null-terminated C string.
* If `p` is NULL, an empty string is created.
*/
MRB_API mrb_value
mrb_str_new_cstr(mrb_state *mrb, const char *p)
{
struct RString *s;
mrb_int len;
if (p) {
len = strlen(p);
}
else {
len = 0;
}
s = str_new(mrb, p, len);
return mrb_obj_value(s);
}
/*
* @param mrb The mruby state.
* @param p A pointer to the static C string.
* @param len The length of the static C string.
* @return A new mruby string referencing the static C string.
*
* Creates a new mruby string that directly references a static C string.
* The C string is not copied and must remain valid for the lifetime of the mruby string.
* This is typically used for string literals.
*/
MRB_API mrb_value
mrb_str_new_static(mrb_state *mrb, const char *p, mrb_int len)
{
struct RString *s = str_new_static(mrb, p, len);
return mrb_obj_value(s);
}
static void
str_decref(mrb_state *mrb, mrb_shared_string *shared)
{
shared->refcnt--;
if (shared->refcnt == 0) {
mrb_free(mrb, shared->ptr);
mrb_free(mrb, shared);
}
}
static void
str_unshare_buffer(mrb_state *mrb, struct RString *s)
{
if (RSTR_SHARED_P(s)) {
mrb_shared_string *shared = s->as.heap.aux.shared;
if (shared->refcnt == 1 && s->as.heap.ptr == shared->ptr) {
s->as.heap.aux.capa = shared->capa;
s->as.heap.ptr[s->as.heap.len] = '\0';
RSTR_SET_TYPE(s, NORMAL);
mrb_free(mrb, shared);
}
else {
str_init_modifiable(mrb, s, s->as.heap.ptr, s->as.heap.len);
str_decref(mrb, shared);
}
}
else if (RSTR_NOFREE_P(s) || RSTR_FSHARED_P(s)) {
str_init_modifiable(mrb, s, s->as.heap.ptr, s->as.heap.len);
}
}
static void
check_null_byte(mrb_state *mrb, struct RString *str)
{
const char *p = RSTR_PTR(str);
if (p && memchr(p, '\0', RSTR_LEN(str))) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "string contains null byte");
}
}
void
mrb_gc_free_str(mrb_state *mrb, struct RString *str)
{
if (RSTR_EMBED_P(str))
/* no code */;
else if (RSTR_SHARED_P(str))
str_decref(mrb, str->as.heap.aux.shared);
else if (!RSTR_NOFREE_P(str) && !RSTR_FSHARED_P(str))
mrb_free(mrb, str->as.heap.ptr);
}
#if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || \
defined(__powerpc64__) || defined(__POWERPC__) || defined(__aarch64__) || \
defined(__mc68020__)
# define ALIGNED_WORD_ACCESS 0
#else
# define ALIGNED_WORD_ACCESS 1
#endif
#ifdef MRB_64BIT
#define bitint uint64_t
#define MASK01 0x0101010101010101ull
#else
#define bitint uint32_t
#define MASK01 0x01010101ul
#endif
/* Encode a Unicode codepoint to UTF-8 bytes, into a buffer of at least four.
Returns the number of bytes written (1-4), or 0 for a value outside
U+0000..U+10FFFF, which spells no character. The value arrives as an
mrb_int so that a negative one and one past the range are both this
function's answer to give; a caller reporting them differs only in which
exception it raises, and each raises what CRuby raises there.
A surrogate does encode. What CRuby writes for one is what mruby writes:
sprintf("%c", 0xD800) and [0xD800].pack("U") both yield ED A0 80 there.
Reading those bytes back is a separate question, and mrb_utf8len() answers
it by RFC 3629, under which a surrogate spells nothing. So what this writes
is deliberately wider than what that reads, and a string built from one is
valid_encoding? == false. */
#if defined(MRB_UTF8_STRING) || defined(HAVE_MRUBY_REGEXP_GEM)
mrb_int
mrb_utf8_to_buf(char *buf, mrb_int cp)
{
if (cp < 0) {
return 0;
}
else if (cp < 0x80) {
buf[0] = (char)cp;
return 1;
}
else if (cp < 0x800) {
buf[0] = (char)(0xC0 | (cp >> 6));
buf[1] = (char)(0x80 | (cp & 0x3F));
return 2;
}
else if (cp < 0x10000) {
buf[0] = (char)(0xE0 | (cp >> 12));
buf[1] = (char)(0x80 | ((cp >> 6) & 0x3F));
buf[2] = (char)(0x80 | (cp & 0x3F));
return 3;
}
else if (cp <= 0x10FFFF) {
buf[0] = (char)(0xF0 | (cp >> 18));
buf[1] = (char)(0x80 | ((cp >> 12) & 0x3F));
buf[2] = (char)(0x80 | ((cp >> 6) & 0x3F));
buf[3] = (char)(0x80 | (cp & 0x3F));
return 4;
}
return 0; /* above U+10FFFF */
}
#endif
/* UTF-8: what a run of bytes spells, and what a string holds character by
character. The scan is mrb_utf8_scan() in internal.h, taken inline by
each reader; what this file adds is the view it reads through. Only a
build that indexes strings by character has to answer, so a build
without MRB_UTF8_STRING carries none of it. */
#ifdef MRB_UTF8_STRING
#define utf8_islead(c) ((unsigned char)((c)&0xc0) != 0x80)
/* What a lead byte's second byte may be for the sequence to spell a
character under RFC 3629 (Unicode D93b), one pair per lead byte from
0xC0: the floor of each length (0xE0 A0, 0xF0 90; 0xC0 and 0xC1 have
nothing to spell), the UTF-16 surrogates (0xED up to 9F), U+10FFFF
(0xF4 up to 8F), and nothing at all for 0xF5 and above. This is the one
view the scan keeps none of; unpack("U") in mruby-pack holds its own. */
#define R(lo, hi) {0x##lo, 0x##hi}
static const uint8_t utf8_rfc3629_bounds[64][2] = {
R(C0,BF), R(C0,BF), R(80,BF), R(80,BF), R(80,BF), R(80,BF), R(80,BF), R(80,BF), /* C0..C7 */
R(80,BF), R(80,BF), R(80,BF), R(80,BF), R(80,BF), R(80,BF), R(80,BF), R(80,BF), /* C8..CF */
R(80,BF), R(80,BF), R(80,BF), R(80,BF), R(80,BF), R(80,BF), R(80,BF), R(80,BF), /* D0..D7 */
R(80,BF), R(80,BF), R(80,BF), R(80,BF), R(80,BF), R(80,BF), R(80,BF), R(80,BF), /* D8..DF */
R(A0,BF), R(80,BF), R(80,BF), R(80,BF), R(80,BF), R(80,BF), R(80,BF), R(80,BF), /* E0..E7 */
R(80,BF), R(80,BF), R(80,BF), R(80,BF), R(80,BF), R(80,9F), R(80,BF), R(80,BF), /* E8..EF */
R(90,BF), R(80,BF), R(80,BF), R(80,BF), R(80,8F), R(80,7F), R(80,7F), R(80,7F), /* F0..F7 */
R(80,7F), R(80,7F), R(80,7F), R(80,7F), R(80,7F), R(80,7F), R(80,7F), R(80,7F), /* F8..FF */
};
#undef R
mrb_int
mrb_utf8len(const char* p, const char* e)
{
mrb_int n = mrb_utf8_scan(p, e, NULL, utf8_rfc3629_bounds);
return n < 0 ? 1 : n;
}
/* The byte the character covering `p` starts at, or `p` itself when `p` is
already a character boundary. A continuation byte belongs to the character
that reaches it; one that no lead byte reaches belongs to none and stands as
a character of its own. Whether a lead byte reaches is mrb_utf8len()'s
answer, so the boundaries found here are the ones the character count is
taken over. Reading back three bytes covers it, since nothing longer than
four bytes spells a character. */
const char*
mrb_utf8_char_head(const char *beg, const char *p, const char *end)
{
if (p >= end || utf8_islead(p[0])) return p;
for (mrb_int back = 1; back <= 3 && back <= p - beg; back++) {
const char *lead = p - back;
if (!utf8_islead(lead[0])) continue; /* another continuation byte */
return mrb_utf8len(lead, end) > back ? lead : p;
}
return p;
}
/* Decode a UTF-8 character and return its codepoint.
*lenp is set to the byte length consumed. What mrb_utf8len() rejects is
rejected here by the same view, so those consume a single byte and come
back as the lead byte itself. */
uint32_t
mrb_utf8_decode(const char *p, const char *e, mrb_int *lenp)
{
uint32_t uv;
mrb_int n = mrb_utf8_scan(p, e, &uv, utf8_rfc3629_bounds);
if (n < 0) {
*lenp = 1;
return (uint8_t)p[0];
}
*lenp = n;
return uv;
}
#define NOASCII(c) ((c) & 0x80)
#ifdef SIMPLE_SEARCH_NONASCII
/* the naive implementation. define SIMPLE_SEARCH_NONASCII, */
/* if you need it for any constraint (e.g. code size). */
static const char*
search_nonascii(const char* p, const char *e)
{
for (; p < e; ++p) {
if (NOASCII(*p)) return p;
}
return e;
}
#elif defined(__SSE2__)
# include <emmintrin.h>
static inline const char *
search_nonascii(const char *p, const char *e)
{
if (sizeof(__m128i) < (size_t)(e - p)) {
if (!_mm_movemask_epi8(_mm_loadu_si128((__m128i const*)p))) {
const intptr_t lowbits = sizeof(__m128i) - 1;
const __m128i *s, *t;
s = (const __m128i*)(~lowbits & ((intptr_t)p + lowbits));
t = (const __m128i*)(~lowbits & (intptr_t)e);
for (; s < t; ++s) {
if (_mm_movemask_epi8(_mm_load_si128(s))) break;
}
p = (const char *)s;
}
}
/* One test per byte the range holds: entering at `case N` runs N of them,
and `default` is only reached where the range holds at least 16, which is
the first `_mm_loadu_si128()` having found a byte among those 16. A test
more than the label promises reads the position the range ends at, which
every mruby string happens to carry as its NUL sentinel and a bare buffer
does not. */
switch (e - p) {
default:
case 16: if (NOASCII(*p)) return p; ++p;
case 15: if (NOASCII(*p)) return p; ++p;
case 14: if (NOASCII(*p)) return p; ++p;
case 13: if (NOASCII(*p)) return p; ++p;
case 12: if (NOASCII(*p)) return p; ++p;
case 11: if (NOASCII(*p)) return p; ++p;
case 10: if (NOASCII(*p)) return p; ++p;
case 9: if (NOASCII(*p)) return p; ++p;
case 8: if (NOASCII(*p)) return p; ++p;
case 7: if (NOASCII(*p)) return p; ++p;
case 6: if (NOASCII(*p)) return p; ++p;
case 5: if (NOASCII(*p)) return p; ++p;
case 4: if (NOASCII(*p)) return p; ++p;
case 3: if (NOASCII(*p)) return p; ++p;
case 2: if (NOASCII(*p)) return p; ++p;
case 1: if (NOASCII(*p)) return p; ++p;
case 0: break;
}
return e;
}
#else
static const char*
search_nonascii(const char *p, const char *e)
{
ptrdiff_t byte_len = e - p;
const char *be = p + sizeof(bitint) * (byte_len / sizeof(bitint));
for (; p < be; p+=sizeof(bitint)) {
bitint t0;
memcpy(&t0, p, sizeof(bitint));
const bitint t1 = t0 & (MASK01*0x80);
if (t1) {
e = p + sizeof(bitint)-1;
byte_len = sizeof(bitint)-1;
break;
}
}
switch (byte_len % sizeof(bitint)) {
#ifdef MRB_64BIT
case 7: if (e[-7]&0x80) return e-7;
case 6: if (e[-6]&0x80) return e-6;
case 5: if (e[-5]&0x80) return e-5;
case 4: if (e[-4]&0x80) return e-4;
#endif
case 3: if (e[-3]&0x80) return e-3;
case 2: if (e[-2]&0x80) return e-2;
case 1: if (e[-1]&0x80) return e-1;
}
return e;
}
#endif /* SIMPLE_SEARCH_NONASCII */
/* Counts characters, and when `restp` is given also answers where the walk
stopped: at the end of the string when every byte spelled a character, and
at the first byte that spells none otherwise, the count reaching only that
far. A caller passing no `restp` is counting rather than asking, and the
walk reads a byte that spells no character as a character of its own and
carries on, which is what the count of a broken string has always been. */
static mrb_int
utf8_strlen_check(const char *str, mrb_int byte_len, const char **restp)
{
const char *p = str;
const char *e = str + byte_len;
mrb_int len = 0;
while (p < e) {
const char *np = search_nonascii(p, e);
len += np - p;
if (np == e) break;
p = np;
while (p < e && NOASCII(*p)) {
mrb_int clen = mrb_utf8len(p, e);
/* mrb_utf8len() answers 1 for a byte that leads no valid sequence. The
byte here is known to be non-ASCII, so a length of 1 means the string
carries a byte that stands for no character. */
if (restp && clen == 1) {
*restp = p;
return len;
}
p += clen;
len++;
}
}
if (restp) *restp = e;
return len;
}
mrb_int
mrb_utf8_strlen(const char *str, mrb_int byte_len)
{
return utf8_strlen_check(str, byte_len, NULL);
}
/* A character of valid UTF-8 is one lead byte and the continuation bytes after
it, so a string of it holds one character per byte that is not 10xxxxxx.
Counting that way reads a word at a time and decodes nothing, which is what
a string already read as UTF-8 is counted with below. */
#define UTF8_LEAD_SHIFT (8 * sizeof(bitint) - 8)
#define UTF8_LEAD_P(c) (((unsigned char)(c) & 0xc0) != 0x80)
static inline mrb_int
utf8_word_leads(bitint w)
{
/* Each byte of `cont` is one where the byte of `w` is a continuation byte,
and the multiplication sums the bytes into the top one. A word holds
sizeof(bitint) bytes, so the sum cannot carry out of that byte. */
const bitint cont = (w >> 7) & ~(w >> 6) & MASK01;
return (mrb_int)sizeof(bitint) - (mrb_int)((cont * MASK01) >> UTF8_LEAD_SHIFT);
}
/* the characters of bytes known to spell them */
static mrb_int
utf8_valid_strlen(const char *p, mrb_int byte_len)
{
const char *e = p + byte_len;
mrb_int len = 0;
while (e - p >= (ptrdiff_t)sizeof(bitint)) {
bitint w;
memcpy(&w, p, sizeof(bitint));
/* A word of nothing but ASCII is one character per byte, and
search_nonascii() crosses a run of those faster than a word at a time
where the machine has an instruction for it. */
if (w & (MASK01*0x80)) {
len += utf8_word_leads(w);
p += sizeof(bitint);
}
else {
const char *np = search_nonascii(p, e);
len += np - p;
p = np;
}
}
if (p < e) {
/* The bytes left over are counted as a word padded with zeros, which are
lead bytes of their own and are taken off again. */
bitint w = 0;
const mrb_int rest = (mrb_int)(e - p);
memcpy(&w, p, (size_t)rest);
len += utf8_word_leads(w) - ((mrb_int)sizeof(bitint) - rest);
}
return len;
}
/* count the characters of a string */
mrb_int
mrb_str_char_len(mrb_state *mrb, mrb_value str)
{
(void)mrb;
struct RString *s = mrb_str_ptr(str);
mrb_int byte_len = RSTR_LEN(s);
/* A single-byte string has one position per byte, which is what
mrb_str_char_to_byte() and mrb_str_byte_to_char() already answer for it.
Asked here only where the string stands, the same string was measured as
UTF-8 and reported a length its own indexing did not agree with.
Nothing is recorded on the way out. A string of nothing but ASCII carries
that already, and a byte-read one returns here because of how it is read
rather than because of what its bytes are: 7BIT would be a claim about
bytes nothing has looked at, and force_encoding() can take the byte
reading away again and leave the claim standing. */
if (RSTR_SINGLE_BYTE_P(s)) {
return byte_len;
}
/* A string that has been read already says what its bytes spell, and what
they spell says how many characters they are without decoding one. */
else if (RSTR_CODERANGE(s) == MRB_STR_CODERANGE_VALID) {
return utf8_valid_strlen(RSTR_PTR(s), byte_len);
}
else {
const char *p = RSTR_PTR(s);
const char *e = p + byte_len;
const char *np = search_nonascii(p, e);
/* Every character a non-ASCII byte begins spells two bytes or more, and a
non-ASCII byte that begins none spells no character at all, so a string
holds one character per byte exactly when every byte of it is ASCII.
Counts that come out equal do not say that: a byte spelling no character
is counted as one too, so a string of them set the flag as well, and the
readers of it went on to hand those bytes back as characters. */
if (np == e) {
RSTR_CODERANGE_SET(s, MRB_STR_CODERANGE_7BIT);
return byte_len;
}
/* A string already known to be broken has nothing left to learn here, and
the count of one is the walk that reads every byte through. */
if (RSTR_CODERANGE(s) == MRB_STR_CODERANGE_BROKEN) {
mrb_int utf8_len = (mrb_int)(np - p) + mrb_utf8_strlen(np, (mrb_int)(e - np));
mrb_assert(utf8_len <= byte_len);
return utf8_len;
}
/* The walk that counts decodes every sequence on the way, which is the
whole of what asking whether the string reads as UTF-8 does. Recording
it here is what spares the next reader of the same string a second walk
of it: character indexing asks that question of every string it is
given, and a string counted first used to arrive with nothing recorded
and be read through again. The count carries past the byte the walk
stopped at, since the length of a broken string is what it has always
been, one character for each byte that spells none. */
const char *stop;
mrb_int utf8_len = (mrb_int)(np - p) + utf8_strlen_check(np, (mrb_int)(e - np), &stop);
if (stop == e) {
RSTR_CODERANGE_SET(s, MRB_STR_CODERANGE_VALID);
}
else {
RSTR_CODERANGE_SET(s, MRB_STR_CODERANGE_BROKEN);
utf8_len += mrb_utf8_strlen(stop, (mrb_int)(e - stop));
}
mrb_assert(utf8_len <= byte_len);
return utf8_len;
}
}
/* whether a string's bytes read as the encoding it is taken to have */
mrb_bool
mrb_str_valid_encoding_p(mrb_state *mrb, mrb_value str)
{
(void)mrb;
struct RString *s = mrb_str_ptr(str);
/* A byte-indexed string makes no such claim, so it is valid whatever its
bytes are. */
if (RSTR_BINARY_P(s)) return TRUE;
/* The walk below reads the whole string to answer either way, so a string
that has been walked already is answered off where it stands instead. A
string of one character per byte is one of those: it holds nothing but
ASCII, and ASCII reads as UTF-8 as it stands. This is what a string
counted before it is asked about comes in carrying. */
mrb_int cr = RSTR_CODERANGE(s);
if (cr == MRB_STR_CODERANGE_7BIT || cr == MRB_STR_CODERANGE_VALID) return TRUE;
if (cr == MRB_STR_CODERANGE_BROKEN) return FALSE;
mrb_int byte_len = RSTR_LEN(s);
const char *p = RSTR_PTR(s);
const char *stop;
mrb_int utf8_len = utf8_strlen_check(p, byte_len, &stop);
if (stop != p + byte_len) {
RSTR_CODERANGE_SET(s, MRB_STR_CODERANGE_BROKEN);
return FALSE;
}
RSTR_CODERANGE_SET(s, byte_len == utf8_len ? MRB_STR_CODERANGE_7BIT
: MRB_STR_CODERANGE_VALID);
return TRUE;
}
/* whether every byte of the string is ASCII. A walk that finds nothing else
has made the statement 7BIT makes, so the answer is left on the string for
the next asker to read off. */
static mrb_bool
str_ascii_p(struct RString *s)
{
if (RSTR_CODERANGE(s) == MRB_STR_CODERANGE_7BIT) return TRUE;
const char *p = RSTR_PTR(s);
const char *e = p + RSTR_LEN(s);
if (search_nonascii(p, e) != e) return FALSE;
RSTR_CODERANGE_SET(s, MRB_STR_CODERANGE_7BIT);
return TRUE;
}
/* Whether a character index into this string is already a byte index, asking
the bytes where the string does not say. RSTR_SINGLE_BYTE_P() reads what is
recorded and answers no for a string nothing has read yet, which sends every
later caller down the walking path however plain the bytes are. A string is
walked whole at most once here: the walk records what it finds, and it is
the same walk the character indexing would go on to do anyway. */
mrb_bool
mrb_str_single_byte_p(mrb_state *mrb, mrb_value str)
{
struct RString *s = mrb_str_ptr(str);
if (RSTR_CODERANGE(s) == MRB_STR_CODERANGE_UNKNOWN) {
mrb_str_valid_encoding_p(mrb, str);
}
return RSTR_SINGLE_BYTE_P(s);
}
/* map character index to byte offset index */
mrb_int
mrb_str_char_to_byte(mrb_state *mrb, mrb_value str, mrb_int off, mrb_int idx)
{
(void)mrb;
struct RString *s = mrb_str_ptr(str);
if (RSTR_SINGLE_BYTE_P(s)) {
return idx;
}
const char *o = RSTR_PTR(s);
const char *p0 = o + off;
const char *p = p0;
const char *e = o + RSTR_LEN(s);
mrb_int i = 0;
/* Where the bytes are known to spell characters, a word of them says how
many it holds, and a word holding fewer than the index still has to reach
is a word to step over whole. What the walk below decodes to step one
character, this reads. */
if (RSTR_CODERANGE(s) == MRB_STR_CODERANGE_VALID) {
if (idx <= 0) return 0;
/* A word spells two characters or more, so one character is stepped by
the loop after this rather than by reading a word to step nothing. */
while (idx > 1 && e - p >= (ptrdiff_t)sizeof(bitint)) {
bitint w;
memcpy(&w, p, sizeof(bitint));
if (w & (MASK01*0x80)) {
const mrb_int n = utf8_word_leads(w);
if (i + n > idx) break;
i += n;
p += sizeof(bitint);
}
else {
/* see the ASCII run below */
if (i == idx) break;
const char *lim = (e - p) > (idx - i) ? p + (idx - i) : e;
const char *np = search_nonascii(p, lim);
i += np - p;
p = np;
}
}
/* The word the loop stopped at holds the character asked for, and a
character starts where a continuation byte does not. */
for (; p < e; p++) {
if (UTF8_LEAD_P(*p)) {
if (i == idx) break;
i++;
}
}
mrb_int len = (mrb_int)(p-p0);
if (i<idx) len++;
return len;
}
while (p<e && i<idx) {
if ((*p & 0x80) == 0) {
/* Every ASCII byte stands for a character of its own, so the run only
has to be followed as far as the index asks for. Reading to the end of
the string instead makes finding the character just past the head cost
what finding the last one does. */
const char *lim = (e - p) > (idx - i) ? p + (idx - i) : e;
const char *np = search_nonascii(p, lim);
i += np - p;
p = np;
}
else {
p += mrb_utf8len(p, e);
i++;
}
}
mrb_int len = (mrb_int)(p-p0);
if (i<idx) len++;
return len;
}
/* map byte offset to character index */
mrb_int
mrb_str_byte_to_char(mrb_state *mrb, mrb_value str, mrb_int bi)
{
(void)mrb;
struct RString *s = mrb_str_ptr(str);
if (bi < 0 || RSTR_LEN(s) < bi) return -1;
if (RSTR_SINGLE_BYTE_P(s)) {
return bi;
}
const char *p = RSTR_PTR(s);
const char *e = p + RSTR_LEN(s);
const char *pivot = p + bi;
mrb_int i = 0;
/* Where the bytes are known to spell characters, the characters before the
offset are the bytes before it that no character continues, and the offset
is inside a character exactly when a continuation byte stands there. */
if (RSTR_CODERANGE(s) == MRB_STR_CODERANGE_VALID) {
if (pivot < e && !UTF8_LEAD_P(*pivot)) return -1;
return utf8_valid_strlen(p, bi);
}
while (p < pivot) {
if ((*p & 0x80) == 0) {
const char *np = search_nonascii(p, pivot);
i += np - p;
p = np;
}
else {
p += mrb_utf8len(p, e);
i++;
}
}
if (p != pivot) return -1;
return i;
}
static mrb_int
str_index_str_by_char(mrb_state *mrb, mrb_value str, mrb_value sub, mrb_int pos)
{
/* see str_index_str() */
if (!mrb_str_valid_encoding_p(mrb, sub)) return -1;
const char *ptr = RSTRING_PTR(sub);
mrb_int len = RSTRING_LEN(sub);
if (pos > 0) {
pos = mrb_str_char_to_byte(mrb, str, 0, pos);
}
pos = mrb_str_index(mrb, str, ptr, len, pos);
if (pos > 0) {
pos = mrb_str_byte_to_char(mrb, str, pos);
}
return pos;
}
#else
/* a byte is a character here, so the count is the byte length and both
conversions are identity */
mrb_int
mrb_str_char_len(mrb_state *mrb, mrb_value str)
{
(void)mrb;
return RSTRING_LEN(str);
}
mrb_int
mrb_str_char_to_byte(mrb_state *mrb, mrb_value str, mrb_int off, mrb_int idx)
{
(void)mrb;
(void)str;
(void)off;
return idx;
}
mrb_int
mrb_str_byte_to_char(mrb_state *mrb, mrb_value str, mrb_int bi)
{
(void)mrb;
if (bi < 0 || RSTRING_LEN(str) < bi) return -1;
return bi;
}
#define str_index_str_by_char(mrb, str, sub, pos) str_index_str((mrb), (str), (sub), (pos))
/* a string is bytes here, with no encoding to disagree with */
mrb_bool
mrb_str_valid_encoding_p(mrb_state *mrb, mrb_value str)
{
(void)mrb;
(void)str;
return TRUE;
}
#define str_ascii_p(s) TRUE
#endif
/* memsearch_swar (SWAR stands for SIMD within a register) */
/* See https://en.wikipedia.org/wiki/SWAR */
/* The function is taken from http://0x80.pl/articles/simd-strfind.html */
/* The original source code is under 2-clause BSD license; see LEGAL file. */
/* The modifications:
* port from C++ to C
* returns mrb_int
* remove alignment issue
* support bigendian CPU
* fixed potential buffer overflow
*/
static inline mrb_int
memsearch_swar(const char *xs, mrb_int m, const char *ys, mrb_int n)
{
#define MASK7f (MASK01*0x7f)
#define MASK80 (MASK01*0x80)
#if defined(MRB_ENDIAN_BIG)
#ifdef MRB_64BIT
#define MASKtop 0x8000000000000000ull
#else
#define MASKtop 0x80000000ul
#endif
#else
#define MASKtop 0x80
#endif
const bitint first = MASK01 * (uint8_t)xs[0];
const bitint last = MASK01 * (uint8_t)xs[m-1];
const char *s0 = ys;
const char *s1 = ys+m-1;
const mrb_int lim = n - m - (mrb_int)sizeof(bitint);