-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.c
12508 lines (11232 loc) · 334 KB
/
string.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
/**********************************************************************
string.c -
$Author$
created at: Mon Aug 9 17:12:58 JST 1993
Copyright (C) 1993-2007 Yukihiro Matsumoto
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
Copyright (C) 2000 Information-technology Promotion Agency, Japan
**********************************************************************/
#include "ruby/internal/config.h"
#include <ctype.h>
#include <errno.h>
#include <math.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include "debug_counter.h"
#include "encindex.h"
#include "id.h"
#include "internal.h"
#include "internal/array.h"
#include "internal/compar.h"
#include "internal/compilers.h"
#include "internal/encoding.h"
#include "internal/error.h"
#include "internal/gc.h"
#include "internal/numeric.h"
#include "internal/object.h"
#include "internal/proc.h"
#include "internal/re.h"
#include "internal/sanitizers.h"
#include "internal/string.h"
#include "internal/transcode.h"
#include "probes.h"
#include "ruby/encoding.h"
#include "ruby/re.h"
#include "ruby/util.h"
#include "ruby_assert.h"
#include "vm_sync.h"
#if defined HAVE_CRYPT_R
# if defined HAVE_CRYPT_H
# include <crypt.h>
# endif
#elif !defined HAVE_CRYPT
# include "missing/crypt.h"
# define HAVE_CRYPT_R 1
#endif
#define BEG(no) (regs->beg[(no)])
#define END(no) (regs->end[(no)])
#undef rb_str_new
#undef rb_usascii_str_new
#undef rb_utf8_str_new
#undef rb_enc_str_new
#undef rb_str_new_cstr
#undef rb_usascii_str_new_cstr
#undef rb_utf8_str_new_cstr
#undef rb_enc_str_new_cstr
#undef rb_external_str_new_cstr
#undef rb_locale_str_new_cstr
#undef rb_str_dup_frozen
#undef rb_str_buf_new_cstr
#undef rb_str_buf_cat
#undef rb_str_buf_cat2
#undef rb_str_cat2
#undef rb_str_cat_cstr
#undef rb_fstring_cstr
VALUE rb_cString;
VALUE rb_cSymbol;
/* Flags of RString
*
* 1: RSTRING_NOEMBED
* The string is not embedded. When a string is embedded, the contents
* follow the header. When a string is not embedded, the contents is
* on a separately allocated buffer.
* 2: STR_SHARED (equal to ELTS_SHARED)
* The string is shared. The buffer this string points to is owned by
* another string (the shared root).
* 3: STR_CHILLED (will be frozen in a future version)
* The string appears frozen but can be mutated with a warning.
* 4: STR_PRECOMPUTED_HASH
* The string is embedded and has its precomputed hascode stored
* after the terminator.
* 5: STR_SHARED_ROOT
* Other strings may point to the contents of this string. When this
* flag is set, STR_SHARED must not be set.
* 6: STR_BORROWED
* When RSTRING_NOEMBED is set and klass is 0, this string is unsafe
* to be unshared by rb_str_tmp_frozen_release.
* 7: STR_TMPLOCK
* The pointer to the buffer is passed to a system call such as
* read(2). Any modification and realloc is prohibited.
* 8-9: ENC_CODERANGE
* Stores the coderange of the string.
* 10-16: ENCODING
* Stores the encoding of the string.
* 17: RSTRING_FSTR
* The string is a fstring. The string is deduplicated in the fstring
* table.
* 18: STR_NOFREE
* Do not free this string's buffer when the string is reclaimed
* by the garbage collector. Used for when the string buffer is a C
* string literal.
* 19: STR_FAKESTR
* The string is not allocated or managed by the garbage collector.
* Typically, the string object header (struct RString) is temporarily
* allocated on C stack.
*/
#define RUBY_MAX_CHAR_LEN 16
#define STR_PRECOMPUTED_HASH FL_USER4
#define STR_SHARED_ROOT FL_USER5
#define STR_BORROWED FL_USER6
#define STR_TMPLOCK FL_USER7
#define STR_NOFREE FL_USER18
#define STR_FAKESTR FL_USER19
#define STR_SET_NOEMBED(str) do {\
FL_SET((str), STR_NOEMBED);\
FL_UNSET((str), STR_SHARED | STR_SHARED_ROOT | STR_BORROWED);\
} while (0)
#define STR_SET_EMBED(str) FL_UNSET((str), STR_NOEMBED | STR_SHARED | STR_NOFREE)
#define STR_SET_LEN(str, n) do { \
RSTRING(str)->len = (n); \
} while (0)
static inline bool
str_enc_fastpath(VALUE str)
{
// The overwhelming majority of strings are in one of these 3 encodings.
switch (ENCODING_GET_INLINED(str)) {
case ENCINDEX_ASCII_8BIT:
case ENCINDEX_UTF_8:
case ENCINDEX_US_ASCII:
return true;
default:
return false;
}
}
#define TERM_LEN(str) (str_enc_fastpath(str) ? 1 : rb_enc_mbminlen(rb_enc_from_index(ENCODING_GET(str))))
#define TERM_FILL(ptr, termlen) do {\
char *const term_fill_ptr = (ptr);\
const int term_fill_len = (termlen);\
*term_fill_ptr = '\0';\
if (UNLIKELY(term_fill_len > 1))\
memset(term_fill_ptr, 0, term_fill_len);\
} while (0)
#define RESIZE_CAPA(str,capacity) do {\
const int termlen = TERM_LEN(str);\
RESIZE_CAPA_TERM(str,capacity,termlen);\
} while (0)
#define RESIZE_CAPA_TERM(str,capacity,termlen) do {\
if (STR_EMBED_P(str)) {\
if (str_embed_capa(str) < capacity + termlen) {\
char *const tmp = ALLOC_N(char, (size_t)(capacity) + (termlen));\
const long tlen = RSTRING_LEN(str);\
memcpy(tmp, RSTRING_PTR(str), tlen);\
RSTRING(str)->as.heap.ptr = tmp;\
RSTRING(str)->len = tlen;\
STR_SET_NOEMBED(str);\
RSTRING(str)->as.heap.aux.capa = (capacity);\
}\
}\
else {\
RUBY_ASSERT(!FL_TEST((str), STR_SHARED)); \
SIZED_REALLOC_N(RSTRING(str)->as.heap.ptr, char, \
(size_t)(capacity) + (termlen), STR_HEAP_SIZE(str)); \
RSTRING(str)->as.heap.aux.capa = (capacity);\
}\
} while (0)
#define STR_SET_SHARED(str, shared_str) do { \
if (!FL_TEST(str, STR_FAKESTR)) { \
RUBY_ASSERT(RSTRING_PTR(shared_str) <= RSTRING_PTR(str)); \
RUBY_ASSERT(RSTRING_PTR(str) <= RSTRING_PTR(shared_str) + RSTRING_LEN(shared_str)); \
RB_OBJ_WRITE((str), &RSTRING(str)->as.heap.aux.shared, (shared_str)); \
FL_SET((str), STR_SHARED); \
FL_SET((shared_str), STR_SHARED_ROOT); \
if (RBASIC_CLASS((shared_str)) == 0) /* for CoW-friendliness */ \
FL_SET_RAW((shared_str), STR_BORROWED); \
} \
} while (0)
#define STR_HEAP_PTR(str) (RSTRING(str)->as.heap.ptr)
#define STR_HEAP_SIZE(str) ((size_t)RSTRING(str)->as.heap.aux.capa + TERM_LEN(str))
/* TODO: include the terminator size in capa. */
#define STR_ENC_GET(str) get_encoding(str)
#if !defined SHARABLE_MIDDLE_SUBSTRING
# define SHARABLE_MIDDLE_SUBSTRING 0
#endif
#if !SHARABLE_MIDDLE_SUBSTRING
#define SHARABLE_SUBSTRING_P(beg, len, end) ((beg) + (len) == (end))
#else
#define SHARABLE_SUBSTRING_P(beg, len, end) 1
#endif
static inline long
str_embed_capa(VALUE str)
{
return rb_gc_obj_slot_size(str) - offsetof(struct RString, as.embed.ary);
}
bool
rb_str_reembeddable_p(VALUE str)
{
return !FL_TEST(str, STR_NOFREE|STR_SHARED_ROOT|STR_SHARED);
}
static inline size_t
rb_str_embed_size(long capa)
{
return offsetof(struct RString, as.embed.ary) + capa;
}
size_t
rb_str_size_as_embedded(VALUE str)
{
size_t real_size;
if (STR_EMBED_P(str)) {
real_size = rb_str_embed_size(RSTRING(str)->len) + TERM_LEN(str);
}
/* if the string is not currently embedded, but it can be embedded, how
* much space would it require */
else if (rb_str_reembeddable_p(str)) {
real_size = rb_str_embed_size(RSTRING(str)->as.heap.aux.capa) + TERM_LEN(str);
}
else {
real_size = sizeof(struct RString);
}
if (FL_TEST_RAW(str, STR_PRECOMPUTED_HASH)) {
real_size += sizeof(st_index_t);
}
return real_size;
}
static inline bool
STR_EMBEDDABLE_P(long len, long termlen)
{
return rb_gc_size_allocatable_p(rb_str_embed_size(len + termlen));
}
static VALUE str_replace_shared_without_enc(VALUE str2, VALUE str);
static VALUE str_new_frozen(VALUE klass, VALUE orig);
static VALUE str_new_frozen_buffer(VALUE klass, VALUE orig, int copy_encoding);
static VALUE str_new_static(VALUE klass, const char *ptr, long len, int encindex);
static VALUE str_new(VALUE klass, const char *ptr, long len);
static void str_make_independent_expand(VALUE str, long len, long expand, const int termlen);
static inline void str_modifiable(VALUE str);
static VALUE rb_str_downcase(int argc, VALUE *argv, VALUE str);
static inline VALUE str_alloc_embed(VALUE klass, size_t capa);
static inline void
str_make_independent(VALUE str)
{
long len = RSTRING_LEN(str);
int termlen = TERM_LEN(str);
str_make_independent_expand((str), len, 0L, termlen);
}
static inline int str_dependent_p(VALUE str);
void
rb_str_make_independent(VALUE str)
{
if (str_dependent_p(str)) {
str_make_independent(str);
}
}
void
rb_str_make_embedded(VALUE str)
{
RUBY_ASSERT(rb_str_reembeddable_p(str));
RUBY_ASSERT(!STR_EMBED_P(str));
char *buf = RSTRING(str)->as.heap.ptr;
long len = RSTRING(str)->len;
STR_SET_EMBED(str);
STR_SET_LEN(str, len);
if (len > 0) {
memcpy(RSTRING_PTR(str), buf, len);
ruby_xfree(buf);
}
TERM_FILL(RSTRING(str)->as.embed.ary + len, TERM_LEN(str));
}
void
rb_debug_rstring_null_ptr(const char *func)
{
fprintf(stderr, "%s is returning NULL!! "
"SIGSEGV is highly expected to follow immediately.\n"
"If you could reproduce, attach your debugger here, "
"and look at the passed string.\n",
func);
}
/* symbols for [up|down|swap]case/capitalize options */
static VALUE sym_ascii, sym_turkic, sym_lithuanian, sym_fold;
static rb_encoding *
get_encoding(VALUE str)
{
return rb_enc_from_index(ENCODING_GET(str));
}
static void
mustnot_broken(VALUE str)
{
if (is_broken_string(str)) {
rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(STR_ENC_GET(str)));
}
}
static void
mustnot_wchar(VALUE str)
{
rb_encoding *enc = STR_ENC_GET(str);
if (rb_enc_mbminlen(enc) > 1) {
rb_raise(rb_eArgError, "wide char encoding: %s", rb_enc_name(enc));
}
}
static int fstring_cmp(VALUE a, VALUE b);
static VALUE register_fstring(VALUE str, bool copy, bool precompute_hash);
const struct st_hash_type rb_fstring_hash_type = {
fstring_cmp,
rb_str_hash,
};
#define BARE_STRING_P(str) (!FL_ANY_RAW(str, FL_EXIVAR) && RBASIC_CLASS(str) == rb_cString)
static inline st_index_t
str_do_hash(VALUE str)
{
st_index_t h = rb_memhash((const void *)RSTRING_PTR(str), RSTRING_LEN(str));
int e = RSTRING_LEN(str) ? ENCODING_GET(str) : 0;
if (e && !is_ascii_string(str)) {
h = rb_hash_end(rb_hash_uint32(h, (uint32_t)e));
}
return h;
}
static VALUE
str_precompute_hash(VALUE str)
{
RUBY_ASSERT(!FL_TEST_RAW(str, STR_PRECOMPUTED_HASH));
RUBY_ASSERT(STR_EMBED_P(str));
#if RUBY_DEBUG
size_t used_bytes = (RSTRING_LEN(str) + TERM_LEN(str));
size_t free_bytes = str_embed_capa(str) - used_bytes;
RUBY_ASSERT(free_bytes >= sizeof(st_index_t));
#endif
st_index_t hash = str_do_hash(str);
memcpy(RSTRING_END(str) + TERM_LEN(str), &hash, sizeof(hash));
FL_SET(str, STR_PRECOMPUTED_HASH);
return str;
}
struct fstr_update_arg {
VALUE fstr;
bool copy;
bool precompute_hash;
};
static int
fstr_update_callback(st_data_t *key, st_data_t *value, st_data_t data, int existing)
{
struct fstr_update_arg *arg = (struct fstr_update_arg *)data;
VALUE str = (VALUE)*key;
if (existing) {
/* because of lazy sweep, str may be unmarked already and swept
* at next time */
if (rb_objspace_garbage_object_p(str)) {
arg->fstr = Qundef;
return ST_DELETE;
}
arg->fstr = str;
return ST_STOP;
}
else {
if (FL_TEST_RAW(str, STR_FAKESTR)) {
if (arg->copy) {
VALUE new_str;
long len = RSTRING_LEN(str);
long capa = len + sizeof(st_index_t);
int term_len = TERM_LEN(str);
if (arg->precompute_hash && STR_EMBEDDABLE_P(capa, term_len)) {
new_str = str_alloc_embed(rb_cString, capa + term_len);
memcpy(RSTRING_PTR(new_str), RSTRING_PTR(str), len);
STR_SET_LEN(new_str, RSTRING_LEN(str));
TERM_FILL(RSTRING_END(new_str), TERM_LEN(str));
rb_enc_copy(new_str, str);
str_precompute_hash(new_str);
}
else {
new_str = str_new(rb_cString, RSTRING(str)->as.heap.ptr, RSTRING(str)->len);
rb_enc_copy(new_str, str);
}
str = new_str;
}
else {
str = str_new_static(rb_cString, RSTRING(str)->as.heap.ptr,
RSTRING(str)->len,
ENCODING_GET(str));
}
OBJ_FREEZE(str);
}
else {
if (!OBJ_FROZEN(str) || CHILLED_STRING_P(str)) {
str = str_new_frozen(rb_cString, str);
}
if (STR_SHARED_P(str)) { /* str should not be shared */
/* shared substring */
str_make_independent(str);
RUBY_ASSERT(OBJ_FROZEN(str));
}
if (!BARE_STRING_P(str)) {
str = str_new_frozen(rb_cString, str);
}
}
RBASIC(str)->flags |= RSTRING_FSTR;
*key = *value = arg->fstr = str;
return ST_CONTINUE;
}
}
VALUE
rb_fstring(VALUE str)
{
VALUE fstr;
int bare;
Check_Type(str, T_STRING);
if (FL_TEST(str, RSTRING_FSTR))
return str;
bare = BARE_STRING_P(str);
if (!bare) {
if (STR_EMBED_P(str)) {
OBJ_FREEZE(str);
return str;
}
if (FL_TEST_RAW(str, STR_SHARED_ROOT | STR_SHARED) == STR_SHARED_ROOT) {
RUBY_ASSERT(OBJ_FROZEN(str));
return str;
}
}
if (!FL_TEST_RAW(str, FL_FREEZE | STR_NOFREE | STR_CHILLED))
rb_str_resize(str, RSTRING_LEN(str));
fstr = register_fstring(str, false, false);
if (!bare) {
str_replace_shared_without_enc(str, fstr);
OBJ_FREEZE(str);
return str;
}
return fstr;
}
static VALUE
register_fstring(VALUE str, bool copy, bool precompute_hash)
{
struct fstr_update_arg args = {
.copy = copy,
.precompute_hash = precompute_hash
};
RB_VM_LOCK_ENTER();
{
st_table *frozen_strings = rb_vm_fstring_table();
do {
args.fstr = str;
st_update(frozen_strings, (st_data_t)str, fstr_update_callback, (st_data_t)&args);
} while (UNDEF_P(args.fstr));
}
RB_VM_LOCK_LEAVE();
RUBY_ASSERT(OBJ_FROZEN(args.fstr));
RUBY_ASSERT(!FL_TEST_RAW(args.fstr, STR_FAKESTR));
RUBY_ASSERT(!FL_TEST_RAW(args.fstr, FL_EXIVAR));
RUBY_ASSERT(RBASIC_CLASS(args.fstr) == rb_cString);
return args.fstr;
}
static VALUE
setup_fake_str(struct RString *fake_str, const char *name, long len, int encidx)
{
fake_str->basic.flags = T_STRING|RSTRING_NOEMBED|STR_NOFREE|STR_FAKESTR;
/* SHARED to be allocated by the callback */
if (!name) {
RUBY_ASSERT_ALWAYS(len == 0);
name = "";
}
ENCODING_SET_INLINED((VALUE)fake_str, encidx);
RBASIC_SET_CLASS_RAW((VALUE)fake_str, rb_cString);
fake_str->len = len;
fake_str->as.heap.ptr = (char *)name;
fake_str->as.heap.aux.capa = len;
return (VALUE)fake_str;
}
/*
* set up a fake string which refers a static string literal.
*/
VALUE
rb_setup_fake_str(struct RString *fake_str, const char *name, long len, rb_encoding *enc)
{
return setup_fake_str(fake_str, name, len, rb_enc_to_index(enc));
}
/*
* rb_fstring_new and rb_fstring_cstr family create or lookup a frozen
* shared string which refers a static string literal. `ptr` must
* point a constant string.
*/
VALUE
rb_fstring_new(const char *ptr, long len)
{
struct RString fake_str;
return register_fstring(setup_fake_str(&fake_str, ptr, len, ENCINDEX_US_ASCII), false, false);
}
VALUE
rb_fstring_enc_new(const char *ptr, long len, rb_encoding *enc)
{
struct RString fake_str;
return register_fstring(rb_setup_fake_str(&fake_str, ptr, len, enc), false, false);
}
VALUE
rb_fstring_cstr(const char *ptr)
{
return rb_fstring_new(ptr, strlen(ptr));
}
static int
fstring_set_class_i(st_data_t key, st_data_t val, st_data_t arg)
{
RBASIC_SET_CLASS((VALUE)key, (VALUE)arg);
return ST_CONTINUE;
}
static int
fstring_cmp(VALUE a, VALUE b)
{
long alen, blen;
const char *aptr, *bptr;
RSTRING_GETMEM(a, aptr, alen);
RSTRING_GETMEM(b, bptr, blen);
return (alen != blen ||
ENCODING_GET(a) != ENCODING_GET(b) ||
memcmp(aptr, bptr, alen) != 0);
}
static inline int
single_byte_optimizable(VALUE str)
{
rb_encoding *enc;
/* Conservative. It may be ENC_CODERANGE_UNKNOWN. */
if (ENC_CODERANGE(str) == ENC_CODERANGE_7BIT)
return 1;
enc = STR_ENC_GET(str);
if (rb_enc_mbmaxlen(enc) == 1)
return 1;
/* Conservative. Possibly single byte.
* "\xa1" in Shift_JIS for example. */
return 0;
}
VALUE rb_fs;
static inline const char *
search_nonascii(const char *p, const char *e)
{
const uintptr_t *s, *t;
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
# if SIZEOF_UINTPTR_T == 8
# define NONASCII_MASK UINT64_C(0x8080808080808080)
# elif SIZEOF_UINTPTR_T == 4
# define NONASCII_MASK UINT32_C(0x80808080)
# else
# error "don't know what to do."
# endif
#else
# if SIZEOF_UINTPTR_T == 8
# define NONASCII_MASK ((uintptr_t)0x80808080UL << 32 | (uintptr_t)0x80808080UL)
# elif SIZEOF_UINTPTR_T == 4
# define NONASCII_MASK 0x80808080UL /* or...? */
# else
# error "don't know what to do."
# endif
#endif
if (UNALIGNED_WORD_ACCESS || e - p >= SIZEOF_VOIDP) {
#if !UNALIGNED_WORD_ACCESS
if ((uintptr_t)p % SIZEOF_VOIDP) {
int l = SIZEOF_VOIDP - (uintptr_t)p % SIZEOF_VOIDP;
p += l;
switch (l) {
default: UNREACHABLE;
#if SIZEOF_VOIDP > 4
case 7: if (p[-7]&0x80) return p-7;
case 6: if (p[-6]&0x80) return p-6;
case 5: if (p[-5]&0x80) return p-5;
case 4: if (p[-4]&0x80) return p-4;
#endif
case 3: if (p[-3]&0x80) return p-3;
case 2: if (p[-2]&0x80) return p-2;
case 1: if (p[-1]&0x80) return p-1;
case 0: break;
}
}
#endif
#if defined(HAVE_BUILTIN___BUILTIN_ASSUME_ALIGNED) &&! UNALIGNED_WORD_ACCESS
#define aligned_ptr(value) \
__builtin_assume_aligned((value), sizeof(uintptr_t))
#else
#define aligned_ptr(value) (uintptr_t *)(value)
#endif
s = aligned_ptr(p);
t = (uintptr_t *)(e - (SIZEOF_VOIDP-1));
#undef aligned_ptr
for (;s < t; s++) {
if (*s & NONASCII_MASK) {
#ifdef WORDS_BIGENDIAN
return (const char *)s + (nlz_intptr(*s&NONASCII_MASK)>>3);
#else
return (const char *)s + (ntz_intptr(*s&NONASCII_MASK)>>3);
#endif
}
}
p = (const char *)s;
}
switch (e - p) {
default: UNREACHABLE;
#if SIZEOF_VOIDP > 4
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;
case 0: return NULL;
}
}
static int
coderange_scan(const char *p, long len, rb_encoding *enc)
{
const char *e = p + len;
if (rb_enc_to_index(enc) == rb_ascii8bit_encindex()) {
/* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
p = search_nonascii(p, e);
return p ? ENC_CODERANGE_VALID : ENC_CODERANGE_7BIT;
}
if (rb_enc_asciicompat(enc)) {
p = search_nonascii(p, e);
if (!p) return ENC_CODERANGE_7BIT;
for (;;) {
int ret = rb_enc_precise_mbclen(p, e, enc);
if (!MBCLEN_CHARFOUND_P(ret)) return ENC_CODERANGE_BROKEN;
p += MBCLEN_CHARFOUND_LEN(ret);
if (p == e) break;
p = search_nonascii(p, e);
if (!p) break;
}
}
else {
while (p < e) {
int ret = rb_enc_precise_mbclen(p, e, enc);
if (!MBCLEN_CHARFOUND_P(ret)) return ENC_CODERANGE_BROKEN;
p += MBCLEN_CHARFOUND_LEN(ret);
}
}
return ENC_CODERANGE_VALID;
}
long
rb_str_coderange_scan_restartable(const char *s, const char *e, rb_encoding *enc, int *cr)
{
const char *p = s;
if (*cr == ENC_CODERANGE_BROKEN)
return e - s;
if (rb_enc_to_index(enc) == rb_ascii8bit_encindex()) {
/* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
if (*cr == ENC_CODERANGE_VALID) return e - s;
p = search_nonascii(p, e);
*cr = p ? ENC_CODERANGE_VALID : ENC_CODERANGE_7BIT;
return e - s;
}
else if (rb_enc_asciicompat(enc)) {
p = search_nonascii(p, e);
if (!p) {
if (*cr != ENC_CODERANGE_VALID) *cr = ENC_CODERANGE_7BIT;
return e - s;
}
for (;;) {
int ret = rb_enc_precise_mbclen(p, e, enc);
if (!MBCLEN_CHARFOUND_P(ret)) {
*cr = MBCLEN_INVALID_P(ret) ? ENC_CODERANGE_BROKEN: ENC_CODERANGE_UNKNOWN;
return p - s;
}
p += MBCLEN_CHARFOUND_LEN(ret);
if (p == e) break;
p = search_nonascii(p, e);
if (!p) break;
}
}
else {
while (p < e) {
int ret = rb_enc_precise_mbclen(p, e, enc);
if (!MBCLEN_CHARFOUND_P(ret)) {
*cr = MBCLEN_INVALID_P(ret) ? ENC_CODERANGE_BROKEN: ENC_CODERANGE_UNKNOWN;
return p - s;
}
p += MBCLEN_CHARFOUND_LEN(ret);
}
}
*cr = ENC_CODERANGE_VALID;
return e - s;
}
static inline void
str_enc_copy(VALUE str1, VALUE str2)
{
rb_enc_set_index(str1, ENCODING_GET(str2));
}
/* Like str_enc_copy, but does not check frozen status of str1.
* You should use this only if you're certain that str1 is not frozen. */
static inline void
str_enc_copy_direct(VALUE str1, VALUE str2)
{
int inlined_encoding = RB_ENCODING_GET_INLINED(str2);
if (inlined_encoding == ENCODING_INLINE_MAX) {
rb_enc_set_index(str1, rb_enc_get_index(str2));
}
else {
ENCODING_SET_INLINED(str1, inlined_encoding);
}
}
static void
rb_enc_cr_str_copy_for_substr(VALUE dest, VALUE src)
{
/* this function is designed for copying encoding and coderange
* from src to new string "dest" which is made from the part of src.
*/
str_enc_copy(dest, src);
if (RSTRING_LEN(dest) == 0) {
if (!rb_enc_asciicompat(STR_ENC_GET(src)))
ENC_CODERANGE_SET(dest, ENC_CODERANGE_VALID);
else
ENC_CODERANGE_SET(dest, ENC_CODERANGE_7BIT);
return;
}
switch (ENC_CODERANGE(src)) {
case ENC_CODERANGE_7BIT:
ENC_CODERANGE_SET(dest, ENC_CODERANGE_7BIT);
break;
case ENC_CODERANGE_VALID:
if (!rb_enc_asciicompat(STR_ENC_GET(src)) ||
search_nonascii(RSTRING_PTR(dest), RSTRING_END(dest)))
ENC_CODERANGE_SET(dest, ENC_CODERANGE_VALID);
else
ENC_CODERANGE_SET(dest, ENC_CODERANGE_7BIT);
break;
default:
break;
}
}
static void
rb_enc_cr_str_exact_copy(VALUE dest, VALUE src)
{
str_enc_copy(dest, src);
ENC_CODERANGE_SET(dest, ENC_CODERANGE(src));
}
static int
enc_coderange_scan(VALUE str, rb_encoding *enc)
{
return coderange_scan(RSTRING_PTR(str), RSTRING_LEN(str), enc);
}
int
rb_enc_str_coderange_scan(VALUE str, rb_encoding *enc)
{
return enc_coderange_scan(str, enc);
}
int
rb_enc_str_coderange(VALUE str)
{
int cr = ENC_CODERANGE(str);
if (cr == ENC_CODERANGE_UNKNOWN) {
cr = enc_coderange_scan(str, get_encoding(str));
ENC_CODERANGE_SET(str, cr);
}
return cr;
}
int
rb_enc_str_asciionly_p(VALUE str)
{
rb_encoding *enc = STR_ENC_GET(str);
if (!rb_enc_asciicompat(enc))
return FALSE;
else if (is_ascii_string(str))
return TRUE;
return FALSE;
}
static inline void
str_mod_check(VALUE s, const char *p, long len)
{
if (RSTRING_PTR(s) != p || RSTRING_LEN(s) != len){
rb_raise(rb_eRuntimeError, "string modified");
}
}
static size_t
str_capacity(VALUE str, const int termlen)
{
if (STR_EMBED_P(str)) {
return str_embed_capa(str) - termlen;
}
else if (FL_TEST(str, STR_SHARED|STR_NOFREE)) {
return RSTRING(str)->len;
}
else {
return RSTRING(str)->as.heap.aux.capa;
}
}
size_t
rb_str_capacity(VALUE str)
{
return str_capacity(str, TERM_LEN(str));
}
static inline void
must_not_null(const char *ptr)
{
if (!ptr) {
rb_raise(rb_eArgError, "NULL pointer given");
}
}
static inline VALUE
str_alloc_embed(VALUE klass, size_t capa)
{
size_t size = rb_str_embed_size(capa);
RUBY_ASSERT(size > 0);
RUBY_ASSERT(rb_gc_size_allocatable_p(size));
NEWOBJ_OF(str, struct RString, klass,
T_STRING | (RGENGC_WB_PROTECTED_STRING ? FL_WB_PROTECTED : 0), size, 0);
return (VALUE)str;
}
static inline VALUE
str_alloc_heap(VALUE klass)
{
NEWOBJ_OF(str, struct RString, klass,
T_STRING | STR_NOEMBED | (RGENGC_WB_PROTECTED_STRING ? FL_WB_PROTECTED : 0), sizeof(struct RString), 0);
return (VALUE)str;
}
static inline VALUE
empty_str_alloc(VALUE klass)
{
RUBY_DTRACE_CREATE_HOOK(STRING, 0);
VALUE str = str_alloc_embed(klass, 0);
memset(RSTRING(str)->as.embed.ary, 0, str_embed_capa(str));
ENC_CODERANGE_SET(str, ENC_CODERANGE_7BIT);
return str;
}
static VALUE
str_new0(VALUE klass, const char *ptr, long len, int termlen)
{
VALUE str;
if (len < 0) {
rb_raise(rb_eArgError, "negative string size (or size too big)");
}
RUBY_DTRACE_CREATE_HOOK(STRING, len);
if (STR_EMBEDDABLE_P(len, termlen)) {
str = str_alloc_embed(klass, len + termlen);
if (len == 0) {
ENC_CODERANGE_SET(str, ENC_CODERANGE_7BIT);
}
}
else {
str = str_alloc_heap(klass);
RSTRING(str)->as.heap.aux.capa = len;
/* :FIXME: @shyouhei guesses `len + termlen` is guaranteed to never
* integer overflow. If we can STATIC_ASSERT that, the following
* mul_add_mul can be reverted to a simple ALLOC_N. */
RSTRING(str)->as.heap.ptr =
rb_xmalloc_mul_add_mul(sizeof(char), len, sizeof(char), termlen);
}
if (ptr) {
memcpy(RSTRING_PTR(str), ptr, len);
}
STR_SET_LEN(str, len);
TERM_FILL(RSTRING_PTR(str) + len, termlen);
return str;
}
static VALUE
str_new(VALUE klass, const char *ptr, long len)
{
return str_new0(klass, ptr, len, 1);
}
VALUE
rb_str_new(const char *ptr, long len)
{
return str_new(rb_cString, ptr, len);
}
VALUE
rb_usascii_str_new(const char *ptr, long len)
{
VALUE str = rb_str_new(ptr, len);
ENCODING_CODERANGE_SET(str, rb_usascii_encindex(), ENC_CODERANGE_7BIT);
return str;
}
VALUE
rb_utf8_str_new(const char *ptr, long len)
{
VALUE str = str_new(rb_cString, ptr, len);
rb_enc_associate_index(str, rb_utf8_encindex());
return str;
}
VALUE
rb_enc_str_new(const char *ptr, long len, rb_encoding *enc)