Ruby 4.1.0dev (2026-11-19 revision 2e6b197f6849401b2c25894b5b8ac39ac3c2558e)
string.c (2e6b197f6849401b2c25894b5b8ac39ac3c2558e)
1/**********************************************************************
2
3 string.c -
4
5 $Author$
6 created at: Mon Aug 9 17:12:58 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9 Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10 Copyright (C) 2000 Information-technology Promotion Agency, Japan
11
12**********************************************************************/
13
14#include "ruby/internal/config.h"
15
16#include <ctype.h>
17#include <errno.h>
18#include <math.h>
19
20#ifdef HAVE_UNISTD_H
21# include <unistd.h>
22#endif
23
24#include "debug_counter.h"
25#include "encindex.h"
26#include "id.h"
27#include "internal.h"
28#include "internal/array.h"
29#include "internal/bits.h"
30#include "internal/compar.h"
31#include "internal/compilers.h"
32#include "internal/concurrent_set.h"
33#include "internal/encoding.h"
34#include "internal/error.h"
35#include "internal/gc.h"
36#include "internal/hash.h"
37#include "internal/numeric.h"
38#include "internal/object.h"
39#include "internal/proc.h"
40#include "internal/re.h"
41#include "internal/sanitizers.h"
42#include "internal/simd.h"
43#include "internal/string.h"
44#include "internal/transcode.h"
45#include "probes.h"
46#include "ruby/encoding.h"
47#include "ruby/re.h"
48#include "ruby/thread.h"
49#include "ruby/util.h"
50#include "ruby/ractor.h"
51#include "ruby_assert.h"
52#include "shape.h"
53#include "vm_core.h"
54#include "vm_sync.h"
55#include "zjit.h"
57
58#if defined HAVE_CRYPT_R
59# if defined HAVE_CRYPT_H
60# include <crypt.h>
61# endif
62#elif !defined HAVE_CRYPT
63# include "missing/crypt.h"
64# define HAVE_CRYPT_R 1
65#endif
66
67#undef rb_str_new
68#undef rb_usascii_str_new
69#undef rb_utf8_str_new
70#undef rb_enc_str_new
71#undef rb_str_new_cstr
72#undef rb_usascii_str_new_cstr
73#undef rb_utf8_str_new_cstr
74#undef rb_enc_str_new_cstr
75#undef rb_external_str_new_cstr
76#undef rb_locale_str_new_cstr
77#undef rb_str_dup_frozen
78#undef rb_str_buf_new_cstr
79#undef rb_str_buf_cat
80#undef rb_str_buf_cat2
81#undef rb_str_cat2
82#undef rb_str_cat_cstr
83#undef rb_fstring_cstr
84
87
88/* Flags of RString
89 *
90 * 0: STR_SHARED (equal to ELTS_SHARED)
91 * The string is shared. The buffer this string points to is owned by
92 * another string (the shared root).
93 * 1: RSTRING_NOEMBED
94 * The string is not embedded. When a string is embedded, the contents
95 * follow the header. When a string is not embedded, the contents is
96 * on a separately allocated buffer.
97 * 2: STR_CHILLED (will be frozen in a future version)
98 * The string was allocated as a literal in a file without an explicit `frozen_string_literal` comment.
99 * It emits a deprecation warning when mutated for the first time.
100 * 4: STR_PRECOMPUTED_HASH
101 * The string is embedded and has its precomputed hashcode stored
102 * after the terminator.
103 * 5: STR_SHARED_ROOT
104 * Other strings may point to the contents of this string. When this
105 * flag is set, STR_SHARED must not be set.
106 * 6: STR_BORROWED
107 * When RSTRING_NOEMBED is set and klass is 0, this string is unsafe
108 * to be unshared by rb_str_tmp_frozen_release.
109 * 7: STR_TMPLOCK
110 * The pointer to the buffer is passed to a system call such as
111 * read(2). Any modification and realloc is prohibited.
112 * 8-9: ENC_CODERANGE
113 * Stores the coderange of the string.
114 * 10-16: ENCODING
115 * Stores the encoding of the string.
116 * 17: RSTRING_FSTR
117 * The string is a fstring. The string is deduplicated in the fstring
118 * table.
119 * 18: STR_NOFREE
120 * Do not free this string's buffer when the string is reclaimed
121 * by the garbage collector. Used for when the string buffer is a C
122 * string literal.
123 * 19: STR_FAKESTR
124 * The string is not allocated or managed by the garbage collector.
125 * Typically, the string object header (struct RString) is temporarily
126 * allocated on C stack.
127 */
128
129#define RUBY_MAX_CHAR_LEN 16
130#define STR_PRECOMPUTED_HASH FL_USER4
131#define STR_SHARED_ROOT FL_USER5
132#define STR_BORROWED FL_USER6
133#define STR_TMPLOCK FL_USER7
134#define STR_NOFREE FL_USER18
135
136#define STR_SET_NOEMBED(str) do {\
137 FL_SET((str), STR_NOEMBED);\
138 FL_UNSET((str), STR_SHARED | STR_SHARED_ROOT | STR_BORROWED);\
139} while (0)
140#define STR_SET_EMBED(str) FL_UNSET((str), STR_NOEMBED | STR_SHARED | STR_NOFREE)
141
142#define STR_SET_LEN(str, n) do { \
143 RSTRING(str)->len = (n); \
144} while (0)
145
146#define TERM_LEN(str) (rb_str_enc_fastpath(str) ? 1 : rb_enc_mbminlen(rb_enc_from_index(ENCODING_GET(str))))
147#define TERM_FILL(ptr, termlen) do {\
148 char *const term_fill_ptr = (ptr);\
149 const int term_fill_len = (termlen);\
150 *term_fill_ptr = '\0';\
151 if (UNLIKELY(term_fill_len > 1))\
152 memset(term_fill_ptr, 0, term_fill_len);\
153} while (0)
154
155#define RESIZE_CAPA(str,capacity) do {\
156 const int termlen = TERM_LEN(str);\
157 RESIZE_CAPA_TERM(str,capacity,termlen);\
158} while (0)
159#define RESIZE_CAPA_TERM(str,capacity,termlen) do {\
160 if (STR_EMBED_P(str)) {\
161 if (str_embed_capa(str) < capacity + termlen) {\
162 char *const tmp = ALLOC_N(char, (size_t)(capacity) + (termlen));\
163 const rb_len_t tlen = RSTRING_LEN(str);\
164 memcpy(tmp, RSTRING_PTR(str), str_embed_capa(str));\
165 RSTRING(str)->as.heap.ptr = tmp;\
166 RSTRING(str)->len = tlen;\
167 STR_SET_NOEMBED(str);\
168 RSTRING(str)->as.heap.aux.capa = (capacity);\
169 }\
170 }\
171 else {\
172 RUBY_ASSERT(!FL_TEST((str), STR_SHARED)); \
173 SIZED_REALLOC_N(RSTRING(str)->as.heap.ptr, char, \
174 (size_t)(capacity) + (termlen), STR_HEAP_SIZE(str)); \
175 RSTRING(str)->as.heap.aux.capa = (capacity);\
176 }\
177} while (0)
178
179#define STR_SET_SHARED(str, shared_str) do { \
180 if (!FL_TEST(str, STR_FAKESTR)) { \
181 RUBY_ASSERT(RSTRING_PTR(shared_str) <= RSTRING_PTR(str)); \
182 RUBY_ASSERT(RSTRING_PTR(str) <= RSTRING_PTR(shared_str) + RSTRING_LEN(shared_str)); \
183 RB_OBJ_WRITE((str), &RSTRING(str)->as.heap.aux.shared, (shared_str)); \
184 FL_SET((str), STR_SHARED); \
185 rb_gc_register_pinning_obj(str); \
186 FL_SET((shared_str), STR_SHARED_ROOT); \
187 if (RBASIC_CLASS((shared_str)) == 0) /* for CoW-friendliness */ \
188 FL_SET_RAW((shared_str), STR_BORROWED); \
189 } \
190} while (0)
191
192#define STR_HEAP_PTR(str) (RSTRING(str)->as.heap.ptr)
193#define STR_HEAP_SIZE(str) ((size_t)RSTRING(str)->as.heap.aux.capa + TERM_LEN(str))
194/* TODO: include the terminator size in capa. */
195
196#define STR_ENC_GET(str) get_encoding(str)
197
198static inline bool
199zero_filled(const char *s, int n)
200{
201 for (; n > 0; --n) {
202 if (*s++) return false;
203 }
204 return true;
205}
206
207#if !defined SHARABLE_MIDDLE_SUBSTRING
208# define SHARABLE_MIDDLE_SUBSTRING 0
209#endif
210
211static inline bool
212SHARABLE_SUBSTRING_P(VALUE str, rb_len_t beg, rb_len_t len)
213{
214#if SHARABLE_MIDDLE_SUBSTRING
215 return true;
216#else
217 rb_len_t end = beg + len;
218 rb_len_t source_len = RSTRING_LEN(str);
219 return end == source_len || zero_filled(RSTRING_PTR(str) + end, TERM_LEN(str));
220#endif
221}
222
223static inline rb_len_t
224str_embed_capa(VALUE str)
225{
226 return rb_obj_shape_slot_size(str) - offsetof(struct RString, as.embed.ary);
227}
228
229bool
230rb_str_reembeddable_p(VALUE str)
231{
232 return !FL_TEST(str, STR_NOFREE|STR_SHARED_ROOT|STR_SHARED);
233}
234
235/* True when other strings read this string's bytes out of its own slot, so the slot
236 * contents must stay valid for as long as the object does. */
237bool
238rb_str_embedded_shared_root_p(VALUE str)
239{
240 return STR_EMBED_P(str) && FL_TEST(str, STR_SHARED_ROOT);
241}
242
243static inline size_t
244rb_str_embed_size(rb_len_t capa, rb_len_t termlen)
245{
246 size_t size = offsetof(struct RString, as.embed.ary) + capa + termlen;
247 if (size < sizeof(struct RString)) size = sizeof(struct RString);
248 return size;
249}
250
251size_t
252rb_str_size_as_embedded(VALUE str)
253{
254 size_t real_size;
255 if (STR_EMBED_P(str)) {
256 size_t capa = RSTRING(str)->len;
257 if (FL_TEST_RAW(str, STR_PRECOMPUTED_HASH)) capa += sizeof(st_index_t);
258
259 real_size = rb_str_embed_size(capa, TERM_LEN(str));
260 }
261 /* if the string is not currently embedded, but it can be embedded, how
262 * much space would it require */
263 else if (rb_str_reembeddable_p(str)) {
264 size_t capa = RSTRING(str)->as.heap.aux.capa;
265 if (FL_TEST_RAW(str, STR_PRECOMPUTED_HASH)) capa += sizeof(st_index_t);
266
267 real_size = rb_str_embed_size(capa, TERM_LEN(str));
268 }
269 else {
270 real_size = sizeof(struct RString);
271 }
272
273 return real_size;
274}
275
276static inline bool
277STR_EMBEDDABLE_P(rb_len_t len, rb_len_t termlen)
278{
279 RUBY_ASSERT(len >= 0, "negative length: %" PRIdLEN, len);
280 RUBY_ASSERT(termlen > 0, "non-positive termlen: %" PRIdLEN, termlen);
281 return rb_gc_size_allocatable_p(rb_str_embed_size(len, termlen));
282}
283
284/* Substrings and duplicated strings that need a slot larger than this are shared
285 * instead of copied. Larger slots hold fewer objects per page and trigger GC
286 * more often, which outweighs the copy they save; see [Feature #22186] for the
287 * benchmarks. */
288#define STR_COPY_MAX_EMBED_SIZE 256
289
290static VALUE str_replace_shared_without_enc(VALUE str2, VALUE str);
291static VALUE str_new_frozen(VALUE klass, VALUE orig);
292static VALUE str_new_frozen_buffer(VALUE klass, VALUE orig, int copy_encoding);
293static VALUE str_new_static(VALUE klass, const char *ptr, rb_len_t len, int encindex);
294static VALUE str_new(VALUE klass, const char *ptr, rb_len_t len);
295static void str_make_independent_expand(VALUE str, rb_len_t len, rb_len_t expand, const int termlen);
296static inline void str_modifiable(VALUE str);
297static VALUE rb_str_downcase(int argc, VALUE *argv, VALUE str);
298static inline VALUE str_alloc_embed(VALUE klass, size_t capa);
299
300static inline void
301str_make_independent(VALUE str)
302{
303 rb_len_t len = RSTRING_LEN(str);
304 int termlen = TERM_LEN(str);
305 str_make_independent_expand((str), len, 0L, termlen);
306}
307
308static inline int str_dependent_p(VALUE str);
309
310void
311rb_str_make_independent(VALUE str)
312{
313 if (str_dependent_p(str)) {
314 str_make_independent(str);
315 }
316}
317
318void
319rb_str_make_embedded(VALUE str)
320{
321 RUBY_ASSERT(rb_str_reembeddable_p(str));
322 RUBY_ASSERT(!STR_EMBED_P(str));
323
324 int termlen = TERM_LEN(str);
325 char *buf = RSTRING(str)->as.heap.ptr;
326 rb_len_t old_capa = RSTRING(str)->as.heap.aux.capa + termlen;
327 rb_len_t len = RSTRING(str)->len;
328
329 STR_SET_EMBED(str);
330 STR_SET_LEN(str, len);
331
332 if (len > 0) {
333 memcpy(RSTRING_PTR(str), buf, len);
334 SIZED_FREE_N(buf, old_capa);
335 }
336
337 TERM_FILL(RSTRING(str)->as.embed.ary + len, termlen);
338}
339
340/* Converts an embedded string into a heap allocated string. The buffer of an
341 * embedded string lives inside a GC heap slot, which the compactor may mprotect
342 * to implement its read barrier. A pointer to such a buffer must not be given
343 * to a system call without the GVL. */
344void
345rb_str_make_heap(VALUE str)
346{
347 if (STR_EMBED_P(str)) {
348 const int termlen = TERM_LEN(str);
349 rb_len_t len = RSTRING_LEN(str);
350 str_make_independent_expand(str, len, (rb_len_t)rb_str_capacity(str) - len, termlen);
351 }
352}
353
354void
355rb_debug_rstring_null_ptr(const char *func)
356{
357 fprintf(stderr, "%s is returning NULL!! "
358 "SIGSEGV is highly expected to follow immediately.\n"
359 "If you could reproduce, attach your debugger here, "
360 "and look at the passed string.\n",
361 func);
362}
363
364/* symbols for [up|down|swap]case/capitalize options */
365static VALUE sym_ascii, sym_turkic, sym_lithuanian, sym_fold;
366
367static rb_encoding *
368get_encoding(VALUE str)
369{
370 return rb_enc_from_index(ENCODING_GET(str));
371}
372
373void
374rb_str_invalid_byte_sequence(VALUE str)
375{
376 rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(STR_ENC_GET(str)));
377}
378
379void
380rb_str_unexpect_wchar(VALUE str)
381{
382 rb_raise(rb_eArgError, "wide char encoding: %s", rb_enc_name(STR_ENC_GET(str)));
383}
384
385static VALUE register_fstring(VALUE str, bool copy, bool force_precompute_hash);
386
387#if SIZEOF_RB_LEN_T == SIZEOF_VOIDP
388#define PRECOMPUTED_FAKESTR_HASH 1
389#else
390#endif
391
392static inline bool
393BARE_STRING_P(VALUE str)
394{
395 return RBASIC_CLASS(str) == rb_cString && !rb_obj_shape_has_ivars(str);
396}
397
398static inline st_index_t
399str_do_hash(VALUE str)
400{
401 st_index_t h = rb_memhash((const void *)RSTRING_PTR(str), RSTRING_LEN(str));
402 int e = RSTRING_LEN(str) ? ENCODING_GET(str) : 0;
403 if (e && !is_ascii_string(str)) {
404 h = rb_hash_end(rb_hash_uint32(h, (uint32_t)e));
405 }
406 return h;
407}
408
409static VALUE
410str_store_precomputed_hash(VALUE str, st_index_t hash)
411{
412 RUBY_ASSERT(!FL_TEST_RAW(str, STR_PRECOMPUTED_HASH));
413 RUBY_ASSERT(STR_EMBED_P(str));
414
415#if RUBY_DEBUG
416 size_t used_bytes = (RSTRING_LEN(str) + TERM_LEN(str));
417 size_t free_bytes = str_embed_capa(str) - used_bytes;
418 RUBY_ASSERT(free_bytes >= sizeof(st_index_t));
419#endif
420
421 memcpy(RSTRING_END(str) + TERM_LEN(str), &hash, sizeof(hash));
422
423 FL_SET(str, STR_PRECOMPUTED_HASH);
424
425 return str;
426}
427
428VALUE
429rb_fstring(VALUE str)
430{
431 VALUE fstr;
432 int bare;
433
434 Check_Type(str, T_STRING);
435
436 if (FL_TEST(str, RSTRING_FSTR))
437 return str;
438
439 bare = BARE_STRING_P(str);
440 if (!bare) {
441 if (STR_EMBED_P(str)) {
442 OBJ_FREEZE(str);
443 return str;
444 }
445
446 if (FL_TEST_RAW(str, STR_SHARED_ROOT | STR_SHARED) == STR_SHARED_ROOT) {
448 return str;
449 }
450 }
451
452 if (!FL_TEST_RAW(str, FL_FREEZE | STR_NOFREE | STR_CHILLED))
453 rb_str_resize(str, RSTRING_LEN(str));
454
455 fstr = register_fstring(str, false, false);
456
457 if (!bare) {
458 str_replace_shared_without_enc(str, fstr);
459 OBJ_FREEZE(str);
460 return str;
461 }
462 return fstr;
463}
464
465static VALUE fstring_table_obj;
466
467static VALUE
468fstring_concurrent_set_hash(VALUE str)
469{
470#ifdef PRECOMPUTED_FAKESTR_HASH
471 st_index_t h;
472 if (FL_TEST_RAW(str, STR_FAKESTR)) {
473 // register_fstring precomputes the hash and stores it in capa for fake strings
474 h = (st_index_t)RSTRING(str)->as.heap.aux.capa;
475 }
476 else {
477 h = rb_str_hash(str);
478 }
479 // rb_str_hash doesn't include the encoding for ascii only strings, so
480 // we add it to avoid common collisions between `:sym.name` (ASCII) and `"sym"` (UTF-8)
481 return (VALUE)rb_hash_end(rb_hash_uint32(h, (uint32_t)ENCODING_GET_INLINED(str)));
482#else
483 return (VALUE)rb_str_hash(str);
484#endif
485}
486
487static bool
488fstring_concurrent_set_cmp(VALUE a, VALUE b)
489{
490 rb_len_t alen, blen;
491 const char *aptr, *bptr;
492
495
496 RSTRING_GETMEM(a, aptr, alen);
497 RSTRING_GETMEM(b, bptr, blen);
498 return (alen == blen &&
499 ENCODING_GET(a) == ENCODING_GET(b) &&
500 memcmp(aptr, bptr, alen) == 0);
501}
502
504 bool copy;
505 bool force_precompute_hash;
506};
507
508static VALUE
509fstring_concurrent_set_create(VALUE str, void *data)
510{
511 struct fstr_create_arg *arg = data;
512
513 // Unless the string is empty or binary, its coderange has been precomputed.
514 int coderange = ENC_CODERANGE(str);
515
516 if (FL_TEST_RAW(str, STR_FAKESTR)) {
517 if (arg->copy) {
518 VALUE new_str;
519 rb_len_t len = RSTRING_LEN(str);
520 rb_len_t capa = len + sizeof(st_index_t);
521 int term_len = TERM_LEN(str);
522
523 if (arg->force_precompute_hash && STR_EMBEDDABLE_P(capa, term_len)) {
524 new_str = str_alloc_embed(rb_cString, capa + term_len);
525 memcpy(RSTRING_PTR(new_str), RSTRING_PTR(str), len);
526 STR_SET_LEN(new_str, RSTRING_LEN(str));
527 TERM_FILL(RSTRING_END(new_str), TERM_LEN(str));
528 rb_enc_copy(new_str, str);
529 str_store_precomputed_hash(new_str, str_do_hash(str));
530 }
531 else {
532 new_str = str_new(rb_cString, RSTRING(str)->as.heap.ptr, RSTRING(str)->len);
533 rb_enc_copy(new_str, str);
534#ifdef PRECOMPUTED_FAKESTR_HASH
535 if (rb_str_capacity(new_str) >= RSTRING_LEN(str) + term_len + sizeof(st_index_t)) {
536 str_store_precomputed_hash(new_str, (st_index_t)RSTRING(str)->as.heap.aux.capa);
537 }
538#endif
539 }
540 str = new_str;
541 }
542 else {
543 str = str_new_static(rb_cString, RSTRING(str)->as.heap.ptr,
544 RSTRING(str)->len,
545 ENCODING_GET(str));
546 }
547 OBJ_FREEZE(str);
548 }
549 else {
550 if (!OBJ_FROZEN(str) || CHILLED_STRING_P(str)) {
551 str = str_new_frozen(rb_cString, str);
552 }
553 if (STR_SHARED_P(str)) { /* str should not be shared */
554 /* shared substring */
555 str_make_independent(str);
557 }
558 if (!BARE_STRING_P(str)) {
559 str = str_new_frozen(rb_cString, str);
560 }
561 }
562
563 ENC_CODERANGE_SET(str, coderange);
564 RBASIC(str)->flags |= RSTRING_FSTR;
565 if (!RB_OBJ_SHAREABLE_P(str)) {
567 }
568 RUBY_ASSERT((rb_gc_verify_shareable(str), 1));
571 RUBY_ASSERT(!FL_TEST_RAW(str, STR_FAKESTR));
572 RUBY_ASSERT(!rb_obj_shape_has_ivars(str));
574 RUBY_ASSERT(!rb_objspace_garbage_object_p(str));
575
576 return str;
577}
578
579static const struct rb_concurrent_set_funcs fstring_concurrent_set_funcs = {
580 .hash = fstring_concurrent_set_hash,
581 .cmp = fstring_concurrent_set_cmp,
582 .create = fstring_concurrent_set_create,
583 .free = NULL,
584};
585
586void
587Init_fstring_table(void)
588{
589 fstring_table_obj = rb_concurrent_set_new(&fstring_concurrent_set_funcs, 8192);
590 rb_gc_register_address(&fstring_table_obj);
591}
592
593static VALUE
594register_fstring(VALUE str, bool copy, bool force_precompute_hash)
595{
596 struct fstr_create_arg args = {
597 .copy = copy,
598 .force_precompute_hash = force_precompute_hash
599 };
600
601#ifdef PRECOMPUTED_FAKESTR_HASH
602 if (FL_TEST_RAW(str, STR_FAKESTR)) {
603 // if the string hasn't been interned, we'll need the hash twice, so we
604 // compute it once and store it in capa
605 RSTRING(str)->as.heap.aux.capa = (rb_len_t)str_do_hash(str);
606 }
607#endif
608
609 VALUE result = rb_concurrent_set_find_or_insert(&fstring_table_obj, str, &args);
610
611 RUBY_ASSERT(!rb_objspace_garbage_object_p(result));
613 RUBY_ASSERT(OBJ_FROZEN(result));
615 RUBY_ASSERT((rb_gc_verify_shareable(result), 1));
616 RUBY_ASSERT(!FL_TEST_RAW(result, STR_FAKESTR));
618
619 return result;
620}
621
622bool
623rb_obj_is_fstring_table(VALUE obj)
624{
625 ASSERT_vm_locking();
626
627 return obj == fstring_table_obj;
628}
629
630void
631rb_gc_free_fstring(VALUE obj)
632{
633 ASSERT_vm_locking_with_barrier();
634
635 RUBY_ASSERT(FL_TEST(obj, RSTRING_FSTR));
637 RUBY_ASSERT(!FL_TEST(obj, STR_SHARED));
638
639 rb_concurrent_set_delete_by_identity(fstring_table_obj, obj);
640
641 RB_DEBUG_COUNTER_INC(obj_str_fstr);
642
643 FL_UNSET(obj, RSTRING_FSTR);
644}
645
646void
647rb_fstring_foreach_with_replace(int (*callback)(VALUE *str, void *data), void *data)
648{
649 if (fstring_table_obj) {
650 rb_concurrent_set_foreach_with_replace(fstring_table_obj, callback, data);
651 }
652}
653
654static VALUE
655setup_fake_str(struct RString *fake_str, const char *name, rb_len_t len, int encidx)
656{
657 fake_str->basic.flags = T_STRING|RSTRING_NOEMBED|STR_NOFREE|STR_FAKESTR;
658 RBASIC_SET_FULL_SHAPE_ID((VALUE)fake_str, ROOT_SHAPE_ID | SHAPE_ID_LAYOUT_OTHER);
659
660 if (!name) {
662 name = "";
663 }
664
665 ENCODING_SET_INLINED((VALUE)fake_str, encidx);
666
667 RBASIC_SET_CLASS_RAW((VALUE)fake_str, rb_cString);
668 fake_str->len = len;
669 fake_str->as.heap.ptr = (char *)name;
670 fake_str->as.heap.aux.capa = len;
671 return (VALUE)fake_str;
672}
673
674/*
675 * set up a fake string which refers a static string literal.
676 */
677VALUE
678rb_setup_fake_str(struct RString *fake_str, const char *name, rb_len_t len, rb_encoding *enc)
679{
680 return setup_fake_str(fake_str, name, len, rb_enc_to_index(enc));
681}
682
683/*
684 * rb_fstring_new and rb_fstring_cstr family create or lookup a frozen
685 * shared string which refers a static string literal. `ptr` must
686 * point a constant string.
687 */
688VALUE
689rb_fstring_new(const char *ptr, rb_len_t len)
690{
691 struct RString fake_str = {RBASIC_INIT};
692 return register_fstring(setup_fake_str(&fake_str, ptr, len, ENCINDEX_US_ASCII), false, false);
693}
694
695VALUE
696rb_fstring_enc_new(const char *ptr, rb_len_t len, rb_encoding *enc)
697{
698 struct RString fake_str = {RBASIC_INIT};
699 return register_fstring(rb_setup_fake_str(&fake_str, ptr, len, enc), false, false);
700}
701
702VALUE
703rb_fstring_cstr(const char *ptr)
704{
705 return rb_fstring_new(ptr, strlen(ptr));
706}
707
708static inline bool
709single_byte_optimizable(VALUE str)
710{
711 int encindex = ENCODING_GET(str);
712 switch (encindex) {
713 case ENCINDEX_ASCII_8BIT:
714 case ENCINDEX_US_ASCII:
715 return true;
716 case ENCINDEX_UTF_8:
717 // For UTF-8 it's worth scanning the string coderange when unknown.
718 return rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT;
719 }
720 /* Conservative. It may be ENC_CODERANGE_UNKNOWN. */
721 if (ENC_CODERANGE(str) == ENC_CODERANGE_7BIT) {
722 return true;
723 }
724
725 if (rb_enc_mbmaxlen(rb_enc_from_index(encindex)) == 1) {
726 return true;
727 }
728
729 /* Conservative. Possibly single byte.
730 * "\xa1" in Shift_JIS for example. */
731 return false;
732}
733
735
736static inline const char *
737search_nonascii(const char *p, const char *e)
738{
739 const char *s, *t;
740
741 if (p < e && !ISASCII(*p)) {
742 return p;
743 }
744
745#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
746# if SIZEOF_UINTPTR_T == 8
747# define NONASCII_MASK UINT64_C(0x8080808080808080)
748# elif SIZEOF_UINTPTR_T == 4
749# define NONASCII_MASK UINT32_C(0x80808080)
750# else
751# error "don't know what to do."
752# endif
753#else
754# if SIZEOF_UINTPTR_T == 8
755# define NONASCII_MASK ((uintptr_t)0x80808080UL << 32 | (uintptr_t)0x80808080UL)
756# elif SIZEOF_UINTPTR_T == 4
757# define NONASCII_MASK 0x80808080UL /* or...? */
758# else
759# error "don't know what to do."
760# endif
761#endif
762
763 if (UNALIGNED_WORD_ACCESS || e - p >= SIZEOF_VOIDP) {
764#if !UNALIGNED_WORD_ACCESS
765 if ((uintptr_t)p % SIZEOF_VOIDP) {
766 int l = SIZEOF_VOIDP - (uintptr_t)p % SIZEOF_VOIDP;
767 p += l;
768 switch (l) {
769 default: UNREACHABLE;
770#if SIZEOF_VOIDP > 4
771 case 7: if (p[-7]&0x80) return p-7;
772 case 6: if (p[-6]&0x80) return p-6;
773 case 5: if (p[-5]&0x80) return p-5;
774 case 4: if (p[-4]&0x80) return p-4;
775#endif
776 case 3: if (p[-3]&0x80) return p-3;
777 case 2: if (p[-2]&0x80) return p-2;
778 case 1: if (p[-1]&0x80) return p-1;
779 case 0: break;
780 }
781 }
782#endif
783#if defined(HAVE_BUILTIN___BUILTIN_ASSUME_ALIGNED) &&! UNALIGNED_WORD_ACCESS
784#define aligned_ptr(value) \
785 __builtin_assume_aligned((value), sizeof(uintptr_t))
786#else
787#define aligned_ptr(value) (value)
788#endif
789 s = aligned_ptr(p);
790 t = (e - (SIZEOF_VOIDP-1));
791#undef aligned_ptr
792 for (;s < t; s += sizeof(uintptr_t)) {
793 uintptr_t word;
794 memcpy(&word, s, sizeof(word));
795 if (word & NONASCII_MASK) {
796#ifdef WORDS_BIGENDIAN
797 return (const char *)s + (nlz_intptr(word&NONASCII_MASK)>>3);
798#else
799 return (const char *)s + (ntz_intptr(word&NONASCII_MASK)>>3);
800#endif
801 }
802 }
803 p = (const char *)s;
804 }
805
806 switch (e - p) {
807 default: UNREACHABLE;
808#if SIZEOF_VOIDP > 4
809 case 7: if (e[-7]&0x80) return e-7;
810 case 6: if (e[-6]&0x80) return e-6;
811 case 5: if (e[-5]&0x80) return e-5;
812 case 4: if (e[-4]&0x80) return e-4;
813#endif
814 case 3: if (e[-3]&0x80) return e-3;
815 case 2: if (e[-2]&0x80) return e-2;
816 case 1: if (e[-1]&0x80) return e-1;
817 case 0: return NULL;
818 }
819}
820
821static int
822coderange_scan(const char *p, rb_len_t len, rb_encoding *enc)
823{
824 const char *e = p + len;
825
826 if (rb_enc_to_index(enc) == rb_ascii8bit_encindex()) {
827 /* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
828 p = search_nonascii(p, e);
830 }
831
832 if (rb_enc_asciicompat(enc)) {
833 p = search_nonascii(p, e);
834 if (!p) return ENC_CODERANGE_7BIT;
835 for (;;) {
836 int ret = rb_enc_precise_mbclen(p, e, enc);
838 p += MBCLEN_CHARFOUND_LEN(ret);
839 if (p == e) break;
840 p = search_nonascii(p, e);
841 if (!p) break;
842 }
843 }
844 else {
845 while (p < e) {
846 int ret = rb_enc_precise_mbclen(p, e, enc);
848 p += MBCLEN_CHARFOUND_LEN(ret);
849 }
850 }
851 return ENC_CODERANGE_VALID;
852}
853
855rb_str_coderange_scan_restartable(const char *s, const char *e, rb_encoding *enc, int *cr)
856{
857 const char *p = s;
858
859 if (*cr == ENC_CODERANGE_BROKEN)
860 return e - s;
861
862 if (rb_enc_to_index(enc) == rb_ascii8bit_encindex()) {
863 /* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
864 if (*cr == ENC_CODERANGE_VALID) return e - s;
865 p = search_nonascii(p, e);
867 return e - s;
868 }
869 else if (rb_enc_asciicompat(enc)) {
870 p = search_nonascii(p, e);
871 if (!p) {
872 if (*cr != ENC_CODERANGE_VALID) *cr = ENC_CODERANGE_7BIT;
873 return e - s;
874 }
875 for (;;) {
876 int ret = rb_enc_precise_mbclen(p, e, enc);
877 if (!MBCLEN_CHARFOUND_P(ret)) {
879 return p - s;
880 }
881 p += MBCLEN_CHARFOUND_LEN(ret);
882 if (p == e) break;
883 p = search_nonascii(p, e);
884 if (!p) break;
885 }
886 }
887 else {
888 while (p < e) {
889 int ret = rb_enc_precise_mbclen(p, e, enc);
890 if (!MBCLEN_CHARFOUND_P(ret)) {
892 return p - s;
893 }
894 p += MBCLEN_CHARFOUND_LEN(ret);
895 }
896 }
898 return e - s;
899}
900
901static inline void
902str_enc_copy(VALUE str1, VALUE str2)
903{
904 rb_enc_set_index(str1, ENCODING_GET(str2));
905}
906
907/* Like str_enc_copy, but does not check frozen status of str1.
908 * You should use this only if you're certain that str1 is not frozen. */
909static inline void
910str_enc_copy_direct(VALUE str1, VALUE str2)
911{
912 RUBY_ASSERT(TERM_LEN(str2) <= TERM_LEN(str1) || str_dependent_p(str1) ||
913 (STR_EMBED_P(str1) &&
914 str_embed_capa(str1) >= RSTRING_LEN(str1) + TERM_LEN(str2)));
915 int inlined_encoding = RB_ENCODING_GET_INLINED(str2);
916 if (inlined_encoding == ENCODING_INLINE_MAX) {
917 rb_enc_set_index(str1, rb_enc_get_index(str2));
918 }
919 else {
920 ENCODING_SET_INLINED(str1, inlined_encoding);
921 }
922}
923
924static void
925rb_enc_cr_str_copy_for_substr(VALUE dest, VALUE src)
926{
927 /* this function is designed for copying encoding and coderange
928 * from src to new string "dest" which is made from the part of src.
929 */
930 str_enc_copy(dest, src);
931 if (RSTRING_LEN(dest) == 0) {
932 if (!rb_enc_asciicompat(STR_ENC_GET(src)))
934 else
936 return;
937 }
938 switch (ENC_CODERANGE(src)) {
941 break;
943 if (!rb_enc_asciicompat(STR_ENC_GET(src)) ||
944 search_nonascii(RSTRING_PTR(dest), RSTRING_END(dest)))
946 else
948 break;
949 default:
950 break;
951 }
952}
953
954static void
955rb_enc_cr_str_exact_copy(VALUE dest, VALUE src)
956{
957 str_enc_copy(dest, src);
959}
960
961static int
962enc_coderange_scan(VALUE str, rb_encoding *enc)
963{
964 return coderange_scan(RSTRING_PTR(str), RSTRING_LEN(str), enc);
965}
966
967int
968rb_enc_str_coderange_scan(VALUE str, rb_encoding *enc)
969{
970 return enc_coderange_scan(str, enc);
971}
972
973int
974rbimpl_enc_str_coderange_scan(VALUE str)
975{
976 int cr = enc_coderange_scan(str, get_encoding(str));
977 ENC_CODERANGE_SET(str, cr);
978 return cr;
979}
980
981#undef rb_enc_str_coderange
982int
983rb_enc_str_coderange(VALUE str)
984{
985 int cr = ENC_CODERANGE(str);
986
987 if (cr == ENC_CODERANGE_UNKNOWN) {
988 cr = rbimpl_enc_str_coderange_scan(str);
989 }
990 return cr;
991}
992#define rb_enc_str_coderange rb_enc_str_coderange_inline
993
994static inline bool
995rb_enc_str_asciicompat(VALUE str)
996{
997 int encindex = ENCODING_GET_INLINED(str);
998 return rb_str_encindex_fastpath(encindex) || rb_enc_asciicompat(rb_enc_get_from_index(encindex));
999}
1000
1001int
1003{
1004 switch(ENC_CODERANGE(str)) {
1006 return rb_enc_str_asciicompat(str) && is_ascii_string(str);
1007 case ENC_CODERANGE_7BIT:
1008 return true;
1009 default:
1010 return false;
1011 }
1012}
1013
1014static inline void
1015str_mod_check(VALUE s, const char *p, rb_len_t len)
1016{
1017 if (RSTRING_PTR(s) != p || RSTRING_LEN(s) != len){
1018 rb_raise(rb_eRuntimeError, "string modified");
1019 }
1020}
1021
1022static size_t
1023str_capacity(VALUE str, const int termlen)
1024{
1025 if (STR_EMBED_P(str)) {
1026 return str_embed_capa(str) - termlen;
1027 }
1028 else if (FL_ANY_RAW(str, STR_SHARED|STR_NOFREE)) {
1029 return RSTRING(str)->len;
1030 }
1031 else {
1032 return RSTRING(str)->as.heap.aux.capa;
1033 }
1034}
1035
1036size_t
1038{
1039 return str_capacity(str, TERM_LEN(str));
1040}
1041
1042static inline void
1043must_not_null(const char *ptr)
1044{
1045 if (!ptr) {
1046 rb_raise(rb_eArgError, "NULL pointer given");
1047 }
1048}
1049
1050static inline VALUE
1051str_alloc_embed(VALUE klass, size_t capa)
1052{
1053 size_t size = rb_str_embed_size(capa, 0);
1054 RUBY_ASSERT(size > 0);
1055 RUBY_ASSERT(rb_gc_size_allocatable_p(size));
1056
1057 NEWOBJ_OF(str, struct RString, klass, T_STRING, size);
1058
1059 str->len = 0;
1060 str->as.embed.ary[0] = 0;
1061
1062 return (VALUE)str;
1063}
1064
1065static inline VALUE
1066str_alloc_heap(VALUE klass)
1067{
1068 NEWOBJ_OF(str, struct RString, klass, T_STRING | STR_NOEMBED, sizeof(struct RString));
1069
1070 str->len = 0;
1071 str->as.heap.aux.capa = 0;
1072 str->as.heap.ptr = NULL;
1073
1074 return (VALUE)str;
1075}
1076
1077static inline VALUE
1078empty_str_alloc(VALUE klass)
1079{
1080 RUBY_DTRACE_CREATE_HOOK(STRING, 0);
1081 VALUE str = str_alloc_embed(klass, 0);
1082 memset(RSTRING(str)->as.embed.ary, 0, str_embed_capa(str));
1084 return str;
1085}
1086
1087static VALUE
1088str_enc_new(VALUE klass, const char *ptr, rb_len_t len, rb_encoding *enc)
1089{
1090 VALUE str;
1091
1092 if (len < 0) {
1093 rb_raise(rb_eArgError, "negative string size (or size too big)");
1094 }
1095
1096 if (enc == NULL) {
1097 enc = rb_ascii8bit_encoding();
1098 }
1099
1100 RUBY_DTRACE_CREATE_HOOK(STRING, len);
1101
1102 int termlen = rb_enc_mbminlen(enc);
1103
1104 if (STR_EMBEDDABLE_P(len, termlen)) {
1105 str = str_alloc_embed(klass, len + termlen);
1106 if (len == 0) {
1107 ENC_CODERANGE_SET(str, rb_enc_asciicompat(enc) ? ENC_CODERANGE_7BIT : ENC_CODERANGE_VALID);
1108 }
1109 }
1110 else {
1111 str = str_alloc_heap(klass);
1112 RSTRING(str)->as.heap.aux.capa = len;
1113 /* :FIXME: @shyouhei guesses `len + termlen` is guaranteed to never
1114 * integer overflow. If we can STATIC_ASSERT that, the following
1115 * mul_add_mul can be reverted to a simple ALLOC_N. */
1116 RSTRING(str)->as.heap.ptr =
1117 rb_xmalloc_mul_add_mul(sizeof(char), len, sizeof(char), termlen);
1118 }
1119
1120 rb_enc_raw_set(str, enc);
1121
1122 if (ptr) {
1123 memcpy(RSTRING_PTR(str), ptr, len);
1124 }
1125 else {
1126 memset(RSTRING_PTR(str), 0, len);
1127 }
1128
1129 STR_SET_LEN(str, len);
1130 TERM_FILL(RSTRING_PTR(str) + len, termlen);
1131 return str;
1132}
1133
1134static VALUE
1135str_new(VALUE klass, const char *ptr, rb_len_t len)
1136{
1137 return str_enc_new(klass, ptr, len, rb_ascii8bit_encoding());
1138}
1139
1140VALUE
1141rb_str_new(const char *ptr, rb_len_t len)
1142{
1143 return str_new(rb_cString, ptr, len);
1144}
1145
1146VALUE
1148{
1149 return str_enc_new(rb_cString, ptr, len, rb_usascii_encoding());
1150}
1151
1152VALUE
1154{
1155 return str_enc_new(rb_cString, ptr, len, rb_utf8_encoding());
1156}
1157
1158VALUE
1159rb_enc_str_new(const char *ptr, rb_len_t len, rb_encoding *enc)
1160{
1161 return str_enc_new(rb_cString, ptr, len, enc);
1162}
1163
1164VALUE
1165rb_str_new_cstr(const char *ptr)
1166{
1167 must_not_null(ptr);
1168 /* rb_str_new_cstr() can take pointer from non-malloc-generated
1169 * memory regions, and that cannot be detected by the MSAN. Just
1170 * trust the programmer that the argument passed here is a sane C
1171 * string. */
1172 __msan_unpoison_string(ptr);
1173 return rb_str_new(ptr, strlen(ptr));
1174}
1175
1176VALUE
1178{
1180}
1181
1182VALUE
1183rb_utf8_str_new_cstr(const char *ptr)
1184{
1186}
1187
1188VALUE
1189rb_enc_str_new_cstr(const char *ptr, rb_encoding *enc)
1190{
1191 must_not_null(ptr);
1192 if (rb_enc_mbminlen(enc) != 1) {
1193 rb_raise(rb_eArgError, "wchar encoding given");
1194 }
1195 return rb_enc_str_new(ptr, strlen(ptr), enc);
1196}
1197
1198static VALUE
1199str_new_static(VALUE klass, const char *ptr, rb_len_t len, int encindex)
1200{
1201 VALUE str;
1202
1203 if (len < 0) {
1204 rb_raise(rb_eArgError, "negative string size (or size too big)");
1205 }
1206
1207 if (!ptr) {
1208 str = str_enc_new(klass, ptr, len, rb_enc_from_index(encindex));
1209 }
1210 else {
1211 RUBY_DTRACE_CREATE_HOOK(STRING, len);
1212 str = str_alloc_heap(klass);
1213 RSTRING(str)->len = len;
1214 RSTRING(str)->as.heap.ptr = (char *)ptr;
1215 RSTRING(str)->as.heap.aux.capa = len;
1216 RBASIC(str)->flags |= STR_NOFREE;
1217 rb_enc_associate_index(str, encindex);
1218 }
1219 return str;
1220}
1221
1222VALUE
1224{
1225 return str_new_static(rb_cString, ptr, len, 0);
1226}
1227
1228/* Take an xmalloc'd buffer as the String's body without copying it; the String owns it
1229 * from here and frees it like any other heap string. ptr must hold capa bytes plus the
1230 * terminator for encindex, which is what a Ractor courier's string node carries. */
1231VALUE
1232rb_str_new_owned(char *ptr, rb_len_t len, rb_len_t capa, int encindex)
1233{
1234 RUBY_DTRACE_CREATE_HOOK(STRING, len);
1235 VALUE str = str_alloc_heap(rb_cString);
1236 RSTRING(str)->len = len;
1237 RSTRING(str)->as.heap.ptr = ptr;
1238 /* Freed by size (STR_HEAP_SIZE = capa + terminator), so capa must describe the
1239 * allocation the caller made, not just the bytes in use. */
1240 RSTRING(str)->as.heap.aux.capa = capa;
1241 rb_enc_associate_index(str, encindex);
1242 return str;
1243}
1244
1245VALUE
1247{
1248 return str_new_static(rb_cString, ptr, len, ENCINDEX_US_ASCII);
1249}
1250
1251VALUE
1253{
1254 return str_new_static(rb_cString, ptr, len, ENCINDEX_UTF_8);
1255}
1256
1257VALUE
1258rb_enc_str_new_static(const char *ptr, rb_len_t len, rb_encoding *enc)
1259{
1260 return str_new_static(rb_cString, ptr, len, rb_enc_to_index(enc));
1261}
1262
1263static VALUE str_cat_conv_enc_opts(VALUE newstr, rb_len_t ofs, const char *ptr, rb_len_t len,
1264 rb_encoding *from, rb_encoding *to,
1265 int ecflags, VALUE ecopts);
1266
1267static inline bool
1268is_enc_ascii_string(VALUE str, rb_encoding *enc)
1269{
1270 int encidx = rb_enc_to_index(enc);
1271 if (rb_enc_get_index(str) == encidx)
1272 return is_ascii_string(str);
1273 return enc_coderange_scan(str, enc) == ENC_CODERANGE_7BIT;
1274}
1275
1276VALUE
1277rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags, VALUE ecopts)
1278{
1279 rb_len_t len;
1280 const char *ptr;
1281 VALUE newstr;
1282
1283 if (!to) return str;
1284 if (!from) from = rb_enc_get(str);
1285 if (from == to) return str;
1286 if ((rb_enc_asciicompat(to) && is_enc_ascii_string(str, from)) ||
1287 rb_is_ascii8bit_enc(to)) {
1288 if (STR_ENC_GET(str) != to) {
1289 str = rb_str_dup(str);
1290 rb_enc_associate(str, to);
1291 }
1292 return str;
1293 }
1294
1295 RSTRING_GETMEM(str, ptr, len);
1296 newstr = str_cat_conv_enc_opts(rb_str_buf_new(len), 0, ptr, len,
1297 from, to, ecflags, ecopts);
1298 if (NIL_P(newstr)) {
1299 /* some error, return original */
1300 return str;
1301 }
1302 return newstr;
1303}
1304
1305VALUE
1306rb_str_cat_conv_enc_opts(VALUE newstr, rb_len_t ofs, const char *ptr, rb_len_t len,
1307 rb_encoding *from, int ecflags, VALUE ecopts)
1308{
1309 rb_len_t olen;
1310
1311 olen = RSTRING_LEN(newstr);
1312 if (ofs < -olen || olen < ofs)
1313 rb_raise(rb_eIndexError, "index %"PRIdLEN" out of string", ofs);
1314 if (ofs < 0) ofs += olen;
1315 if (!from) {
1316 STR_SET_LEN(newstr, ofs);
1317 return rb_str_cat(newstr, ptr, len);
1318 }
1319
1320 rb_str_modify(newstr);
1321 return str_cat_conv_enc_opts(newstr, ofs, ptr, len, from,
1322 rb_enc_get(newstr),
1323 ecflags, ecopts);
1324}
1325
1326VALUE
1327rb_str_initialize(VALUE str, const char *ptr, rb_len_t len, rb_encoding *enc)
1328{
1329 STR_SET_LEN(str, 0);
1330 rb_enc_associate(str, enc);
1331 rb_str_cat(str, ptr, len);
1332 return str;
1333}
1334
1335static VALUE
1336str_cat_conv_enc_opts(VALUE newstr, rb_len_t ofs, const char *ptr, rb_len_t len,
1337 rb_encoding *from, rb_encoding *to,
1338 int ecflags, VALUE ecopts)
1339{
1340 rb_econv_t *ec;
1342 rb_len_t olen;
1343 VALUE econv_wrapper;
1344 const unsigned char *start, *sp;
1345 unsigned char *dest, *dp;
1346 size_t converted_output = (size_t)ofs;
1347
1348 olen = rb_str_capacity(newstr);
1349
1350 econv_wrapper = rb_obj_alloc(rb_cEncodingConverter);
1351 RBASIC_CLEAR_CLASS(econv_wrapper);
1352 ec = rb_econv_open_opts(from->name, to->name, ecflags, ecopts);
1353 if (!ec) return Qnil;
1354 DATA_PTR(econv_wrapper) = ec;
1355
1356 sp = (unsigned char*)ptr;
1357 start = sp;
1358 while ((dest = (unsigned char*)RSTRING_PTR(newstr)),
1359 (dp = dest + converted_output),
1360 (ret = rb_econv_convert(ec, &sp, start + len, &dp, dest + olen, 0)),
1362 /* destination buffer short */
1363 size_t converted_input = sp - start;
1364 size_t rest = len - converted_input;
1365 converted_output = dp - dest;
1366 rb_str_set_len(newstr, converted_output);
1367 if (converted_input && converted_output &&
1368 rest < (RB_LEN_MAX / converted_output)) {
1369 rest = (rest * converted_output) / converted_input;
1370 }
1371 else {
1372 rest = olen;
1373 }
1374 olen += rest < 2 ? 2 : rest;
1375 rb_str_resize(newstr, olen);
1376 }
1377 DATA_PTR(econv_wrapper) = 0;
1378 RB_GC_GUARD(econv_wrapper);
1379 rb_econv_close(ec);
1380 switch (ret) {
1381 case econv_finished:
1382 len = dp - (unsigned char*)RSTRING_PTR(newstr);
1383 rb_str_set_len(newstr, len);
1384 rb_enc_associate(newstr, to);
1385 return newstr;
1386
1387 default:
1388 return Qnil;
1389 }
1390}
1391
1392VALUE
1393rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
1394{
1395 return rb_str_conv_enc_opts(str, from, to, 0, Qnil);
1396}
1397
1398VALUE
1399rb_external_str_new_with_enc(const char *ptr, rb_len_t len, rb_encoding *eenc)
1400{
1401 rb_encoding *ienc;
1402 VALUE str;
1403 const int eidx = rb_enc_to_index(eenc);
1404
1405 if (!ptr) {
1406 return rb_enc_str_new(ptr, len, eenc);
1407 }
1408
1409 /* ASCII-8BIT case, no conversion */
1410 if ((eidx == rb_ascii8bit_encindex()) ||
1411 (eidx == rb_usascii_encindex() && search_nonascii(ptr, ptr + len))) {
1412 return rb_str_new(ptr, len);
1413 }
1414 /* no default_internal or same encoding, no conversion */
1416 if (!ienc || eenc == ienc) {
1417 return rb_enc_str_new(ptr, len, eenc);
1418 }
1419 /* ASCII compatible, and ASCII only string, no conversion in
1420 * default_internal */
1421 if ((eidx == rb_ascii8bit_encindex()) ||
1422 (eidx == rb_usascii_encindex()) ||
1423 (rb_enc_asciicompat(eenc) && !search_nonascii(ptr, ptr + len))) {
1424 return rb_enc_str_new(ptr, len, ienc);
1425 }
1426 /* convert from the given encoding to default_internal */
1427 str = rb_enc_str_new(NULL, 0, ienc);
1428 /* when the conversion failed for some reason, just ignore the
1429 * default_internal and result in the given encoding as-is. */
1430 if (NIL_P(rb_str_cat_conv_enc_opts(str, 0, ptr, len, eenc, 0, Qnil))) {
1431 rb_str_initialize(str, ptr, len, eenc);
1432 }
1433 return str;
1434}
1435
1436VALUE
1437rb_external_str_with_enc(VALUE str, rb_encoding *eenc)
1438{
1439 int eidx = rb_enc_to_index(eenc);
1440 if (eidx == rb_usascii_encindex() &&
1441 !is_ascii_string(str)) {
1442 rb_enc_associate_index(str, rb_ascii8bit_encindex());
1443 return str;
1444 }
1445 rb_enc_associate_index(str, eidx);
1446 return rb_str_conv_enc(str, eenc, rb_default_internal_encoding());
1447}
1448
1449VALUE
1454
1455VALUE
1457{
1459}
1460
1461VALUE
1463{
1465}
1466
1467VALUE
1469{
1470 return rb_external_str_new_with_enc(ptr, strlen(ptr), rb_locale_encoding());
1471}
1472
1473VALUE
1478
1479VALUE
1481{
1482 return rb_external_str_new_with_enc(ptr, strlen(ptr), rb_filesystem_encoding());
1483}
1484
1485VALUE
1490
1491VALUE
1496
1497VALUE
1498rb_str_export_to_enc(VALUE str, rb_encoding *enc)
1499{
1500 return rb_str_conv_enc(str, STR_ENC_GET(str), enc);
1501}
1502
1503static VALUE
1504str_replace_shared_without_enc(VALUE str2, VALUE str)
1505{
1506 const int termlen = TERM_LEN(str);
1507 char *ptr;
1508 rb_len_t len;
1509
1510 RSTRING_GETMEM(str, ptr, len);
1511 if (str_embed_capa(str2) >= len + termlen) {
1512 char *ptr2 = RSTRING(str2)->as.embed.ary;
1513 STR_SET_EMBED(str2);
1514 memcpy(ptr2, RSTRING_PTR(str), len);
1515 TERM_FILL(ptr2+len, termlen);
1516 }
1517 else {
1518 VALUE root;
1519 if (STR_SHARED_P(str)) {
1520 root = RSTRING(str)->as.heap.aux.shared;
1521 RSTRING_GETMEM(str, ptr, len);
1522 }
1523 else {
1524 root = rb_str_new_frozen(str);
1525 RSTRING_GETMEM(root, ptr, len);
1526 }
1527 RUBY_ASSERT(OBJ_FROZEN(root));
1528
1529 if (!STR_EMBED_P(str2) && !FL_TEST_RAW(str2, STR_SHARED|STR_NOFREE)) {
1530 if (FL_TEST_RAW(str2, STR_SHARED_ROOT)) {
1531 rb_fatal("about to free a possible shared root");
1532 }
1533 char *ptr2 = STR_HEAP_PTR(str2);
1534 if (ptr2 != ptr) {
1535 SIZED_FREE_N(ptr2, STR_HEAP_SIZE(str2));
1536 }
1537 }
1538 FL_SET(str2, STR_NOEMBED);
1539 RSTRING(str2)->as.heap.ptr = ptr;
1540 STR_SET_SHARED(str2, root);
1541 }
1542
1543 STR_SET_LEN(str2, len);
1544
1545 return str2;
1546}
1547
1548static VALUE
1549str_replace_shared(VALUE str2, VALUE str)
1550{
1551 str_replace_shared_without_enc(str2, str);
1552 rb_enc_cr_str_exact_copy(str2, str);
1553 return str2;
1554}
1555
1556static VALUE
1557str_new_shared(VALUE klass, VALUE str)
1558{
1559 return str_replace_shared(str_alloc_heap(klass), str);
1560}
1561
1562VALUE
1564{
1565 return str_new_shared(rb_obj_class(str), str);
1566}
1567
1568VALUE
1570{
1571 if (RB_FL_TEST_RAW(orig, FL_FREEZE | STR_CHILLED) == FL_FREEZE) return orig;
1572 return str_new_frozen(rb_obj_class(orig), orig);
1573}
1574
1575static VALUE
1576rb_str_new_frozen_String(VALUE orig)
1577{
1578 if (OBJ_FROZEN(orig) && rb_obj_class(orig) == rb_cString) return orig;
1579 return str_new_frozen(rb_cString, orig);
1580}
1581
1582
1583VALUE
1584rb_str_frozen_bare_string(VALUE orig)
1585{
1586 if (RB_LIKELY(BARE_STRING_P(orig) && OBJ_FROZEN_RAW(orig))) return orig;
1587 return str_new_frozen(rb_cString, orig);
1588}
1589
1590VALUE
1591rb_str_tmp_frozen_acquire(VALUE orig)
1592{
1593 if (OBJ_FROZEN_RAW(orig)) return orig;
1594 return str_new_frozen_buffer(0, orig, FALSE);
1595}
1596
1597VALUE
1599{
1600 if (OBJ_FROZEN_RAW(orig) && !STR_EMBED_P(orig)) return orig;
1601 if (STR_SHARED_P(orig) && !STR_EMBED_P(RSTRING(orig)->as.heap.aux.shared)) return rb_str_tmp_frozen_acquire(orig);
1602
1603 VALUE str = str_alloc_heap(0);
1604 OBJ_FREEZE(str);
1605 /* Always set the STR_SHARED_ROOT to ensure it does not get re-embedded. */
1606 FL_SET(str, STR_SHARED_ROOT);
1607
1608 size_t capa = str_capacity(orig, TERM_LEN(orig));
1609
1610 /* If the string is embedded then we want to create a copy that is heap
1611 * allocated. If the string is shared then the shared root must be
1612 * embedded, so we want to create a copy. If the string is a shared root
1613 * then it must be embedded, so we want to create a copy. */
1614 if (STR_EMBED_P(orig) || FL_TEST_RAW(orig, STR_SHARED | STR_SHARED_ROOT | RSTRING_FSTR)) {
1615 RSTRING(str)->as.heap.ptr = rb_xmalloc_mul_add_mul(sizeof(char), capa, sizeof(char), TERM_LEN(orig));
1616 memcpy(RSTRING(str)->as.heap.ptr, RSTRING_PTR(orig), capa);
1617 }
1618 else {
1619 /* orig must be heap allocated and not shared, so we can safely transfer
1620 * the pointer to str. */
1621 RSTRING(str)->as.heap.ptr = RSTRING(orig)->as.heap.ptr;
1622 RBASIC(str)->flags |= RBASIC(orig)->flags & STR_NOFREE;
1623 RBASIC(orig)->flags &= ~STR_NOFREE;
1624 STR_SET_SHARED(orig, str);
1625 /* str was just allocated here, so orig is its only child and it is
1626 * safe for rb_str_tmp_frozen_release to give the buffer back. */
1627 FL_UNSET_RAW(str, STR_BORROWED);
1628 if (RB_OBJ_SHAREABLE_P(orig)) {
1630 RUBY_ASSERT((rb_gc_verify_shareable(str), 1));
1631 }
1632 }
1633
1634 RSTRING(str)->len = RSTRING(orig)->len;
1635 RSTRING(str)->as.heap.aux.capa = capa + (TERM_LEN(orig) - TERM_LEN(str));
1636
1637 return str;
1638}
1639
1640void
1642{
1643 rb_str_tmp_frozen_release(orig, tmp);
1644}
1645
1646void
1647rb_str_tmp_frozen_release(VALUE orig, VALUE tmp)
1648{
1649 if (RBASIC_CLASS(tmp) != 0)
1650 return;
1651
1652 if (STR_EMBED_P(tmp)) {
1654 }
1655 else if (FL_TEST_RAW(orig, STR_SHARED | STR_TMPLOCK) == STR_SHARED &&
1656 !OBJ_FROZEN_RAW(orig)) {
1657 VALUE shared = RSTRING(orig)->as.heap.aux.shared;
1658
1659 if (shared == tmp && !FL_TEST_RAW(tmp, STR_BORROWED)) {
1660 RUBY_ASSERT(RSTRING(orig)->as.heap.ptr == RSTRING(tmp)->as.heap.ptr);
1661 RUBY_ASSERT(RSTRING_LEN(orig) == RSTRING_LEN(tmp));
1662
1663 /* Unshare orig since the root (tmp) only has this one child. */
1664 FL_UNSET_RAW(orig, STR_SHARED);
1665 RSTRING(orig)->as.heap.aux.capa = RSTRING(tmp)->as.heap.aux.capa + TERM_LEN(tmp) - TERM_LEN(orig);
1666 RBASIC(orig)->flags |= RBASIC(tmp)->flags & STR_NOFREE;
1668
1669 /* Make tmp embedded and empty so it is safe for sweeping. */
1670 STR_SET_EMBED(tmp);
1671 STR_SET_LEN(tmp, 0);
1672 }
1673 }
1674}
1675
1676static VALUE
1677str_new_frozen(VALUE klass, VALUE orig)
1678{
1679 return str_new_frozen_buffer(klass, orig, TRUE);
1680}
1681
1682/* Transfers ownership of orig's buffer to a new shared root string.
1683 * termlen is the terminator length of the returned string, which may differ
1684 * from orig's terminator length when the caller does not copy the encoding.
1685 * The capacity is stored without the terminator, so it must be adjusted for
1686 * the difference to keep the buffer size (capa + termlen) unchanged. */
1687static VALUE
1688heap_str_make_shared(VALUE klass, VALUE orig, int termlen)
1689{
1690 RUBY_ASSERT(!STR_EMBED_P(orig));
1691 RUBY_ASSERT(!STR_SHARED_P(orig));
1693
1694 VALUE str = str_alloc_heap(klass);
1695 STR_SET_LEN(str, RSTRING_LEN(orig));
1696 RSTRING(str)->as.heap.ptr = RSTRING_PTR(orig);
1697 RSTRING(str)->as.heap.aux.capa = RSTRING(orig)->as.heap.aux.capa + TERM_LEN(orig) - termlen;
1698 RBASIC(str)->flags |= RBASIC(orig)->flags & STR_NOFREE;
1699 RBASIC(orig)->flags &= ~STR_NOFREE;
1700 STR_SET_SHARED(orig, str);
1701 if (klass == 0)
1702 FL_UNSET_RAW(str, STR_BORROWED);
1703 return str;
1704}
1705
1706static VALUE
1707str_new_frozen_buffer(VALUE klass, VALUE orig, int copy_encoding)
1708{
1709 VALUE str;
1710
1711 rb_len_t len = RSTRING_LEN(orig);
1712 rb_encoding *enc = copy_encoding ? STR_ENC_GET(orig) : rb_ascii8bit_encoding();
1713 int termlen = copy_encoding ? TERM_LEN(orig) : 1;
1714
1715 if (STR_EMBED_P(orig) || STR_EMBEDDABLE_P(len, termlen)) {
1716 str = str_enc_new(klass, RSTRING_PTR(orig), len, enc);
1717 RUBY_ASSERT(STR_EMBED_P(str));
1718 }
1719 else {
1720 if (FL_TEST_RAW(orig, STR_SHARED)) {
1721 VALUE shared = RSTRING(orig)->as.heap.aux.shared;
1722 rb_len_t ofs = RSTRING(orig)->as.heap.ptr - RSTRING_PTR(shared);
1723 rb_len_t rest = RSTRING_LEN(shared) - ofs - RSTRING_LEN(orig);
1724 RUBY_ASSERT(ofs >= 0);
1725 RUBY_ASSERT(rest >= 0);
1726 RUBY_ASSERT(ofs + rest <= RSTRING_LEN(shared));
1728
1729 if ((ofs > 0) || (rest > 0) ||
1730 (klass != RBASIC(shared)->klass) ||
1731 ENCODING_GET(shared) != ENCODING_GET(orig)) {
1732 str = str_new_shared(klass, shared);
1733 RUBY_ASSERT(!STR_EMBED_P(str));
1734 RSTRING(str)->as.heap.ptr += ofs;
1735 STR_SET_LEN(str, RSTRING_LEN(str) - (ofs + rest));
1736 }
1737 else {
1738 if (RBASIC_CLASS(shared) == 0)
1739 FL_SET_RAW(shared, STR_BORROWED);
1740 return shared;
1741 }
1742 }
1743 else if (STR_EMBEDDABLE_P(RSTRING_LEN(orig), TERM_LEN(orig))) {
1744 str = str_alloc_embed(klass, RSTRING_LEN(orig) + TERM_LEN(orig));
1745 STR_SET_EMBED(str);
1746 memcpy(RSTRING_PTR(str), RSTRING_PTR(orig), RSTRING_LEN(orig));
1747 STR_SET_LEN(str, RSTRING_LEN(orig));
1748 ENC_CODERANGE_SET(str, ENC_CODERANGE(orig));
1749 TERM_FILL(RSTRING_END(str), TERM_LEN(orig));
1750 }
1751 else {
1752 if (RB_OBJ_SHAREABLE_P(orig)) {
1753 str = str_enc_new(klass, RSTRING_PTR(orig), RSTRING_LEN(orig), enc);
1754 }
1755 else {
1756 str = heap_str_make_shared(klass, orig, termlen);
1757 }
1758 }
1759 }
1760
1761 if (copy_encoding) rb_enc_cr_str_exact_copy(str, orig);
1762 OBJ_FREEZE(str);
1763 return str;
1764}
1765
1766VALUE
1768{
1769 return str_enc_new(rb_obj_class(obj), ptr, len, STR_ENC_GET(obj));
1770}
1771
1772static VALUE
1773str_new_empty_String(VALUE str)
1774{
1775 VALUE v = rb_str_new(0, 0);
1776 rb_enc_copy(v, str);
1777 return v;
1778}
1779
1780#define STR_BUF_MIN_SIZE 63
1781
1782VALUE
1784{
1785 if (STR_EMBEDDABLE_P(capa, 1)) {
1786 return str_alloc_embed(rb_cString, capa + 1);
1787 }
1788
1789 VALUE str = str_alloc_heap(rb_cString);
1790
1791 RSTRING(str)->as.heap.aux.capa = capa;
1792 RSTRING(str)->as.heap.ptr = ALLOC_N(char, (size_t)capa + 1);
1793 RSTRING(str)->as.heap.ptr[0] = '\0';
1794
1795 return str;
1796}
1797
1798VALUE
1799rb_str_buf_new_cstr(const char *ptr)
1800{
1801 VALUE str;
1802 rb_len_t len = strlen(ptr);
1803
1804 str = rb_str_buf_new(len);
1805 rb_str_buf_cat(str, ptr, len);
1806
1807 return str;
1808}
1809
1810VALUE
1812{
1813 return str_new(0, 0, len);
1814}
1815
1816void
1818{
1819 if (STR_EMBED_P(str)) {
1820 RB_DEBUG_COUNTER_INC(obj_str_embed);
1821 }
1822 else if (FL_TEST(str, STR_SHARED | STR_NOFREE)) {
1823 (void)RB_DEBUG_COUNTER_INC_IF(obj_str_shared, FL_TEST(str, STR_SHARED));
1824 (void)RB_DEBUG_COUNTER_INC_IF(obj_str_shared, FL_TEST(str, STR_NOFREE));
1825 }
1826 else {
1827 RB_DEBUG_COUNTER_INC(obj_str_ptr);
1828 SIZED_FREE_N(STR_HEAP_PTR(str), STR_HEAP_SIZE(str));
1829 }
1830}
1831
1832size_t
1833rb_str_memsize(VALUE str)
1834{
1835 if (FL_TEST(str, STR_NOEMBED|STR_SHARED|STR_NOFREE) == STR_NOEMBED) {
1836 return STR_HEAP_SIZE(str);
1837 }
1838 else {
1839 return 0;
1840 }
1841}
1842
1843VALUE
1845{
1846 return rb_convert_type_with_id(str, T_STRING, "String", idTo_str);
1847}
1848
1849static inline void str_discard(VALUE str);
1850static void str_shared_replace(VALUE str, VALUE str2);
1851
1852void
1854{
1855 if (str != str2) str_shared_replace(str, str2);
1856}
1857
1858static void
1859str_shared_replace(VALUE str, VALUE str2)
1860{
1861 rb_encoding *enc;
1862 int cr;
1863 int termlen;
1864
1865 RUBY_ASSERT(str2 != str);
1866 enc = STR_ENC_GET(str2);
1867 cr = ENC_CODERANGE(str2);
1868 str_discard(str);
1869 termlen = rb_enc_mbminlen(enc);
1870
1871 STR_SET_LEN(str, RSTRING_LEN(str2));
1872
1873 if (str_embed_capa(str) >= RSTRING_LEN(str2) + termlen) {
1874 STR_SET_EMBED(str);
1875 memcpy(RSTRING_PTR(str), RSTRING_PTR(str2), (size_t)RSTRING_LEN(str2) + termlen);
1876 }
1877 else {
1878 if (STR_EMBED_P(str2)) {
1879 RUBY_ASSERT(!FL_TEST(str2, STR_SHARED));
1880 rb_len_t len = RSTRING_LEN(str2);
1881 RUBY_ASSERT(len + termlen <= str_embed_capa(str2));
1882
1883 char *new_ptr = ALLOC_N(char, len + termlen);
1884 memcpy(new_ptr, RSTRING(str2)->as.embed.ary, len + termlen);
1885 RSTRING(str2)->as.heap.ptr = new_ptr;
1886 STR_SET_LEN(str2, len);
1887 RSTRING(str2)->as.heap.aux.capa = len;
1888 STR_SET_NOEMBED(str2);
1889 }
1890
1891 STR_SET_NOEMBED(str);
1892 FL_UNSET(str, STR_SHARED);
1893 RSTRING(str)->as.heap.ptr = RSTRING_PTR(str2);
1894
1895 if (FL_TEST(str2, STR_SHARED)) {
1896 VALUE shared = RSTRING(str2)->as.heap.aux.shared;
1897 STR_SET_SHARED(str, shared);
1898 }
1899 else {
1900 RSTRING(str)->as.heap.aux.capa = RSTRING(str2)->as.heap.aux.capa;
1901 }
1902
1903 /* abandon str2 */
1904 STR_SET_EMBED(str2);
1905 RSTRING_PTR(str2)[0] = 0;
1906 STR_SET_LEN(str2, 0);
1907 }
1908
1909 // We used str2's termlen above so we set enc raw
1910 // to avoid adjusting it based on str1's old enc.
1911 rb_enc_raw_set(str, enc);
1912 ENC_CODERANGE_SET(str, cr);
1913}
1914
1915VALUE
1917{
1918 VALUE str;
1919
1920 if (RB_TYPE_P(obj, T_STRING)) {
1921 return obj;
1922 }
1923 str = rb_funcall(obj, idTo_s, 0);
1924 return rb_obj_as_string_result(str, obj);
1925}
1926
1927VALUE
1928rb_obj_as_string_result(VALUE str, VALUE obj)
1929{
1930 if (!RB_TYPE_P(str, T_STRING))
1931 return rb_any_to_s(obj);
1932 return str;
1933}
1934
1935static VALUE
1936str_replace(VALUE str, VALUE str2)
1937{
1938 rb_len_t len;
1939
1940 len = RSTRING_LEN(str2);
1941 if (STR_SHARED_P(str2)) {
1942 VALUE shared = RSTRING(str2)->as.heap.aux.shared;
1944 STR_SET_NOEMBED(str);
1945 STR_SET_LEN(str, len);
1946 RSTRING(str)->as.heap.ptr = RSTRING_PTR(str2);
1947 STR_SET_SHARED(str, shared);
1948 rb_enc_cr_str_exact_copy(str, str2);
1949 }
1950 else {
1951 str_replace_shared(str, str2);
1952 }
1953
1954 return str;
1955}
1956
1957static inline VALUE
1958ec_str_alloc_embed(struct rb_execution_context_struct *ec, VALUE klass, size_t capa)
1959{
1960 size_t size = rb_str_embed_size(capa, 0);
1961 RUBY_ASSERT(size > 0);
1962 RUBY_ASSERT(rb_gc_size_allocatable_p(size));
1963
1964 EC_NEWOBJ_OF(str, struct RString, klass, T_STRING, size, ec);
1965
1966 str->len = 0;
1967
1968 return (VALUE)str;
1969}
1970
1971static inline VALUE
1972ec_str_alloc_heap(struct rb_execution_context_struct *ec, VALUE klass)
1973{
1974 EC_NEWOBJ_OF(str, struct RString, klass, T_STRING | STR_NOEMBED, sizeof(struct RString), ec);
1975
1976 str->as.heap.aux.capa = 0;
1977 str->as.heap.ptr = NULL;
1978
1979 return (VALUE)str;
1980}
1981
1982static inline void
1983str_duplicate_setup_encoding(VALUE str, VALUE dup, VALUE flags)
1984{
1985 int encidx = 0;
1986 if ((flags & ENCODING_MASK) == (ENCODING_INLINE_MAX<<ENCODING_SHIFT)) {
1987 encidx = rb_enc_get_index(str);
1988 flags &= ~ENCODING_MASK;
1989 }
1990 FL_SET_RAW(dup, flags & ~FL_FREEZE);
1991 if (encidx) rb_enc_associate_index(dup, encidx);
1992}
1993
1994static const VALUE flag_mask = ENC_CODERANGE_MASK | ENCODING_MASK | FL_FREEZE;
1995
1996static inline void
1997str_duplicate_setup_embed(VALUE klass, VALUE str, VALUE dup)
1998{
1999 VALUE flags = FL_TEST_RAW(str, flag_mask);
2000 rb_len_t len = RSTRING_LEN(str);
2001
2002 RUBY_ASSERT(STR_EMBED_P(dup));
2003 RUBY_ASSERT(str_embed_capa(dup) >= len + TERM_LEN(str));
2004 MEMCPY(RSTRING(dup)->as.embed.ary, RSTRING(str)->as.embed.ary, char, len + TERM_LEN(str));
2005 STR_SET_LEN(dup, RSTRING_LEN(str));
2006 str_duplicate_setup_encoding(str, dup, flags);
2007}
2008
2009static inline void
2010str_duplicate_setup_heap(VALUE klass, VALUE str, VALUE dup)
2011{
2012 VALUE flags = FL_TEST_RAW(str, flag_mask);
2013 VALUE root = str;
2014 if (FL_TEST_RAW(str, STR_SHARED)) {
2015 root = RSTRING(str)->as.heap.aux.shared;
2016 }
2017 else if (UNLIKELY(!OBJ_FROZEN_RAW(str))) {
2018 root = str = str_new_frozen(klass, str);
2019 flags = FL_TEST_RAW(str, flag_mask);
2020 }
2021 RUBY_ASSERT(!STR_SHARED_P(root));
2023
2024 RSTRING(dup)->as.heap.ptr = RSTRING_PTR(str);
2025 FL_SET_RAW(dup, RSTRING_NOEMBED);
2026 STR_SET_SHARED(dup, root);
2027 flags |= RSTRING_NOEMBED | STR_SHARED;
2028
2029 STR_SET_LEN(dup, RSTRING_LEN(str));
2030 str_duplicate_setup_encoding(str, dup, flags);
2031}
2032
2033static inline VALUE
2034str_duplicate(VALUE klass, VALUE str)
2035{
2036 VALUE dup;
2037 if (STR_EMBED_P(str) && rb_str_embed_size(RSTRING_LEN(str), 1) <= STR_COPY_MAX_EMBED_SIZE) {
2038 dup = str_alloc_embed(klass, RSTRING_LEN(str) + TERM_LEN(str));
2039
2040 str_duplicate_setup_embed(klass, str, dup);
2041 }
2042 else {
2043 dup = str_alloc_heap(klass);
2044
2045 str_duplicate_setup_heap(klass, str, dup);
2046 }
2047
2048 return dup;
2049}
2050
2051VALUE
2053{
2054 return str_duplicate(rb_obj_class(str), str);
2055}
2056
2057/* :nodoc: */
2058VALUE
2059rb_str_dup_m(VALUE str)
2060{
2061 if (LIKELY(BARE_STRING_P(str))) {
2062 return str_duplicate(rb_cString, str);
2063 }
2064 else {
2065 return rb_obj_dup(str);
2066 }
2067}
2068
2069VALUE
2071{
2072 RUBY_DTRACE_CREATE_HOOK(STRING, RSTRING_LEN(str));
2073 return str_duplicate(rb_cString, str);
2074}
2075
2076VALUE
2077rb_ec_str_resurrect(struct rb_execution_context_struct *ec, VALUE str, bool chilled)
2078{
2079 RUBY_DTRACE_CREATE_HOOK(STRING, RSTRING_LEN(str));
2080 VALUE new_str, klass = rb_cString;
2081
2082 if (!(chilled && RTEST(rb_ivar_defined(str, id_debug_created_info))) && STR_EMBED_P(str)) {
2083 new_str = ec_str_alloc_embed(ec, klass, RSTRING_LEN(str) + TERM_LEN(str));
2084 str_duplicate_setup_embed(klass, str, new_str);
2085 }
2086 else {
2087 new_str = ec_str_alloc_heap(ec, klass);
2088 str_duplicate_setup_heap(klass, str, new_str);
2089 }
2090 if (chilled) {
2091 FL_SET_RAW(new_str, STR_CHILLED);
2092 }
2093 return new_str;
2094}
2095
2096#if USE_ZJIT
2097bool
2098rb_zjit_str_resurrect_fastpath(VALUE str, bool chilled, size_t *size_out,
2099 VALUE *flags_out,
2100 rb_len_t *len_out, size_t *byte_size_out)
2101{
2102 if (chilled && RTEST(rb_ivar_defined(str, id_debug_created_info))) return false;
2103
2104 if (!STR_EMBED_P(str)) return false;
2105
2106 rb_len_t len = RSTRING_LEN(str);
2107 rb_len_t termlen = TERM_LEN(str);
2108 size_t size = rb_str_embed_size(len + termlen, 0);
2109 if (!rb_gc_size_allocatable_p(size)) return false;
2110
2111 VALUE flags = FL_TEST_RAW(str, flag_mask);
2112
2113 if ((flags & ENCODING_MASK) == ((VALUE)ENCODING_INLINE_MAX << ENCODING_SHIFT)) {
2114 return false;
2115 }
2116
2117 flags &= ~FL_FREEZE;
2118 flags |= T_STRING;
2119 if (chilled) flags |= STR_CHILLED;
2120
2121 *size_out = size;
2122 *flags_out = flags;
2123 *len_out = len;
2124 *byte_size_out = (size_t)(len + termlen);
2125 return true;
2126}
2127#endif
2128
2129VALUE
2130rb_str_with_debug_created_info(VALUE str, VALUE path, int line)
2131{
2132 VALUE debug_info = rb_ary_new_from_args(2, path, INT2FIX(line));
2133 if (OBJ_FROZEN_RAW(str)) str = rb_str_dup(str);
2134 rb_ivar_set(str, id_debug_created_info, rb_ary_freeze(debug_info));
2135 FL_SET_RAW(str, STR_CHILLED);
2136 return rb_str_freeze(str);
2137}
2138
2139static VALUE
2140str_init(VALUE str, VALUE orig, VALUE venc, VALUE vcapa)
2141{
2142 orig = UNDEF_P(orig) ? 0 : StringValue(orig);
2143
2144 rb_len_t capa = NUM2LEN(vcapa);
2145 if (capa < 0) {
2146 capa = 0;
2147 }
2148
2149 rb_encoding *enc = UNDEF_P(venc) ? 0 : rb_to_encoding(venc);
2150
2151 if (capa || enc) {
2152 if (capa) {
2153 rb_len_t len = 0;
2154 int termlen;
2155
2156 if (capa < STR_BUF_MIN_SIZE) {
2157 capa = STR_BUF_MIN_SIZE;
2158 }
2159 if (orig) {
2160 len = RSTRING_LEN(orig);
2161 if (capa < len) {
2162 capa = len;
2163 }
2164 if (orig == str) orig = 0;
2165 }
2166 termlen = enc ? rb_enc_mbminlen(enc) : TERM_LEN(orig ? orig : str);
2167 str_modifiable(str);
2168
2169 const size_t size = (size_t)capa + termlen;
2170
2171 if (STR_EMBED_P(str) && (size_t)str_embed_capa(str) >= size) {
2172 // noop
2173 }
2174 else if (STR_EMBED_P(str) || FL_TEST(str, STR_SHARED|STR_NOFREE)) {
2175 const char *const old_ptr = RSTRING_PTR(str);
2176 const size_t osize = RSTRING_LEN(str) + TERM_LEN(str);
2177 char *new_ptr = ALLOC_N(char, size);
2178 if (STR_EMBED_P(str)) RUBY_ASSERT((rb_len_t)osize <= str_embed_capa(str));
2179 memcpy(new_ptr, old_ptr, osize < size ? osize : size);
2180 FL_UNSET_RAW(str, STR_SHARED|STR_NOFREE);
2181 RSTRING(str)->as.heap.ptr = new_ptr;
2182 RSTRING(str)->as.heap.aux.capa = capa;
2183 FL_SET(str, STR_NOEMBED);
2184 }
2185 else if (STR_HEAP_SIZE(str) != (size_t)capa + termlen) {
2186 SIZED_REALLOC_N(RSTRING(str)->as.heap.ptr, char,
2187 (size_t)capa + termlen, STR_HEAP_SIZE(str));
2188 RSTRING(str)->as.heap.aux.capa = capa;
2189 }
2190 STR_SET_LEN(str, len);
2191 char *buf = RSTRING_PTR(str);
2192 TERM_FILL(buf + len, termlen);
2193 if (orig) {
2194 memcpy(buf, RSTRING_PTR(orig), len);
2195 if (!enc || rb_enc_get(orig) == enc) {
2196 rb_enc_cr_str_exact_copy(str, orig);
2197 enc = 0;
2198 }
2199 }
2200 }
2201 else if (orig) {
2202 rb_str_replace(str, orig);
2203 }
2204 if (enc) {
2205 str_modifiable(str);
2206 if (capa) {
2207 /* the buffer is already sized for enc's terminator */
2208 rb_enc_raw_set(str, enc);
2209 }
2210 else {
2211 rb_enc_associate(str, enc);
2212 }
2214 }
2215 }
2216 else if (orig) {
2217 rb_str_replace(str, orig);
2218 }
2219 return str;
2220}
2221
2222static VALUE
2223rb_str_init(rb_execution_context_t *ec, VALUE str, VALUE orig, VALUE venc, VALUE venc_unset, VALUE vcapa)
2224{
2225 if (!NIL_P(venc_unset)) {
2226 venc = Qundef;
2227 }
2228
2229 return str_init(str, orig, venc, vcapa);
2230}
2231
2232static VALUE
2233rb_str_new_enc_capa(rb_execution_context_t *ec, VALUE str, VALUE venc, VALUE venc_unset, VALUE vcapa)
2234{
2235 if (!NIL_P(venc_unset)) {
2236 venc = Qundef;
2237 }
2238
2239 return str_init(str, Qundef, venc, vcapa);
2240}
2241
2242VALUE
2243rb_str_alloc_enc_capa(VALUE klass, VALUE encoding, VALUE capacity)
2244{
2245 rb_len_t capa = 0;
2246 if (FIXNUM_P(capacity)) {
2247 capa = NUM2LEN(capacity);
2248 if (capa < 0) capa = 0;
2249 }
2250
2251 rb_encoding *enc = rb_ascii8bit_encoding();
2252 int termlen = 1;
2253 if (!UNDEF_P(encoding) && !NIL_P(encoding)) {
2254 enc = rb_to_encoding(encoding);
2255 termlen = rb_enc_mbmaxlen(enc);
2256 }
2257
2258 RUBY_DTRACE_CREATE_HOOK(STRING, capa);
2259
2260 if (STR_EMBEDDABLE_P(capa, termlen)) {
2261 return str_alloc_embed(klass, capa + termlen);
2262 }
2263 else {
2264 return str_alloc_heap(klass);
2265 }
2266}
2267/* :nodoc: */
2268VALUE
2269rb_str_s_new(int argc, VALUE *argv, VALUE klass)
2270{
2271 if (klass != rb_cString) {
2272 return rb_class_new_instance_pass_kw(argc, argv, klass);
2273 }
2274
2275 VALUE venc = Qundef;
2276 VALUE vcapa = INT2FIX(0);
2277
2278 VALUE orig, opt;
2279 int n = rb_scan_args(argc, argv, "01:", &orig, &opt);
2280 if (n == 0) orig = Qundef;
2281
2282 if (!NIL_P(opt)) {
2283 ID keyword_ids[2] = {idEncoding, idCapacity};
2284 VALUE kwargs[2];
2285 rb_get_kwargs(opt, keyword_ids, 0, 2, kwargs);
2286 venc = kwargs[0];
2287 if (!UNDEF_P(kwargs[1])) {
2288 vcapa = kwargs[1];
2289 }
2290 }
2291
2292 VALUE str = rb_str_alloc_enc_capa(klass, venc, vcapa);
2293 str_init(str, orig, venc, vcapa);
2294 return str;
2295}
2296
2297#ifdef NONASCII_MASK
2298#define is_utf8_lead_byte(c) (((c)&0xC0) != 0x80)
2299
2300/*
2301 * UTF-8 leading bytes have either 0xxxxxxx or 11xxxxxx
2302 * bit representation. (see https://en.wikipedia.org/wiki/UTF-8)
2303 * Therefore, the following pseudocode can detect UTF-8 leading bytes.
2304 *
2305 * if (!(byte & 0x80))
2306 * byte |= 0x40; // turn on bit6
2307 * return ((byte>>6) & 1); // bit6 represent whether this byte is leading or not.
2308 *
2309 * This function calculates whether a byte is leading or not for all bytes
2310 * in the argument word by concurrently using the above logic, and then
2311 * adds up the number of leading bytes in the word.
2312 */
2313static inline uintptr_t
2314count_utf8_lead_bytes_with_word(const uintptr_t *s)
2315{
2316 uintptr_t d = *s;
2317
2318 /* Transform so that bit0 indicates whether we have a UTF-8 leading byte or not. */
2319 d = (d>>6) | (~d>>7);
2320 d &= NONASCII_MASK >> 7;
2321
2322 /* Gather all bytes. */
2323#if defined(HAVE_BUILTIN___BUILTIN_POPCOUNT) && defined(__POPCNT__)
2324 /* use only if it can use POPCNT */
2325 return rb_popcount_intptr(d);
2326#else
2327 d += (d>>8);
2328 d += (d>>16);
2329# if SIZEOF_VOIDP == 8
2330 d += (d>>32);
2331# endif
2332 return (d&0xF);
2333#endif
2334}
2335#endif
2336
2337static inline rb_len_t
2338enc_strlen(const char *p, const char *e, rb_encoding *enc, int cr)
2339{
2340 rb_len_t c;
2341 const char *q;
2342
2343 if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) {
2344 rb_len_t diff = (rb_len_t)(e - p);
2345 return diff / rb_enc_mbminlen(enc) + !!(diff % rb_enc_mbminlen(enc));
2346 }
2347#ifdef NONASCII_MASK
2348 else if (cr == ENC_CODERANGE_VALID && enc == rb_utf8_encoding()) {
2349 uintptr_t len = 0;
2350 if ((int)sizeof(uintptr_t) * 2 < e - p) {
2351 const uintptr_t *s, *t;
2352 const uintptr_t lowbits = sizeof(uintptr_t) - 1;
2353 s = (const uintptr_t*)(~lowbits & ((uintptr_t)p + lowbits));
2354 t = (const uintptr_t*)(~lowbits & (uintptr_t)e);
2355 while (p < (const char *)s) {
2356 if (is_utf8_lead_byte(*p)) len++;
2357 p++;
2358 }
2359 while (s < t) {
2360 len += count_utf8_lead_bytes_with_word(s);
2361 s++;
2362 }
2363 p = (const char *)s;
2364 }
2365 while (p < e) {
2366 if (is_utf8_lead_byte(*p)) len++;
2367 p++;
2368 }
2369 return (rb_len_t)len;
2370 }
2371#endif
2372 else if (rb_enc_asciicompat(enc)) {
2373 c = 0;
2374 if (ENC_CODERANGE_CLEAN_P(cr)) {
2375 while (p < e) {
2376 q = search_nonascii(p, e);
2377 if (!q)
2378 return c + (e - p);
2379 c += q - p;
2380 p = q;
2381 p += rb_enc_fast_mbclen(p, e, enc);
2382 c++;
2383 }
2384 }
2385 else {
2386 while (p < e) {
2387 q = search_nonascii(p, e);
2388 if (!q)
2389 return c + (e - p);
2390 c += q - p;
2391 p = q;
2392 p += rb_enc_mbclen(p, e, enc);
2393 c++;
2394 }
2395 }
2396 return c;
2397 }
2398
2399 for (c=0; p<e; c++) {
2400 p += rb_enc_mbclen(p, e, enc);
2401 }
2402 return c;
2403}
2404
2406rb_enc_strlen(const char *p, const char *e, rb_encoding *enc)
2407{
2408 return enc_strlen(p, e, enc, ENC_CODERANGE_UNKNOWN);
2409}
2410
2411/* To get strlen with cr
2412 * Note that given cr is not used.
2413 */
2415rb_enc_strlen_cr(const char *p, const char *e, rb_encoding *enc, int *cr)
2416{
2417 rb_len_t c;
2418 const char *q;
2419 int ret;
2420
2421 *cr = 0;
2422 if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) {
2423 rb_len_t diff = (rb_len_t)(e - p);
2424 return diff / rb_enc_mbminlen(enc) + !!(diff % rb_enc_mbminlen(enc));
2425 }
2426 else if (rb_enc_asciicompat(enc)) {
2427 c = 0;
2428 while (p < e) {
2429 q = search_nonascii(p, e);
2430 if (!q) {
2431 if (!*cr) *cr = ENC_CODERANGE_7BIT;
2432 return c + (e - p);
2433 }
2434 c += q - p;
2435 p = q;
2436 ret = rb_enc_precise_mbclen(p, e, enc);
2437 if (MBCLEN_CHARFOUND_P(ret)) {
2438 *cr |= ENC_CODERANGE_VALID;
2439 p += MBCLEN_CHARFOUND_LEN(ret);
2440 }
2441 else {
2443 p++;
2444 }
2445 c++;
2446 }
2447 if (!*cr) *cr = ENC_CODERANGE_7BIT;
2448 return c;
2449 }
2450
2451 for (c=0; p<e; c++) {
2452 ret = rb_enc_precise_mbclen(p, e, enc);
2453 if (MBCLEN_CHARFOUND_P(ret)) {
2454 *cr |= ENC_CODERANGE_VALID;
2455 p += MBCLEN_CHARFOUND_LEN(ret);
2456 }
2457 else {
2459 if (p + rb_enc_mbminlen(enc) <= e)
2460 p += rb_enc_mbminlen(enc);
2461 else
2462 p = e;
2463 }
2464 }
2465 if (!*cr) *cr = ENC_CODERANGE_7BIT;
2466 return c;
2467}
2468
2469/* enc must be str's enc or rb_enc_check(str, str2) */
2470static rb_len_t
2471str_strlen(VALUE str, rb_encoding *enc)
2472{
2473 const char *p, *e;
2474 int cr;
2475
2476 if (single_byte_optimizable(str)) return RSTRING_LEN(str);
2477 if (!enc) enc = STR_ENC_GET(str);
2478 p = RSTRING_PTR(str);
2479 e = RSTRING_END(str);
2480 cr = ENC_CODERANGE(str);
2481
2482 if (cr == ENC_CODERANGE_UNKNOWN) {
2483 rb_len_t n = rb_enc_strlen_cr(p, e, enc, &cr);
2484 if (cr) ENC_CODERANGE_SET(str, cr);
2485 return n;
2486 }
2487 else {
2488 return enc_strlen(p, e, enc, cr);
2489 }
2490}
2491
2494{
2495 return str_strlen(str, NULL);
2496}
2497
2498/*
2499 * call-seq:
2500 * length -> integer
2501 *
2502 * Returns the count of characters (not bytes) in +self+:
2503 *
2504 * 'foo'.length # => 3
2505 * 'こんにちは'.length # => 5
2506 *
2507 * Contrast with String#bytesize:
2508 *
2509 * 'foo'.bytesize # => 3
2510 * 'こんにちは'.bytesize # => 15
2511 *
2512 * Related: see {Querying}[rdoc-ref:String@Querying].
2513 *
2514 */
2515
2516VALUE
2518{
2519 return LEN2NUM(str_strlen(str, NULL));
2520}
2521
2522/*
2523 * call-seq:
2524 * bytesize -> integer
2525 *
2526 * Returns the count of bytes in +self+.
2527 *
2528 * Note that the byte count may be different from the character count (returned by #size):
2529 *
2530 * s = 'foo'
2531 * s.bytesize # => 3
2532 * s.size # => 3
2533 * s = 'こんにちは'
2534 * s.bytesize # => 15
2535 * s.size # => 5
2536 *
2537 * Related: see {Querying}[rdoc-ref:String@Querying].
2538 *
2539 */
2540
2541VALUE
2542rb_str_bytesize(VALUE str)
2543{
2544 return LEN2NUM(RSTRING_LEN(str));
2545}
2546
2547/*
2548 * call-seq:
2549 * empty? -> true or false
2550 *
2551 * Returns whether the length of +self+ is zero:
2552 *
2553 * 'hello'.empty? # => false
2554 * ' '.empty? # => false
2555 * ''.empty? # => true
2556 *
2557 * Related: see {Querying}[rdoc-ref:String@Querying].
2558 */
2559
2560static VALUE
2561rb_str_empty(VALUE str)
2562{
2563 return RBOOL(RSTRING_LEN(str) == 0);
2564}
2565
2566/*
2567 * call-seq:
2568 * self + other_string -> new_string
2569 *
2570 * Returns a new string containing +other_string+ concatenated to +self+:
2571 *
2572 * 'Hello from ' + self.to_s # => "Hello from main"
2573 *
2574 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
2575 */
2576
2577VALUE
2579{
2580 VALUE str3;
2581 rb_encoding *enc;
2582 const char *ptr1, *ptr2;
2583 char *ptr3;
2584 rb_len_t len1, len2;
2585 int termlen;
2586
2587 StringValue(str2);
2588 enc = rb_enc_check_str(str1, str2);
2589 RSTRING_GETMEM(str1, ptr1, len1);
2590 RSTRING_GETMEM(str2, ptr2, len2);
2591 termlen = rb_enc_mbminlen(enc);
2592 if (len1 > RB_LEN_MAX - len2) {
2593 rb_raise(rb_eArgError, "string size too big");
2594 }
2595 str3 = str_enc_new(rb_cString, 0, len1+len2, enc);
2596 ptr3 = RSTRING_PTR(str3);
2597 memcpy(ptr3, ptr1, len1);
2598 memcpy(ptr3+len1, ptr2, len2);
2599 TERM_FILL(&ptr3[len1+len2], termlen);
2600
2601 ENCODING_CODERANGE_SET(str3, rb_enc_to_index(enc),
2603 RB_GC_GUARD(str1);
2604 RB_GC_GUARD(str2);
2605 return str3;
2606}
2607
2608/* A variant of rb_str_plus that does not raise but return Qundef instead. */
2609VALUE
2610rb_str_opt_plus(VALUE str1, VALUE str2)
2611{
2614 rb_len_t len1, len2;
2615 MAYBE_UNUSED(char) *ptr1, *ptr2;
2616 RSTRING_GETMEM(str1, ptr1, len1);
2617 RSTRING_GETMEM(str2, ptr2, len2);
2618 int enc1 = rb_enc_get_index(str1);
2619 int enc2 = rb_enc_get_index(str2);
2620
2621 if (enc1 < 0) {
2622 return Qundef;
2623 }
2624 else if (enc2 < 0) {
2625 return Qundef;
2626 }
2627 else if (enc1 != enc2) {
2628 return Qundef;
2629 }
2630 else if (len1 > RB_LEN_MAX - len2) {
2631 return Qundef;
2632 }
2633 else {
2634 return rb_str_plus(str1, str2);
2635 }
2636
2637}
2638
2639/*
2640 * call-seq:
2641 * self * n -> new_string
2642 *
2643 * Returns a new string containing +n+ copies of +self+:
2644 *
2645 * 'Ho!' * 3 # => "Ho!Ho!Ho!"
2646 * 'No!' * 0 # => ""
2647 *
2648 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
2649 */
2650
2651VALUE
2653{
2654 VALUE str2;
2655 rb_len_t n, len;
2656 char *ptr2;
2657 int termlen;
2658
2659 if (times == INT2FIX(1)) {
2660 return str_duplicate(rb_cString, str);
2661 }
2662 if (times == INT2FIX(0)) {
2663 str2 = str_alloc_embed(rb_cString, 0);
2664 rb_enc_copy(str2, str);
2665 return str2;
2666 }
2667 len = NUM2LEN(times);
2668 if (len < 0) {
2669 rb_raise(rb_eArgError, "negative argument");
2670 }
2671 if (RSTRING_LEN(str) == 1 && RSTRING_PTR(str)[0] == 0) {
2672 if (STR_EMBEDDABLE_P(len, 1)) {
2673 str2 = str_alloc_embed(rb_cString, len + 1);
2674 memset(RSTRING_PTR(str2), 0, len + 1);
2675 }
2676 else {
2677 str2 = str_alloc_heap(rb_cString);
2678 RSTRING(str2)->as.heap.aux.capa = len;
2679 RSTRING(str2)->as.heap.ptr = ZALLOC_N(char, (size_t)len + 1);
2680 }
2681 STR_SET_LEN(str2, len);
2682 rb_enc_copy(str2, str);
2683 return str2;
2684 }
2685 if (len && RB_LEN_MAX/len < RSTRING_LEN(str)) {
2686 rb_raise(rb_eArgError, "argument too big");
2687 }
2688
2689 len *= RSTRING_LEN(str);
2690 termlen = TERM_LEN(str);
2691 str2 = str_enc_new(rb_cString, 0, len, STR_ENC_GET(str));
2692 ptr2 = RSTRING_PTR(str2);
2693 if (len) {
2694 n = RSTRING_LEN(str);
2695 memcpy(ptr2, RSTRING_PTR(str), n);
2696 while (n <= len/2) {
2697 memcpy(ptr2 + n, ptr2, n);
2698 n *= 2;
2699 }
2700 memcpy(ptr2 + n, ptr2, len-n);
2701 }
2702 STR_SET_LEN(str2, len);
2703 TERM_FILL(&ptr2[len], termlen);
2704 rb_enc_cr_str_copy_for_substr(str2, str);
2705
2706 return str2;
2707}
2708
2709/*
2710 * call-seq:
2711 * self % object -> new_string
2712 *
2713 * Returns the result of formatting +object+ into the format specifications
2714 * contained in +self+
2715 * (see {Format Specifications}[rdoc-ref:language/format_specifications.rdoc]):
2716 *
2717 * '%05d' % 123 # => "00123"
2718 *
2719 * If +self+ contains multiple format specifications,
2720 * +object+ must be an array or hash containing the objects to be formatted:
2721 *
2722 * '%-5s: %016x' % [ 'ID', self.object_id ] # => "ID : 00002b054ec93168"
2723 * 'foo = %{foo}' % {foo: 'bar'} # => "foo = bar"
2724 * 'foo = %{foo}, baz = %{baz}' % {foo: 'bar', baz: 'bat'} # => "foo = bar, baz = bat"
2725 *
2726 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
2727 */
2728
2729static VALUE
2730rb_str_format_m(VALUE str, VALUE arg)
2731{
2732 VALUE tmp = rb_check_array_type(arg);
2733
2734 if (!NIL_P(tmp)) {
2735 VALUE result = rb_str_format_ary(RARRAY_LENINT(tmp), RARRAY_CONST_PTR(tmp), str, tmp);
2736 RB_GC_GUARD(tmp);
2737 return result;
2738 }
2739 return rb_str_format(1, &arg, str);
2740}
2741
2742static inline void
2743rb_check_lockedtmp(VALUE str)
2744{
2745 if (FL_TEST(str, STR_TMPLOCK)) {
2746 rb_raise(rb_eRuntimeError, "can't modify string; temporarily locked");
2747 }
2748}
2749
2750// If none of these flags are set, we know we have an modifiable string.
2751// If any is set, we need to do more detailed checks.
2752#define STR_UNMODIFIABLE_MASK (FL_FREEZE | STR_TMPLOCK | STR_CHILLED)
2753static inline void
2754str_modifiable(VALUE str)
2755{
2757
2758 if (RB_UNLIKELY(FL_ANY_RAW(str, STR_UNMODIFIABLE_MASK))) {
2759 if (CHILLED_STRING_P(str)) {
2760 CHILLED_STRING_MUTATED(str);
2761 }
2762 rb_check_lockedtmp(str);
2763 rb_check_frozen(str);
2764 }
2765}
2766
2767static inline int
2768str_dependent_p(VALUE str)
2769{
2770 if (STR_EMBED_P(str) || !FL_TEST(str, STR_SHARED|STR_NOFREE)) {
2771 return FALSE;
2772 }
2773 else {
2774 return TRUE;
2775 }
2776}
2777
2778// If none of these flags are set, we know we have an independent string.
2779// If any is set, we need to do more detailed checks.
2780#define STR_DEPENDANT_MASK (STR_UNMODIFIABLE_MASK | STR_SHARED | STR_NOFREE)
2781static inline int
2782str_independent(VALUE str)
2783{
2785
2786 if (RB_UNLIKELY(FL_ANY_RAW(str, STR_DEPENDANT_MASK))) {
2787 str_modifiable(str);
2788 return !str_dependent_p(str);
2789 }
2790 return TRUE;
2791}
2792
2793static void
2794str_make_independent_expand(VALUE str, rb_len_t len, rb_len_t expand, const int termlen)
2795{
2797
2798 char *ptr;
2799 char *oldptr;
2800 rb_len_t capa = len + expand;
2801
2802 if (len > capa) len = capa;
2803
2804 if (!STR_EMBED_P(str) && str_embed_capa(str) >= capa + termlen) {
2805 ptr = RSTRING(str)->as.heap.ptr;
2806 STR_SET_EMBED(str);
2807 memcpy(RSTRING(str)->as.embed.ary, ptr, len);
2808 TERM_FILL(RSTRING(str)->as.embed.ary + len, termlen);
2809 STR_SET_LEN(str, len);
2810 return;
2811 }
2812
2813 ptr = ALLOC_N(char, (size_t)capa + termlen);
2814 oldptr = RSTRING_PTR(str);
2815 if (oldptr) {
2816 memcpy(ptr, oldptr, len);
2817 }
2818 if (FL_TEST_RAW(str, STR_NOEMBED|STR_NOFREE|STR_SHARED) == STR_NOEMBED) {
2819 SIZED_FREE_N(oldptr, STR_HEAP_SIZE(str));
2820 }
2821 STR_SET_NOEMBED(str);
2822 FL_UNSET(str, STR_SHARED|STR_NOFREE);
2823 TERM_FILL(ptr + len, termlen);
2824 RSTRING(str)->as.heap.ptr = ptr;
2825 STR_SET_LEN(str, len);
2826 RSTRING(str)->as.heap.aux.capa = capa;
2827}
2828
2829void
2830rb_str_modify(VALUE str)
2831{
2832 if (!str_independent(str))
2833 str_make_independent(str);
2835}
2836
2837void
2839{
2841
2842 int termlen = TERM_LEN(str);
2843 rb_len_t len = RSTRING_LEN(str);
2844
2845 if (expand < 0) {
2846 rb_raise(rb_eArgError, "negative expanding string size");
2847 }
2848 if (expand > RB_LEN_MAX - termlen - len) {
2849 rb_raise(rb_eArgError, "string size too big");
2850 }
2851
2852 if (!str_independent(str)) {
2853 str_make_independent_expand(str, len, expand, termlen);
2854 }
2855 else if (expand > 0) {
2856 RESIZE_CAPA_TERM(str, len + expand, termlen);
2857 }
2859}
2860
2861/* As rb_str_modify(), but don't clear coderange */
2862static void
2863str_modify_keep_cr(VALUE str)
2864{
2865 if (!str_independent(str))
2866 str_make_independent(str);
2868 /* Force re-scan later */
2870}
2871
2872static inline void
2873str_discard(VALUE str)
2874{
2875 str_modifiable(str);
2876 if (!STR_EMBED_P(str) && !FL_TEST(str, STR_SHARED|STR_NOFREE)) {
2877 SIZED_FREE_N(STR_HEAP_PTR(str), STR_HEAP_SIZE(str));
2878 RSTRING(str)->as.heap.ptr = 0;
2879 STR_SET_LEN(str, 0);
2880 }
2881}
2882
2883void
2885{
2886 int encindex = rb_enc_get_index(str);
2887
2888 if (RB_UNLIKELY(encindex == -1)) {
2889 rb_raise(rb_eTypeError, "not encoding capable object");
2890 }
2891
2892 if (RB_LIKELY(rb_str_encindex_fastpath(encindex))) {
2893 return;
2894 }
2895
2896 rb_encoding *enc = rb_enc_from_index(encindex);
2897 if (!rb_enc_asciicompat(enc)) {
2898 rb_raise(rb_eEncCompatError, "ASCII incompatible encoding: %s", rb_enc_name(enc));
2899 }
2900}
2901
2902VALUE
2904{
2906
2907 VALUE s = *ptr;
2908 if (!RB_TYPE_P(s, T_STRING)) {
2909 s = rb_str_to_str(s);
2910 *ptr = s;
2911 }
2912 return s;
2913}
2914
2915char *
2917{
2918 VALUE str = rb_string_value(ptr);
2919 return RSTRING_PTR(str);
2920}
2921
2922static const char *
2923str_null_char(const char *s, rb_len_t len, const int minlen, rb_encoding *enc)
2924{
2925 const char *e = s + len;
2926
2927 for (; s + minlen <= e; s += rb_enc_mbclen(s, e, enc)) {
2928 if (zero_filled(s, minlen)) return s;
2929 }
2930 return 0;
2931}
2932
2933static char *
2934str_fill_term(VALUE str, char *s, rb_len_t len, int termlen)
2935{
2936 /* This function assumes that (capa + termlen) bytes of memory
2937 * is allocated, like many other functions in this file.
2938 */
2939 if (str_dependent_p(str)) {
2940 if (!zero_filled(s + len, termlen))
2941 str_make_independent_expand(str, len, 0L, termlen);
2942 }
2943 else {
2944 TERM_FILL(s + len, termlen);
2945 return s;
2946 }
2947 return RSTRING_PTR(str);
2948}
2949
2950void
2951rb_str_change_terminator_length(VALUE str, const int oldtermlen, const int termlen)
2952{
2953 rb_len_t capa = str_capacity(str, oldtermlen) + oldtermlen;
2954 rb_len_t len = RSTRING_LEN(str);
2955
2956 RUBY_ASSERT(capa >= len);
2957 if (capa - len < termlen) {
2958 rb_check_lockedtmp(str);
2959 str_make_independent_expand(str, len, 0L, termlen);
2960 }
2961 else if (str_dependent_p(str)) {
2962 if (termlen > oldtermlen)
2963 str_make_independent_expand(str, len, 0L, termlen);
2964 }
2965 else {
2966 if (!STR_EMBED_P(str)) {
2967 /* modify capa instead of realloc */
2968 RUBY_ASSERT(!FL_TEST((str), STR_SHARED));
2969 RSTRING(str)->as.heap.aux.capa = capa - termlen;
2970 }
2971 if (termlen > oldtermlen) {
2972 TERM_FILL(RSTRING_PTR(str) + len, termlen);
2973 }
2974 }
2975
2976 return;
2977}
2978
2979static char *
2980str_null_check(VALUE str, int *w)
2981{
2982 char *s = RSTRING_PTR(str);
2983 rb_len_t len = RSTRING_LEN(str);
2984 int minlen = 1;
2985
2986 if (RB_UNLIKELY(!rb_str_enc_fastpath(str))) {
2987 rb_encoding *enc = rb_str_enc_get(str);
2988 minlen = rb_enc_mbminlen(enc);
2989
2990 if (minlen > 1) {
2991 *w = 1;
2992 if (str_null_char(s, len, minlen, enc)) {
2993 return NULL;
2994 }
2995 return str_fill_term(str, s, len, minlen);
2996 }
2997 }
2998
2999 *w = 0;
3000 if (!s || memchr(s, 0, len)) {
3001 return NULL;
3002 }
3003 if (s[len]) {
3004 s = str_fill_term(str, s, len, minlen);
3005 }
3006 return s;
3007}
3008
3009static char *str_to_cstr(VALUE str);
3010
3011const char *
3012rb_str_null_check(VALUE str)
3013{
3015
3016 const char *s;
3017 rb_len_t len;
3018 RSTRING_GETMEM(str, s, len);
3019
3020 if (RB_LIKELY(rb_str_enc_fastpath(str))) {
3021 if (!s || memchr(s, 0, len)) {
3022 rb_raise(rb_eArgError, "string contains null byte");
3023 }
3024 }
3025 else {
3026 str_to_cstr(str);
3027 }
3028
3029 return s;
3030}
3031
3032char *
3033rb_str_to_cstr(VALUE str)
3034{
3035 int w;
3036 return str_null_check(str, &w);
3037}
3038
3039char *
3041{
3042 VALUE str = rb_string_value(ptr);
3043 return str_to_cstr(str);
3044}
3045
3046static char *
3047str_to_cstr(VALUE str)
3048{
3049 int w;
3050 char *s = str_null_check(str, &w);
3051 if (!s) {
3052 if (w) {
3053 rb_raise(rb_eArgError, "string contains null char");
3054 }
3055 rb_raise(rb_eArgError, "string contains null byte");
3056 }
3057 return s;
3058}
3059
3060char *
3061rb_str_fill_terminator(VALUE str, const int newminlen)
3062{
3063 char *s = RSTRING_PTR(str);
3064 rb_len_t len = RSTRING_LEN(str);
3065 return str_fill_term(str, s, len, newminlen);
3066}
3067
3068VALUE
3070{
3071 str = rb_check_convert_type_with_id(str, T_STRING, "String", idTo_str);
3072 return str;
3073}
3074
3075/*
3076 * call-seq:
3077 * String.try_convert(object) -> object, new_string, or nil
3078 *
3079 * Attempts to convert the given +object+ to a string.
3080 *
3081 * If +object+ is already a string, returns +object+, unmodified.
3082 *
3083 * Otherwise if +object+ responds to <tt>:to_str</tt>,
3084 * calls <tt>object.to_str</tt> and returns the result.
3085 *
3086 * Returns +nil+ if +object+ does not respond to <tt>:to_str</tt>.
3087 *
3088 * Raises an exception unless <tt>object.to_str</tt> returns a string.
3089 */
3090static VALUE
3091rb_str_s_try_convert(VALUE dummy, VALUE str)
3092{
3093 return rb_check_string_type(str);
3094}
3095
3096static char*
3097str_nth_len(const char *p, const char *e, rb_len_t *nthp, rb_encoding *enc)
3098{
3099 rb_len_t nth = *nthp;
3100 if (rb_enc_mbmaxlen(enc) == 1) {
3101 p += nth;
3102 }
3103 else if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) {
3104 p += nth * rb_enc_mbmaxlen(enc);
3105 }
3106 else if (rb_enc_asciicompat(enc)) {
3107 const char *p2, *e2;
3108 int n;
3109
3110 while (p < e && 0 < nth) {
3111 e2 = p + nth;
3112 if (e < e2) {
3113 *nthp = nth;
3114 return (char *)e;
3115 }
3116 p2 = search_nonascii(p, e2);
3117 if (!p2) {
3118 nth -= e2 - p;
3119 *nthp = nth;
3120 return (char *)e2;
3121 }
3122 nth -= p2 - p;
3123 p = p2;
3124 n = rb_enc_mbclen(p, e, enc);
3125 p += n;
3126 nth--;
3127 }
3128 *nthp = nth;
3129 if (nth != 0) {
3130 return (char *)e;
3131 }
3132 return (char *)p;
3133 }
3134 else {
3135 while (p < e && nth--) {
3136 p += rb_enc_mbclen(p, e, enc);
3137 }
3138 }
3139 if (p > e) p = e;
3140 *nthp = nth;
3141 return (char*)p;
3142}
3143
3144char*
3145rb_enc_nth(const char *p, const char *e, rb_len_t nth, rb_encoding *enc)
3146{
3147 return str_nth_len(p, e, &nth, enc);
3148}
3149
3150static char*
3151str_nth(const char *p, const char *e, rb_len_t nth, rb_encoding *enc, int singlebyte)
3152{
3153 if (singlebyte)
3154 p += nth;
3155 else {
3156 p = str_nth_len(p, e, &nth, enc);
3157 }
3158 if (!p) return 0;
3159 if (p > e) p = e;
3160 return (char *)p;
3161}
3162
3163/* char offset to byte offset */
3164static rb_len_t
3165str_offset(const char *p, const char *e, rb_len_t nth, rb_encoding *enc, int singlebyte)
3166{
3167 const char *pp = str_nth(p, e, nth, enc, singlebyte);
3168 if (!pp) return e - p;
3169 return pp - p;
3170}
3171
3174{
3175 return str_offset(RSTRING_PTR(str), RSTRING_END(str), pos,
3176 STR_ENC_GET(str), single_byte_optimizable(str));
3177}
3178
3179#ifdef NONASCII_MASK
3180static char *
3181str_utf8_nth(const char *p, const char *e, rb_len_t *nthp)
3182{
3183 rb_len_t nth = *nthp;
3184 if ((int)SIZEOF_VOIDP * 2 < e - p && (int)SIZEOF_VOIDP * 2 < nth) {
3185 const uintptr_t *s, *t;
3186 const uintptr_t lowbits = SIZEOF_VOIDP - 1;
3187 s = (const uintptr_t*)(~lowbits & ((uintptr_t)p + lowbits));
3188 t = (const uintptr_t*)(~lowbits & (uintptr_t)e);
3189 while (p < (const char *)s) {
3190 if (is_utf8_lead_byte(*p)) nth--;
3191 p++;
3192 }
3193 do {
3194 nth -= count_utf8_lead_bytes_with_word(s);
3195 s++;
3196 } while (s < t && (int)SIZEOF_VOIDP <= nth);
3197 p = (char *)s;
3198 }
3199 while (p < e) {
3200 if (is_utf8_lead_byte(*p)) {
3201 if (nth == 0) break;
3202 nth--;
3203 }
3204 p++;
3205 }
3206 *nthp = nth;
3207 return (char *)p;
3208}
3209
3210static rb_len_t
3211str_utf8_offset(const char *p, const char *e, rb_len_t nth)
3212{
3213 const char *pp = str_utf8_nth(p, e, &nth);
3214 return pp - p;
3215}
3216#endif
3217
3218/* byte offset to char offset */
3221{
3222 if (single_byte_optimizable(str) || pos < 0)
3223 return pos;
3224 else {
3225 const char *p = RSTRING_PTR(str);
3226 return enc_strlen(p, p + pos, STR_ENC_GET(str), ENC_CODERANGE(str));
3227 }
3228}
3229
3230static VALUE
3231str_subseq(VALUE str, rb_len_t beg, rb_len_t len)
3232{
3233 VALUE str2;
3234
3235 RUBY_ASSERT(beg >= 0);
3236 RUBY_ASSERT(len >= 0);
3237 RUBY_ASSERT(beg+len <= RSTRING_LEN(str));
3238
3239 const int termlen = TERM_LEN(str);
3240 if (!SHARABLE_SUBSTRING_P(str, beg, len)) {
3241 str2 = rb_enc_str_new(RSTRING_PTR(str) + beg, len, rb_str_enc_get(str));
3242 if (ENC_CODERANGE(str) == ENC_CODERANGE_7BIT) {
3244 }
3245 RB_GC_GUARD(str);
3246 return str2;
3247 }
3248
3249 /* Sharing allocates a shared root as well unless str can be one itself, so
3250 * a copy is worth a larger slot only when it saves that second object. */
3251 const bool root_available = STR_SHARED_P(str) ||
3252 RB_FL_TEST_RAW(str, FL_FREEZE | STR_CHILLED) == FL_FREEZE;
3253 const size_t max_embed_size = root_available ?
3254 rb_gc_size_slot_size(sizeof(struct RString)) : STR_COPY_MAX_EMBED_SIZE;
3255 const size_t embed_size = rb_str_embed_size(len, termlen);
3256
3257 if (embed_size <= max_embed_size && rb_gc_size_allocatable_p(embed_size)) {
3258 str2 = str_alloc_embed(rb_cString, len + termlen);
3259 char *ptr2 = RSTRING(str2)->as.embed.ary;
3260 memcpy(ptr2, RSTRING_PTR(str) + beg, len);
3261 TERM_FILL(ptr2 + len, termlen);
3262
3263 STR_SET_LEN(str2, len);
3264 if (ENC_CODERANGE(str) == ENC_CODERANGE_7BIT) {
3266 }
3267
3268 RB_GC_GUARD(str);
3269 }
3270 else {
3271 str2 = str_alloc_heap(rb_cString);
3272 str_replace_shared(str2, str);
3273 RUBY_ASSERT(!STR_EMBED_P(str2));
3274 if (ENC_CODERANGE(str) != ENC_CODERANGE_7BIT) {
3275 ENC_CODERANGE_CLEAR(str2);
3276 }
3277
3278 RSTRING(str2)->as.heap.ptr += beg;
3279 if (RSTRING_LEN(str2) > len) {
3280 STR_SET_LEN(str2, len);
3281 }
3282 }
3283
3284 return str2;
3285}
3286
3287VALUE
3289{
3290 VALUE str2 = str_subseq(str, beg, len);
3291 rb_enc_cr_str_copy_for_substr(str2, str);
3292 return str2;
3293}
3294
3295char *
3297{
3298 rb_len_t len = *lenp;
3299 rb_len_t slen = -1L;
3300 const rb_len_t blen = RSTRING_LEN(str);
3301 rb_encoding *enc = STR_ENC_GET(str);
3302 const char *p, *s = RSTRING_PTR(str), *e = s + blen;
3303
3304 if (len < 0) return 0;
3305 if (beg == RB_LEN_MIN) return 0;
3306 if (!blen) {
3307 len = 0;
3308 }
3309 if (single_byte_optimizable(str)) {
3310 if (beg > blen) return 0;
3311 if (beg < 0) {
3312 beg += blen;
3313 if (beg < 0) return 0;
3314 }
3315 if (len > blen - beg)
3316 len = blen - beg;
3317 if (len < 0) return 0;
3318 p = s + beg;
3319 goto end;
3320 }
3321 if (beg < 0) {
3322 if (len > -beg) len = -beg;
3323 if ((ENC_CODERANGE(str) == ENC_CODERANGE_VALID) &&
3324 (-beg * rb_enc_mbmaxlen(enc) < blen / 8)) {
3325 beg = -beg;
3326 while (beg-- > len && (e = rb_enc_prev_char(s, e, e, enc)) != 0);
3327 p = e;
3328 if (!p) return 0;
3329 while (len-- > 0 && (p = rb_enc_prev_char(s, p, e, enc)) != 0);
3330 if (!p) return 0;
3331 len = e - p;
3332 goto end;
3333 }
3334 else {
3335 slen = str_strlen(str, enc);
3336 beg += slen;
3337 if (beg < 0) return 0;
3338 p = s + beg;
3339 if (len == 0) goto end;
3340 }
3341 }
3342 else if (beg > 0 && beg > blen) {
3343 return 0;
3344 }
3345 if (len == 0) {
3346 if (beg > str_strlen(str, enc)) return 0; /* str's enc */
3347 p = s + beg;
3348 }
3349#ifdef NONASCII_MASK
3350 else if (ENC_CODERANGE(str) == ENC_CODERANGE_VALID &&
3351 enc == rb_utf8_encoding()) {
3352 p = str_utf8_nth(s, e, &beg);
3353 if (beg > 0) return 0;
3354 len = str_utf8_offset(p, e, len);
3355 }
3356#endif
3357 else if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) {
3358 int char_sz = rb_enc_mbmaxlen(enc);
3359
3360 p = s + beg * char_sz;
3361 if (p > e) {
3362 return 0;
3363 }
3364 else if (len * char_sz > e - p)
3365 len = e - p;
3366 else
3367 len *= char_sz;
3368 }
3369 else if ((p = str_nth_len(s, e, &beg, enc)) == e) {
3370 if (beg > 0) return 0;
3371 len = 0;
3372 }
3373 else {
3374 len = str_offset(p, e, len, enc, 0);
3375 }
3376 end:
3377 *lenp = len;
3378 RB_GC_GUARD(str);
3379 return (char *)p;
3380}
3381
3382static VALUE str_substr(VALUE str, rb_len_t beg, rb_len_t len, int empty);
3383
3384VALUE
3386{
3387 return str_substr(str, beg, len, TRUE);
3388}
3389
3390VALUE
3391rb_str_substr_two_fixnums(VALUE str, VALUE beg, VALUE len, int empty)
3392{
3393 return str_substr(str, NUM2LEN(beg), NUM2LEN(len), empty);
3394}
3395
3396static VALUE
3397str_substr(VALUE str, rb_len_t beg, rb_len_t len, int empty)
3398{
3399 const char *p = rb_str_subpos(str, beg, &len);
3400
3401 if (!p) return Qnil;
3402 if (!len && !empty) return Qnil;
3403
3404 beg = p - RSTRING_PTR(str);
3405
3406 VALUE str2 = str_subseq(str, beg, len);
3407 rb_enc_cr_str_copy_for_substr(str2, str);
3408 return str2;
3409}
3410
3411/* :nodoc: */
3412VALUE
3414{
3415 if (CHILLED_STRING_P(str)) {
3416 FL_UNSET_RAW(str, STR_CHILLED);
3417 }
3418
3419 if (OBJ_FROZEN(str)) return str;
3420 rb_str_resize(str, RSTRING_LEN(str));
3421 return rb_obj_freeze(str);
3422}
3423
3424/*
3425 * call-seq:
3426 * +string -> new_string or self
3427 *
3428 * Returns +self+ if +self+ is not frozen and can be mutated
3429 * without warning issuance.
3430 *
3431 * Otherwise returns <tt>self.dup</tt>, which is not frozen.
3432 *
3433 * Related: see {Freezing/Unfreezing}[rdoc-ref:String@FreezingUnfreezing].
3434 */
3435static VALUE
3436str_uplus(VALUE str)
3437{
3438 if (OBJ_FROZEN(str) || CHILLED_STRING_P(str)) {
3439 return rb_str_dup(str);
3440 }
3441 else {
3442 return str;
3443 }
3444}
3445
3446/*
3447 * call-seq:
3448 * -self -> frozen_string
3449 *
3450 * Returns a frozen string equal to +self+.
3451 *
3452 * The returned string is +self+ if and only if all of the following are true:
3453 *
3454 * - +self+ is already frozen.
3455 * - +self+ is an instance of \String (rather than of a subclass of \String)
3456 * - +self+ has no instance variables set on it.
3457 *
3458 * Otherwise, the returned string is a frozen copy of +self+.
3459 *
3460 * Returning +self+, when possible, saves duplicating +self+;
3461 * see {Data deduplication}[https://en.wikipedia.org/wiki/Data_deduplication].
3462 *
3463 * It may also save duplicating other, already-existing, strings:
3464 *
3465 * s0 = 'foo'
3466 * s1 = 'foo'
3467 * s0.object_id == s1.object_id # => false
3468 * (-s0).object_id == (-s1).object_id # => true
3469 *
3470 * Note that method #-@ is convenient for defining a constant:
3471 *
3472 * FileName = -'config/database.yml'
3473 *
3474 * While its alias #dedup is better suited for chaining:
3475 *
3476 * 'foo'.dedup.gsub!('o')
3477 *
3478 * Related: see {Freezing/Unfreezing}[rdoc-ref:String@FreezingUnfreezing].
3479 */
3480static VALUE
3481str_uminus(VALUE str)
3482{
3483 if (!BARE_STRING_P(str) && !rb_obj_frozen_p(str)) {
3484 str = rb_str_dup(str);
3485 }
3486 return rb_fstring(str);
3487}
3488
3489RUBY_ALIAS_FUNCTION(rb_str_dup_frozen(VALUE str), rb_str_new_frozen, (str))
3490#define rb_str_dup_frozen rb_str_new_frozen
3491
3492VALUE
3494{
3495 rb_check_frozen(str);
3496 if (FL_TEST(str, STR_TMPLOCK)) {
3497 rb_raise(rb_eRuntimeError, "temporal locking already locked string");
3498 }
3499 FL_SET(str, STR_TMPLOCK);
3500 return str;
3501}
3502
3503VALUE
3505{
3506 rb_check_frozen(str);
3507 if (!FL_TEST(str, STR_TMPLOCK)) {
3508 rb_raise(rb_eRuntimeError, "temporal unlocking already unlocked string");
3509 }
3510 FL_UNSET(str, STR_TMPLOCK);
3511 return str;
3512}
3513
3514VALUE
3515rb_str_locktmp_ensure(VALUE str, VALUE (*func)(VALUE), VALUE arg)
3516{
3517 rb_str_locktmp(str);
3518 return rb_ensure(func, arg, rb_str_unlocktmp, str);
3519}
3520
3521void
3523{
3525
3526 rb_len_t capa;
3527 const int termlen = TERM_LEN(str);
3528
3529 str_modifiable(str);
3530 if (STR_SHARED_P(str)) {
3531 rb_raise(rb_eRuntimeError, "can't set length of shared string");
3532 }
3533 if (len > (capa = (rb_len_t)str_capacity(str, termlen)) || len < 0) {
3534 rb_bug("probable buffer overflow: %"PRIdLEN" for %"PRIdLEN, len, capa);
3535 }
3536
3537 int cr = ENC_CODERANGE(str);
3538 if (len == 0) {
3539 /* Empty string does not contain non-ASCII */
3541 }
3542 else if (cr == ENC_CODERANGE_UNKNOWN) {
3543 /* Leave unknown. */
3544 }
3545 else if (len > RSTRING_LEN(str)) {
3546 if (ENC_CODERANGE_CLEAN_P(cr)) {
3547 /* Update the coderange regarding the extended part. */
3548 const char *const prev_end = RSTRING_END(str);
3549 const char *const new_end = RSTRING_PTR(str) + len;
3550 rb_encoding *enc = rb_enc_get(str);
3551 rb_str_coderange_scan_restartable(prev_end, new_end, enc, &cr);
3552 ENC_CODERANGE_SET(str, cr);
3553 }
3554 else if (cr == ENC_CODERANGE_BROKEN) {
3555 /* May be valid now, by appended part. */
3557 }
3558 }
3559 else if (len < RSTRING_LEN(str)) {
3560 if (cr != ENC_CODERANGE_7BIT) {
3561 /* ASCII-only string is keeping after truncated. Valid
3562 * and broken may be invalid or valid, leave unknown. */
3564 }
3565 }
3566
3567 STR_SET_LEN(str, len);
3568 TERM_FILL(&RSTRING_PTR(str)[len], termlen);
3569}
3570
3571VALUE
3572rb_str_resize(VALUE str, rb_len_t len)
3573{
3574 if (len < 0) {
3575 rb_raise(rb_eArgError, "negative string size (or size too big)");
3576 }
3577
3578 int independent = str_independent(str);
3579 rb_len_t slen = RSTRING_LEN(str);
3580 const int termlen = TERM_LEN(str);
3581 if (len > RB_LEN_MAX - termlen) {
3582 rb_raise(rb_eArgError, "string size too big");
3583 }
3584
3585 if (slen > len || (termlen != 1 && slen < len)) {
3587 }
3588
3589 {
3590 rb_len_t capa;
3591 if (STR_EMBED_P(str)) {
3592 if (len == slen) return str;
3593 if (str_embed_capa(str) >= len + termlen) {
3594 STR_SET_LEN(str, len);
3595 TERM_FILL(RSTRING(str)->as.embed.ary + len, termlen);
3596 return str;
3597 }
3598 str_make_independent_expand(str, slen, len - slen, termlen);
3599 }
3600 else if (str_embed_capa(str) >= len + termlen) {
3601 capa = RSTRING(str)->as.heap.aux.capa;
3602 char *ptr = STR_HEAP_PTR(str);
3603 STR_SET_EMBED(str);
3604 if (slen > len) slen = len;
3605 if (slen > 0) MEMCPY(RSTRING(str)->as.embed.ary, ptr, char, slen);
3606 TERM_FILL(RSTRING(str)->as.embed.ary + len, termlen);
3607 STR_SET_LEN(str, len);
3608 if (independent) {
3609 SIZED_FREE_N(ptr, capa + termlen);
3610 }
3611 return str;
3612 }
3613 else if (!independent) {
3614 if (len == slen) return str;
3615 str_make_independent_expand(str, slen, len - slen, termlen);
3616 }
3617 else if ((capa = RSTRING(str)->as.heap.aux.capa) < len ||
3618 (capa - len) > (len < 1024 ? len : 1024)) {
3619 SIZED_REALLOC_N(RSTRING(str)->as.heap.ptr, char,
3620 (size_t)len + termlen, STR_HEAP_SIZE(str));
3621 RSTRING(str)->as.heap.aux.capa = len;
3622 }
3623 else if (len == slen) return str;
3624 STR_SET_LEN(str, len);
3625 TERM_FILL(RSTRING(str)->as.heap.ptr + len, termlen); /* sentinel */
3626 }
3627 return str;
3628}
3629
3630static void
3631str_ensure_available_capa(VALUE str, rb_len_t len)
3632{
3633 str_modify_keep_cr(str);
3634
3635 const int termlen = TERM_LEN(str);
3636 rb_len_t olen = RSTRING_LEN(str);
3637
3638 if (RB_UNLIKELY(olen > RB_LEN_MAX - len)) {
3639 rb_raise(rb_eArgError, "string sizes too big");
3640 }
3641
3642 rb_len_t total = olen + len;
3643 rb_len_t capa = str_capacity(str, termlen);
3644
3645 if (capa < total) {
3646 if (total >= RB_LEN_MAX / 2) {
3647 capa = total;
3648 }
3649 while (total > capa) {
3650 capa = 2 * capa + termlen; /* == 2*(capa+termlen)-termlen */
3651 }
3652 RESIZE_CAPA_TERM(str, capa, termlen);
3653 }
3654}
3655
3656static VALUE
3657str_buf_cat4(VALUE str, const char *ptr, rb_len_t len, bool keep_cr)
3658{
3659 if (keep_cr) {
3660 str_modify_keep_cr(str);
3661 }
3662 else {
3663 rb_str_modify(str);
3664 }
3665 if (len == 0) return 0;
3666
3667 rb_len_t total, olen, off = -1;
3668 char *sptr;
3669 const int termlen = TERM_LEN(str);
3670
3671 RSTRING_GETMEM(str, sptr, olen);
3672 if (ptr >= sptr && ptr <= sptr + olen) {
3673 off = ptr - sptr;
3674 }
3675
3676 rb_len_t capa = str_capacity(str, termlen);
3677
3678 if (olen > RB_LEN_MAX - len) {
3679 rb_raise(rb_eArgError, "string sizes too big");
3680 }
3681 total = olen + len;
3682 if (capa < total) {
3683 if (total >= RB_LEN_MAX / 2) {
3684 capa = total;
3685 }
3686 while (total > capa) {
3687 capa = 2 * capa + termlen; /* == 2*(capa+termlen)-termlen */
3688 }
3689 RESIZE_CAPA_TERM(str, capa, termlen);
3690 sptr = RSTRING_PTR(str);
3691 }
3692 if (off != -1) {
3693 ptr = sptr + off;
3694 }
3695 memcpy(sptr + olen, ptr, len);
3696 STR_SET_LEN(str, total);
3697 TERM_FILL(sptr + total, termlen); /* sentinel */
3698
3699 return str;
3700}
3701
3702#define str_buf_cat(str, ptr, len) str_buf_cat4((str), (ptr), len, false)
3703#define str_buf_cat2(str, ptr) str_buf_cat4((str), (ptr), rb_strlen_lit(ptr), false)
3704
3705VALUE
3706rb_str_cat(VALUE str, const char *ptr, rb_len_t len)
3707{
3708 if (len == 0) return str;
3709 if (len < 0) {
3710 rb_raise(rb_eArgError, "negative string size (or size too big)");
3711 }
3712 return str_buf_cat(str, ptr, len);
3713}
3714
3715VALUE
3716rb_str_cat_cstr(VALUE str, const char *ptr)
3717{
3718 must_not_null(ptr);
3719 return rb_str_buf_cat(str, ptr, strlen(ptr));
3720}
3721
3722static void
3723rb_str_buf_cat_byte(VALUE str, unsigned char byte)
3724{
3725 RUBY_ASSERT(RB_ENCODING_GET_INLINED(str) == ENCINDEX_ASCII_8BIT || RB_ENCODING_GET_INLINED(str) == ENCINDEX_US_ASCII);
3726
3727 // We can't write directly to shared strings without impacting others, so we must make the string independent.
3728 if (UNLIKELY(!str_independent(str))) {
3729 str_make_independent(str);
3730 }
3731
3732 rb_len_t string_length = -1;
3733 const int null_terminator_length = 1;
3734 char *sptr;
3735 RSTRING_GETMEM(str, sptr, string_length);
3736
3737 // Ensure the resulting string wouldn't be too long.
3738 if (UNLIKELY(string_length > RB_LEN_MAX - 1)) {
3739 rb_raise(rb_eArgError, "string sizes too big");
3740 }
3741
3742 rb_len_t string_capacity = str_capacity(str, null_terminator_length);
3743
3744 // Get the code range before any modifications since those might clear the code range.
3745 int cr = ENC_CODERANGE(str);
3746
3747 // Check if the string has spare string_capacity to write the new byte.
3748 if (LIKELY(string_capacity >= string_length + 1)) {
3749 // In fast path we can write the new byte and note the string's new length.
3750 sptr[string_length] = byte;
3751 STR_SET_LEN(str, string_length + 1);
3752 TERM_FILL(sptr + string_length + 1, null_terminator_length);
3753 }
3754 else {
3755 // If there's not enough string_capacity, make a call into the general string concatenation function.
3756 str_buf_cat(str, (char *)&byte, 1);
3757 }
3758
3759 // If the code range is already known, we can derive the resulting code range cheaply by looking at the byte we
3760 // just appended. If the code range is unknown, but the string was empty, then we can also derive the code range
3761 // by looking at the byte we just appended. Otherwise, we'd have to scan the bytes to determine the code range so
3762 // we leave it as unknown. It cannot be broken for binary strings so we don't need to handle that option.
3763 if (cr == ENC_CODERANGE_7BIT || string_length == 0) {
3764 if (ISASCII(byte)) {
3766 }
3767 else {
3769
3770 // Promote a US-ASCII string to ASCII-8BIT when a non-ASCII byte is appended.
3771 if (UNLIKELY(RB_ENCODING_GET_INLINED(str) == ENCINDEX_US_ASCII)) {
3772 rb_enc_associate_index(str, ENCINDEX_ASCII_8BIT);
3773 }
3774 }
3775 }
3776}
3777
3778RUBY_ALIAS_FUNCTION(rb_str_buf_cat(VALUE str, const char *ptr, rb_len_t len), rb_str_cat, (str, ptr, len))
3779RUBY_ALIAS_FUNCTION(rb_str_buf_cat2(VALUE str, const char *ptr), rb_str_cat_cstr, (str, ptr))
3780RUBY_ALIAS_FUNCTION(rb_str_cat2(VALUE str, const char *ptr), rb_str_cat_cstr, (str, ptr))
3781
3782static VALUE
3783rb_enc_cr_str_buf_cat(VALUE str, const char *ptr, rb_len_t len,
3784 int ptr_encindex, int ptr_cr, int *ptr_cr_ret)
3785{
3786 int str_encindex = ENCODING_GET(str);
3787 int res_encindex;
3788 int str_cr, res_cr;
3789 rb_encoding *str_enc, *ptr_enc;
3790
3791 str_cr = RSTRING_LEN(str) ? ENC_CODERANGE(str) : ENC_CODERANGE_7BIT;
3792
3793 if (str_encindex == ptr_encindex) {
3794 if (str_cr != ENC_CODERANGE_UNKNOWN && ptr_cr == ENC_CODERANGE_UNKNOWN) {
3795 ptr_cr = coderange_scan(ptr, len, rb_enc_from_index(ptr_encindex));
3796 }
3797 }
3798 else {
3799 str_enc = rb_enc_from_index(str_encindex);
3800 ptr_enc = rb_enc_from_index(ptr_encindex);
3801 if (!rb_enc_asciicompat(str_enc) || !rb_enc_asciicompat(ptr_enc)) {
3802 if (len == 0)
3803 return str;
3804 if (RSTRING_LEN(str) == 0) {
3805 rb_str_buf_cat(str, ptr, len);
3806 rb_str_change_terminator_length(str, rb_enc_mbminlen(str_enc), rb_enc_mbminlen(ptr_enc));
3807 ENCODING_CODERANGE_SET(str, ptr_encindex, ptr_cr);
3808 return str;
3809 }
3810 goto incompatible;
3811 }
3812 if (ptr_cr == ENC_CODERANGE_UNKNOWN) {
3813 ptr_cr = coderange_scan(ptr, len, ptr_enc);
3814 }
3815 if (str_cr == ENC_CODERANGE_UNKNOWN) {
3816 if (ENCODING_IS_ASCII8BIT(str) || ptr_cr != ENC_CODERANGE_7BIT) {
3817 str_cr = rb_enc_str_coderange(str);
3818 }
3819 }
3820 }
3821 if (ptr_cr_ret)
3822 *ptr_cr_ret = ptr_cr;
3823
3824 if (str_encindex != ptr_encindex &&
3825 str_cr != ENC_CODERANGE_7BIT &&
3826 ptr_cr != ENC_CODERANGE_7BIT) {
3827 str_enc = rb_enc_from_index(str_encindex);
3828 ptr_enc = rb_enc_from_index(ptr_encindex);
3829 goto incompatible;
3830 }
3831
3832 if (str_cr == ENC_CODERANGE_UNKNOWN) {
3833 res_encindex = str_encindex;
3834 res_cr = ENC_CODERANGE_UNKNOWN;
3835 }
3836 else if (str_cr == ENC_CODERANGE_7BIT) {
3837 if (ptr_cr == ENC_CODERANGE_7BIT) {
3838 res_encindex = str_encindex;
3839 res_cr = ENC_CODERANGE_7BIT;
3840 }
3841 else {
3842 res_encindex = ptr_encindex;
3843 res_cr = ptr_cr;
3844 }
3845 }
3846 else if (str_cr == ENC_CODERANGE_VALID) {
3847 res_encindex = str_encindex;
3848 if (ENC_CODERANGE_CLEAN_P(ptr_cr))
3849 res_cr = str_cr;
3850 else
3851 res_cr = ptr_cr;
3852 }
3853 else { /* str_cr == ENC_CODERANGE_BROKEN */
3854 res_encindex = str_encindex;
3855 res_cr = str_cr;
3856 if (0 < len) res_cr = ENC_CODERANGE_UNKNOWN;
3857 }
3858
3859 if (len < 0) {
3860 rb_raise(rb_eArgError, "negative string size (or size too big)");
3861 }
3862 str_buf_cat(str, ptr, len);
3863 ENCODING_CODERANGE_SET(str, res_encindex, res_cr);
3864 return str;
3865
3866 incompatible:
3867 rb_raise(rb_eEncCompatError, "incompatible character encodings: %s and %s",
3868 rb_enc_inspect_name(str_enc), rb_enc_inspect_name(ptr_enc));
3870}
3871
3872VALUE
3873rb_enc_str_buf_cat(VALUE str, const char *ptr, rb_len_t len, rb_encoding *ptr_enc)
3874{
3875 return rb_enc_cr_str_buf_cat(str, ptr, len,
3876 rb_enc_to_index(ptr_enc), ENC_CODERANGE_UNKNOWN, NULL);
3877}
3878
3879VALUE
3880rb_str_buf_cat_ascii(VALUE str, const char *ptr)
3881{
3882 /* ptr must reference NUL terminated ASCII string. */
3883 int encindex = ENCODING_GET(str);
3884 rb_encoding *enc = rb_enc_from_index(encindex);
3885 if (rb_enc_asciicompat(enc)) {
3886 return rb_enc_cr_str_buf_cat(str, ptr, strlen(ptr),
3887 encindex, ENC_CODERANGE_7BIT, 0);
3888 }
3889 else {
3890 char *buf = ALLOCA_N(char, rb_enc_mbmaxlen(enc));
3891 while (*ptr) {
3892 unsigned int c = (unsigned char)*ptr;
3893 int len = rb_enc_codelen(c, enc);
3894 rb_enc_mbcput(c, buf, enc);
3895 rb_enc_cr_str_buf_cat(str, buf, len,
3896 encindex, ENC_CODERANGE_VALID, 0);
3897 ptr++;
3898 }
3899 return str;
3900 }
3901}
3902
3903VALUE
3905{
3906 str_modify_keep_cr(str);
3907
3908 int str2_cr = rb_enc_str_coderange(str2);
3909
3910 if (rb_str_enc_fastpath(str)) {
3911 switch (str2_cr) {
3912 case ENC_CODERANGE_7BIT:
3913 // If RHS is 7bit we can do simple concatenation
3914 str_buf_cat4(str, RSTRING_PTR(str2), RSTRING_LEN(str2), true);
3915 RB_GC_GUARD(str2);
3916 return str;
3918 // If RHS is valid, we can do simple concatenation if encodings are the same
3919 if (ENCODING_GET_INLINED(str) == ENCODING_GET_INLINED(str2)) {
3920 str_buf_cat4(str, RSTRING_PTR(str2), RSTRING_LEN(str2), true);
3921 int str_cr = ENC_CODERANGE(str);
3922 if (UNLIKELY(str_cr != ENC_CODERANGE_VALID)) {
3923 ENC_CODERANGE_SET(str, RB_ENC_CODERANGE_AND(str_cr, str2_cr));
3924 }
3925 RB_GC_GUARD(str2);
3926 return str;
3927 }
3928 }
3929 }
3930
3931 // NULL: str2's coderange is already cached
3932 rb_enc_cr_str_buf_cat(str, RSTRING_PTR(str2), RSTRING_LEN(str2),
3933 ENCODING_GET(str2), str2_cr, NULL);
3934 RB_GC_GUARD(str2);
3935
3936 return str;
3937}
3938
3939VALUE
3941{
3942 StringValue(str2);
3943 return rb_str_buf_append(str, str2);
3944}
3945
3946VALUE
3947rb_str_concat_literals(size_t num, const VALUE *strary)
3948{
3949 VALUE str;
3950 size_t i, s = 0;
3951 rb_ulen_t len = 1;
3952
3953 if (UNLIKELY(!num)) return rb_str_new(0, 0);
3954 if (UNLIKELY(num == 1)) return rb_str_resurrect(strary[0]);
3955
3956 for (i = 0; i < num; ++i) { len += RSTRING_LEN(strary[i]); }
3957 str = rb_str_buf_new(len);
3958 rb_enc_associate_index(str, ENCODING_GET(strary[0]));
3959
3960 for (i = s; i < num; ++i) {
3961 const VALUE v = strary[i];
3962 int encidx = ENCODING_GET(v);
3963
3964 rb_str_buf_append(str, v);
3965 if (encidx != ENCINDEX_US_ASCII) {
3966 if (ENCODING_GET_INLINED(str) == ENCINDEX_US_ASCII)
3967 rb_enc_associate_index(str, encidx);
3968 }
3969 }
3970 return str;
3971}
3972
3973/*
3974 * call-seq:
3975 * concat(*objects) -> string
3976 *
3977 * Concatenates each object in +objects+ to +self+; returns +self+:
3978 *
3979 * 'foo'.concat('bar', 'baz') # => "foobarbaz"
3980 *
3981 * For each given object +object+ that is an integer,
3982 * the value is considered a codepoint and converted to a character before concatenation:
3983 *
3984 * 'foo'.concat(32, 'bar', 32, 'baz') # => "foo bar baz" # Embeds spaces.
3985 * 'こん'.concat(12395, 12385, 12399) # => "こんにちは"
3986 *
3987 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
3988 */
3989static VALUE
3990rb_str_concat_multi(int argc, VALUE *argv, VALUE str)
3991{
3992 str_modifiable(str);
3993
3994 if (argc == 1) {
3995 return rb_str_concat(str, argv[0]);
3996 }
3997 else if (argc > 1) {
3998 int i;
3999 VALUE arg_str = rb_str_tmp_new(0);
4000 rb_enc_copy(arg_str, str);
4001 for (i = 0; i < argc; i++) {
4002 rb_str_concat(arg_str, argv[i]);
4003 }
4004 rb_str_buf_append(str, arg_str);
4005 }
4006
4007 return str;
4008}
4009
4010/*
4011 * call-seq:
4012 * append_as_bytes(*objects) -> self
4013 *
4014 * Concatenates each object in +objects+ into +self+; returns +self+;
4015 * performs no encoding validation or conversion:
4016 *
4017 * s = 'foo'
4018 * s.append_as_bytes(" \xE2\x82") # => "foo \xE2\x82"
4019 * s.valid_encoding? # => false
4020 * s.append_as_bytes("\xAC 12")
4021 * s.valid_encoding? # => true
4022 *
4023 * When a given object is an integer,
4024 * the value is considered an 8-bit byte;
4025 * if the integer occupies more than one byte (i.e,. is greater than 255),
4026 * appends only the low-order byte (similar to String#setbyte):
4027 *
4028 * s = ""
4029 * s.append_as_bytes(0, 257) # => "\u0000\u0001"
4030 * s.bytesize # => 2
4031 *
4032 * Related: see {Modifying}[rdoc-ref:String@Modifying].
4033 */
4034
4035VALUE
4036rb_str_append_as_bytes(int argc, VALUE *argv, VALUE str)
4037{
4038 rb_len_t needed_capacity = 0;
4039 volatile VALUE t0;
4040
4041 str_modify_keep_cr(str);
4042
4043 enum ruby_value_type *types = ALLOCV_N(enum ruby_value_type, t0, argc);
4044
4045 for (int index = 0; index < argc; index++) {
4046 VALUE obj = argv[index];
4047 enum ruby_value_type type = types[index] = rb_type(obj);
4048 switch (type) {
4049 case T_FIXNUM:
4050 case T_BIGNUM:
4051 needed_capacity++;
4052 break;
4053 case T_STRING:
4054 needed_capacity += RSTRING_LEN(obj);
4055 break;
4056 default:
4057 rb_raise(
4059 "wrong argument type %"PRIsVALUE" (expected String or Integer)",
4060 rb_obj_class(obj)
4061 );
4062 break;
4063 }
4064 }
4065
4066 str_ensure_available_capa(str, needed_capacity);
4067 char *sptr = RSTRING_END(str);
4068
4069 for (int index = 0; index < argc; index++) {
4070 VALUE obj = argv[index];
4071 enum ruby_value_type type = types[index];
4072 switch (type) {
4073 case T_FIXNUM:
4074 case T_BIGNUM: {
4075 argv[index] = obj = rb_int_and(obj, INT2FIX(0xff));
4076 char byte = (char)(NUM2INT(obj) & 0xFF);
4077 *sptr = byte;
4078 sptr++;
4079 break;
4080 }
4081 case T_STRING: {
4082 const char *ptr;
4083 rb_len_t len;
4084 RSTRING_GETMEM(obj, ptr, len);
4085 memcpy(sptr, ptr, len);
4086 sptr += len;
4087 break;
4088 }
4089 default:
4090 rb_bug("append_as_bytes arguments should have been validated");
4091 }
4092 }
4093
4094 STR_SET_LEN(str, RSTRING_LEN(str) + needed_capacity);
4095 TERM_FILL(sptr, TERM_LEN(str)); /* sentinel */
4096
4097 int cr = ENC_CODERANGE(str);
4098 switch (cr) {
4099 case ENC_CODERANGE_7BIT: {
4100 for (int index = 0; index < argc; index++) {
4101 VALUE obj = argv[index];
4102 enum ruby_value_type type = types[index];
4103 switch (type) {
4104 case T_FIXNUM:
4105 case T_BIGNUM: {
4106 if (!ISASCII(NUM2INT(obj))) {
4107 goto clear_cr;
4108 }
4109 break;
4110 }
4111 case T_STRING: {
4112 if (ENC_CODERANGE(obj) != ENC_CODERANGE_7BIT) {
4113 goto clear_cr;
4114 }
4115 break;
4116 }
4117 default:
4118 rb_bug("append_as_bytes arguments should have been validated");
4119 }
4120 }
4121 break;
4122 }
4124 if (ENCODING_GET_INLINED(str) == ENCINDEX_ASCII_8BIT) {
4125 goto keep_cr;
4126 }
4127 else {
4128 goto clear_cr;
4129 }
4130 break;
4131 default:
4132 goto clear_cr;
4133 break;
4134 }
4135
4136 clear_cr:
4137 // If no fast path was hit, we clear the coderange.
4138 // append_as_bytes is predominantly meant to be used in
4139 // buffering situation, hence it's likely the coderange
4140 // will never be scanned, so it's not worth spending time
4141 // precomputing the coderange except for simple and common
4142 // situations.
4144 keep_cr:
4145 ALLOCV_END(t0);
4146 return str;
4147}
4148
4149/*
4150 * call-seq:
4151 * self << object -> self
4152 *
4153 * Appends a string representation of +object+ to +self+;
4154 * returns +self+.
4155 *
4156 * If +object+ is a string, appends it to +self+:
4157 *
4158 * s = 'foo'
4159 * s << 'bar' # => "foobar"
4160 * s # => "foobar"
4161 *
4162 * If +object+ is an integer,
4163 * its value is considered a codepoint;
4164 * converts the value to a character before concatenating:
4165 *
4166 * s = 'foo'
4167 * s << 33 # => "foo!"
4168 *
4169 * Additionally, if the codepoint is in range <tt>0..0xff</tt>
4170 * and the encoding of +self+ is Encoding::US_ASCII,
4171 * changes the encoding to Encoding::ASCII_8BIT:
4172 *
4173 * s = 'foo'.encode(Encoding::US_ASCII)
4174 * s.encoding # => #<Encoding:US-ASCII>
4175 * s << 0xff # => "foo\xFF"
4176 * s.encoding # => #<Encoding:BINARY (ASCII-8BIT)>
4177 *
4178 * Raises RangeError if that codepoint is not representable in the encoding of +self+:
4179 *
4180 * s = 'foo'
4181 * s.encoding # => #<Encoding:UTF-8>
4182 * s << 0x00110000 # 1114112 out of char range (RangeError)
4183 * s = 'foo'.encode(Encoding::EUC_JP)
4184 * s << 0x00800080 # invalid codepoint 0x800080 in EUC-JP (RangeError)
4185 *
4186 * Related: see {Modifying}[rdoc-ref:String@Modifying].
4187 */
4188VALUE
4190{
4191 unsigned int code;
4192 rb_encoding *enc;
4193 int encidx;
4194
4195 /* For a chilled string this emits the deprecation warning, which may run
4196 * Ruby code (Warning.warn) that modifies str1. Do it before reading the
4197 * encoding, length and coderange used by the codepoint path below. */
4198 str_modify_keep_cr(str1);
4199 enc = STR_ENC_GET(str1);
4200
4201 if (RB_INTEGER_TYPE_P(str2)) {
4202 if (rb_num_to_uint(str2, &code) == 0) {
4203 }
4204 else if (FIXNUM_P(str2)) {
4205 rb_raise(rb_eRangeError, "%ld out of char range", FIX2LONG(str2));
4206 }
4207 else {
4208 rb_raise(rb_eRangeError, "bignum out of char range");
4209 }
4210 }
4211 else {
4212 return rb_str_append(str1, str2);
4213 }
4214
4215 encidx = rb_ascii8bit_appendable_encoding_index(enc, code);
4216
4217 if (encidx >= 0) {
4218 rb_str_buf_cat_byte(str1, (unsigned char)code);
4219 }
4220 else {
4221 rb_len_t pos = RSTRING_LEN(str1);
4222 int cr = ENC_CODERANGE(str1);
4223 int len;
4224 char *buf;
4225
4226 switch (len = rb_enc_codelen(code, enc)) {
4227 case ONIGERR_INVALID_CODE_POINT_VALUE:
4228 rb_raise(rb_eRangeError, "invalid codepoint 0x%X in %s", code, rb_enc_name(enc));
4229 break;
4230 case ONIGERR_TOO_BIG_WIDE_CHAR_VALUE:
4231 case 0:
4232 rb_raise(rb_eRangeError, "%u out of char range", code);
4233 break;
4234 }
4235 buf = ALLOCA_N(char, len + 1);
4236 rb_enc_mbcput(code, buf, enc);
4237 if (rb_enc_precise_mbclen(buf, buf + len + 1, enc) != len) {
4238 rb_raise(rb_eRangeError, "invalid codepoint 0x%X in %s", code, rb_enc_name(enc));
4239 }
4240 rb_str_resize(str1, pos+len);
4241 memcpy(RSTRING_PTR(str1) + pos, buf, len);
4242 if (cr == ENC_CODERANGE_7BIT && code > 127) {
4244 }
4245 else if (cr == ENC_CODERANGE_BROKEN) {
4247 }
4248 ENC_CODERANGE_SET(str1, cr);
4249 }
4250 return str1;
4251}
4252
4253int
4254rb_ascii8bit_appendable_encoding_index(rb_encoding *enc, unsigned int code)
4255{
4256 int encidx = rb_enc_to_index(enc);
4257
4258 if (encidx == ENCINDEX_ASCII_8BIT || encidx == ENCINDEX_US_ASCII) {
4259 /* US-ASCII automatically extended to ASCII-8BIT */
4260 if (code > 0xFF) {
4261 rb_raise(rb_eRangeError, "%u out of char range", code);
4262 }
4263 if (encidx == ENCINDEX_US_ASCII && code > 127) {
4264 return ENCINDEX_ASCII_8BIT;
4265 }
4266 return encidx;
4267 }
4268 else {
4269 return -1;
4270 }
4271}
4272
4273/*
4274 * call-seq:
4275 * prepend(*other_strings) -> new_string
4276 *
4277 * Prefixes to +self+ the concatenation of the given +other_strings+; returns +self+:
4278 *
4279 * 'baz'.prepend('foo', 'bar') # => "foobarbaz"
4280 *
4281 * Related: see {Modifying}[rdoc-ref:String@Modifying].
4282 *
4283 */
4284
4285static VALUE
4286rb_str_prepend_multi(int argc, VALUE *argv, VALUE str)
4287{
4288 str_modifiable(str);
4289
4290 if (argc == 1) {
4291 rb_str_update(str, 0L, 0L, argv[0]);
4292 }
4293 else if (argc > 1) {
4294 int i;
4295 VALUE arg_str = rb_str_tmp_new(0);
4296 rb_enc_copy(arg_str, str);
4297 for (i = 0; i < argc; i++) {
4298 rb_str_append(arg_str, argv[i]);
4299 }
4300 rb_str_update(str, 0L, 0L, arg_str);
4301 }
4302
4303 return str;
4304}
4305
4306st_index_t
4308{
4309 if (FL_TEST_RAW(str, STR_PRECOMPUTED_HASH)) {
4310 st_index_t precomputed_hash;
4311 memcpy(&precomputed_hash, RSTRING_END(str) + TERM_LEN(str), sizeof(precomputed_hash));
4312
4313 RUBY_ASSERT(precomputed_hash == str_do_hash(str));
4314 return precomputed_hash;
4315 }
4316
4317 return str_do_hash(str);
4318}
4319
4320int
4322{
4323 rb_len_t len1, len2;
4324 const char *ptr1, *ptr2;
4325 RSTRING_GETMEM(str1, ptr1, len1);
4326 RSTRING_GETMEM(str2, ptr2, len2);
4327 return (len1 != len2 ||
4328 !rb_str_comparable(str1, str2) ||
4329 memcmp(ptr1, ptr2, len1) != 0);
4330}
4331
4332/*
4333 * call-seq:
4334 * hash -> integer
4335 *
4336 * Returns the integer hash value for +self+.
4337 *
4338 * Two \String objects that have identical content and compatible encodings
4339 * also have the same hash value;
4340 * see Object#hash and {Encodings}[rdoc-ref:encodings.rdoc]:
4341 *
4342 * s = 'foo'
4343 * h = s.hash # => -569050784
4344 * h == 'foo'.hash # => true
4345 * h == 'food'.hash # => false
4346 * h == 'FOO'.hash # => false
4347 *
4348 * s0 = "äöü"
4349 * s1 = s0.encode(Encoding::ISO_8859_1)
4350 * s0.encoding # => #<Encoding:UTF-8>
4351 * s1.encoding # => #<Encoding:ISO-8859-1>
4352 * s0.hash == s1.hash # => false
4353 *
4354 * Related: see {Querying}[rdoc-ref:String@Querying].
4355 *
4356 */
4357
4358static VALUE
4359rb_str_hash_m(VALUE str)
4360{
4361 st_index_t hval = rb_str_hash(str);
4362 return ST2FIX(hval);
4363}
4364
4365#define lesser(a,b) (((a)>(b))?(b):(a))
4366
4367int
4369{
4370 int idx1, idx2;
4371 int rc1, rc2;
4372
4373 if (RSTRING_LEN(str1) == 0) return TRUE;
4374 if (RSTRING_LEN(str2) == 0) return TRUE;
4375 idx1 = ENCODING_GET(str1);
4376 idx2 = ENCODING_GET(str2);
4377 if (idx1 == idx2) return TRUE;
4378 rc1 = rb_enc_str_coderange(str1);
4379 rc2 = rb_enc_str_coderange(str2);
4380 if (rc1 == ENC_CODERANGE_7BIT) {
4381 if (rc2 == ENC_CODERANGE_7BIT) return TRUE;
4382 if (rb_enc_asciicompat(rb_enc_from_index(idx2)))
4383 return TRUE;
4384 }
4385 if (rc2 == ENC_CODERANGE_7BIT) {
4386 if (rb_enc_asciicompat(rb_enc_from_index(idx1)))
4387 return TRUE;
4388 }
4389 return FALSE;
4390}
4391
4392int
4394{
4395 rb_len_t len1, len2;
4396 const char *ptr1, *ptr2;
4397 int retval;
4398
4399 if (str1 == str2) return 0;
4400 RSTRING_GETMEM(str1, ptr1, len1);
4401 RSTRING_GETMEM(str2, ptr2, len2);
4402 if (ptr1 == ptr2 || (retval = memcmp(ptr1, ptr2, lesser(len1, len2))) == 0) {
4403 if (len1 == len2) {
4404 if (!rb_str_comparable(str1, str2)) {
4405 if (ENCODING_GET(str1) > ENCODING_GET(str2))
4406 return 1;
4407 return -1;
4408 }
4409 return 0;
4410 }
4411 if (len1 > len2) return 1;
4412 return -1;
4413 }
4414 if (retval > 0) return 1;
4415 return -1;
4416}
4417
4418/*
4419 * call-seq:
4420 * self == other -> true or false
4421 *
4422 * Returns whether +other+ is equal to +self+.
4423 *
4424 * When +other+ is a string, returns whether +other+ has the same length and content as +self+:
4425 *
4426 * s = 'foo'
4427 * s == 'foo' # => true
4428 * s == 'food' # => false
4429 * s == 'FOO' # => false
4430 *
4431 * Returns +false+ if the two strings' encodings are not compatible:
4432 *
4433 * "\u{e4 f6 fc}".encode(Encoding::ISO_8859_1) == ("\u{c4 d6 dc}") # => false
4434 *
4435 * When +other+ is not a string:
4436 *
4437 * - If +other+ responds to method <tt>to_str</tt>,
4438 * <tt>other == self</tt> is called and its return value is returned.
4439 * - If +other+ does not respond to <tt>to_str</tt>,
4440 * +false+ is returned.
4441 *
4442 * Related: {Comparing}[rdoc-ref:String@Comparing].
4443 */
4444
4445VALUE
4447{
4448 if (str1 == str2) return Qtrue;
4449 if (!RB_TYPE_P(str2, T_STRING)) {
4450 if (!rb_respond_to(str2, idTo_str)) {
4451 return Qfalse;
4452 }
4453 return rb_equal(str2, str1);
4454 }
4455 return rb_str_eql_internal(str1, str2);
4456}
4457
4458/*
4459 * call-seq:
4460 * eql?(object) -> true or false
4461 *
4462 * Returns whether +self+ and +object+ have the same length and content:
4463 *
4464 * s = 'foo'
4465 * s.eql?('foo') # => true
4466 * s.eql?('food') # => false
4467 * s.eql?('FOO') # => false
4468 *
4469 * Returns +false+ if the two strings' encodings are not compatible:
4470 *
4471 * s0 = "äöü" # => "äöü"
4472 * s1 = s0.encode(Encoding::ISO_8859_1) # => "\xE4\xF6\xFC"
4473 * s0.encoding # => #<Encoding:UTF-8>
4474 * s1.encoding # => #<Encoding:ISO-8859-1>
4475 * s0.eql?(s1) # => false
4476 *
4477 * See {Encodings}[rdoc-ref:encodings.rdoc].
4478 *
4479 * Related: see {Querying}[rdoc-ref:String@Querying].
4480 *
4481 */
4482
4483VALUE
4484rb_str_eql(VALUE str1, VALUE str2)
4485{
4486 if (str1 == str2) return Qtrue;
4487 if (!RB_TYPE_P(str2, T_STRING)) return Qfalse;
4488 return rb_str_eql_internal(str1, str2);
4489}
4490
4491/*
4492 * call-seq:
4493 * self <=> other -> -1, 0, 1, or nil
4494 *
4495 * Compares +self+ and +other+,
4496 * evaluating their _contents_, not their _lengths_.
4497 *
4498 * Returns:
4499 *
4500 * - +-1+, if +self+ is smaller.
4501 * - +0+, if the two are equal.
4502 * - +1+, if +self+ is larger.
4503 * - +nil+, if the two are incomparable.
4504 *
4505 * Examples:
4506 *
4507 * 'a' <=> 'b' # => -1
4508 * 'a' <=> 'ab' # => -1
4509 * 'a' <=> 'a' # => 0
4510 * 'b' <=> 'a' # => 1
4511 * 'ab' <=> 'a' # => 1
4512 * 'a' <=> :a # => nil
4513 *
4514 * \Class \String includes module Comparable,
4515 * each of whose methods uses String#<=> for comparison.
4516 *
4517 * Related: see {Comparing}[rdoc-ref:String@Comparing].
4518 */
4519
4520static VALUE
4521rb_str_cmp_m(VALUE str1, VALUE str2)
4522{
4523 int result;
4524 VALUE s = rb_check_string_type(str2);
4525 if (NIL_P(s)) {
4526 return rb_invcmp(str1, str2);
4527 }
4528 result = rb_str_cmp(str1, s);
4529 return INT2FIX(result);
4530}
4531
4532static VALUE str_casecmp(VALUE str1, VALUE str2);
4533static VALUE str_casecmp_p(VALUE str1, VALUE str2);
4534
4535/*
4536 * call-seq:
4537 * casecmp(other_string) -> -1, 0, 1, or nil
4538 *
4539 * Ignoring case, compares +self+ and +other_string+; returns:
4540 *
4541 * - -1 if <tt>self.downcase</tt> is smaller than <tt>other_string.downcase</tt>.
4542 * - 0 if the two are equal.
4543 * - 1 if <tt>self.downcase</tt> is larger than <tt>other_string.downcase</tt>.
4544 * - +nil+ if the two are incomparable.
4545 *
4546 * See {Case Mapping}[rdoc-ref:case_mapping.rdoc].
4547 *
4548 * Examples:
4549 *
4550 * 'foo'.casecmp('goo') # => -1
4551 * 'goo'.casecmp('foo') # => 1
4552 * 'foo'.casecmp('food') # => -1
4553 * 'food'.casecmp('foo') # => 1
4554 * 'FOO'.casecmp('foo') # => 0
4555 * 'foo'.casecmp('FOO') # => 0
4556 * 'foo'.casecmp(1) # => nil
4557 *
4558 * Related: see {Comparing}[rdoc-ref:String@Comparing].
4559 */
4560
4561VALUE
4562rb_str_casecmp(VALUE str1, VALUE str2)
4563{
4564 VALUE s = rb_check_string_type(str2);
4565 if (NIL_P(s)) {
4566 return Qnil;
4567 }
4568 return str_casecmp(str1, s);
4569}
4570
4571static VALUE
4572str_casecmp(VALUE str1, VALUE str2)
4573{
4574 rb_len_t len;
4575 rb_encoding *enc;
4576 const char *p1, *p1end, *p2, *p2end;
4577
4578 enc = rb_enc_compatible(str1, str2);
4579 if (!enc) {
4580 return Qnil;
4581 }
4582
4583 p1 = RSTRING_PTR(str1); p1end = RSTRING_END(str1);
4584 p2 = RSTRING_PTR(str2); p2end = RSTRING_END(str2);
4585 if (single_byte_optimizable(str1) && single_byte_optimizable(str2)) {
4586 while (p1 < p1end && p2 < p2end) {
4587 if (*p1 != *p2) {
4588 unsigned int c1 = TOLOWER(*p1 & 0xff);
4589 unsigned int c2 = TOLOWER(*p2 & 0xff);
4590 if (c1 != c2)
4591 return INT2FIX(c1 < c2 ? -1 : 1);
4592 }
4593 p1++;
4594 p2++;
4595 }
4596 }
4597 else {
4598 while (p1 < p1end && p2 < p2end) {
4599 int l1, c1 = rb_enc_ascget(p1, p1end, &l1, enc);
4600 int l2, c2 = rb_enc_ascget(p2, p2end, &l2, enc);
4601
4602 if (0 <= c1 && 0 <= c2) {
4603 c1 = TOLOWER(c1);
4604 c2 = TOLOWER(c2);
4605 if (c1 != c2)
4606 return INT2FIX(c1 < c2 ? -1 : 1);
4607 }
4608 else {
4609 int r;
4610 l1 = rb_enc_mbclen(p1, p1end, enc);
4611 l2 = rb_enc_mbclen(p2, p2end, enc);
4612 len = l1 < l2 ? l1 : l2;
4613 r = memcmp(p1, p2, len);
4614 if (r != 0)
4615 return INT2FIX(r < 0 ? -1 : 1);
4616 if (l1 != l2)
4617 return INT2FIX(l1 < l2 ? -1 : 1);
4618 }
4619 p1 += l1;
4620 p2 += l2;
4621 }
4622 }
4623 if (p1 == p1end && p2 == p2end) return INT2FIX(0);
4624 if (p1 == p1end) return INT2FIX(-1);
4625 return INT2FIX(1);
4626}
4627
4628/*
4629 * call-seq:
4630 * casecmp?(other_string) -> true, false, or nil
4631 *
4632 * Returns +true+ if +self+ and +other_string+ are equal after
4633 * Unicode case folding, +false+ if unequal, +nil+ if incomparable.
4634 *
4635 * See {Case Mapping}[rdoc-ref:case_mapping.rdoc].
4636 *
4637 * Examples:
4638 *
4639 * 'foo'.casecmp?('goo') # => false
4640 * 'goo'.casecmp?('foo') # => false
4641 * 'foo'.casecmp?('food') # => false
4642 * 'food'.casecmp?('foo') # => false
4643 * 'FOO'.casecmp?('foo') # => true
4644 * 'foo'.casecmp?('FOO') # => true
4645 * 'foo'.casecmp?(1) # => nil
4646 *
4647 * Related: see {Comparing}[rdoc-ref:String@Comparing].
4648 */
4649
4650static VALUE
4651rb_str_casecmp_p(VALUE str1, VALUE str2)
4652{
4653 VALUE s = rb_check_string_type(str2);
4654 if (NIL_P(s)) {
4655 return Qnil;
4656 }
4657 return str_casecmp_p(str1, s);
4658}
4659
4660static VALUE
4661str_casecmp_p(VALUE str1, VALUE str2)
4662{
4663 rb_encoding *enc;
4664 VALUE folded_str1, folded_str2;
4665 VALUE fold_opt = sym_fold;
4666
4667 enc = rb_enc_compatible(str1, str2);
4668 if (!enc) {
4669 return Qnil;
4670 }
4671
4672 if (is_ascii_string(str1) && is_ascii_string(str2)) {
4673 if (RSTRING_LEN(str1) != RSTRING_LEN(str2)) return Qfalse;
4674 const char *p1 = RSTRING_PTR(str1), *p1end = RSTRING_END(str1);
4675 const char *p2 = RSTRING_PTR(str2);
4676 while (p1 < p1end) {
4677 if (*p1 != *p2 && TOLOWER((unsigned char)*p1) != TOLOWER((unsigned char)*p2)) {
4678 return Qfalse;
4679 }
4680 p1++;
4681 p2++;
4682 }
4683 return Qtrue;
4684 }
4685
4686 folded_str1 = rb_str_downcase(1, &fold_opt, str1);
4687 folded_str2 = rb_str_downcase(1, &fold_opt, str2);
4688
4689 return rb_str_eql(folded_str1, folded_str2);
4690}
4691
4692static rb_len_t
4693strseq_core(const char *str_ptr, const char *str_ptr_end, rb_len_t str_len,
4694 const char *sub_ptr, rb_len_t sub_len, rb_len_t offset, rb_encoding *enc)
4695{
4696 const char *search_start = str_ptr;
4697 rb_len_t pos, search_len = str_len - offset;
4698
4699 for (;;) {
4700 const char *t;
4701 pos = rb_memsearch(sub_ptr, sub_len, search_start, search_len, enc);
4702 if (pos < 0) return pos;
4703 t = rb_enc_right_char_head(search_start, search_start+pos, str_ptr_end, enc);
4704 if (t == search_start + pos) break;
4705 search_len -= t - search_start;
4706 if (search_len <= 0) return -1;
4707 offset += t - search_start;
4708 search_start = t;
4709 }
4710 return pos + offset;
4711}
4712
4713/* found index in byte */
4714#define rb_str_index(str, sub, offset) rb_strseq_index(str, sub, offset, 0)
4715#define rb_str_byteindex(str, sub, offset) rb_strseq_index(str, sub, offset, 1)
4716
4717static rb_len_t
4718rb_strseq_index(VALUE str, VALUE sub, rb_len_t offset, int in_byte)
4719{
4720 const char *str_ptr, *str_ptr_end, *sub_ptr;
4721 rb_len_t str_len, sub_len;
4722 rb_encoding *enc;
4723
4724 enc = rb_enc_check(str, sub);
4725 if (is_broken_string(sub)) return -1;
4726
4727 str_ptr = RSTRING_PTR(str);
4728 str_ptr_end = RSTRING_END(str);
4729 str_len = RSTRING_LEN(str);
4730 sub_ptr = RSTRING_PTR(sub);
4731 sub_len = RSTRING_LEN(sub);
4732
4733 if (str_len < sub_len) return -1;
4734
4735 if (offset != 0) {
4736 rb_len_t str_len_char, sub_len_char;
4737 int single_byte = single_byte_optimizable(str);
4738 str_len_char = (in_byte || single_byte) ? str_len : str_strlen(str, enc);
4739 sub_len_char = in_byte ? sub_len : str_strlen(sub, enc);
4740 if (offset < 0) {
4741 offset += str_len_char;
4742 if (offset < 0) return -1;
4743 }
4744 if (str_len_char - offset < sub_len_char) return -1;
4745 if (!in_byte) offset = str_offset(str_ptr, str_ptr_end, offset, enc, single_byte);
4746 str_ptr += offset;
4747 }
4748 if (sub_len == 0) return offset;
4749
4750 /* need proceed one character at a time */
4751 return strseq_core(str_ptr, str_ptr_end, str_len, sub_ptr, sub_len, offset, enc);
4752}
4753
4754
4755/*
4756 * call-seq:
4757 * index(pattern, offset = 0) -> integer or nil
4758 *
4759 * Returns the integer position of the first substring that matches the given argument +pattern+,
4760 * or +nil+ if none found.
4761 *
4762 * When +pattern+ is a string,
4763 * returns the index of the first matching substring in +self+:
4764 *
4765 * 'foo'.index('f') # => 0
4766 * 'foo'.index('o') # => 1
4767 * 'foo'.index('oo') # => 1
4768 * 'foo'.index('ooo') # => nil
4769 * 'こんにちは'.index('ち') # => 3
4770 *
4771 * When +pattern+ is a Regexp, returns the index of the first match in +self+:
4772 *
4773 * 'foo'.index(/o./) # => 1
4774 * 'foo'.index(/.o/) # => 0
4775 *
4776 * When +offset+ is non-negative, begins the search at position +offset+;
4777 * the returned index is relative to the beginning of +self+:
4778 *
4779 * 'bar'.index('r', 0) # => 2
4780 * 'bar'.index('r', 1) # => 2
4781 * 'bar'.index('r', 2) # => 2
4782 * 'bar'.index('r', 3) # => nil
4783 * 'bar'.index(/[r-z]/, 0) # => 2
4784 * 'こんにちは'.index('ち', 2) # => 3
4785 *
4786 * With negative integer argument +offset+, selects the search position by counting backward
4787 * from the end of +self+:
4788 *
4789 * 'foo'.index('o', -1) # => 2
4790 * 'foo'.index('o', -2) # => 1
4791 * 'foo'.index('o', -3) # => 1
4792 * 'foo'.index('o', -4) # => nil
4793 * 'foo'.index(/o./, -2) # => 1
4794 * 'foo'.index(/.o/, -2) # => 1
4795 *
4796 * Related: see {Querying}[rdoc-ref:String@Querying].
4797 *
4798 */
4799
4800static VALUE
4801rb_str_index_m(int argc, VALUE *argv, VALUE str)
4802{
4803 VALUE sub;
4804 VALUE initpos;
4805 rb_encoding *enc = STR_ENC_GET(str);
4806 rb_len_t pos;
4807
4808 if (rb_scan_args(argc, argv, "11", &sub, &initpos) == 2) {
4809 rb_len_t slen = str_strlen(str, enc); /* str's enc */
4810 pos = NUM2LEN(initpos);
4811 if (pos < 0 ? (pos += slen) < 0 : pos > slen) {
4812 if (RB_TYPE_P(sub, T_REGEXP)) {
4814 }
4815 return Qnil;
4816 }
4817 }
4818 else {
4819 pos = 0;
4820 }
4821
4822 if (RB_TYPE_P(sub, T_REGEXP)) {
4823 pos = str_offset(RSTRING_PTR(str), RSTRING_END(str), pos,
4824 enc, single_byte_optimizable(str));
4825
4826 if (rb_reg_search(sub, str, pos, 0) >= 0) {
4827 VALUE match = rb_backref_get();
4828 pos = rb_str_sublen(str, RMATCH_BEG(match, 0));
4829 return LEN2NUM(pos);
4830 }
4831 }
4832 else {
4833 StringValue(sub);
4834 pos = rb_str_index(str, sub, pos);
4835 if (pos >= 0) {
4836 pos = rb_str_sublen(str, pos);
4837 return LEN2NUM(pos);
4838 }
4839 }
4840 return Qnil;
4841}
4842
4843/* Ensure that the given pos is a valid character boundary.
4844 * Note that in this function, "character" means a code point
4845 * (Unicode scalar value), not a grapheme cluster.
4846 */
4847static void
4848str_ensure_byte_pos(VALUE str, rb_len_t pos)
4849{
4850 if (!single_byte_optimizable(str)) {
4851 const char *s = RSTRING_PTR(str);
4852 const char *e = RSTRING_END(str);
4853 const char *p = s + pos;
4854 if (!at_char_boundary(s, p, e, rb_enc_get(str))) {
4855 rb_raise(rb_eIndexError,
4856 "offset %"PRIdLEN" does not land on character boundary", pos);
4857 }
4858 }
4859}
4860
4861/*
4862 * call-seq:
4863 * byteindex(object, offset = 0) -> integer or nil
4864 *
4865 * Returns the 0-based integer index of a substring of +self+
4866 * specified by +object+ (a string or Regexp) and +offset+,
4867 * or +nil+ if there is no such substring;
4868 * the returned index is the count of _bytes_ (not characters).
4869 *
4870 * When +object+ is a string,
4871 * returns the index of the first found substring equal to +object+:
4872 *
4873 * s = 'foo' # => "foo"
4874 * s.size # => 3 # Three 1-byte characters.
4875 * s.bytesize # => 3 # Three bytes.
4876 * s.byteindex('f') # => 0
4877 * s.byteindex('o') # => 1
4878 * s.byteindex('oo') # => 1
4879 * s.byteindex('ooo') # => nil
4880 *
4881 * When +object+ is a Regexp,
4882 * returns the index of the first found substring matching +object+;
4883 * updates {Regexp-related global variables}[rdoc-ref:Regexp@Global+Variables]:
4884 *
4885 * s = 'foo'
4886 * s.byteindex(/f/) # => 0
4887 * $~ # => #<MatchData "f">
4888 * s.byteindex(/o/) # => 1
4889 * s.byteindex(/oo/) # => 1
4890 * s.byteindex(/ooo/) # => nil
4891 * $~ # => nil
4892 *
4893 * \Integer argument +offset+, if given, specifies the 0-based index
4894 * of the byte where searching is to begin.
4895 *
4896 * When +offset+ is non-negative,
4897 * searching begins at byte position +offset+:
4898 *
4899 * s = 'foo'
4900 * s.byteindex('o', 1) # => 1
4901 * s.byteindex('o', 2) # => 2
4902 * s.byteindex('o', 3) # => nil
4903 *
4904 * When +offset+ is negative, counts backward from the end of +self+:
4905 *
4906 * s = 'foo'
4907 * s.byteindex('o', -1) # => 2
4908 * s.byteindex('o', -2) # => 1
4909 * s.byteindex('o', -3) # => 1
4910 * s.byteindex('o', -4) # => nil
4911 *
4912 * Raises IndexError if the byte at +offset+ is not the first byte of a character:
4913 *
4914 * s = "\uFFFF\uFFFF" # => "\uFFFF\uFFFF"
4915 * s.size # => 2 # Two 3-byte characters.
4916 * s.bytesize # => 6 # Six bytes.
4917 * s.byteindex("\uFFFF") # => 0
4918 * s.byteindex("\uFFFF", 1) # Raises IndexError
4919 * s.byteindex("\uFFFF", 2) # Raises IndexError
4920 * s.byteindex("\uFFFF", 3) # => 3
4921 * s.byteindex("\uFFFF", 4) # Raises IndexError
4922 * s.byteindex("\uFFFF", 5) # Raises IndexError
4923 * s.byteindex("\uFFFF", 6) # => nil
4924 *
4925 * Related: see {Querying}[rdoc-ref:String@Querying].
4926 */
4927
4928static VALUE
4929rb_str_byteindex_m(int argc, VALUE *argv, VALUE str)
4930{
4931 VALUE sub;
4932 VALUE initpos;
4933 rb_len_t pos;
4934
4935 if (rb_scan_args(argc, argv, "11", &sub, &initpos) == 2) {
4936 pos = NUM2LEN(initpos);
4937 rb_len_t slen = RSTRING_LEN(str);
4938 if (pos < 0 ? (pos += slen) < 0 : pos > slen) {
4939 if (RB_TYPE_P(sub, T_REGEXP)) {
4941 }
4942 return Qnil;
4943 }
4944 }
4945 else {
4946 pos = 0;
4947 }
4948
4949 str_ensure_byte_pos(str, pos);
4950
4951 if (RB_TYPE_P(sub, T_REGEXP)) {
4952 if (rb_reg_search(sub, str, pos, 0) >= 0) {
4953 VALUE match = rb_backref_get();
4954 pos = RMATCH_BEG(match, 0);
4955 return LEN2NUM(pos);
4956 }
4957 }
4958 else {
4959 StringValue(sub);
4960 pos = rb_str_byteindex(str, sub, pos);
4961 if (pos >= 0) return LEN2NUM(pos);
4962 }
4963 return Qnil;
4964}
4965
4966static rb_len_t
4967str_rindex(VALUE str, VALUE sub, const char *s, rb_encoding *enc)
4968{
4969 const char *hit, *adjusted, *sbeg, *e, *t;
4970 int c;
4971 rb_len_t slen, searchlen;
4972
4973 sbeg = RSTRING_PTR(str);
4974 slen = RSTRING_LEN(sub);
4975 if (slen == 0) return s - sbeg;
4976 e = RSTRING_END(str);
4977 t = RSTRING_PTR(sub);
4978 c = *t & 0xff;
4979 searchlen = s - sbeg + 1;
4980
4981 if (s + slen <= e && memcmp(s, t, slen) == 0) {
4982 return s - sbeg;
4983 }
4984
4985 do {
4986 hit = memrchr(sbeg, c, searchlen);
4987 if (!hit) break;
4988 adjusted = rb_enc_left_char_head(sbeg, hit, e, enc);
4989 if (hit != adjusted) {
4990 searchlen = adjusted - sbeg;
4991 continue;
4992 }
4993 if (hit + slen <= e && memcmp(hit, t, slen) == 0)
4994 return hit - sbeg;
4995 searchlen = adjusted - sbeg;
4996 } while (searchlen > 0);
4997
4998 return -1;
4999}
5000
5001/* found index in byte */
5002static rb_len_t
5003rb_str_rindex(VALUE str, VALUE sub, rb_len_t pos)
5004{
5005 rb_len_t len, slen;
5006 const char *sbeg, *s;
5007 rb_encoding *enc;
5008 int singlebyte;
5009
5010 enc = rb_enc_check(str, sub);
5011 if (is_broken_string(sub)) return -1;
5012 singlebyte = single_byte_optimizable(str);
5013 len = singlebyte ? RSTRING_LEN(str) : str_strlen(str, enc); /* rb_enc_check */
5014 slen = str_strlen(sub, enc); /* rb_enc_check */
5015
5016 /* substring longer than string */
5017 if (len < slen) return -1;
5018 /* character counts, so the byte tail can still be shorter than sub */
5019 if (len - pos < slen) pos = len - slen;
5020 if (len == 0) return pos;
5021
5022 sbeg = RSTRING_PTR(str);
5023
5024 if (pos == 0) {
5025 if (RSTRING_LEN(sub) <= RSTRING_LEN(str) &&
5026 memcmp(sbeg, RSTRING_PTR(sub), RSTRING_LEN(sub)) == 0) {
5027 return 0;
5028 }
5029 else {
5030 return -1;
5031 }
5032 }
5033
5034 s = str_nth(sbeg, RSTRING_END(str), pos, enc, singlebyte);
5035 return str_rindex(str, sub, s, enc);
5036}
5037
5038/*
5039 * call-seq:
5040 * rindex(pattern, offset = self.length) -> integer or nil
5041 *
5042 * Returns the integer position of the _last_ substring that matches the given argument +pattern+,
5043 * or +nil+ if none found.
5044 *
5045 * When +pattern+ is a string, returns the index of the last matching substring in self:
5046 *
5047 * 'foo'.rindex('f') # => 0
5048 * 'foo'.rindex('o') # => 2
5049 * 'foo'.rindex('oo' # => 1
5050 * 'foo'.rindex('ooo') # => nil
5051 * 'こんにちは'.rindex('ち') # => 3
5052 *
5053 * When +pattern+ is a Regexp, returns the index of the last match in self:
5054 *
5055 * 'foo'.rindex(/f/) # => 0
5056 * 'foo'.rindex(/o/) # => 2
5057 * 'foo'.rindex(/oo/) # => 1
5058 * 'foo'.rindex(/ooo/) # => nil
5059 *
5060 * When +offset+ is non-negative, it specifies the maximum starting position in the
5061 * string to end the search:
5062 *
5063 * 'foo'.rindex('o', 0) # => nil
5064 * 'foo'.rindex('o', 1) # => 1
5065 * 'foo'.rindex('o', 2) # => 2
5066 * 'foo'.rindex('o', 3) # => 2
5067 *
5068 * With negative integer argument +offset+,
5069 * selects the search position by counting backward from the end of +self+:
5070 *
5071 * 'foo'.rindex('o', -1) # => 2
5072 * 'foo'.rindex('o', -2) # => 1
5073 * 'foo'.rindex('o', -3) # => nil
5074 * 'foo'.rindex('o', -4) # => nil
5075 *
5076 * The last match means starting at the possible last position, not
5077 * the last of longest matches:
5078 *
5079 * 'foo'.rindex(/o+/) # => 2
5080 * $~ # => #<MatchData "o">
5081 *
5082 * To get the last longest match, combine with negative lookbehind:
5083 *
5084 * 'foo'.rindex(/(?<!o)o+/) # => 1
5085 * $~ # => #<MatchData "oo">
5086 *
5087 * Or String#index with negative lookforward.
5088 *
5089 * 'foo'.index(/o+(?!.*o)/) # => 1
5090 * $~ # => #<MatchData "oo">
5091 *
5092 * Related: see {Querying}[rdoc-ref:String@Querying].
5093 *
5094 */
5095
5096static VALUE
5097rb_str_rindex_m(int argc, VALUE *argv, VALUE str)
5098{
5099 VALUE sub;
5100 VALUE initpos;
5101 rb_encoding *enc = STR_ENC_GET(str);
5102 rb_len_t pos, len = str_strlen(str, enc); /* str's enc */
5103
5104 if (rb_scan_args(argc, argv, "11", &sub, &initpos) == 2) {
5105 pos = NUM2LEN(initpos);
5106 if (pos < 0 && (pos += len) < 0) {
5107 if (RB_TYPE_P(sub, T_REGEXP)) {
5109 }
5110 return Qnil;
5111 }
5112 if (pos > len) pos = len;
5113 }
5114 else {
5115 pos = len;
5116 }
5117
5118 if (RB_TYPE_P(sub, T_REGEXP)) {
5119 /* enc = rb_enc_check(str, sub); */
5120 pos = str_offset(RSTRING_PTR(str), RSTRING_END(str), pos,
5121 enc, single_byte_optimizable(str));
5122
5123 if (rb_reg_search(sub, str, pos, 1) >= 0) {
5124 VALUE match = rb_backref_get();
5125 pos = rb_str_sublen(str, RMATCH_BEG(match, 0));
5126 return LEN2NUM(pos);
5127 }
5128 }
5129 else {
5130 StringValue(sub);
5131 pos = rb_str_rindex(str, sub, pos);
5132 if (pos >= 0) {
5133 pos = rb_str_sublen(str, pos);
5134 return LEN2NUM(pos);
5135 }
5136 }
5137 return Qnil;
5138}
5139
5140static rb_len_t
5141rb_str_byterindex(VALUE str, VALUE sub, rb_len_t pos)
5142{
5143 rb_len_t len, slen;
5144 const char *sbeg, *s;
5145 rb_encoding *enc;
5146
5147 enc = rb_enc_check(str, sub);
5148 if (is_broken_string(sub)) return -1;
5149 len = RSTRING_LEN(str);
5150 slen = RSTRING_LEN(sub);
5151
5152 /* substring longer than string */
5153 if (len < slen) return -1;
5154 if (len - pos < slen) pos = len - slen;
5155 if (len == 0) return pos;
5156
5157 sbeg = RSTRING_PTR(str);
5158
5159 if (pos == 0) {
5160 if (memcmp(sbeg, RSTRING_PTR(sub), RSTRING_LEN(sub)) == 0)
5161 return 0;
5162 else
5163 return -1;
5164 }
5165
5166 s = sbeg + pos;
5167 return str_rindex(str, sub, s, enc);
5168}
5169
5170/*
5171 * call-seq:
5172 * byterindex(object, offset = self.bytesize) -> integer or nil
5173 *
5174 * Returns the 0-based integer index of a substring of +self+
5175 * that is the _last_ match for the given +object+ (a string or Regexp) and +offset+,
5176 * or +nil+ if there is no such substring;
5177 * the returned index is the count of _bytes_ (not characters).
5178 *
5179 * When +object+ is a string,
5180 * returns the index of the _last_ found substring equal to +object+:
5181 *
5182 * s = 'foo' # => "foo"
5183 * s.size # => 3 # Three 1-byte characters.
5184 * s.bytesize # => 3 # Three bytes.
5185 * s.byterindex('f') # => 0
5186 * s.byterindex('o') # => 2
5187 * s.byterindex('oo') # => 1
5188 * s.byterindex('ooo') # => nil
5189 *
5190 * When +object+ is a Regexp,
5191 * returns the index of the last found substring matching +object+;
5192 * updates {Regexp-related global variables}[rdoc-ref:Regexp@Global+Variables]:
5193 *
5194 * s = 'foo'
5195 * s.byterindex(/f/) # => 0
5196 * $~ # => #<MatchData "f">
5197 * s.byterindex(/o/) # => 2
5198 * s.byterindex(/oo/) # => 1
5199 * s.byterindex(/ooo/) # => nil
5200 * $~ # => nil
5201 *
5202 * The last match means starting at the possible last position,
5203 * not the last of the longest matches:
5204 *
5205 * s = 'foo'
5206 * s.byterindex(/o+/) # => 2
5207 * $~ #=> #<MatchData "o">
5208 *
5209 * To get the last longest match, use a negative lookbehind:
5210 *
5211 * s = 'foo'
5212 * s.byterindex(/(?<!o)o+/) # => 1
5213 * $~ # => #<MatchData "oo">
5214 *
5215 * Or use method #byteindex with negative lookahead:
5216 *
5217 * s = 'foo'
5218 * s.byteindex(/o+(?!.*o)/) # => 1
5219 * $~ #=> #<MatchData "oo">
5220 *
5221 * \Integer argument +offset+, if given, specifies the 0-based index
5222 * of the byte where searching is to end.
5223 *
5224 * When +offset+ is non-negative,
5225 * searching ends at byte position +offset+:
5226 *
5227 * s = 'foo'
5228 * s.byterindex('o', 0) # => nil
5229 * s.byterindex('o', 1) # => 1
5230 * s.byterindex('o', 2) # => 2
5231 * s.byterindex('o', 3) # => 2
5232 *
5233 * When +offset+ is negative, counts backward from the end of +self+:
5234 *
5235 * s = 'foo'
5236 * s.byterindex('o', -1) # => 2
5237 * s.byterindex('o', -2) # => 1
5238 * s.byterindex('o', -3) # => nil
5239 *
5240 * Raises IndexError if the byte at +offset+ is not the first byte of a character:
5241 *
5242 * s = "\uFFFF\uFFFF" # => "\uFFFF\uFFFF"
5243 * s.size # => 2 # Two 3-byte characters.
5244 * s.bytesize # => 6 # Six bytes.
5245 * s.byterindex("\uFFFF") # => 3
5246 * s.byterindex("\uFFFF", 1) # Raises IndexError
5247 * s.byterindex("\uFFFF", 2) # Raises IndexError
5248 * s.byterindex("\uFFFF", 3) # => 3
5249 * s.byterindex("\uFFFF", 4) # Raises IndexError
5250 * s.byterindex("\uFFFF", 5) # Raises IndexError
5251 * s.byterindex("\uFFFF", 6) # => nil
5252 *
5253 * Related: see {Querying}[rdoc-ref:String@Querying].
5254 */
5255
5256static VALUE
5257rb_str_byterindex_m(int argc, VALUE *argv, VALUE str)
5258{
5259 VALUE sub;
5260 VALUE initpos;
5261 rb_len_t pos;
5262
5263 if (rb_scan_args(argc, argv, "11", &sub, &initpos) == 2) {
5264 pos = NUM2LEN(initpos);
5265 rb_len_t len = RSTRING_LEN(str);
5266 if (pos < 0 && (pos += len) < 0) {
5267 if (RB_TYPE_P(sub, T_REGEXP)) {
5269 }
5270 return Qnil;
5271 }
5272 if (pos > len) pos = len;
5273 }
5274 else {
5275 pos = RSTRING_LEN(str);
5276 }
5277
5278 str_ensure_byte_pos(str, pos);
5279
5280 if (RB_TYPE_P(sub, T_REGEXP)) {
5281 if (rb_reg_search(sub, str, pos, 1) >= 0) {
5282 VALUE match = rb_backref_get();
5283 pos = RMATCH_BEG(match, 0);
5284 return LEN2NUM(pos);
5285 }
5286 }
5287 else {
5288 StringValue(sub);
5289 pos = rb_str_byterindex(str, sub, pos);
5290 if (pos >= 0) return LEN2NUM(pos);
5291 }
5292 return Qnil;
5293}
5294
5295/*
5296 * call-seq:
5297 * self =~ other -> integer or nil
5298 *
5299 * When +other+ is a Regexp:
5300 *
5301 * - Returns the integer index (in characters) of the first match
5302 * for +self+ and +other+, or +nil+ if none;
5303 * - Updates {Regexp-related global variables}[rdoc-ref:Regexp@Global+Variables].
5304 *
5305 * Examples:
5306 *
5307 * 'foo' =~ /f/ # => 0
5308 * $~ # => #<MatchData "f">
5309 * 'foo' =~ /o/ # => 1
5310 * $~ # => #<MatchData "o">
5311 * 'foo' =~ /x/ # => nil
5312 * $~ # => nil
5313 *
5314 * Note that <tt>string =~ regexp</tt> is different from <tt>regexp =~ string</tt>
5315 * (see Regexp#=~):
5316 *
5317 * number = nil
5318 * 'no. 9' =~ /(?<number>\d+)/ # => 4
5319 * number # => nil # Not assigned.
5320 * /(?<number>\d+)/ =~ 'no. 9' # => 4
5321 * number # => "9" # Assigned.
5322 *
5323 * When +other+ is not a Regexp, returns the value
5324 * returned by <tt>other =~ self</tt>.
5325 *
5326 * Related: see {Querying}[rdoc-ref:String@Querying].
5327 */
5328
5329static VALUE
5330rb_str_match(VALUE x, VALUE y)
5331{
5332 switch (OBJ_BUILTIN_TYPE(y)) {
5333 case T_STRING:
5334 rb_raise(rb_eTypeError, "type mismatch: String given");
5335
5336 case T_REGEXP:
5337 return rb_reg_match(y, x);
5338
5339 default:
5340 return rb_funcall(y, idEqTilde, 1, x);
5341 }
5342}
5343
5344
5345static VALUE get_pat(VALUE);
5346
5347
5348/*
5349 * call-seq:
5350 * match(pattern, offset = 0) -> matchdata or nil
5351 * match(pattern, offset = 0) {|matchdata| ... } -> object
5352 *
5353 * Creates a MatchData object based on +self+ and the given arguments;
5354 * updates {Regexp Global Variables}[rdoc-ref:Regexp@Global+Variables].
5355 *
5356 * - Computes +regexp+ by converting +pattern+ (if not already a Regexp).
5357 *
5358 * regexp = Regexp.new(pattern)
5359 *
5360 * - Calls <tt>regexp.match</tt> with +self+ to compute +matchdata+.
5361 * If +offset+ is given, it is also passed (see Regexp#match).
5362 *
5363 * With no block given, returns the computed +matchdata+ or +nil+:
5364 *
5365 * 'foo'.match('f') # => #<MatchData "f">
5366 * 'foo'.match('o') # => #<MatchData "o">
5367 * 'foo'.match('x') # => nil
5368 * 'foo'.match('f', 1) # => nil
5369 * 'foo'.match('o', 1) # => #<MatchData "o">
5370 *
5371 * With a block given and computed +matchdata+ non-nil, calls the block with +matchdata+;
5372 * returns the block's return value:
5373 *
5374 * 'foo'.match(/o/) {|matchdata| matchdata } # => #<MatchData "o">
5375 *
5376 * With a block given and +nil+ +matchdata+, does not call the block:
5377 *
5378 * 'foo'.match(/x/) {|matchdata| fail 'Cannot happen' } # => nil
5379 *
5380 * Related: see {Querying}[rdoc-ref:String@Querying].
5381 */
5382
5383static VALUE
5384rb_str_match_m(int argc, VALUE *argv, VALUE str)
5385{
5386 VALUE re, result;
5387 if (argc < 1)
5388 rb_check_arity(argc, 1, 2);
5389 re = argv[0];
5390 argv[0] = str;
5391 result = rb_funcallv(get_pat(re), rb_intern("match"), argc, argv);
5392 if (!NIL_P(result) && rb_block_given_p()) {
5393 return rb_yield(result);
5394 }
5395 return result;
5396}
5397
5398/*
5399 * call-seq:
5400 * match?(pattern, offset = 0) -> true or false
5401 *
5402 * Returns whether a match is found for +self+ and the given arguments;
5403 * does not update {Regexp Global Variables}[rdoc-ref:Regexp@Global+Variables].
5404 *
5405 * Computes +regexp+ by converting +pattern+ (if not already a Regexp):
5406 *
5407 * regexp = Regexp.new(pattern)
5408 *
5409 * The search for +regexp+ in +self+ begins at the given character +offset+.
5410 * Returns +true+ if a match is found, +false+ otherwise:
5411 *
5412 * 'foo'.match?(/o/) # => true
5413 * 'foo'.match?('o') # => true
5414 * 'foo'.match?(/x/) # => false
5415 * 'foo'.match?('f', 1) # => false
5416 * 'foo'.match?('o', 1) # => true
5417 *
5418 * Related: see {Querying}[rdoc-ref:String@Querying].
5419 */
5420
5421static VALUE
5422rb_str_match_m_p(int argc, VALUE *argv, VALUE str)
5423{
5424 VALUE re;
5425 rb_check_arity(argc, 1, 2);
5426 re = get_pat(argv[0]);
5427 return rb_reg_match_p(re, str, argc > 1 ? NUM2LEN(argv[1]) : 0);
5428}
5429
5430enum neighbor_char {
5431 NEIGHBOR_NOT_CHAR,
5432 NEIGHBOR_FOUND,
5433 NEIGHBOR_WRAPPED
5434};
5435
5436static enum neighbor_char
5437enc_succ_char(char *p, rb_len_t len, rb_encoding *enc)
5438{
5439 rb_len_t i;
5440 int l;
5441
5442 if (rb_enc_mbminlen(enc) > 1) {
5443 /* wchar, trivial case */
5444 int r = rb_enc_precise_mbclen(p, p + len, enc), c;
5445 if (!MBCLEN_CHARFOUND_P(r)) {
5446 return NEIGHBOR_NOT_CHAR;
5447 }
5448 c = rb_enc_mbc_to_codepoint(p, p + len, enc) + 1;
5449 l = rb_enc_code_to_mbclen(c, enc);
5450 if (!l) return NEIGHBOR_NOT_CHAR;
5451 if (l != len) return NEIGHBOR_WRAPPED;
5452 rb_enc_mbcput(c, p, enc);
5453 r = rb_enc_precise_mbclen(p, p + len, enc);
5454 if (!MBCLEN_CHARFOUND_P(r)) {
5455 return NEIGHBOR_NOT_CHAR;
5456 }
5457 return NEIGHBOR_FOUND;
5458 }
5459 while (1) {
5460 for (i = len-1; 0 <= i && (unsigned char)p[i] == 0xff; i--)
5461 p[i] = '\0';
5462 if (i < 0)
5463 return NEIGHBOR_WRAPPED;
5464 ++((unsigned char*)p)[i];
5465 l = rb_enc_precise_mbclen(p, p+len, enc);
5466 if (MBCLEN_CHARFOUND_P(l)) {
5467 l = MBCLEN_CHARFOUND_LEN(l);
5468 if (l == len) {
5469 return NEIGHBOR_FOUND;
5470 }
5471 else {
5472 memset(p+l, 0xff, len-l);
5473 }
5474 }
5475 if (MBCLEN_INVALID_P(l) && i < len-1) {
5476 rb_len_t len2;
5477 int l2;
5478 for (len2 = len-1; 0 < len2; len2--) {
5479 l2 = rb_enc_precise_mbclen(p, p+len2, enc);
5480 if (!MBCLEN_INVALID_P(l2))
5481 break;
5482 }
5483 memset(p+len2+1, 0xff, len-(len2+1));
5484 }
5485 }
5486}
5487
5488static enum neighbor_char
5489enc_pred_char(char *p, rb_len_t len, rb_encoding *enc)
5490{
5491 rb_len_t i;
5492 int l;
5493 if (rb_enc_mbminlen(enc) > 1) {
5494 /* wchar, trivial case */
5495 int r = rb_enc_precise_mbclen(p, p + len, enc), c;
5496 if (!MBCLEN_CHARFOUND_P(r)) {
5497 return NEIGHBOR_NOT_CHAR;
5498 }
5499 c = rb_enc_mbc_to_codepoint(p, p + len, enc);
5500 if (!c) return NEIGHBOR_NOT_CHAR;
5501 --c;
5502 l = rb_enc_code_to_mbclen(c, enc);
5503 if (!l) return NEIGHBOR_NOT_CHAR;
5504 if (l != len) return NEIGHBOR_WRAPPED;
5505 rb_enc_mbcput(c, p, enc);
5506 r = rb_enc_precise_mbclen(p, p + len, enc);
5507 if (!MBCLEN_CHARFOUND_P(r)) {
5508 return NEIGHBOR_NOT_CHAR;
5509 }
5510 return NEIGHBOR_FOUND;
5511 }
5512 while (1) {
5513 for (i = len-1; 0 <= i && (unsigned char)p[i] == 0; i--)
5514 p[i] = '\xff';
5515 if (i < 0)
5516 return NEIGHBOR_WRAPPED;
5517 --((unsigned char*)p)[i];
5518 l = rb_enc_precise_mbclen(p, p+len, enc);
5519 if (MBCLEN_CHARFOUND_P(l)) {
5520 l = MBCLEN_CHARFOUND_LEN(l);
5521 if (l == len) {
5522 return NEIGHBOR_FOUND;
5523 }
5524 else {
5525 memset(p+l, 0, len-l);
5526 }
5527 }
5528 if (MBCLEN_INVALID_P(l) && i < len-1) {
5529 rb_len_t len2;
5530 int l2;
5531 for (len2 = len-1; 0 < len2; len2--) {
5532 l2 = rb_enc_precise_mbclen(p, p+len2, enc);
5533 if (!MBCLEN_INVALID_P(l2))
5534 break;
5535 }
5536 memset(p+len2+1, 0, len-(len2+1));
5537 }
5538 }
5539}
5540
5541/*
5542 overwrite +p+ by succeeding letter in +enc+ and returns
5543 NEIGHBOR_FOUND or NEIGHBOR_WRAPPED.
5544 When NEIGHBOR_WRAPPED, carried-out letter is stored into carry.
5545 assuming each ranges are successive, and mbclen
5546 never change in each ranges.
5547 NEIGHBOR_NOT_CHAR is returned if invalid character or the range has only one
5548 character.
5549 */
5550static enum neighbor_char
5551enc_succ_alnum_char(char *p, rb_len_t len, rb_encoding *enc, char *carry)
5552{
5553 enum neighbor_char ret;
5554 unsigned int c;
5555 int ctype;
5556 int range;
5557 char save[ONIGENC_CODE_TO_MBC_MAXLEN];
5558
5559 /* skip 03A2, invalid char between GREEK CAPITAL LETTERS */
5560 int try;
5561 const int max_gaps = 1;
5562
5563 c = rb_enc_mbc_to_codepoint(p, p+len, enc);
5564 if (rb_enc_isctype(c, ONIGENC_CTYPE_DIGIT, enc))
5565 ctype = ONIGENC_CTYPE_DIGIT;
5566 else if (rb_enc_isctype(c, ONIGENC_CTYPE_ALPHA, enc))
5567 ctype = ONIGENC_CTYPE_ALPHA;
5568 else
5569 return NEIGHBOR_NOT_CHAR;
5570
5571 MEMCPY(save, p, char, len);
5572 for (try = 0; try <= max_gaps; ++try) {
5573 ret = enc_succ_char(p, len, enc);
5574 if (ret == NEIGHBOR_FOUND) {
5575 c = rb_enc_mbc_to_codepoint(p, p+len, enc);
5576 if (rb_enc_isctype(c, ctype, enc))
5577 return NEIGHBOR_FOUND;
5578 }
5579 }
5580 MEMCPY(p, save, char, len);
5581 range = 1;
5582 while (1) {
5583 MEMCPY(save, p, char, len);
5584 ret = enc_pred_char(p, len, enc);
5585 if (ret == NEIGHBOR_FOUND) {
5586 c = rb_enc_mbc_to_codepoint(p, p+len, enc);
5587 if (!rb_enc_isctype(c, ctype, enc)) {
5588 MEMCPY(p, save, char, len);
5589 break;
5590 }
5591 }
5592 else {
5593 MEMCPY(p, save, char, len);
5594 break;
5595 }
5596 range++;
5597 }
5598 if (range == 1) {
5599 return NEIGHBOR_NOT_CHAR;
5600 }
5601
5602 if (ctype != ONIGENC_CTYPE_DIGIT) {
5603 MEMCPY(carry, p, char, len);
5604 return NEIGHBOR_WRAPPED;
5605 }
5606
5607 MEMCPY(carry, p, char, len);
5608 enc_succ_char(carry, len, enc);
5609 return NEIGHBOR_WRAPPED;
5610}
5611
5612
5613static VALUE str_succ(VALUE str);
5614
5615/*
5616 * call-seq:
5617 * succ -> new_str
5618 *
5619 * Returns the successor to +self+. The successor is calculated by
5620 * incrementing characters.
5621 *
5622 * The first character to be incremented is the rightmost alphanumeric:
5623 * or, if no alphanumerics, the rightmost character:
5624 *
5625 * 'THX1138'.succ # => "THX1139"
5626 * '<<koala>>'.succ # => "<<koalb>>"
5627 * '***'.succ # => '**+'
5628 * 'こんにちは'.succ # => "こんにちば"
5629 *
5630 * The successor to a digit is another digit, "carrying" to the next-left
5631 * character for a "rollover" from 9 to 0, and prepending another digit
5632 * if necessary:
5633 *
5634 * '00'.succ # => "01"
5635 * '09'.succ # => "10"
5636 * '99'.succ # => "100"
5637 *
5638 * The successor to a letter is another letter of the same case,
5639 * carrying to the next-left character for a rollover,
5640 * and prepending another same-case letter if necessary:
5641 *
5642 * 'aa'.succ # => "ab"
5643 * 'az'.succ # => "ba"
5644 * 'zz'.succ # => "aaa"
5645 * 'AA'.succ # => "AB"
5646 * 'AZ'.succ # => "BA"
5647 * 'ZZ'.succ # => "AAA"
5648 *
5649 * The successor to a non-alphanumeric character is the next character
5650 * in the underlying character set's collating sequence,
5651 * carrying to the next-left character for a rollover,
5652 * and prepending another character if necessary:
5653 *
5654 * s = 0.chr * 3 # => "\x00\x00\x00"
5655 * s.succ # => "\x00\x00\x01"
5656 * s = 255.chr * 3 # => "\xFF\xFF\xFF"
5657 * s.succ # => "\x01\x00\x00\x00"
5658 *
5659 * Carrying can occur between and among mixtures of alphanumeric characters:
5660 *
5661 * s = 'zz99zz99' # => "zz99zz99"
5662 * s.succ # => "aaa00aa00"
5663 * s = '99zz99zz' # => "99zz99zz"
5664 * s.succ # => "100aa00aa"
5665 *
5666 * The successor to an empty +String+ is a new empty +String+:
5667 *
5668 * ''.succ # => ""
5669 *
5670 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
5671 *
5672 */
5673
5674VALUE
5676{
5677 VALUE str;
5678 str = str_enc_new(rb_cString, RSTRING_PTR(orig), RSTRING_LEN(orig), STR_ENC_GET(orig));
5679 rb_enc_cr_str_copy_for_substr(str, orig);
5680 return str_succ(str);
5681}
5682
5683static VALUE
5684str_succ(VALUE str)
5685{
5686 rb_encoding *enc;
5687 char *sbeg, *s, *e, *last_alnum = 0;
5688 int found_alnum = 0;
5689 rb_len_t l, slen;
5690 char carry[ONIGENC_CODE_TO_MBC_MAXLEN] = "\1";
5691 rb_len_t carry_pos = 0, carry_len = 1;
5692 enum neighbor_char neighbor = NEIGHBOR_FOUND;
5693
5694 slen = RSTRING_LEN(str);
5695 if (slen == 0) return str;
5696
5697 enc = STR_ENC_GET(str);
5698 sbeg = RSTRING_PTR(str);
5699 s = e = sbeg + slen;
5700
5701 while ((s = rb_enc_prev_char(sbeg, s, e, enc)) != 0) {
5702 if (neighbor == NEIGHBOR_NOT_CHAR && last_alnum) {
5703 if (ISALPHA(*last_alnum) ? ISDIGIT(*s) :
5704 ISDIGIT(*last_alnum) ? ISALPHA(*s) : 0) {
5705 break;
5706 }
5707 }
5708 l = rb_enc_precise_mbclen(s, e, enc);
5709 if (!ONIGENC_MBCLEN_CHARFOUND_P(l)) continue;
5710 l = ONIGENC_MBCLEN_CHARFOUND_LEN(l);
5711 neighbor = enc_succ_alnum_char(s, l, enc, carry);
5712 switch (neighbor) {
5713 case NEIGHBOR_NOT_CHAR:
5714 continue;
5715 case NEIGHBOR_FOUND:
5716 return str;
5717 case NEIGHBOR_WRAPPED:
5718 last_alnum = s;
5719 break;
5720 }
5721 found_alnum = 1;
5722 carry_pos = s - sbeg;
5723 carry_len = l;
5724 }
5725 if (!found_alnum) { /* str contains no alnum */
5726 s = e;
5727 while ((s = rb_enc_prev_char(sbeg, s, e, enc)) != 0) {
5728 enum neighbor_char neighbor;
5729 char tmp[ONIGENC_CODE_TO_MBC_MAXLEN];
5730 l = rb_enc_precise_mbclen(s, e, enc);
5731 if (!ONIGENC_MBCLEN_CHARFOUND_P(l)) continue;
5732 l = ONIGENC_MBCLEN_CHARFOUND_LEN(l);
5733 MEMCPY(tmp, s, char, l);
5734 neighbor = enc_succ_char(tmp, l, enc);
5735 switch (neighbor) {
5736 case NEIGHBOR_FOUND:
5737 MEMCPY(s, tmp, char, l);
5738 return str;
5739 break;
5740 case NEIGHBOR_WRAPPED:
5741 MEMCPY(s, tmp, char, l);
5742 break;
5743 case NEIGHBOR_NOT_CHAR:
5744 break;
5745 }
5746 if (rb_enc_precise_mbclen(s, s+l, enc) != l) {
5747 /* wrapped to \0...\0. search next valid char. */
5748 enc_succ_char(s, l, enc);
5749 }
5750 if (!rb_enc_asciicompat(enc)) {
5751 MEMCPY(carry, s, char, l);
5752 carry_len = l;
5753 }
5754 carry_pos = s - sbeg;
5755 }
5757 }
5758 RESIZE_CAPA(str, slen + carry_len);
5759 sbeg = RSTRING_PTR(str);
5760 s = sbeg + carry_pos;
5761 memmove(s + carry_len, s, slen - carry_pos);
5762 memmove(s, carry, carry_len);
5763 slen += carry_len;
5764 STR_SET_LEN(str, slen);
5765 TERM_FILL(&sbeg[slen], rb_enc_mbminlen(enc));
5766 rb_enc_str_coderange(str);
5767 return str;
5768}
5769
5770
5771/*
5772 * call-seq:
5773 * succ! -> self
5774 *
5775 * Like String#succ, but modifies +self+ in place; returns +self+.
5776 *
5777 * Related: see {Modifying}[rdoc-ref:String@Modifying].
5778 */
5779
5780static VALUE
5781rb_str_succ_bang(VALUE str)
5782{
5783 rb_str_modify(str);
5784 str_succ(str);
5785 return str;
5786}
5787
5788static int
5789all_digits_p(const char *s, rb_len_t len)
5790{
5791 while (len-- > 0) {
5792 if (!ISDIGIT(*s)) return 0;
5793 s++;
5794 }
5795 return 1;
5796}
5797
5798static int
5799str_upto_i(VALUE str, VALUE arg)
5800{
5801 rb_yield(str);
5802 return 0;
5803}
5804
5805/*
5806 * call-seq:
5807 * upto(other_string, exclusive = false) {|string| ... } -> self
5808 * upto(other_string, exclusive = false) -> new_enumerator
5809 *
5810 * With a block given, calls the block with each +String+ value
5811 * returned by successive calls to String#succ;
5812 * the first value is +self+, the next is <tt>self.succ</tt>, and so on;
5813 * the sequence terminates when value +other_string+ is reached;
5814 * returns +self+:
5815 *
5816 * a = []
5817 * 'a'.upto('f') {|c| a.push(c) }
5818 * a # => ["a", "b", "c", "d", "e", "f"]
5819 *
5820 * a = []
5821 * 'Ж'.upto('П') {|c| a.push(c) }
5822 * a # => ["Ж", "З", "И", "Й", "К", "Л", "М", "Н", "О", "П"]
5823 *
5824 * a = []
5825 * 'よ'.upto('ろ') {|c| a.push(c) }
5826 * a # => ["よ", "ら", "り", "る", "れ", "ろ"]
5827 *
5828 * a = []
5829 * 'a8'.upto('b6') {|c| a.push(c) }
5830 * a # => ["a8", "a9", "b0", "b1", "b2", "b3", "b4", "b5", "b6"]
5831 *
5832 * If argument +exclusive+ is given as a truthy object, the last value is omitted:
5833 *
5834 * a = []
5835 * 'a'.upto('f', true) {|c| a.push(c) }
5836 * a # => ["a", "b", "c", "d", "e"]
5837 *
5838 * If +other_string+ would not be reached, does not call the block:
5839 *
5840 * '25'.upto('5') {|s| fail s }
5841 * 'aa'.upto('a') {|s| fail s }
5842 *
5843 * With no block given, returns a new Enumerator:
5844 *
5845 * 'a8'.upto('b6') # => #<Enumerator: "a8":upto("b6")>
5846 *
5847 * Related: see {Iterating}[rdoc-ref:String@Iterating].
5848 *
5849 */
5850
5851static VALUE
5852rb_str_upto(int argc, VALUE *argv, VALUE beg)
5853{
5854 VALUE end, exclusive;
5855
5856 rb_scan_args(argc, argv, "11", &end, &exclusive);
5857 RETURN_ENUMERATOR(beg, argc, argv);
5858 return rb_str_upto_each(beg, end, RTEST(exclusive), str_upto_i, Qnil);
5859}
5860
5861VALUE
5862rb_str_upto_each(VALUE beg, VALUE end, int excl, int (*each)(VALUE, VALUE), VALUE arg)
5863{
5864 VALUE current, after_end;
5865 ID succ;
5866 int n, ascii;
5867 rb_encoding *enc;
5868
5869 CONST_ID(succ, "succ");
5870 StringValue(end);
5871 enc = rb_enc_check(beg, end);
5872 ascii = (is_ascii_string(beg) && is_ascii_string(end));
5873 /* single character */
5874 if (RSTRING_LEN(beg) == 1 && RSTRING_LEN(end) == 1 && ascii) {
5875 char c = RSTRING_PTR(beg)[0];
5876 char e = RSTRING_PTR(end)[0];
5877
5878 if (c > e || (excl && c == e)) return beg;
5879 for (;;) {
5880 VALUE str = rb_enc_str_new(&c, 1, enc);
5882 if ((*each)(str, arg)) break;
5883 if (!excl && c == e) break;
5884 c++;
5885 if (excl && c == e) break;
5886 }
5887 return beg;
5888 }
5889 /* both edges are all digits */
5890 if (ascii && ISDIGIT(RSTRING_PTR(beg)[0]) && ISDIGIT(RSTRING_PTR(end)[0]) &&
5891 all_digits_p(RSTRING_PTR(beg), RSTRING_LEN(beg)) &&
5892 all_digits_p(RSTRING_PTR(end), RSTRING_LEN(end))) {
5893 VALUE b, e;
5894 int width;
5895
5896 width = RSTRING_LENINT(beg);
5897 b = rb_str_to_inum(beg, 10, FALSE);
5898 e = rb_str_to_inum(end, 10, FALSE);
5899 if (FIXNUM_P(b) && FIXNUM_P(e)) {
5900 long bi = FIX2LONG(b);
5901 long ei = FIX2LONG(e);
5902 rb_encoding *usascii = rb_usascii_encoding();
5903
5904 while (bi <= ei) {
5905 if (excl && bi == ei) break;
5906 if ((*each)(rb_enc_sprintf(usascii, "%.*ld", width, bi), arg)) break;
5907 bi++;
5908 }
5909 }
5910 else {
5911 ID op = excl ? '<' : idLE;
5912 VALUE args[2], fmt = rb_fstring_lit("%.*d");
5913
5914 args[0] = INT2FIX(width);
5915 while (rb_funcall(b, op, 1, e)) {
5916 args[1] = b;
5917 if ((*each)(rb_str_format(numberof(args), args, fmt), arg)) break;
5918 b = rb_funcallv(b, succ, 0, 0);
5919 }
5920 }
5921 return beg;
5922 }
5923 /* normal case */
5924 n = rb_str_cmp(beg, end);
5925 if (n > 0 || (excl && n == 0)) return beg;
5926
5927 after_end = rb_funcallv(end, succ, 0, 0);
5928 current = str_duplicate(rb_cString, beg);
5929 while (!rb_str_equal(current, after_end)) {
5930 VALUE next = Qnil;
5931 if (excl || !rb_str_equal(current, end))
5932 next = rb_funcallv(current, succ, 0, 0);
5933 if ((*each)(current, arg)) break;
5934 if (NIL_P(next)) break;
5935 current = next;
5936 StringValue(current);
5937 if (excl && rb_str_equal(current, end)) break;
5938 if (RSTRING_LEN(current) > RSTRING_LEN(end) || RSTRING_LEN(current) == 0)
5939 break;
5940 }
5941
5942 return beg;
5943}
5944
5945VALUE
5946rb_str_upto_endless_each(VALUE beg, int (*each)(VALUE, VALUE), VALUE arg)
5947{
5948 VALUE current;
5949 ID succ;
5950
5951 CONST_ID(succ, "succ");
5952 /* both edges are all digits */
5953 if (is_ascii_string(beg) && ISDIGIT(RSTRING_PTR(beg)[0]) &&
5954 all_digits_p(RSTRING_PTR(beg), RSTRING_LEN(beg))) {
5955 VALUE b, args[2], fmt = rb_fstring_lit("%.*d");
5956 int width = RSTRING_LENINT(beg);
5957 b = rb_str_to_inum(beg, 10, FALSE);
5958 if (FIXNUM_P(b)) {
5959 long bi = FIX2LONG(b);
5960 rb_encoding *usascii = rb_usascii_encoding();
5961
5962 while (FIXABLE(bi)) {
5963 if ((*each)(rb_enc_sprintf(usascii, "%.*ld", width, bi), arg)) break;
5964 bi++;
5965 }
5966 b = LONG2NUM(bi);
5967 }
5968 args[0] = INT2FIX(width);
5969 while (1) {
5970 args[1] = b;
5971 if ((*each)(rb_str_format(numberof(args), args, fmt), arg)) break;
5972 b = rb_funcallv(b, succ, 0, 0);
5973 }
5974 }
5975 /* normal case */
5976 current = str_duplicate(rb_cString, beg);
5977 while (1) {
5978 VALUE next = rb_funcallv(current, succ, 0, 0);
5979 if ((*each)(current, arg)) break;
5980 current = next;
5981 StringValue(current);
5982 if (RSTRING_LEN(current) == 0)
5983 break;
5984 }
5985
5986 return beg;
5987}
5988
5989static int
5990include_range_i(VALUE str, VALUE arg)
5991{
5992 VALUE *argp = (VALUE *)arg;
5993 if (!rb_equal(str, *argp)) return 0;
5994 *argp = Qnil;
5995 return 1;
5996}
5997
5998VALUE
5999rb_str_include_range_p(VALUE beg, VALUE end, VALUE val, VALUE exclusive)
6000{
6001 beg = rb_str_new_frozen(beg);
6002 StringValue(end);
6003 end = rb_str_new_frozen(end);
6004 if (NIL_P(val)) return Qfalse;
6005 val = rb_check_string_type(val);
6006 if (NIL_P(val)) return Qfalse;
6007 if (rb_enc_asciicompat(STR_ENC_GET(beg)) &&
6008 rb_enc_asciicompat(STR_ENC_GET(end)) &&
6009 rb_enc_asciicompat(STR_ENC_GET(val))) {
6010 const char *bp = RSTRING_PTR(beg);
6011 const char *ep = RSTRING_PTR(end);
6012 const char *vp = RSTRING_PTR(val);
6013 if (RSTRING_LEN(beg) == 1 && RSTRING_LEN(end) == 1) {
6014 if (RSTRING_LEN(val) == 0 || RSTRING_LEN(val) > 1)
6015 return Qfalse;
6016 else {
6017 char b = *bp;
6018 char e = *ep;
6019 char v = *vp;
6020
6021 if (ISASCII(b) && ISASCII(e) && ISASCII(v)) {
6022 if (b <= v && v < e) return Qtrue;
6023 return RBOOL(!RTEST(exclusive) && b <= v && v == e);
6024 }
6025 }
6026 }
6027#if 0
6028 /* both edges are all digits */
6029 if (ISDIGIT(*bp) && ISDIGIT(*ep) &&
6030 all_digits_p(bp, RSTRING_LEN(beg)) &&
6031 all_digits_p(ep, RSTRING_LEN(end))) {
6032 /* TODO */
6033 }
6034#endif
6035 }
6036 rb_str_upto_each(beg, end, RTEST(exclusive), include_range_i, (VALUE)&val);
6037
6038 return RBOOL(NIL_P(val));
6039}
6040
6041static VALUE
6042rb_str_subpat(VALUE str, VALUE re, VALUE backref)
6043{
6044 if (rb_reg_search(re, str, 0, 0) >= 0) {
6045 VALUE match = rb_backref_get();
6046 int nth = rb_reg_backref_number(match, backref);
6047 return rb_reg_nth_match(nth, match);
6048 }
6049 return Qnil;
6050}
6051
6052static VALUE
6053rb_str_aref(VALUE str, VALUE indx)
6054{
6055 rb_len_t idx;
6056
6057 if (FIXNUM_P(indx)) {
6058 idx = FIX2LONG(indx);
6059 }
6060 else if (RB_TYPE_P(indx, T_REGEXP)) {
6061 return rb_str_subpat(str, indx, INT2FIX(0));
6062 }
6063 else if (RB_TYPE_P(indx, T_STRING)) {
6064 if (rb_str_index(str, indx, 0) != -1)
6065 return str_duplicate(rb_cString, indx);
6066 return Qnil;
6067 }
6068 else {
6069 /* check if indx is Range */
6070 rb_len_t beg, len = str_strlen(str, NULL);
6071 switch (rb_range_beg_len(indx, &beg, &len, len, 0)) {
6072 case Qfalse:
6073 break;
6074 case Qnil:
6075 return Qnil;
6076 default:
6077 return rb_str_substr(str, beg, len);
6078 }
6079 idx = NUM2LEN(indx);
6080 }
6081
6082 return str_substr(str, idx, 1, FALSE);
6083}
6084
6085
6086/*
6087 * call-seq:
6088 * self[offset] -> new_string or nil
6089 * self[offset, size] -> new_string or nil
6090 * self[range] -> new_string or nil
6091 * self[regexp, capture = 0] -> new_string or nil
6092 * self[substring] -> new_string or nil
6093 *
6094 * Returns the substring of +self+ specified by the arguments.
6095 *
6096 * <b>Form <tt>self[offset]</tt></b>
6097 *
6098 * With non-negative integer argument +offset+ given,
6099 * returns the 1-character substring found in self at character offset +offset+:
6100 *
6101 * 'hello'[0] # => "h"
6102 * 'hello'[4] # => "o"
6103 * 'hello'[5] # => nil
6104 * 'こんにちは'[4] # => "は"
6105 *
6106 * With negative integer argument +offset+ given,
6107 * counts backward from the end of +self+:
6108 *
6109 * 'hello'[-1] # => "o"
6110 * 'hello'[-5] # => "h"
6111 * 'hello'[-6] # => nil
6112 *
6113 * <b>Form <tt>self[offset, size]</tt></b>
6114 *
6115 * With integer arguments +offset+ and +size+ given,
6116 * returns a substring of size +size+ characters (as available)
6117 * beginning at character offset specified by +offset+.
6118 *
6119 * If argument +offset+ is non-negative,
6120 * the offset is +offset+:
6121 *
6122 * 'hello'[0, 1] # => "h"
6123 * 'hello'[0, 5] # => "hello"
6124 * 'hello'[0, 6] # => "hello"
6125 * 'hello'[2, 3] # => "llo"
6126 * 'hello'[2, 0] # => ""
6127 * 'hello'[2, -1] # => nil
6128 *
6129 * If argument +offset+ is negative,
6130 * counts backward from the end of +self+:
6131 *
6132 * 'hello'[-1, 1] # => "o"
6133 * 'hello'[-5, 5] # => "hello"
6134 * 'hello'[-1, 0] # => ""
6135 * 'hello'[-6, 5] # => nil
6136 *
6137 * Special case: if +offset+ equals the size of +self+,
6138 * returns a new empty string:
6139 *
6140 * 'hello'[5, 3] # => ""
6141 *
6142 * <b>Form <tt>self[range]</tt></b>
6143 *
6144 * With Range argument +range+ given,
6145 * forms substring <tt>self[range.start, range.size]</tt>:
6146 *
6147 * 'hello'[0..2] # => "hel"
6148 * 'hello'[0, 3] # => "hel"
6149 *
6150 * 'hello'[0...2] # => "he"
6151 * 'hello'[0, 2] # => "he"
6152 *
6153 * 'hello'[0, 0] # => ""
6154 * 'hello'[0...0] # => ""
6155 *
6156 * <b>Form <tt>self[regexp, capture = 0]</tt></b>
6157 *
6158 * With Regexp argument +regexp+ given and +capture+ as zero,
6159 * searches for a matching substring in +self+;
6160 * updates {Regexp-related global variables}[rdoc-ref:Regexp@Global+Variables]:
6161 *
6162 * 'hello'[/ell/] # => "ell"
6163 * 'hello'[/l+/] # => "ll"
6164 * 'hello'[//] # => ""
6165 * 'hello'[/nosuch/] # => nil
6166 *
6167 * With +capture+ as a positive integer +n+,
6168 * returns the +n+th matched group:
6169 *
6170 * 'hello'[/(h)(e)(l+)(o)/] # => "hello"
6171 * 'hello'[/(h)(e)(l+)(o)/, 1] # => "h"
6172 * $1 # => "h"
6173 * 'hello'[/(h)(e)(l+)(o)/, 2] # => "e"
6174 * $2 # => "e"
6175 * 'hello'[/(h)(e)(l+)(o)/, 3] # => "ll"
6176 * 'hello'[/(h)(e)(l+)(o)/, 4] # => "o"
6177 * 'hello'[/(h)(e)(l+)(o)/, 5] # => nil
6178 *
6179 * <b>Form <tt>self[substring]</tt></b>
6180 *
6181 * With string argument +substring+ given,
6182 * returns the matching substring of +self+, if found:
6183 *
6184 * 'hello'['ell'] # => "ell"
6185 * 'hello'[''] # => ""
6186 * 'hello'['nosuch'] # => nil
6187 * 'こんにちは'['んにち'] # => "んにち"
6188 *
6189 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
6190 *
6191 */
6192
6193static VALUE
6194rb_str_aref_m(int argc, VALUE *argv, VALUE str)
6195{
6196 if (argc == 2) {
6197 if (RB_TYPE_P(argv[0], T_REGEXP)) {
6198 return rb_str_subpat(str, argv[0], argv[1]);
6199 }
6200 else {
6201 return rb_str_substr_two_fixnums(str, argv[0], argv[1], TRUE);
6202 }
6203 }
6204 rb_check_arity(argc, 1, 2);
6205 return rb_str_aref(str, argv[0]);
6206}
6207
6208VALUE
6210{
6211 str_modifiable(str);
6212
6213 char *ptr;
6214 rb_len_t olen;
6215 RSTRING_GETMEM(str, ptr, olen);
6216 if (len > olen) len = olen;
6217 rb_len_t nlen = olen - len;
6218 if (str_embed_capa(str) >= nlen + TERM_LEN(str)) {
6219 char *oldptr = ptr;
6220 size_t old_capa = RSTRING(str)->as.heap.aux.capa + TERM_LEN(str);
6221 int fl = (int)(RBASIC(str)->flags & (STR_NOEMBED|STR_SHARED|STR_NOFREE));
6222 STR_SET_EMBED(str);
6223 ptr = RSTRING(str)->as.embed.ary;
6224 memmove(ptr, oldptr + len, nlen);
6225 if (fl == STR_NOEMBED) {
6226 SIZED_FREE_N(oldptr, old_capa);
6227 }
6228 }
6229 else {
6230 if (!STR_SHARED_P(str)) {
6231 VALUE shared = heap_str_make_shared(rb_obj_class(str), str, TERM_LEN(str));
6232 rb_enc_cr_str_exact_copy(shared, str);
6234 }
6235 ptr = RSTRING(str)->as.heap.ptr += len;
6236 }
6237 STR_SET_LEN(str, nlen);
6238
6239 if (!SHARABLE_MIDDLE_SUBSTRING) {
6240 TERM_FILL(ptr + nlen, TERM_LEN(str));
6241 }
6243 return str;
6244}
6245
6246static void
6247rb_str_update_1(VALUE str, rb_len_t beg, rb_len_t len, VALUE val, rb_len_t vbeg, rb_len_t vlen)
6248{
6249 char *sptr;
6250 rb_len_t slen;
6251 int cr;
6252
6253 if (beg == 0 && vlen == 0) {
6254 rb_str_drop_bytes(str, len);
6255 return;
6256 }
6257
6258 str_modify_keep_cr(str);
6259 RSTRING_GETMEM(str, sptr, slen);
6260 if (len < vlen) {
6261 if (RB_UNLIKELY(slen > RB_LEN_MAX - (vlen - len))) {
6262 rb_raise(rb_eArgError, "string sizes too big");
6263 }
6264 if ((rb_len_t)str_capacity(str, TERM_LEN(str)) < slen + vlen - len) {
6265 /* expand string */
6266 RESIZE_CAPA(str, slen + vlen - len);
6267 sptr = RSTRING_PTR(str);
6268 }
6269 }
6270
6272 cr = rb_enc_str_coderange(val);
6273 else
6275
6276 if (vlen != len) {
6277 memmove(sptr + beg + vlen,
6278 sptr + beg + len,
6279 slen - (beg + len));
6280 }
6281 if (vlen < beg && len < 0) {
6282 MEMZERO(sptr + slen, char, -len);
6283 }
6284 if (vlen > 0) {
6285 memmove(sptr + beg, RSTRING_PTR(val) + vbeg, vlen);
6286 }
6287 slen += vlen - len;
6288 STR_SET_LEN(str, slen);
6289 TERM_FILL(&sptr[slen], TERM_LEN(str));
6290 ENC_CODERANGE_SET(str, cr);
6291}
6292
6293static inline void
6294rb_str_update_0(VALUE str, rb_len_t beg, rb_len_t len, VALUE val)
6295{
6296 rb_str_update_1(str, beg, len, val, 0, RSTRING_LEN(val));
6297}
6298
6299void
6301{
6302 rb_len_t slen;
6303 char *p, *e;
6304 rb_encoding *enc;
6305 int singlebyte = single_byte_optimizable(str);
6306 int cr;
6307
6308 if (len < 0) rb_raise(rb_eIndexError, "negative length %"PRIdLEN, len);
6309
6310 StringValue(val);
6311 enc = rb_enc_check(str, val);
6312 slen = str_strlen(str, enc); /* rb_enc_check */
6313
6314 if ((slen < beg) || ((beg < 0) && (beg + slen < 0))) {
6315 rb_raise(rb_eIndexError, "index %"PRIdLEN" out of string", beg);
6316 }
6317 if (beg < 0) {
6318 beg += slen;
6319 }
6320 RUBY_ASSERT(beg >= 0);
6321 RUBY_ASSERT(beg <= slen);
6322
6323 if (len > slen - beg) {
6324 len = slen - beg;
6325 }
6326 p = str_nth(RSTRING_PTR(str), RSTRING_END(str), beg, enc, singlebyte);
6327 if (!p) p = RSTRING_END(str);
6328 e = str_nth(p, RSTRING_END(str), len, enc, singlebyte);
6329 if (!e) e = RSTRING_END(str);
6330 /* error check */
6331 beg = p - RSTRING_PTR(str); /* physical position */
6332 len = e - p; /* physical length */
6333 rb_str_update_0(str, beg, len, val);
6334 rb_enc_associate(str, enc);
6336 if (cr != ENC_CODERANGE_BROKEN)
6337 ENC_CODERANGE_SET(str, cr);
6338}
6339
6340static void
6341rb_str_subpat_set(VALUE str, VALUE re, VALUE backref, VALUE val)
6342{
6343 int nth;
6344 VALUE match;
6345 rb_len_t start, end, len;
6346 rb_encoding *enc;
6347
6348 if (rb_reg_search(re, str, 0, 0) < 0) {
6349 rb_raise(rb_eIndexError, "regexp not matched");
6350 }
6351 match = rb_backref_get();
6352 nth = rb_reg_backref_number(match, backref);
6353 int num_regs = RMATCH_NREGS(match); /* 1 <= num_regs <= ONIG_MAX_CAPTURE_GROUP_NUM (= 32767) */
6354 if (nth >= num_regs || nth + num_regs <= 0) {
6355 rb_raise(rb_eIndexError, "index %d out of regexp", nth);
6356 }
6357 if (nth < 0) {
6358 nth += num_regs;
6359 }
6360
6361 start = RMATCH_BEG(match, nth);
6362 if (start == -1) {
6363 rb_raise(rb_eIndexError, "regexp group %d not matched", nth);
6364 }
6365 end = RMATCH_END(match, nth);
6366 len = end - start;
6367
6368 StringValue(val);
6369 if (start + len > RSTRING_LEN(str)) {
6370 rb_raise(rb_eRuntimeError, "string modified");
6371 }
6372
6373 enc = rb_enc_check_str(str, val);
6374 rb_str_update_0(str, start, len, val);
6375 rb_enc_associate(str, enc);
6376}
6377
6378static VALUE
6379rb_str_aset(VALUE str, VALUE indx, VALUE val)
6380{
6381 rb_len_t idx, beg;
6382
6383 switch (TYPE(indx)) {
6384 case T_REGEXP:
6385 rb_str_subpat_set(str, indx, INT2FIX(0), val);
6386 return val;
6387
6388 case T_STRING:
6389 beg = rb_str_index(str, indx, 0);
6390 if (beg < 0) {
6391 rb_raise(rb_eIndexError, "string not matched");
6392 }
6393 beg = rb_str_sublen(str, beg);
6394 rb_str_update(str, beg, str_strlen(indx, NULL), val);
6395 return val;
6396
6397 default:
6398 /* check if indx is Range */
6399 {
6400 rb_len_t beg, len;
6401 if (rb_range_beg_len(indx, &beg, &len, str_strlen(str, NULL), 2)) {
6402 rb_str_update(str, beg, len, val);
6403 return val;
6404 }
6405 }
6406 /* FALLTHROUGH */
6407
6408 case T_FIXNUM:
6409 idx = NUM2LEN(indx);
6410 rb_str_update(str, idx, 1, val);
6411 return val;
6412 }
6413}
6414
6415/*
6416 * call-seq:
6417 * self[index] = other_string -> new_string
6418 * self[start, length] = other_string -> new_string
6419 * self[range] = other_string -> new_string
6420 * self[regexp, capture = 0] = other_string -> new_string
6421 * self[substring] = other_string -> new_string
6422 *
6423 * Returns +self+ with all, a substring, or none of its contents replaced;
6424 * returns the argument +other_string+.
6425 *
6426 * <b>Form <tt>self[index] = other_string</tt></b>
6427 *
6428 * With non-negative integer argument +index+ given,
6429 * searches for the 1-character substring found in self at character offset index:
6430 *
6431 * s = 'hello'
6432 * s[0] = 'foo' # => "foo"
6433 * s # => "fooello"
6434 *
6435 * s = 'hello'
6436 * s[4] = 'foo' # => "foo"
6437 * s # => "hellfoo"
6438 *
6439 * s = 'hello'
6440 * s[5] = 'foo' # => "foo"
6441 * s # => "hellofoo"
6442 *
6443 * s = 'hello'
6444 * s[6] = 'foo' # Raises IndexError: index 6 out of string.
6445 *
6446 * With negative integer argument +index+ given,
6447 * counts backward from the end of +self+:
6448 *
6449 * s = 'hello'
6450 * s[-1] = 'foo' # => "foo"
6451 * s # => "hellfoo"
6452 *
6453 * s = 'hello'
6454 * s[-5] = 'foo' # => "foo"
6455 * s # => "fooello"
6456 *
6457 * s = 'hello'
6458 * s[-6] = 'foo' # Raises IndexError: index -6 out of string.
6459 *
6460 * <b>Form <tt>self[start, length] = other_string</tt></b>
6461 *
6462 * With integer arguments +start+ and +length+ given,
6463 * searches for a substring of size +length+ characters (as available)
6464 * beginning at character offset specified by +start+.
6465 *
6466 * If argument +start+ is non-negative,
6467 * the offset is +start+:
6468 *
6469 * s = 'hello'
6470 * s[0, 1] = 'foo' # => "foo"
6471 * s # => "fooello"
6472 *
6473 * s = 'hello'
6474 * s[0, 5] = 'foo' # => "foo"
6475 * s # => "foo"
6476 *
6477 * s = 'hello'
6478 * s[0, 9] = 'foo' # => "foo"
6479 * s # => "foo"
6480 *
6481 * s = 'hello'
6482 * s[2, 0] = 'foo' # => "foo"
6483 * s # => "hefoollo"
6484 *
6485 * s = 'hello'
6486 * s[2, -1] = 'foo' # Raises IndexError: negative length -1.
6487 *
6488 * If argument +start+ is negative,
6489 * counts backward from the end of +self+:
6490 *
6491 * s = 'hello'
6492 * s[-1, 1] = 'foo' # => "foo"
6493 * s # => "hellfoo"
6494 *
6495 * s = 'hello'
6496 * s[-1, 9] = 'foo' # => "foo"
6497 * s # => "hellfoo"
6498 *
6499 * s = 'hello'
6500 * s[-5, 2] = 'foo' # => "foo"
6501 * s # => "foollo"
6502 *
6503 * s = 'hello'
6504 * s[-3, 0] = 'foo' # => "foo"
6505 * s # => "hefoollo"
6506 *
6507 * s = 'hello'
6508 * s[-6, 2] = 'foo' # Raises IndexError: index -6 out of string.
6509 *
6510 * Special case: if +start+ equals the length of +self+,
6511 * the argument is appended to +self+:
6512 *
6513 * s = 'hello'
6514 * s[5, 3] = 'foo' # => "foo"
6515 * s # => "hellofoo"
6516 *
6517 * <b>Form <tt>self[range] = other_string</tt></b>
6518 *
6519 * With Range argument +range+ given,
6520 * equivalent to <tt>self[range.start, range.size] = other_string</tt>:
6521 *
6522 * s0 = 'hello'
6523 * s1 = 'hello'
6524 * s0[0..2] = 'foo' # => "foo"
6525 * s1[0, 3] = 'foo' # => "foo"
6526 * s0 # => "foolo"
6527 * s1 # => "foolo"
6528 *
6529 * s = 'hello'
6530 * s[0...2] = 'foo' # => "foo"
6531 * s # => "foollo"
6532 *
6533 * s = 'hello'
6534 * s[0...0] = 'foo' # => "foo"
6535 * s # => "foohello"
6536 *
6537 * s = 'hello'
6538 * s[9..10] = 'foo' # Raises RangeError: 9..10 out of range
6539 *
6540 * <b>Form <tt>self[regexp, capture = 0] = other_string</tt></b>
6541 *
6542 * With Regexp argument +regexp+ given and +capture+ as zero,
6543 * searches for a matching substring in +self+;
6544 * updates {Regexp-related global variables}[rdoc-ref:Regexp@Global+Variables]:
6545 *
6546 * s = 'hello'
6547 * s[/l/] = 'L' # => "L"
6548 * [$`, $&, $'] # => ["he", "l", "lo"]
6549 * s[/eLlo/] = 'owdy' # => "owdy"
6550 * [$`, $&, $'] # => ["h", "eLlo", ""]
6551 * s[/eLlo/] = 'owdy' # Raises IndexError: regexp not matched.
6552 * [$`, $&, $'] # => [nil, nil, nil]
6553 *
6554 * With +capture+ as a positive integer +n+,
6555 * searches for the +n+th matched group:
6556 *
6557 * s = 'hello'
6558 * s[/(h)(e)(l+)(o)/] = 'foo' # => "foo"
6559 * [$`, $&, $'] # => ["", "hello", ""]
6560 *
6561 * s = 'hello'
6562 * s[/(h)(e)(l+)(o)/, 1] = 'foo' # => "foo"
6563 * s # => "fooello"
6564 * [$`, $&, $'] # => ["", "hello", ""]
6565 *
6566 * s = 'hello'
6567 * s[/(h)(e)(l+)(o)/, 2] = 'foo' # => "foo"
6568 * s # => "hfoollo"
6569 * [$`, $&, $'] # => ["", "hello", ""]
6570 *
6571 * s = 'hello'
6572 * s[/(h)(e)(l+)(o)/, 4] = 'foo' # => "foo"
6573 * s # => "hellfoo"
6574 * [$`, $&, $'] # => ["", "hello", ""]
6575 *
6576 * s = 'hello'
6577 * # => "hello"
6578 * s[/(h)(e)(l+)(o)/, 5] = 'foo' # Raises IndexError: index 5 out of regexp.
6579 *
6580 * s = 'hello'
6581 * s[/nosuch/] = 'foo' # Raises IndexError: regexp not matched.
6582 *
6583 * <b>Form <tt>self[substring] = other_string</tt></b>
6584 *
6585 * With string argument +substring+ given:
6586 *
6587 * s = 'hello'
6588 * s['l'] = 'foo' # => "foo"
6589 * s # => "hefoolo"
6590 *
6591 * s = 'hello'
6592 * s['ll'] = 'foo' # => "foo"
6593 * s # => "hefooo"
6594 *
6595 * s = 'こんにちは'
6596 * s['んにち'] = 'foo' # => "foo"
6597 * s # => "こfooは"
6598 *
6599 * s['nosuch'] = 'foo' # Raises IndexError: string not matched.
6600 *
6601 * Related: see {Modifying}[rdoc-ref:String@Modifying].
6602 *
6603 */
6604
6605static VALUE
6606rb_str_aset_m(int argc, VALUE *argv, VALUE str)
6607{
6608 if (argc == 3) {
6609 if (RB_TYPE_P(argv[0], T_REGEXP)) {
6610 rb_str_subpat_set(str, argv[0], argv[1], argv[2]);
6611 }
6612 else {
6613 rb_str_update(str, NUM2LEN(argv[0]), NUM2LEN(argv[1]), argv[2]);
6614 }
6615 return argv[2];
6616 }
6617 rb_check_arity(argc, 2, 3);
6618 return rb_str_aset(str, argv[0], argv[1]);
6619}
6620
6621/*
6622 * call-seq:
6623 * insert(offset, other_string) -> self
6624 *
6625 * Inserts the given +other_string+ into +self+; returns +self+.
6626 *
6627 * If the given +index+ is non-negative, inserts +other_string+ at offset +index+:
6628 *
6629 * 'foo'.insert(0, 'bar') # => "barfoo"
6630 * 'foo'.insert(1, 'bar') # => "fbaroo"
6631 * 'foo'.insert(3, 'bar') # => "foobar"
6632 * 'こんにちは'.insert(2, 'bar') # => "こんbarにちは"
6633 *
6634 * If the +index+ is negative, counts backward from the end of +self+
6635 * and inserts +other_string+ _after_ the offset:
6636 *
6637 * 'foo'.insert(-2, 'bar') # => "fobaro"
6638 *
6639 * Related: see {Modifying}[rdoc-ref:String@Modifying].
6640 *
6641 */
6642
6643static VALUE
6644rb_str_insert(VALUE str, VALUE idx, VALUE str2)
6645{
6646 rb_len_t pos = NUM2LEN(idx);
6647
6648 if (pos == -1) {
6649 return rb_str_append(str, str2);
6650 }
6651 else if (pos < 0) {
6652 pos++;
6653 }
6654 rb_str_update(str, pos, 0, str2);
6655 return str;
6656}
6657
6658
6659/*
6660 * call-seq:
6661 * slice!(index) -> new_string or nil
6662 * slice!(start, length) -> new_string or nil
6663 * slice!(range) -> new_string or nil
6664 * slice!(regexp, capture = 0) -> new_string or nil
6665 * slice!(substring) -> new_string or nil
6666 *
6667 * Like String#[] (and its alias String#slice), except that:
6668 *
6669 * - Performs substitutions in +self+ (not in a copy of +self+).
6670 * - Returns the removed substring if any modifications were made, +nil+ otherwise.
6671 *
6672 * A few examples:
6673 *
6674 * s = 'hello'
6675 * s.slice!('e') # => "e"
6676 * s # => "hllo"
6677 * s.slice!('e') # => nil
6678 * s # => "hllo"
6679 *
6680 * Related: see {Modifying}[rdoc-ref:String@Modifying].
6681 */
6682
6683static VALUE
6684rb_str_slice_bang(int argc, VALUE *argv, VALUE str)
6685{
6686 VALUE result = Qnil;
6687 VALUE indx;
6688 rb_len_t beg, len = 1;
6689 char *p;
6690
6691 rb_check_arity(argc, 1, 2);
6692 str_modify_keep_cr(str);
6693 indx = argv[0];
6694 if (RB_TYPE_P(indx, T_REGEXP)) {
6695 if (rb_reg_search(indx, str, 0, 0) < 0) return Qnil;
6696 VALUE match = rb_backref_get();
6697 int num_regs = RMATCH_NREGS(match);
6698 int nth = 0;
6699 if (argc > 1 && (nth = rb_reg_backref_number(match, argv[1])) < 0) {
6700 if ((nth += num_regs) <= 0) return Qnil;
6701 }
6702 else if (nth >= num_regs) return Qnil;
6703 beg = RMATCH_BEG(match, nth);
6704 len = RMATCH_END(match, nth) - beg;
6705 /* Converting the backref may have modified the string. */
6706 if (beg > RSTRING_LEN(str)) return Qnil;
6707 if (len > RSTRING_LEN(str) - beg) len = RSTRING_LEN(str) - beg;
6708 goto subseq;
6709 }
6710 else if (argc == 2) {
6711 beg = NUM2LEN(indx);
6712 len = NUM2LEN(argv[1]);
6713 goto num_index;
6714 }
6715 else if (FIXNUM_P(indx)) {
6716 beg = FIX2LONG(indx);
6717 if (!(p = rb_str_subpos(str, beg, &len))) return Qnil;
6718 if (!len) return Qnil;
6719 beg = p - RSTRING_PTR(str);
6720 goto subseq;
6721 }
6722 else if (RB_TYPE_P(indx, T_STRING)) {
6723 beg = rb_str_index(str, indx, 0);
6724 if (beg == -1) return Qnil;
6725 len = RSTRING_LEN(indx);
6726 result = str_duplicate(rb_cString, indx);
6727 goto squash;
6728 }
6729 else {
6730 switch (rb_range_beg_len(indx, &beg, &len, str_strlen(str, NULL), 0)) {
6731 case Qnil:
6732 return Qnil;
6733 case Qfalse:
6734 beg = NUM2LEN(indx);
6735 if (!(p = rb_str_subpos(str, beg, &len))) return Qnil;
6736 if (!len) return Qnil;
6737 beg = p - RSTRING_PTR(str);
6738 goto subseq;
6739 default:
6740 goto num_index;
6741 }
6742 }
6743
6744 num_index:
6745 if (!(p = rb_str_subpos(str, beg, &len))) return Qnil;
6746 beg = p - RSTRING_PTR(str);
6747
6748 subseq:
6749 result = str_enc_new(rb_cString, RSTRING_PTR(str)+beg, len, STR_ENC_GET(str));
6750 rb_enc_cr_str_copy_for_substr(result, str);
6751
6752 squash:
6753 if (len > 0) {
6754 if (beg == 0) {
6755 rb_str_drop_bytes(str, len);
6756 }
6757 else {
6758 char *sptr = RSTRING_PTR(str);
6759 rb_len_t slen = RSTRING_LEN(str);
6760 if (beg + len > slen) /* pathological check */
6761 len = slen - beg;
6762 memmove(sptr + beg,
6763 sptr + beg + len,
6764 slen - (beg + len));
6765 slen -= len;
6766 STR_SET_LEN(str, slen);
6767 TERM_FILL(&sptr[slen], TERM_LEN(str));
6768 }
6769 }
6770 return result;
6771}
6772
6773static VALUE
6774get_pat(VALUE pat)
6775{
6776 VALUE val;
6777
6778 switch (OBJ_BUILTIN_TYPE(pat)) {
6779 case T_REGEXP:
6780 return pat;
6781
6782 case T_STRING:
6783 break;
6784
6785 default:
6786 val = rb_check_string_type(pat);
6787 if (NIL_P(val)) {
6788 Check_Type(pat, T_REGEXP);
6789 }
6790 pat = val;
6791 }
6792
6793 return rb_reg_regcomp(pat);
6794}
6795
6796static VALUE
6797get_pat_quoted(VALUE pat, int check)
6798{
6799 VALUE val;
6800
6801 switch (OBJ_BUILTIN_TYPE(pat)) {
6802 case T_REGEXP:
6803 return pat;
6804
6805 case T_STRING:
6806 break;
6807
6808 default:
6809 val = rb_check_string_type(pat);
6810 if (NIL_P(val)) {
6811 Check_Type(pat, T_REGEXP);
6812 }
6813 pat = val;
6814 }
6815 if (check && is_broken_string(pat)) {
6816 rb_exc_raise(rb_reg_check_preprocess(pat));
6817 }
6818 return pat;
6819}
6820
6821static rb_len_t
6822rb_pat_search0(VALUE pat, VALUE str, rb_len_t pos, int set_backref_str, VALUE *match)
6823{
6824 if (BUILTIN_TYPE(pat) == T_STRING) {
6825 pos = rb_str_byteindex(str, pat, pos);
6826 if (set_backref_str) {
6827 if (pos >= 0) {
6828 str = rb_str_new_frozen_String(str);
6829 VALUE match_data = rb_backref_set_string(str, pos, RSTRING_LEN(pat));
6830 if (match) {
6831 *match = match_data;
6832 }
6833 }
6834 else {
6836 }
6837 }
6838 return pos;
6839 }
6840 else {
6841 return rb_reg_search0(pat, str, pos, 0, set_backref_str, match);
6842 }
6843}
6844
6845static rb_len_t
6846rb_pat_search(VALUE pat, VALUE str, rb_len_t pos, int set_backref_str)
6847{
6848 return rb_pat_search0(pat, str, pos, set_backref_str, NULL);
6849}
6850
6851
6852/*
6853 * call-seq:
6854 * sub!(pattern, replacement) -> self or nil
6855 * sub!(pattern) {|match| ... } -> self or nil
6856 *
6857 * Like String#sub, except that:
6858 *
6859 * - Changes are made to +self+, not to copy of +self+.
6860 * - Returns +self+ if any changes are made, +nil+ otherwise.
6861 *
6862 * Related: see {Modifying}[rdoc-ref:String@Modifying].
6863 */
6864
6865static VALUE
6866rb_str_sub_bang(int argc, VALUE *argv, VALUE str)
6867{
6868 VALUE pat, repl, hash = Qnil;
6869 int iter = 0;
6870 rb_len_t plen;
6871 int min_arity = rb_block_given_p() ? 1 : 2;
6872 rb_len_t beg;
6873
6874 rb_check_arity(argc, min_arity, 2);
6875 if (argc == 1) {
6876 iter = 1;
6877 }
6878 else {
6879 repl = argv[1];
6880 if (!RB_TYPE_P(repl, T_STRING)) {
6881 hash = rb_check_hash_type(repl);
6882 if (NIL_P(hash)) {
6883 StringValue(repl);
6884 }
6885 }
6886 }
6887
6888 pat = get_pat_quoted(argv[0], 1);
6889
6890 str_modifiable(str);
6891 beg = rb_pat_search(pat, str, 0, 1);
6892 if (beg >= 0) {
6893 rb_encoding *enc;
6894 int cr = ENC_CODERANGE(str);
6895 rb_len_t beg0, end0;
6896 VALUE match, match0 = Qnil;
6897 char *p, *rp;
6898 rb_len_t len, rlen;
6899
6900 match = rb_backref_get();
6901 if (RB_TYPE_P(pat, T_STRING)) {
6902 beg0 = beg;
6903 end0 = beg0 + RSTRING_LEN(pat);
6904 match0 = pat;
6905 }
6906 else {
6907 beg0 = RMATCH_BEG(match, 0);
6908 end0 = RMATCH_END(match, 0);
6909 if (iter) match0 = rb_reg_nth_match(0, match);
6910 }
6911
6912 if (iter || !NIL_P(hash)) {
6913 p = RSTRING_PTR(str); len = RSTRING_LEN(str);
6914
6915 if (iter) {
6916 repl = rb_obj_as_string(rb_yield(match0));
6917 }
6918 else {
6919 repl = rb_hash_aref(hash, rb_str_subseq(str, beg0, end0 - beg0));
6920 repl = rb_obj_as_string(repl);
6921 }
6922 str_mod_check(str, p, len);
6923 rb_check_frozen(str);
6924 }
6925 else {
6926 repl = rb_reg_regsub_match(repl, str, match);
6927 }
6928
6929 enc = rb_enc_compatible(str, repl);
6930 if (!enc) {
6931 rb_encoding *str_enc = STR_ENC_GET(str);
6932 p = RSTRING_PTR(str); len = RSTRING_LEN(str);
6933 if (coderange_scan(p, beg0, str_enc) != ENC_CODERANGE_7BIT ||
6934 coderange_scan(p+end0, len-end0, str_enc) != ENC_CODERANGE_7BIT) {
6935 rb_raise(rb_eEncCompatError, "incompatible character encodings: %s and %s",
6936 rb_enc_inspect_name(str_enc),
6937 rb_enc_inspect_name(STR_ENC_GET(repl)));
6938 }
6939 enc = STR_ENC_GET(repl);
6940 }
6941 rb_str_modify(str);
6942 rb_enc_associate(str, enc);
6944 int cr2 = ENC_CODERANGE(repl);
6945 if (cr2 == ENC_CODERANGE_BROKEN ||
6946 (cr == ENC_CODERANGE_VALID && cr2 == ENC_CODERANGE_7BIT))
6948 else
6949 cr = cr2;
6950 }
6951 plen = end0 - beg0;
6952 rlen = RSTRING_LEN(repl);
6953 len = RSTRING_LEN(str);
6954 if (rlen > plen) {
6955 RESIZE_CAPA(str, len + rlen - plen);
6956 }
6957 p = RSTRING_PTR(str);
6958 if (rlen != plen) {
6959 memmove(p + beg0 + rlen, p + beg0 + plen, len - beg0 - plen);
6960 }
6961 rp = RSTRING_PTR(repl);
6962 memmove(p + beg0, rp, rlen);
6963 len += rlen - plen;
6964 STR_SET_LEN(str, len);
6965 TERM_FILL(&RSTRING_PTR(str)[len], TERM_LEN(str));
6966 ENC_CODERANGE_SET(str, cr);
6967
6968 RB_GC_GUARD(match);
6969
6970 return str;
6971 }
6972 return Qnil;
6973}
6974
6975
6976/*
6977 * call-seq:
6978 * sub(pattern, replacement) -> new_string
6979 * sub(pattern) {|match| ... } -> new_string
6980 *
6981 * Returns a copy of self, possibly with a substring replaced.
6982 *
6983 * Argument +pattern+ may be a string or a Regexp;
6984 * argument +replacement+ may be a string or a Hash.
6985 *
6986 * Varying types for the argument values makes this method very versatile.
6987 *
6988 * Below are some simple examples; for many more examples,
6989 * see {Substitution Methods}[rdoc-ref:String@Substitution+Methods].
6990 *
6991 * With arguments +pattern+ and string +replacement+ given,
6992 * replaces the first matching substring with the given replacement string:
6993 *
6994 * s = 'abracadabra' # => "abracadabra"
6995 * s.sub('bra', 'xyzzy') # => "axyzzycadabra"
6996 * s.sub(/bra/, 'xyzzy') # => "axyzzycadabra"
6997 * s.sub('nope', 'xyzzy') # => "abracadabra"
6998 *
6999 * With arguments +pattern+ and hash +replacement+ given,
7000 * replaces the first matching substring with a value from the given replacement hash, or removes it:
7001 *
7002 * h = {'a' => 'A', 'b' => 'B', 'c' => 'C'}
7003 * s.sub('b', h) # => "aBracadabra"
7004 * s.sub(/b/, h) # => "aBracadabra"
7005 * s.sub(/d/, h) # => "abracaabra" # 'd' removed.
7006 *
7007 * With argument +pattern+ and a block given,
7008 * calls the block with each matching substring;
7009 * replaces that substring with the block’s return value:
7010 *
7011 * s.sub('b') {|match| match.upcase } # => "aBracadabra"
7012 *
7013 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
7014 */
7015
7016static VALUE
7017rb_str_sub(int argc, VALUE *argv, VALUE str)
7018{
7019 str = str_duplicate(rb_cString, str);
7020 rb_str_sub_bang(argc, argv, str);
7021 return str;
7022}
7023
7024static VALUE
7025str_gsub(int argc, VALUE *argv, VALUE str, int bang)
7026{
7027 VALUE pat, val = Qnil, repl, match0 = Qnil, dest, hash = Qnil, match = Qnil;
7028 rb_len_t beg, beg0, end0;
7029 rb_len_t offset, blen, slen, len, last;
7030 enum {STR, ITER, FAST_MAP, MAP} mode = STR;
7031 char *sp, *cp;
7032 int need_backref_str = -1;
7033 rb_encoding *str_enc;
7034
7035 switch (argc) {
7036 case 1:
7037 RETURN_ENUMERATOR(str, argc, argv);
7038 mode = ITER;
7039 break;
7040 case 2:
7041 repl = argv[1];
7042 if (!RB_TYPE_P(repl, T_STRING)) {
7043 hash = rb_check_hash_type(repl);
7044 if (NIL_P(hash)) {
7045 StringValue(repl);
7046 }
7047 else if (rb_hash_default_unredefined(hash) && !FL_TEST_RAW(hash, RHASH_PROC_DEFAULT)) {
7048 mode = FAST_MAP;
7049 }
7050 else {
7051 mode = MAP;
7052 }
7053 }
7054 break;
7055 default:
7056 rb_error_arity(argc, 1, 2);
7057 }
7058
7059 pat = get_pat_quoted(argv[0], 1);
7060 beg = rb_pat_search0(pat, str, 0, need_backref_str, &match);
7061
7062 if (beg < 0) {
7063 if (bang) return Qnil; /* no match, no substitution */
7064 return str_duplicate(rb_cString, str);
7065 }
7066 if (bang) str_modify_keep_cr(str);
7067
7068 offset = 0;
7069 blen = RSTRING_LEN(str) + 30; /* len + margin */
7070 dest = rb_str_buf_new(blen);
7071 sp = RSTRING_PTR(str);
7072 slen = RSTRING_LEN(str);
7073 cp = sp;
7074 str_enc = STR_ENC_GET(str);
7075 rb_enc_associate(dest, str_enc);
7076 ENC_CODERANGE_SET(dest, rb_enc_asciicompat(str_enc) ? ENC_CODERANGE_7BIT : ENC_CODERANGE_VALID);
7077
7078 do {
7079 if (RB_TYPE_P(pat, T_STRING)) {
7080 beg0 = beg;
7081 end0 = beg0 + RSTRING_LEN(pat);
7082 match0 = pat;
7083 }
7084 else {
7085 beg0 = RMATCH_BEG(match, 0);
7086 end0 = RMATCH_END(match, 0);
7087 if (mode == ITER) match0 = rb_reg_nth_match(0, match);
7088 }
7089
7090 if (mode != STR) {
7091 if (mode == ITER) {
7092 val = rb_obj_as_string(rb_yield(match0));
7093 }
7094 else {
7095 struct RString fake_str = {RBASIC_INIT};
7096 VALUE key;
7097 if (mode == FAST_MAP) {
7098 // It is safe to use a fake_str here because we established that it won't escape,
7099 // as it's only used for `rb_hash_aref` and we checked the hash doesn't have a
7100 // default proc.
7101 key = setup_fake_str(&fake_str, sp + beg0, end0 - beg0, ENCODING_GET_INLINED(str));
7102 }
7103 else {
7104 key = rb_str_subseq(str, beg0, end0 - beg0);
7105 }
7106 val = rb_hash_aref(hash, key);
7107 val = rb_obj_as_string(val);
7108 }
7109 str_mod_check(str, sp, slen);
7110 if (val == dest) { /* paranoid check [ruby-dev:24827] */
7111 rb_raise(rb_eRuntimeError, "block should not cheat");
7112 }
7113 }
7114 else if (need_backref_str) {
7115 val = rb_reg_regsub_match(repl, str, match);
7116 if (need_backref_str < 0) {
7117 need_backref_str = val != repl;
7118 }
7119 }
7120 else {
7121 val = repl;
7122 }
7123
7124 len = beg0 - offset; /* copy pre-match substr */
7125 if (len) {
7126 rb_enc_str_buf_cat(dest, cp, len, str_enc);
7127 }
7128
7129 rb_str_buf_append(dest, val);
7130
7131 last = offset;
7132 offset = end0;
7133 if (beg0 == end0) {
7134 /*
7135 * Always consume at least one character of the input string
7136 * in order to prevent infinite loops.
7137 */
7138 if (RSTRING_LEN(str) <= end0) break;
7139 len = rb_enc_fast_mbclen(RSTRING_PTR(str)+end0, RSTRING_END(str), str_enc);
7140 rb_enc_str_buf_cat(dest, RSTRING_PTR(str)+end0, len, str_enc);
7141 offset = end0 + len;
7142 }
7143 cp = RSTRING_PTR(str) + offset;
7144 if (offset > RSTRING_LEN(str)) break;
7145
7146 // In FAST_MAP and STR mode the backref can't escape so we can re-use the MatchData safely.
7147 if (mode != FAST_MAP && mode != STR) {
7148 match = Qnil;
7149 }
7150 beg = rb_pat_search0(pat, str, offset, need_backref_str, &match);
7151
7152 RB_GC_GUARD(match);
7153 } while (beg >= 0);
7154
7155 if (RSTRING_LEN(str) > offset) {
7156 rb_enc_str_buf_cat(dest, cp, RSTRING_LEN(str) - offset, str_enc);
7157 }
7158 rb_pat_search0(pat, str, last, 1, &match);
7159 if (bang) {
7160 str_shared_replace(str, dest);
7161 }
7162 else {
7163 str = dest;
7164 }
7165
7166 return str;
7167}
7168
7169
7170/*
7171 * call-seq:
7172 * gsub!(pattern, replacement) -> self or nil
7173 * gsub!(pattern) {|match| ... } -> self or nil
7174 * gsub!(pattern) -> an_enumerator
7175 *
7176 * Like String#gsub, except that:
7177 *
7178 * - Performs substitutions in +self+ (not in a copy of +self+).
7179 * - Returns +self+ if any substitutions were performed, +nil+ otherwise.
7180 *
7181 * Related: see {Modifying}[rdoc-ref:String@Modifying].
7182 */
7183
7184static VALUE
7185rb_str_gsub_bang(int argc, VALUE *argv, VALUE str)
7186{
7187 str_modifiable(str);
7188 return str_gsub(argc, argv, str, 1);
7189}
7190
7191
7192/*
7193 * call-seq:
7194 * gsub(pattern, replacement) -> new_string
7195 * gsub(pattern) {|match| ... } -> new_string
7196 * gsub(pattern) -> enumerator
7197 *
7198 * Returns a copy of +self+ with zero or more substrings replaced.
7199 *
7200 * Argument +pattern+ may be a string or a Regexp;
7201 * argument +replacement+ may be a string or a Hash.
7202 * Varying types for the argument values makes this method very versatile.
7203 *
7204 * Below are some simple examples;
7205 * for many more examples, see {Substitution Methods}[rdoc-ref:String@Substitution+Methods].
7206 *
7207 * With arguments +pattern+ and string +replacement+ given,
7208 * replaces each matching substring with the given +replacement+ string:
7209 *
7210 * s = 'abracadabra'
7211 * s.gsub('ab', 'AB') # => "ABracadABra"
7212 * s.gsub(/[a-c]/, 'X') # => "XXrXXXdXXrX"
7213 *
7214 * With arguments +pattern+ and hash +replacement+ given,
7215 * replaces each matching substring with a value from the given +replacement+ hash,
7216 * or removes it:
7217 *
7218 * h = {'a' => 'A', 'b' => 'B', 'c' => 'C'}
7219 * s.gsub(/[a-c]/, h) # => "ABrACAdABrA" # 'a', 'b', 'c' replaced.
7220 * s.gsub(/[a-d]/, h) # => "ABrACAABrA" # 'd' removed.
7221 *
7222 * With argument +pattern+ and a block given,
7223 * calls the block with each matching substring;
7224 * replaces that substring with the block's return value:
7225 *
7226 * s.gsub(/[a-d]/) {|substring| substring.upcase }
7227 * # => "ABrACADABrA"
7228 *
7229 * With argument +pattern+ and no block given,
7230 * returns a new Enumerator.
7231 *
7232 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
7233 */
7234
7235static VALUE
7236rb_str_gsub(int argc, VALUE *argv, VALUE str)
7237{
7238 return str_gsub(argc, argv, str, 0);
7239}
7240
7241
7242/*
7243 * call-seq:
7244 * replace(other_string) -> self
7245 *
7246 * Replaces the contents of +self+ with the contents of +other_string+;
7247 * returns +self+:
7248 *
7249 * s = 'foo' # => "foo"
7250 * s.replace('bar') # => "bar"
7251 *
7252 * Related: see {Modifying}[rdoc-ref:String@Modifying].
7253 */
7254
7255VALUE
7257{
7258 str_modifiable(str);
7259 if (str == str2) return str;
7260
7261 StringValue(str2);
7262 str_discard(str);
7263 return str_replace(str, str2);
7264}
7265
7266/*
7267 * call-seq:
7268 * clear -> self
7269 *
7270 * Removes the contents of +self+:
7271 *
7272 * s = 'foo'
7273 * s.clear # => ""
7274 * s # => ""
7275 *
7276 * Related: see {Modifying}[rdoc-ref:String@Modifying].
7277 */
7278
7279static VALUE
7280rb_str_clear(VALUE str)
7281{
7282 str_discard(str);
7283 STR_SET_EMBED(str);
7284 STR_SET_LEN(str, 0);
7285 RSTRING_PTR(str)[0] = 0;
7286 if (rb_enc_asciicompat(STR_ENC_GET(str)))
7288 else
7290 return str;
7291}
7292
7293/*
7294 * call-seq:
7295 * chr -> string
7296 *
7297 * Returns a string containing the first character of +self+:
7298 *
7299 * 'hello'.chr # => "h"
7300 * 'こんにちは'.chr # => "こ"
7301 * ''.chr # => ""
7302 *
7303 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
7304 *
7305 */
7306
7307static VALUE
7308rb_str_chr(VALUE str)
7309{
7310 return rb_str_substr(str, 0, 1);
7311}
7312
7313/*
7314 * call-seq:
7315 * getbyte(index) -> integer or nil
7316 *
7317 * Returns the byte at zero-based +index+ as an integer:
7318 *
7319 * s = 'foo'
7320 * s.getbyte(0) # => 102
7321 * s.getbyte(1) # => 111
7322 * s.getbyte(2) # => 111
7323 *
7324 * Counts backward from the end if +index+ is negative:
7325 *
7326 * s.getbyte(-3) # => 102
7327 *
7328 * Returns +nil+ if +index+ is out of range:
7329 *
7330 * s.getbyte(3) # => nil
7331 * s.getbyte(-4) # => nil
7332 *
7333 * More examples:
7334 *
7335 * s = 'こんにちは'
7336 * s.bytes # => [227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]
7337 * s.getbyte(2) # => 147
7338 *
7339 * Related: see {Converting to Non-String}[rdoc-ref:String@Converting+to+Non-String].
7340 *
7341 */
7342VALUE
7343rb_str_getbyte(VALUE str, VALUE index)
7344{
7345 rb_len_t pos = NUM2LEN(index);
7346
7347 if (pos < 0)
7348 pos += RSTRING_LEN(str);
7349 if (pos < 0 || RSTRING_LEN(str) <= pos)
7350 return Qnil;
7351
7352 return INT2FIX((unsigned char)RSTRING_PTR(str)[pos]);
7353}
7354
7355/*
7356 * call-seq:
7357 * setbyte(index, integer) -> integer
7358 *
7359 * Sets the byte at zero-based offset +index+ to the value of the given +integer+;
7360 * returns +integer+:
7361 *
7362 * s = 'xyzzy'
7363 * s.setbyte(2, 129) # => 129
7364 * s # => "xy\x81zy"
7365 *
7366 * Related: see {Modifying}[rdoc-ref:String@Modifying].
7367 */
7368VALUE
7369rb_str_setbyte(VALUE str, VALUE index, VALUE value)
7370{
7371 rb_len_t pos = NUM2LEN(index);
7372 char *ptr, *head, *left = 0;
7373 rb_encoding *enc;
7374 int cr = ENC_CODERANGE_UNKNOWN, width, nlen;
7375
7376 VALUE v = rb_to_int(value);
7377 VALUE w = rb_int_and(v, INT2FIX(0xff));
7378 char byte = (char)(NUM2INT(w) & 0xFF);
7379
7380 if (!str_independent(str))
7381 str_make_independent(str);
7382
7383 rb_len_t len = RSTRING_LEN(str);
7384 if (pos < -len || len <= pos)
7385 rb_raise(rb_eIndexError, "index %"PRIdLEN" out of string", pos);
7386 if (pos < 0)
7387 pos += len;
7388
7389 enc = STR_ENC_GET(str);
7390 head = RSTRING_PTR(str);
7391 ptr = &head[pos];
7392 if (!STR_EMBED_P(str)) {
7393 cr = ENC_CODERANGE(str);
7394 switch (cr) {
7395 case ENC_CODERANGE_7BIT:
7396 left = ptr;
7397 *ptr = byte;
7398 if (ISASCII(byte)) goto end;
7399 nlen = rb_enc_precise_mbclen(left, head+len, enc);
7400 if (!MBCLEN_CHARFOUND_P(nlen))
7402 else
7404 goto end;
7406 left = rb_enc_left_char_head(head, ptr, head+len, enc);
7407 width = rb_enc_precise_mbclen(left, head+len, enc);
7408 *ptr = byte;
7409 nlen = rb_enc_precise_mbclen(left, head+len, enc);
7410 if (!MBCLEN_CHARFOUND_P(nlen))
7412 else if (MBCLEN_CHARFOUND_LEN(nlen) != width || ISASCII(byte))
7414 goto end;
7415 }
7416 }
7418 *ptr = byte;
7419
7420 end:
7421 return value;
7422}
7423
7424static inline bool
7425str_bit_offset_out_of_range(rb_len_t byte_len, uint64_t bit_offset)
7426{
7427 /* Compare byte indexes to avoid overflowing byte_len * CHAR_BIT. */
7428 return bit_offset / CHAR_BIT >= (uint64_t)byte_len;
7429}
7430
7431/*
7432 * Keep both the full bit offset and its rb_len_t representation. Most calls
7433 * use a Fixnum-sized offset and can stay on the rb_len_t fast path; only large
7434 * Bignum offsets need the uint64_t path below. This matters on platforms where
7435 * rb_len_t is 32 bits.
7436 */
7438 uint64_t value;
7439 rb_len_t index_value;
7440 bool fits_index;
7441};
7442
7443static inline struct str_bit_offset
7444str_bit_offset_from_index(VALUE index)
7445{
7446 VALUE integer = rb_to_int(index);
7447 struct str_bit_offset offset;
7448
7449 /*
7450 * FIXNUM_P only decides whether the common rb_len_t path is immediately
7451 * usable. This covers practically all offsets on LP64 platforms; Bignum
7452 * offsets are still accepted below when they fit in uint64_t, mainly for
7453 * platforms with 32-bit rb_len_t where large strings can have Bignum bit
7454 * offsets.
7455 */
7456 if (FIXNUM_P(integer)) {
7457 offset.index_value = FIX2LONG(integer);
7458 if (offset.index_value < 0) {
7459 rb_raise(rb_eIndexError, "bit index out of range");
7460 }
7461 offset.value = (uint64_t)offset.index_value;
7462 offset.fits_index = true;
7463 return offset;
7464 }
7465
7466 RUBY_ASSERT(RB_TYPE_P(integer, T_BIGNUM));
7467 if (rb_int_negative_p(integer)) {
7468 rb_raise(rb_eIndexError, "bit index out of range");
7469 }
7470 if (rb_cmpint(rb_int_cmp(integer, ULL2NUM(UINT64_MAX)), integer, ULL2NUM(UINT64_MAX)) > 0) {
7471 rb_raise(rb_eArgError, "bit index out of representable range");
7472 }
7473
7474 offset.value = (uint64_t)NUM2ULL(integer);
7475 if (offset.value <= (uint64_t)RB_LEN_MAX) {
7476 offset.index_value = (rb_len_t)offset.value;
7477 offset.fits_index = true;
7478 }
7479 else {
7480 offset.index_value = 0;
7481 offset.fits_index = false;
7482 }
7483 return offset;
7484}
7485
7486/*
7487 * Bit lengths share the offset's representable range.
7488 * A negative length is an ArgumentError rather than an IndexError.
7489 */
7490static uint64_t
7491str_bit_length_from_index(VALUE index)
7492{
7493 VALUE integer = rb_to_int(index);
7494
7495 if (FIXNUM_P(integer)) {
7496 rb_len_t value = FIX2LONG(integer);
7497 if (value < 0) {
7498 rb_raise(rb_eArgError, "negative bit length");
7499 }
7500 return (uint64_t)value;
7501 }
7502
7503 RUBY_ASSERT(RB_TYPE_P(integer, T_BIGNUM));
7504 if (rb_int_negative_p(integer)) {
7505 rb_raise(rb_eArgError, "negative bit length");
7506 }
7507 if (rb_cmpint(rb_int_cmp(integer, ULL2NUM(UINT64_MAX)), integer, ULL2NUM(UINT64_MAX)) > 0) {
7508 rb_raise(rb_eArgError, "bit length out of representable range");
7509 }
7510 return (uint64_t)NUM2ULL(integer);
7511}
7512
7513static inline uint64_t
7514str_bit_size(rb_len_t byte_len)
7515{
7516 /*
7517 * byte_len * CHAR_BIT overflows uint64_t only for byte_len >= 2**61 which cannot
7518 * be allocated. Saturate so that unreachable cases cannot wrap.
7519 */
7520 if ((uint64_t)byte_len > UINT64_MAX / CHAR_BIT) return UINT64_MAX;
7521 return (uint64_t)byte_len * CHAR_BIT;
7522}
7523
7525 uint64_t beg;
7526 uint64_t end_exclusive; /* meaningful only when end_open is false */
7527 bool end_open; /* a nil end: the region runs to the end of self */
7528};
7529
7530/*
7531 * Coerce a bit Range's endpoints to bit offsets. This may run arbitrary Ruby
7532 * (Integer#to_int on the endpoints), so it does NOT read the string's length:
7533 * The caller must resolve the length only after this returns, otherwise
7534 * to_int that reallocates self would leave a stale size.
7535 */
7536static void
7537str_bit_range_to_offsets(VALUE range, struct str_bit_range *out)
7538{
7539 VALUE beg_v, end_v;
7540 int excl;
7541
7542 /*
7543 * We don't use rb_range_beg_len: it counts negative endpoints from the end,
7544 * which is an IndexError for bit positions, and it is limited to long instead
7545 * of uint64_t.
7546 */
7547 rb_range_values(range, &beg_v, &end_v, &excl);
7548
7549 out->beg = NIL_P(beg_v) ? 0 : str_bit_offset_from_index(beg_v).value;
7550 if (NIL_P(end_v)) {
7551 out->end_open = true;
7552 out->end_exclusive = 0;
7553 }
7554 else {
7555 uint64_t end = str_bit_offset_from_index(end_v).value;
7556 out->end_open = false;
7557 /*
7558 * The saturation loses one position only for an inclusive end of
7559 * 2**64-1, which lies beyond any real string either way.
7560 */
7561 out->end_exclusive = (excl || end == UINT64_MAX) ? end : end + 1;
7562 }
7563}
7564
7565/*
7566 * Turn a coerced Range into (beg, len) against the now-current total bit size.
7567 * The length is deliberately not clamped to the bits available, so a reading
7568 * caller can clamp while a writing caller detects the overrun and raises.
7569 */
7570static bool
7571str_bit_range_resolve(const struct str_bit_range *range, uint64_t total_bits, uint64_t *begp, uint64_t *lenp)
7572{
7573 uint64_t beg = range->beg;
7574 if (beg > total_bits) return false;
7575
7576 uint64_t end_exclusive = range->end_open ? total_bits : range->end_exclusive;
7577 if (end_exclusive < beg) end_exclusive = beg;
7578
7579 *begp = beg;
7580 *lenp = end_exclusive - beg;
7581 return true;
7582}
7583
7584static bool
7585str_lsb_first_from_opts(VALUE opts)
7586{
7587 static ID keywords[1];
7588 VALUE vlsb_first;
7589
7590 if (!keywords[0]) {
7591 keywords[0] = rb_intern_const("lsb_first");
7592 }
7593
7594 rb_get_kwargs(opts, keywords, 0, 1, &vlsb_first);
7595 if (vlsb_first == Qundef || vlsb_first == Qtrue) {
7596 return true;
7597 }
7598 if (vlsb_first == Qfalse) {
7599 return false;
7600 }
7601 rb_raise(rb_eArgError, "lsb_first must be true or false");
7602 UNREACHABLE_RETURN(false);
7603}
7604
7605static bool
7606str_lsb_first(int argc, VALUE *argv, VALUE *index)
7607{
7608 VALUE opts;
7609
7610 rb_scan_args(argc, argv, "1:", index, &opts);
7611 return str_lsb_first_from_opts(opts);
7612}
7613
7614static inline uint64_t
7615str_logical_to_physical_bit64(uint64_t logical, bool lsb_first)
7616{
7617 return lsb_first ? logical : ((logical & ~(uint64_t)7) | (7 - (logical & 7)));
7618}
7619
7620static inline rb_len_t
7621str_logical_to_physical_bit(rb_len_t logical, bool lsb_first)
7622{
7623 return lsb_first ? logical : ((logical & ~7L) | (7 - (logical & 7L)));
7624}
7625
7627 rb_len_t byte_index;
7628 unsigned int bit_offset;
7629};
7630
7631static inline struct str_bit_location
7632str_bit_location_from_offset(uint64_t logical, bool lsb_first)
7633{
7634 /*
7635 * When rb_len_t is 32-bit, a bit offset for a large string can be a Bignum
7636 * while the byte index still fits in rb_len_t, which is RSTRING_LEN's type.
7637 */
7638 uint64_t physical = str_logical_to_physical_bit64(logical, lsb_first);
7639 struct str_bit_location location;
7640 location.byte_index = (rb_len_t)(physical / CHAR_BIT);
7641 location.bit_offset = (unsigned int)(physical % CHAR_BIT);
7642 return location;
7643}
7644
7645static inline int
7646str_get_bit(const char *ptr, rb_len_t bit_index)
7647{
7648 return (((unsigned char)ptr[bit_index / CHAR_BIT]) >> (bit_index % CHAR_BIT)) & 1;
7649}
7650
7651static inline int
7652str_get_bit_location(const char *ptr, struct str_bit_location location)
7653{
7654 return (((unsigned char)ptr[location.byte_index]) >> location.bit_offset) & 1;
7655}
7656
7657static int
7658str_bit_get(int argc, VALUE *argv, VALUE str)
7659{
7660 VALUE index;
7661 bool lsb_first = str_lsb_first(argc, argv, &index);
7662 struct str_bit_offset offset = str_bit_offset_from_index(index);
7663
7664 if (str_bit_offset_out_of_range(RSTRING_LEN(str), offset.value)) {
7665 return -1;
7666 }
7667
7668 if (offset.fits_index) {
7669 return str_get_bit(RSTRING_PTR(str), str_logical_to_physical_bit(offset.index_value, lsb_first));
7670 }
7671 else {
7672 return str_get_bit_location(RSTRING_PTR(str), str_bit_location_from_offset(offset.value, lsb_first));
7673 }
7674}
7675
7676/*
7677 * call-seq:
7678 * bit_get(offset, lsb_first: true) -> 0, 1, or nil
7679 *
7680 * Returns +0+ or +1+ for the bit at zero-based bit +offset+:
7681 *
7682 * s = "\xAA" # 0b10101010
7683 * s.bit_get(0) # => 0
7684 * s.bit_get(1) # => 1
7685 *
7686 * Returns +nil+ if +offset+ is beyond the end of +self+:
7687 *
7688 * s.bit_get(8) # => nil
7689 *
7690 * By default, bits within each byte are numbered from least-significant to
7691 * most-significant. If +lsb_first+ is +false+, byte order is unchanged but bits
7692 * within each byte are numbered from most-significant to least-significant:
7693 *
7694 * s.bit_get(0, lsb_first: false) # => 1
7695 * s.bit_get(1, lsb_first: false) # => 0
7696 *
7697 * Raises +IndexError+ if +offset+ is negative.
7698 * Raises +ArgumentError+ if +offset+ is too large to be represented.
7699 * Raises +ArgumentError+ if +lsb_first+ is neither +true+ nor +false+.
7700 *
7701 */
7702static VALUE
7703rb_str_bit_get(int argc, VALUE *argv, VALUE str)
7704{
7705 int bit = str_bit_get(argc, argv, str);
7706 return bit < 0 ? Qnil : INT2FIX(bit);
7707}
7708
7709/*
7710 * call-seq:
7711 * bit_set?(offset, lsb_first: true) -> true, false, or nil
7712 *
7713 * Returns +true+ or +false+ for whether the bit at zero-based bit +offset+ is set:
7714 *
7715 * s = "\xAA" # 0b10101010
7716 * s.bit_set?(0) # => false
7717 * s.bit_set?(1) # => true
7718 *
7719 * Returns +nil+ if +offset+ is beyond the end of +self+:
7720 *
7721 * s.bit_set?(8) # => nil
7722 *
7723 * By default, bits within each byte are numbered from least-significant to
7724 * most-significant. If +lsb_first+ is +false+, byte order is unchanged but bits
7725 * within each byte are numbered from most-significant to least-significant:
7726 *
7727 * s.bit_set?(0, lsb_first: false) # => true
7728 * s.bit_set?(1, lsb_first: false) # => false
7729 *
7730 * Raises +IndexError+ if +offset+ is negative.
7731 * Raises +ArgumentError+ if +offset+ is too large to be represented.
7732 * Raises +ArgumentError+ if +lsb_first+ is neither +true+ nor +false+.
7733 *
7734 */
7735static VALUE
7736rb_str_bit_set_p(int argc, VALUE *argv, VALUE str)
7737{
7738 int bit = str_bit_get(argc, argv, str);
7739 return bit < 0 ? Qnil : RBOOL(bit);
7740}
7741
7742enum str_bit_mutation {
7743 STR_BIT_SET,
7744 STR_BIT_CLEAR,
7745 STR_BIT_FLIP
7746};
7747
7748/*
7749 * Mask for the logical in-byte positions lo..hi (0 <= lo <= hi <= 7) of one
7750 * byte. A contiguous logical run stays contiguous within a byte under both
7751 * numbering conventions; MSB-first only mirrors it.
7752 */
7753static inline unsigned char
7754str_bit_region_byte_mask(unsigned int lo, unsigned int hi, bool lsb_first)
7755{
7756 if (lsb_first) {
7757 return (unsigned char)((0xFFu >> (7 - hi)) & (0xFFu << lo));
7758 }
7759 else {
7760 return (unsigned char)((0xFFu >> lo) & (0xFFu << (7 - hi)));
7761 }
7762}
7763
7764static inline void
7765str_apply_bit_mask(unsigned char *byte, unsigned char mask, enum str_bit_mutation mutation)
7766{
7767 switch (mutation) {
7768 case STR_BIT_SET:
7769 *byte |= mask;
7770 break;
7771 case STR_BIT_CLEAR:
7772 *byte &= (unsigned char)~mask;
7773 break;
7774 case STR_BIT_FLIP:
7775 *byte ^= mask;
7776 break;
7777 }
7778}
7779
7780/* The caller has bounds-checked [beg, beg+len) and called rb_str_modify. */
7781static void
7782str_mutate_bit_region(unsigned char *ptr, uint64_t beg, uint64_t len, bool lsb_first, enum str_bit_mutation mutation)
7783{
7784 uint64_t first_bit = beg;
7785 uint64_t last_bit = beg + len - 1;
7786 rb_len_t first_byte = (rb_len_t)(first_bit / CHAR_BIT);
7787 rb_len_t last_byte = (rb_len_t)(last_bit / CHAR_BIT);
7788 unsigned int first_off = (unsigned int)(first_bit % CHAR_BIT);
7789 unsigned int last_off = (unsigned int)(last_bit % CHAR_BIT);
7790
7791 if (first_byte == last_byte) {
7792 str_apply_bit_mask(ptr + first_byte, str_bit_region_byte_mask(first_off, last_off, lsb_first), mutation);
7793 return;
7794 }
7795
7796 str_apply_bit_mask(ptr + first_byte, str_bit_region_byte_mask(first_off, 7, lsb_first), mutation);
7797 rb_len_t middle_len = last_byte - first_byte - 1;
7798 if (middle_len > 0) {
7799 unsigned char *middle = ptr + first_byte + 1;
7800 switch (mutation) {
7801 case STR_BIT_SET:
7802 memset(middle, 0xFF, middle_len);
7803 break;
7804 case STR_BIT_CLEAR:
7805 memset(middle, 0, middle_len);
7806 break;
7807 case STR_BIT_FLIP:
7808 /*
7809 * Byte loop on purpose: the compiler auto-vectorizes it (verified on gcc 13.3
7810 * and clang 18.1 with x86_64), and being read-modify-write, the flip is memory-bound,
7811 * so a manual word-at-a-time XOR loop was measured to be no faster.
7812 */
7813 for (rb_len_t i = 0; i < middle_len; i++) {
7814 middle[i] ^= 0xFF;
7815 }
7816 break;
7817 }
7818 }
7819 str_apply_bit_mask(ptr + last_byte, str_bit_region_byte_mask(0, last_off, lsb_first), mutation);
7820}
7821
7822static VALUE
7823str_mutate_single_bit(VALUE str, VALUE index, bool lsb_first, enum str_bit_mutation mutation)
7824{
7825 struct str_bit_offset offset = str_bit_offset_from_index(index);
7826 struct str_bit_location location;
7827 rb_len_t bit_index;
7828 unsigned char *ptr;
7829 unsigned char mask;
7830
7831 rb_check_frozen(str);
7832
7833 if (str_bit_offset_out_of_range(RSTRING_LEN(str), offset.value)) {
7834 rb_raise(rb_eIndexError, "bit index out of range");
7835 }
7836
7837 rb_str_modify(str);
7838 ptr = (unsigned char *)RSTRING_PTR(str);
7839 if (offset.fits_index) {
7840 bit_index = str_logical_to_physical_bit(offset.index_value, lsb_first);
7841 mask = (unsigned char)(1u << (bit_index % CHAR_BIT));
7842 location.byte_index = bit_index / CHAR_BIT;
7843 }
7844 else {
7845 location = str_bit_location_from_offset(offset.value, lsb_first);
7846 mask = (unsigned char)(1u << location.bit_offset);
7847 }
7848
7849 str_apply_bit_mask(ptr + location.byte_index, mask, mutation);
7850 return str;
7851}
7852
7853static VALUE
7854str_mutate_bit(int argc, VALUE *argv, VALUE str, enum str_bit_mutation mutation)
7855{
7856 VALUE target, length_v, opts;
7857 uint64_t beg = 0, len = 0;
7858
7859 /* Count positional arguments so that an explicit nil is not mistaken for an omitted one. */
7860 int nargs = rb_scan_args(argc, argv, "11:", &target, &length_v, &opts);
7861 bool lsb_first = str_lsb_first_from_opts(opts);
7862
7863 bool is_range = rb_obj_is_kind_of(target, rb_cRange);
7864 if (nargs == 1 && !is_range) {
7865 return str_mutate_single_bit(str, target, lsb_first, mutation);
7866 }
7867
7868 struct str_bit_range range = {0};
7869 struct str_bit_offset offset;
7870 if (is_range) {
7871 if (nargs == 2) {
7872 rb_raise(rb_eArgError, "bit length not allowed with a Range");
7873 }
7874 str_bit_range_to_offsets(target, &range);
7875 }
7876 else {
7877 offset = str_bit_offset_from_index(target);
7878 len = str_bit_length_from_index(length_v);
7879 }
7880
7881 /* Even a zero-length write requires a mutable receiver. */
7882 rb_check_frozen(str);
7883
7884 /*
7885 * A region that begins past the end is out of range even when it is
7886 * empty, and one that runs past the end is not allowed to silently
7887 * shrink: both are errors for a mutation, unlike the clamping reads.
7888 * An empty region whose start is within 0..bitsize writes nothing.
7889 */
7890 uint64_t total_bits = str_bit_size(RSTRING_LEN(str));
7891 if (is_range) {
7892 if (!str_bit_range_resolve(&range, total_bits, &beg, &len) || len > total_bits - beg) {
7893 rb_raise(rb_eIndexError, "bit range out of range");
7894 }
7895 }
7896 else {
7897 beg = offset.value;
7898 if (beg > total_bits || len > total_bits - beg) {
7899 rb_raise(rb_eIndexError, "bit range out of range");
7900 }
7901 }
7902
7903 if (len == 0) return str;
7904
7905 rb_str_modify(str);
7906 str_mutate_bit_region((unsigned char *)RSTRING_PTR(str), beg, len, lsb_first, mutation);
7907 return str;
7908}
7909
7910/*
7911 * call-seq:
7912 * bit_set(offset, lsb_first: true) -> self
7913 * bit_set(offset, length, lsb_first: true) -> self
7914 * bit_set(range, lsb_first: true) -> self
7915 *
7916 * Sets the bit at zero-based bit +offset+ to 1; returns +self+:
7917 *
7918 * s = "\x00"
7919 * s.bit_set(1) # => "\x02"
7920 * s # => "\x02"
7921 *
7922 * With +length+, sets the +length+ consecutive bits beginning at +offset+;
7923 * with a Range, sets the bits covered by +range+:
7924 *
7925 * s = "\x00\x00"
7926 * s.bit_set(4, 8) # => "\xF0\x0F"
7927 * s = "\x00\x00"
7928 * s.bit_set(4..11) # => "\xF0\x0F"
7929 *
7930 * The entire region must lie within +self+; a region that extends beyond the
7931 * end raises +IndexError+ rather than being clamped. Writing zero bits is a
7932 * no-op, but even an empty region raises +IndexError+ when it begins beyond
7933 * the end of +self+.
7934 *
7935 * By default, bits within each byte are numbered from least-significant to
7936 * most-significant. If +lsb_first+ is +false+, byte order is unchanged but bits
7937 * within each byte are numbered from most-significant to least-significant:
7938 *
7939 * s = "\x00"
7940 * s.bit_set(1, lsb_first: false) # => "\x40"
7941 * s = "\x00\x00"
7942 * s.bit_set(4, 8, lsb_first: false) # => "\x0F\xF0"
7943 *
7944 * Raises +IndexError+ if +offset+ is out of range, or if any part of the
7945 * region lies outside +self+.
7946 * Raises +ArgumentError+ if +length+ is negative.
7947 * Raises +ArgumentError+ if a bit position is too large to be represented.
7948 * Raises +ArgumentError+ if +lsb_first+ is neither +true+ nor +false+.
7949 * Raises +FrozenError+ if +self+ is frozen, even when writing zero bits.
7950 *
7951 */
7952static VALUE
7953rb_str_bit_set(int argc, VALUE *argv, VALUE str)
7954{
7955 return str_mutate_bit(argc, argv, str, STR_BIT_SET);
7956}
7957
7958/*
7959 * call-seq:
7960 * bit_clear(offset, lsb_first: true) -> self
7961 * bit_clear(offset, length, lsb_first: true) -> self
7962 * bit_clear(range, lsb_first: true) -> self
7963 *
7964 * Sets the bit at zero-based bit +offset+ to 0; returns +self+:
7965 *
7966 * s = "\xFF"
7967 * s.bit_clear(1) # => "\xFD"
7968 * s # => "\xFD"
7969 *
7970 * With +length+, clears the +length+ consecutive bits beginning at +offset+;
7971 * with a Range, clears the bits covered by +range+:
7972 *
7973 * s = "\xFF\xFF"
7974 * s.bit_clear(4, 8) # => "\x0F\xF0"
7975 * s = "\xFF\xFF"
7976 * s.bit_clear(4..11) # => "\x0F\xF0"
7977 *
7978 * The entire region must lie within +self+; a region that extends beyond the
7979 * end raises +IndexError+ rather than being clamped. Writing zero bits is a
7980 * no-op, but even an empty region raises +IndexError+ when it begins beyond
7981 * the end of +self+.
7982 *
7983 * By default, bits within each byte are numbered from least-significant to
7984 * most-significant. If +lsb_first+ is +false+, byte order is unchanged but bits
7985 * within each byte are numbered from most-significant to least-significant:
7986 *
7987 * s = "\xFF"
7988 * s.bit_clear(1, lsb_first: false) # => "\xBF"
7989 * s = "\xFF\xFF"
7990 * s.bit_clear(4, 8, lsb_first: false) # => "\xF0\x0F"
7991 *
7992 * Raises +IndexError+ if +offset+ is out of range, or if any part of the
7993 * region lies outside +self+.
7994 * Raises +ArgumentError+ if +length+ is negative.
7995 * Raises +ArgumentError+ if a bit position is too large to be represented.
7996 * Raises +ArgumentError+ if +lsb_first+ is neither +true+ nor +false+.
7997 * Raises +FrozenError+ if +self+ is frozen, even when writing zero bits.
7998 *
7999 */
8000static VALUE
8001rb_str_bit_clear(int argc, VALUE *argv, VALUE str)
8002{
8003 return str_mutate_bit(argc, argv, str, STR_BIT_CLEAR);
8004}
8005
8006/*
8007 * call-seq:
8008 * bit_flip(offset, lsb_first: true) -> self
8009 * bit_flip(offset, length, lsb_first: true) -> self
8010 * bit_flip(range, lsb_first: true) -> self
8011 *
8012 * Flips the bit at zero-based bit +offset+; returns +self+:
8013 *
8014 * s = "\x00"
8015 * s.bit_flip(1) # => "\x02"
8016 * s.bit_flip(1) # => "\x00"
8017 *
8018 * With +length+, flips the +length+ consecutive bits beginning at +offset+;
8019 * with a Range, flips the bits covered by +range+:
8020 *
8021 * s = "\x00\xFF"
8022 * s.bit_flip(4, 8) # => "\xF0\xF0"
8023 * s = "\x00\xFF"
8024 * s.bit_flip(4..11) # => "\xF0\xF0"
8025 *
8026 * The entire region must lie within +self+; a region that extends beyond the
8027 * end raises +IndexError+ rather than being clamped. Writing zero bits is a
8028 * no-op, but even an empty region raises +IndexError+ when it begins beyond
8029 * the end of +self+.
8030 *
8031 * By default, bits within each byte are numbered from least-significant to
8032 * most-significant. If +lsb_first+ is +false+, byte order is unchanged but bits
8033 * within each byte are numbered from most-significant to least-significant:
8034 *
8035 * s = "\x00"
8036 * s.bit_flip(1, lsb_first: false) # => "\x40"
8037 * s = "\x00\xFF"
8038 * s.bit_flip(4, 8, lsb_first: false) # => "\x0F\x0F"
8039 *
8040 * Raises +IndexError+ if +offset+ is out of range, or if any part of the
8041 * region lies outside +self+.
8042 * Raises +ArgumentError+ if +length+ is negative.
8043 * Raises +ArgumentError+ if a bit position is too large to be represented.
8044 * Raises +ArgumentError+ if +lsb_first+ is neither +true+ nor +false+.
8045 * Raises +FrozenError+ if +self+ is frozen, even when writing zero bits.
8046 *
8047 */
8048static VALUE
8049rb_str_bit_flip(int argc, VALUE *argv, VALUE str)
8050{
8051 return str_mutate_bit(argc, argv, str, STR_BIT_FLIP);
8052}
8053
8054static uint64_t
8055str_count_bits(const unsigned char *ptr, rb_len_t len)
8056{
8057 uint64_t count = 0;
8058 rb_len_t off = 0;
8059 rb_len_t unrolled_end = len & ~31L;
8060 rb_len_t aligned_end = len & ~7L;
8061
8062 // 32 bytes (256 bits) at a time
8063 for (; off < unrolled_end; off += 32) {
8064 uint64_t w0, w1, w2, w3;
8065 memcpy(&w0, ptr + off, 8);
8066 memcpy(&w1, ptr + off + 8, 8);
8067 memcpy(&w2, ptr + off + 16, 8);
8068 memcpy(&w3, ptr + off + 24, 8);
8069 count += rb_popcount64(w0);
8070 count += rb_popcount64(w1);
8071 count += rb_popcount64(w2);
8072 count += rb_popcount64(w3);
8073 }
8074
8075 // 8 bytes (64 bits) at a time
8076 for (; off < aligned_end; off += 8) {
8077 uint64_t word;
8078 memcpy(&word, ptr + off, 8);
8079 count += rb_popcount64(word);
8080 }
8081
8082 // remaining bytes
8083 if (off < len) {
8084 uint64_t word = 0;
8085 int shift = 0;
8086 for (; off < len; off++, shift += CHAR_BIT) {
8087 word |= (uint64_t)ptr[off] << shift;
8088 }
8089 count += rb_popcount64(word);
8090 }
8091
8092 return count;
8093}
8094
8095static uint64_t
8096str_count_bits_region(const unsigned char *ptr, uint64_t beg, uint64_t len, bool lsb_first)
8097{
8098 uint64_t first_bit = beg;
8099 uint64_t last_bit = beg + len - 1;
8100 rb_len_t first_byte = (rb_len_t)(first_bit / CHAR_BIT);
8101 rb_len_t last_byte = (rb_len_t)(last_bit / CHAR_BIT);
8102 unsigned int first_off = (unsigned int)(first_bit % CHAR_BIT);
8103 unsigned int last_off = (unsigned int)(last_bit % CHAR_BIT);
8104
8105 if (first_byte == last_byte) {
8106 return rb_popcount32((uint32_t)(ptr[first_byte] & str_bit_region_byte_mask(first_off, last_off, lsb_first)));
8107 }
8108
8109 uint64_t count = rb_popcount32((uint32_t)(ptr[first_byte] & str_bit_region_byte_mask(first_off, 7, lsb_first)));
8110 count += str_count_bits(ptr + first_byte + 1, last_byte - first_byte - 1);
8111 count += rb_popcount32((uint32_t)(ptr[last_byte] & str_bit_region_byte_mask(0, last_off, lsb_first)));
8112 return count;
8113}
8114
8115/*
8116 * call-seq:
8117 * bit_count -> integer
8118 * bit_count(offset, length, lsb_first: true) -> integer
8119 * bit_count(range, lsb_first: true) -> integer
8120 *
8121 * Returns the number of set bits in +self+.
8122 *
8123 * With no arguments, counts over all bytes of +self+:
8124 *
8125 * "\x00".bit_count # => 0
8126 * "\xFF".bit_count # => 8
8127 * "\xAA".bit_count # => 4
8128 *
8129 * With +offset+ and +length+, counts only the +length+ bits beginning at
8130 * zero-based bit +offset+; with a Range, counts the bits covered by +range+:
8131 *
8132 * data = "\xFF\x00\xF0"
8133 * data.bit_count(0, 8) # => 8
8134 * data.bit_count(8, 8) # => 0
8135 * data.bit_count(0..7) # => 8
8136 * data.bit_count(8...16) # => 0
8137 * data.bit_count(16..) # => 4
8138 *
8139 * A region that extends beyond the end of +self+ is clamped to the bits that
8140 * exist; a region that begins at or beyond the end counts zero:
8141 *
8142 * data.bit_count(16, 100) # => 4
8143 * data.bit_count(100, 8) # => 0
8144 *
8145 * By default, bits within each byte are numbered from least-significant to
8146 * most-significant. If +lsb_first+ is +false+, byte order is unchanged but bits
8147 * within each byte are numbered from most-significant to least-significant.
8148 * The numbering matters only when the region is not byte-aligned; the
8149 * no-argument form is independent of it:
8150 *
8151 * "\xF0".bit_count(0, 4) # => 0
8152 * "\xF0".bit_count(0, 4, lsb_first: false) # => 4
8153 *
8154 * The count is over the bytes of +self+ and is independent of string encoding.
8155 * Raises +IndexError+ if +offset+ or a Range endpoint is negative.
8156 * Raises +ArgumentError+ if +length+ is negative.
8157 * Raises +ArgumentError+ if a bit position is too large to be represented.
8158 * Raises +ArgumentError+ if +lsb_first+ is neither +true+ nor +false+.
8159 *
8160 */
8161static VALUE
8162rb_str_bit_count(int argc, VALUE *argv, VALUE str)
8163{
8164 VALUE v0, v1, opts;
8165 uint64_t beg = 0, len = 0;
8166
8167 /* Count positional arguments so that an explicit nil is not mistaken for an omitted one. */
8168 int nargs = rb_scan_args(argc, argv, "02:", &v0, &v1, &opts);
8169 /*
8170 * A whole-string popcount is independent of bit numbering.
8171 * no-(offset|range)-argument form only validates lsb_first.
8172 */
8173 bool lsb_first = str_lsb_first_from_opts(opts);
8174
8175 if (nargs == 0) {
8176 return ULL2NUM(str_count_bits((const unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str)));
8177 }
8178
8179 bool is_range = rb_obj_is_kind_of(v0, rb_cRange);
8180 struct str_bit_range range = {0};
8181 if (is_range) {
8182 if (nargs == 2) {
8183 rb_raise(rb_eArgError, "bit length not allowed with a Range");
8184 }
8185 str_bit_range_to_offsets(v0, &range);
8186 }
8187 else if (nargs == 1) {
8188 rb_raise(rb_eArgError, "no bit length given");
8189 }
8190 else {
8191 beg = str_bit_offset_from_index(v0).value;
8192 len = str_bit_length_from_index(v1);
8193 }
8194
8195 const unsigned char *ptr = (const unsigned char *)RSTRING_PTR(str);
8196 uint64_t total_bits = str_bit_size(RSTRING_LEN(str));
8197 if (is_range) {
8198 if (!str_bit_range_resolve(&range, total_bits, &beg, &len)) {
8199 return INT2FIX(0);
8200 }
8201 }
8202 else if (beg >= total_bits) {
8203 return INT2FIX(0);
8204 }
8205
8206 /* Reads clamp: only the part of the region that exists is counted. */
8207 if (len > total_bits - beg) len = total_bits - beg;
8208 if (len == 0) return INT2FIX(0);
8209 return ULL2NUM(str_count_bits_region(ptr, beg, len, lsb_first));
8210}
8211
8212static void
8213str_check_bitwise_length(VALUE str, VALUE other)
8214{
8215 if (RSTRING_LEN(str) != RSTRING_LEN(other)) {
8216 rb_raise(rb_eArgError, "operands must have the same length (%"PRIdLEN" vs %"PRIdLEN")",
8217 RSTRING_LEN(str), RSTRING_LEN(other));
8218 }
8219}
8220
8221static VALUE
8222str_bitwise_result(VALUE str)
8223{
8224 rb_len_t len = RSTRING_LEN(str);
8225 VALUE result = rb_str_buf_new(len);
8226 rb_str_resize(result, len);
8227 rb_enc_associate(result, rb_ascii8bit_encoding());
8228 ENC_CODERANGE_CLEAR(result);
8229 return result;
8230}
8231
8232#define STR_DEFINE_UNARY_BITWISE_KERNEL(name, expr_word, expr_byte) \
8233 static void \
8234 name(unsigned char *dst, const unsigned char *src, rb_len_t len) \
8235 { \
8236 rb_len_t off = 0; \
8237 rb_len_t unrolled_end = len & ~31L; \
8238 rb_len_t aligned_end = len & ~7L; \
8239 for (; off < unrolled_end; off += 32) { \
8240 uint64_t s0, s1, s2, s3; \
8241 memcpy(&s0, src + off, 8); \
8242 memcpy(&s1, src + off + 8, 8); \
8243 memcpy(&s2, src + off + 16, 8); \
8244 memcpy(&s3, src + off + 24, 8); \
8245 s0 = (expr_word(s0)); \
8246 s1 = (expr_word(s1)); \
8247 s2 = (expr_word(s2)); \
8248 s3 = (expr_word(s3)); \
8249 memcpy(dst + off, &s0, 8); \
8250 memcpy(dst + off + 8, &s1, 8); \
8251 memcpy(dst + off + 16, &s2, 8); \
8252 memcpy(dst + off + 24, &s3, 8); \
8253 } \
8254 for (; off < aligned_end; off += 8) { \
8255 uint64_t word; \
8256 memcpy(&word, src + off, 8); \
8257 word = (expr_word(word)); \
8258 memcpy(dst + off, &word, 8); \
8259 } \
8260 for (; off < len; off++) dst[off] = (expr_byte(src[off])); \
8261 }
8262
8263#define STR_DEFINE_BINARY_BITWISE_KERNEL(name, expr_word, expr_byte) \
8264 static void \
8265 name(unsigned char *dst, const unsigned char *lhs, \
8266 const unsigned char *rhs, rb_len_t len) \
8267 { \
8268 rb_len_t off = 0; \
8269 rb_len_t unrolled_end = len & ~31L; \
8270 rb_len_t aligned_end = len & ~7L; \
8271 for (; off < unrolled_end; off += 32) { \
8272 uint64_t l0, l1, l2, l3, r0, r1, r2, r3; \
8273 memcpy(&l0, lhs + off, 8); memcpy(&r0, rhs + off, 8); \
8274 memcpy(&l1, lhs + off + 8, 8); memcpy(&r1, rhs + off + 8, 8); \
8275 memcpy(&l2, lhs + off + 16, 8); memcpy(&r2, rhs + off + 16, 8); \
8276 memcpy(&l3, lhs + off + 24, 8); memcpy(&r3, rhs + off + 24, 8); \
8277 l0 = expr_word(l0, r0); \
8278 l1 = expr_word(l1, r1); \
8279 l2 = expr_word(l2, r2); \
8280 l3 = expr_word(l3, r3); \
8281 memcpy(dst + off, &l0, 8); \
8282 memcpy(dst + off + 8, &l1, 8); \
8283 memcpy(dst + off + 16, &l2, 8); \
8284 memcpy(dst + off + 24, &l3, 8); \
8285 } \
8286 for (; off < aligned_end; off += 8) { \
8287 uint64_t lhs_word, rhs_word; \
8288 memcpy(&lhs_word, lhs + off, 8); \
8289 memcpy(&rhs_word, rhs + off, 8); \
8290 lhs_word = expr_word(lhs_word, rhs_word); \
8291 memcpy(dst + off, &lhs_word, 8); \
8292 } \
8293 for (; off < len; off++) dst[off] = expr_byte(lhs[off], rhs[off]); \
8294 }
8295
8296#define STR_BITWISE_NOT_WORD(x) (~(x))
8297#define STR_BITWISE_NOT_BYTE(x) ((unsigned char)~(x))
8298#define STR_BITWISE_AND_WORD(x, y) ((x) & (y))
8299#define STR_BITWISE_AND_BYTE(x, y) ((unsigned char)((x) & (y)))
8300#define STR_BITWISE_OR_WORD(x, y) ((x) | (y))
8301#define STR_BITWISE_OR_BYTE(x, y) ((unsigned char)((x) | (y)))
8302#define STR_BITWISE_XOR_WORD(x, y) ((x) ^ (y))
8303#define STR_BITWISE_XOR_BYTE(x, y) ((unsigned char)((x) ^ (y)))
8304
8305STR_DEFINE_UNARY_BITWISE_KERNEL(str_bitwise_not, STR_BITWISE_NOT_WORD, STR_BITWISE_NOT_BYTE)
8306STR_DEFINE_BINARY_BITWISE_KERNEL(str_bitwise_and, STR_BITWISE_AND_WORD, STR_BITWISE_AND_BYTE)
8307STR_DEFINE_BINARY_BITWISE_KERNEL(str_bitwise_or, STR_BITWISE_OR_WORD, STR_BITWISE_OR_BYTE)
8308STR_DEFINE_BINARY_BITWISE_KERNEL(str_bitwise_xor, STR_BITWISE_XOR_WORD, STR_BITWISE_XOR_BYTE)
8309
8310/*
8311 * call-seq:
8312 * bitwise_not -> string
8313 *
8314 * Returns a new string whose bytes are the bitwise complement of +self+:
8315 *
8316 * "\x00\xAA".bitwise_not # => "\xFF\x55"
8317 *
8318 * The returned string has BINARY encoding.
8319 *
8320 */
8321static VALUE
8322rb_str_bitwise_not(VALUE str)
8323{
8324 rb_len_t len = RSTRING_LEN(str);
8325 VALUE result = str_bitwise_result(str);
8326 str_bitwise_not((unsigned char *)RSTRING_PTR(result),
8327 (const unsigned char *)RSTRING_PTR(str), len);
8328 return result;
8329}
8330
8331/*
8332 * call-seq:
8333 * bitwise_not! -> self
8334 *
8335 * Replaces each byte in +self+ with its bitwise complement; returns +self+:
8336 *
8337 * s = "\x00\xAA"
8338 * s.bitwise_not! # => "\xFF\x55"
8339 * s # => "\xFF\x55"
8340 *
8341 * The encoding of +self+ is not changed.
8342 *
8343 */
8344static VALUE
8345rb_str_bitwise_not_bang(VALUE str)
8346{
8347 rb_len_t len;
8348 unsigned char *ptr;
8349
8350 rb_str_modify(str);
8351 len = RSTRING_LEN(str);
8352 ptr = (unsigned char *)RSTRING_PTR(str);
8353 str_bitwise_not(ptr, ptr, len);
8354 return str;
8355}
8356
8357/*
8358 * Document-method: String#bitwise_and
8359 *
8360 * call-seq:
8361 * bitwise_and -> new_string
8362 *
8363 * Returns a new string whose bytes are the bitwise AND of +self+ and +other+:
8364 *
8365 * "\xF0".bitwise_and("\xCC") # => "\xC0"
8366 *
8367 * +other+ is converted to a string using +to_str+.
8368 * Raises +ArgumentError+ if the two strings have different byte sizes.
8369 * The returned string has BINARY encoding.
8370 *
8371 */
8372
8373/*
8374 * Document-method: String#bitwise_and!
8375 *
8376 * call-seq:
8377 * bitwise_and! -> self
8378 *
8379 * Replaces each byte in +self+ with the bitwise AND of that byte and the
8380 * corresponding byte in +other+; returns +self+:
8381 *
8382 * s = "\xF0"
8383 * s.bitwise_and!("\xCC") # => "\xC0"
8384 * s # => "\xC0"
8385 *
8386 * +other+ is converted to a string using +to_str+.
8387 * Raises +ArgumentError+ if the two strings have different byte sizes.
8388 * The encoding of +self+ is not changed.
8389 *
8390 */
8391
8392/*
8393 * Document-method: String#bitwise_or
8394 *
8395 * call-seq:
8396 * bitwise_or -> new_string
8397 *
8398 * Returns a new string whose bytes are the bitwise OR of +self+ and +other+:
8399 *
8400 * "\xF0".bitwise_or("\x0C") # => "\xFC"
8401 *
8402 * +other+ is converted to a string using +to_str+.
8403 * Raises +ArgumentError+ if the two strings have different byte sizes.
8404 * The returned string has BINARY encoding.
8405 *
8406 */
8407
8408/*
8409 * Document-method: String#bitwise_or!
8410 *
8411 * call-seq:
8412 * bitwise_or! -> self
8413 *
8414 * Replaces each byte in +self+ with the bitwise OR of that byte and the
8415 * corresponding byte in +other+; returns +self+:
8416 *
8417 * s = "\xF0"
8418 * s.bitwise_or!("\x0C") # => "\xFC"
8419 * s # => "\xFC"
8420 *
8421 * +other+ is converted to a string using +to_str+.
8422 * Raises +ArgumentError+ if the two strings have different byte sizes.
8423 * The encoding of +self+ is not changed.
8424 *
8425 */
8426
8427/*
8428 * Document-method: String#bitwise_xor
8429 *
8430 * call-seq:
8431 * bitwise_xor -> new_string
8432 *
8433 * Returns a new string whose bytes are the bitwise XOR of +self+ and +other+:
8434 *
8435 * "\xF0".bitwise_xor("\xCC") # => "\x3C"
8436 *
8437 * +other+ is converted to a string using +to_str+.
8438 * Raises +ArgumentError+ if the two strings have different byte sizes.
8439 * The returned string has BINARY encoding.
8440 *
8441 */
8442
8443/*
8444 * Document-method: String#bitwise_xor!
8445 *
8446 * call-seq:
8447 * bitwise_xor! -> self
8448 *
8449 * Replaces each byte in +self+ with the bitwise XOR of that byte and the
8450 * corresponding byte in +other+; returns +self+:
8451 *
8452 * s = "\xF0"
8453 * s.bitwise_xor!("\xCC") # => "\x3C"
8454 * s # => "\x3C"
8455 *
8456 * +other+ is converted to a string using +to_str+.
8457 * Raises +ArgumentError+ if the two strings have different byte sizes.
8458 * The encoding of +self+ is not changed.
8459 *
8460 */
8461
8462#define STR_DEFINE_BINARY_BITWISE_METHOD(name) \
8463 static VALUE \
8464 rb_str_bitwise_##name(VALUE str, VALUE other) \
8465 { \
8466 rb_len_t len; \
8467 VALUE result; \
8468 StringValue(other); \
8469 str_check_bitwise_length(str, other); \
8470 len = RSTRING_LEN(str); \
8471 result = str_bitwise_result(str); \
8472 str_bitwise_##name((unsigned char *)RSTRING_PTR(result), \
8473 (const unsigned char *)RSTRING_PTR(str), \
8474 (const unsigned char *)RSTRING_PTR(other), len); \
8475 return result; \
8476 } \
8477 static VALUE \
8478 rb_str_bitwise_##name##_bang(VALUE str, VALUE other) \
8479 { \
8480 rb_len_t len; \
8481 unsigned char *ptr; \
8482 StringValue(other); \
8483 str_check_bitwise_length(str, other); \
8484 rb_str_modify(str); \
8485 len = RSTRING_LEN(str); \
8486 ptr = (unsigned char *)RSTRING_PTR(str); \
8487 str_bitwise_##name(ptr, ptr, \
8488 (const unsigned char *)RSTRING_PTR(other), len); \
8489 return str; \
8490 }
8491
8492STR_DEFINE_BINARY_BITWISE_METHOD(and)
8493STR_DEFINE_BINARY_BITWISE_METHOD(or)
8494STR_DEFINE_BINARY_BITWISE_METHOD(xor)
8495
8496static VALUE
8497str_byte_substr(VALUE str, rb_len_t beg, rb_len_t len, int empty)
8498{
8499 rb_len_t n = RSTRING_LEN(str);
8500
8501 if (beg > n || len < 0) return Qnil;
8502 if (beg < 0) {
8503 beg += n;
8504 if (beg < 0) return Qnil;
8505 }
8506 if (len > n - beg)
8507 len = n - beg;
8508 if (len <= 0) {
8509 if (!empty) return Qnil;
8510 len = 0;
8511 }
8512
8513 VALUE str2 = str_subseq(str, beg, len);
8514
8515 str_enc_copy_direct(str2, str);
8516
8517 if (RSTRING_LEN(str2) == 0) {
8518 if (!rb_enc_asciicompat(STR_ENC_GET(str)))
8520 else
8522 }
8523 else {
8524 switch (ENC_CODERANGE(str)) {
8525 case ENC_CODERANGE_7BIT:
8527 break;
8528 default:
8530 break;
8531 }
8532 }
8533
8534 return str2;
8535}
8536
8537VALUE
8538rb_str_byte_substr(VALUE str, VALUE beg, VALUE len)
8539{
8540 return str_byte_substr(str, NUM2LEN(beg), NUM2LEN(len), TRUE);
8541}
8542
8543static VALUE
8544str_byte_aref(VALUE str, VALUE indx)
8545{
8546 rb_len_t idx;
8547 if (FIXNUM_P(indx)) {
8548 idx = FIX2LONG(indx);
8549 }
8550 else {
8551 /* check if indx is Range */
8552 rb_len_t beg, len = RSTRING_LEN(str);
8553
8554 switch (rb_range_beg_len(indx, &beg, &len, len, 0)) {
8555 case Qfalse:
8556 break;
8557 case Qnil:
8558 return Qnil;
8559 default:
8560 return str_byte_substr(str, beg, len, TRUE);
8561 }
8562
8563 idx = NUM2LEN(indx);
8564 }
8565 return str_byte_substr(str, idx, 1, FALSE);
8566}
8567
8568/*
8569 * call-seq:
8570 * byteslice(offset, length = 1) -> string or nil
8571 * byteslice(range) -> string or nil
8572 *
8573 * Returns a substring of +self+, or +nil+ if the substring cannot be constructed.
8574 *
8575 * With integer arguments +offset+ and +length+ given,
8576 * returns the substring beginning at the given +offset+
8577 * and of the given +length+ (as available):
8578 *
8579 * s = '0123456789' # => "0123456789"
8580 * s.byteslice(2) # => "2"
8581 * s.byteslice(200) # => nil
8582 * s.byteslice(4, 3) # => "456"
8583 * s.byteslice(4, 30) # => "456789"
8584 *
8585 * Returns +nil+ if +length+ is negative or +offset+ falls outside of +self+:
8586 *
8587 * s.byteslice(4, -1) # => nil
8588 * s.byteslice(40, 2) # => nil
8589 *
8590 * Counts backwards from the end of +self+
8591 * if +offset+ is negative:
8592 *
8593 * s = '0123456789' # => "0123456789"
8594 * s.byteslice(-4) # => "6"
8595 * s.byteslice(-4, 3) # => "678"
8596 *
8597 * With Range argument +range+ given, returns
8598 * <tt>byteslice(range.begin, range.size)</tt>:
8599 *
8600 * s = '0123456789' # => "0123456789"
8601 * s.byteslice(4..6) # => "456"
8602 * s.byteslice(-6..-4) # => "456"
8603 * s.byteslice(5..2) # => "" # range.size is zero.
8604 * s.byteslice(40..42) # => nil
8605 *
8606 * The starting and ending offsets need not be on character boundaries:
8607 *
8608 * s = 'こんにちは'
8609 * s.byteslice(0, 3) # => "こ"
8610 * s.byteslice(1, 3) # => "\x81\x93\xE3"
8611 *
8612 * The encodings of +self+ and the returned substring
8613 * are always the same:
8614 *
8615 * s.encoding # => #<Encoding:UTF-8>
8616 * s.byteslice(0, 3).encoding # => #<Encoding:UTF-8>
8617 * s.byteslice(1, 3).encoding # => #<Encoding:UTF-8>
8618 *
8619 * But, depending on the character boundaries,
8620 * the encoding of the returned substring may not be valid:
8621 *
8622 * s.valid_encoding? # => true
8623 * s.byteslice(0, 3).valid_encoding? # => true
8624 * s.byteslice(1, 3).valid_encoding? # => false
8625 *
8626 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
8627 */
8628
8629static VALUE
8630rb_str_byteslice(int argc, VALUE *argv, VALUE str)
8631{
8632 if (argc == 2) {
8633 rb_len_t beg = NUM2LEN(argv[0]);
8634 rb_len_t len = NUM2LEN(argv[1]);
8635 return str_byte_substr(str, beg, len, TRUE);
8636 }
8637 rb_check_arity(argc, 1, 2);
8638 return str_byte_aref(str, argv[0]);
8639}
8640
8641static void
8642str_check_beg_len(VALUE str, rb_len_t *beg, rb_len_t *len)
8643{
8644 rb_len_t end, slen = RSTRING_LEN(str);
8645
8646 if (*len < 0) rb_raise(rb_eIndexError, "negative length %"PRIdLEN, *len);
8647 if ((slen < *beg) || ((*beg < 0) && (*beg + slen < 0))) {
8648 rb_raise(rb_eIndexError, "index %"PRIdLEN" out of string", *beg);
8649 }
8650 if (*beg < 0) {
8651 *beg += slen;
8652 }
8653 RUBY_ASSERT(*beg >= 0);
8654 RUBY_ASSERT(*beg <= slen);
8655
8656 if (*len > slen - *beg) {
8657 *len = slen - *beg;
8658 }
8659 end = *beg + *len;
8660 str_ensure_byte_pos(str, *beg);
8661 str_ensure_byte_pos(str, end);
8662}
8663
8664/*
8665 * call-seq:
8666 * bytesplice(offset, length, str) -> self
8667 * bytesplice(offset, length, str, str_offset, str_length) -> self
8668 * bytesplice(range, str) -> self
8669 * bytesplice(range, str, str_range) -> self
8670 *
8671 * Replaces <i>target bytes</i> in +self+ with <i>source bytes</i> from the given string +str+;
8672 * returns +self+.
8673 *
8674 * In the first form, arguments +offset+ and +length+ determine the target bytes,
8675 * and the source bytes are all of the given +str+:
8676 *
8677 * '0123456789'.bytesplice(0, 3, 'abc') # => "abc3456789"
8678 * '0123456789'.bytesplice(3, 3, 'abc') # => "012abc6789"
8679 * '0123456789'.bytesplice(0, 50, 'abc') # => "abc"
8680 * '0123456789'.bytesplice(50, 3, 'abc') # Raises IndexError.
8681 *
8682 * The counts of the target bytes and source source bytes may be different:
8683 *
8684 * '0123456789'.bytesplice(0, 6, 'abc') # => "abc6789" # Shorter source.
8685 * '0123456789'.bytesplice(0, 1, 'abc') # => "abc123456789" # Shorter target.
8686 *
8687 * And either count may be zero (i.e., specifying an empty string):
8688 *
8689 * '0123456789'.bytesplice(0, 3, '') # => "3456789" # Empty source.
8690 * '0123456789'.bytesplice(0, 0, 'abc') # => "abc0123456789" # Empty target.
8691 *
8692 * In the second form, just as in the first,
8693 * arguments +offset+ and +length+ determine the target bytes;
8694 * argument +str+ _contains_ the source bytes,
8695 * and the additional arguments +str_offset+ and +str_length+
8696 * determine the actual source bytes:
8697 *
8698 * '0123456789'.bytesplice(0, 3, 'abc', 0, 3) # => "abc3456789"
8699 * '0123456789'.bytesplice(0, 3, 'abc', 1, 1) # => "b3456789" # Shorter source.
8700 * '0123456789'.bytesplice(0, 1, 'abc', 0, 3) # => "abc123456789" # Shorter target.
8701 * '0123456789'.bytesplice(0, 3, 'abc', 1, 0) # => "3456789" # Empty source.
8702 * '0123456789'.bytesplice(0, 0, 'abc', 0, 3) # => "abc0123456789" # Empty target.
8703 *
8704 * In the third form, argument +range+ determines the target bytes
8705 * and the source bytes are all of the given +str+:
8706 *
8707 * '0123456789'.bytesplice(0..2, 'abc') # => "abc3456789"
8708 * '0123456789'.bytesplice(3..5, 'abc') # => "012abc6789"
8709 * '0123456789'.bytesplice(0..5, 'abc') # => "abc6789" # Shorter source.
8710 * '0123456789'.bytesplice(0..0, 'abc') # => "abc123456789" # Shorter target.
8711 * '0123456789'.bytesplice(0..2, '') # => "3456789" # Empty source.
8712 * '0123456789'.bytesplice(0...0, 'abc') # => "abc0123456789" # Empty target.
8713 *
8714 * In the fourth form, just as in the third,
8715 * argument +range+ determines the target bytes;
8716 * argument +str+ _contains_ the source bytes,
8717 * and the additional argument +str_range+
8718 * determines the actual source bytes:
8719 *
8720 * '0123456789'.bytesplice(0..2, 'abc', 0..2) # => "abc3456789"
8721 * '0123456789'.bytesplice(3..5, 'abc', 0..2) # => "012abc6789"
8722 * '0123456789'.bytesplice(0..2, 'abc', 0..1) # => "ab3456789" # Shorter source.
8723 * '0123456789'.bytesplice(0..1, 'abc', 0..2) # => "abc23456789" # Shorter target.
8724 * '0123456789'.bytesplice(0..2, 'abc', 0...0) # => "3456789" # Empty source.
8725 * '0123456789'.bytesplice(0...0, 'abc', 0..2) # => "abc0123456789" # Empty target.
8726 *
8727 * In any of the forms, the beginnings and endings of both source and target
8728 * must be on character boundaries.
8729 *
8730 * In these examples, +self+ has five 3-byte characters,
8731 * and so has character boundaries at offsets 0, 3, 6, 9, 12, and 15.
8732 *
8733 * 'こんにちは'.bytesplice(0, 3, 'abc') # => "abcんにちは"
8734 * 'こんにちは'.bytesplice(1, 3, 'abc') # Raises IndexError.
8735 * 'こんにちは'.bytesplice(0, 2, 'abc') # Raises IndexError.
8736 */
8737
8738static VALUE
8739rb_str_bytesplice(int argc, VALUE *argv, VALUE str)
8740{
8741 rb_len_t beg, len, vbeg, vlen;
8742 VALUE val;
8743 int cr;
8744
8745 rb_check_arity(argc, 2, 5);
8746 if (!(argc == 2 || argc == 3 || argc == 5)) {
8747 rb_raise(rb_eArgError, "wrong number of arguments (given %d, expected 2, 3, or 5)", argc);
8748 }
8749 if (argc == 2 || (argc == 3 && !RB_INTEGER_TYPE_P(argv[0]))) {
8750 if (!rb_range_beg_len(argv[0], &beg, &len, RSTRING_LEN(str), 2)) {
8751 rb_raise(rb_eTypeError, "wrong argument type %s (expected Range)",
8752 rb_builtin_class_name(argv[0]));
8753 }
8754 val = argv[1];
8755 StringValue(val);
8756 if (argc == 2) {
8757 /* bytesplice(range, str) */
8758 vbeg = 0;
8759 vlen = RSTRING_LEN(val);
8760 }
8761 else {
8762 /* bytesplice(range, str, str_range) */
8763 if (!rb_range_beg_len(argv[2], &vbeg, &vlen, RSTRING_LEN(val), 2)) {
8764 rb_raise(rb_eTypeError, "wrong argument type %s (expected Range)",
8765 rb_builtin_class_name(argv[2]));
8766 }
8767 }
8768 }
8769 else {
8770 beg = NUM2LEN(argv[0]);
8771 len = NUM2LEN(argv[1]);
8772 val = argv[2];
8773 StringValue(val);
8774 if (argc == 3) {
8775 /* bytesplice(index, length, str) */
8776 vbeg = 0;
8777 vlen = RSTRING_LEN(val);
8778 }
8779 else {
8780 /* bytesplice(index, length, str, str_index, str_length) */
8781 vbeg = NUM2LEN(argv[3]);
8782 vlen = NUM2LEN(argv[4]);
8783 }
8784 }
8785 str_check_beg_len(str, &beg, &len);
8786 str_check_beg_len(val, &vbeg, &vlen);
8787 str_modify_keep_cr(str);
8788
8789 if (RB_UNLIKELY(ENCODING_GET_INLINED(str) != ENCODING_GET_INLINED(val))) {
8790 rb_enc_associate(str, rb_enc_check(str, val));
8791 }
8792
8793 rb_str_update_1(str, beg, len, val, vbeg, vlen);
8795 if (cr != ENC_CODERANGE_BROKEN)
8796 ENC_CODERANGE_SET(str, cr);
8797 return str;
8798}
8799
8800/*
8801 * call-seq:
8802 * reverse -> new_string
8803 *
8804 * Returns a new string with the characters from +self+ in reverse order.
8805 *
8806 * 'drawer'.reverse # => "reward"
8807 * 'reviled'.reverse # => "deliver"
8808 * 'stressed'.reverse # => "desserts"
8809 * 'semordnilaps'.reverse # => "spalindromes"
8810 *
8811 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
8812 */
8813
8814static VALUE
8815rb_str_reverse(VALUE str)
8816{
8817 rb_encoding *enc;
8818 VALUE rev;
8819 char *s, *e, *p;
8820 int cr;
8821
8822 if (RSTRING_LEN(str) <= 1) return str_duplicate(rb_cString, str);
8823 enc = STR_ENC_GET(str);
8824 rev = str_enc_new(rb_cString, 0, RSTRING_LEN(str), enc);
8825 s = RSTRING_PTR(str); e = RSTRING_END(str);
8826 p = RSTRING_END(rev);
8827 cr = ENC_CODERANGE(str);
8828
8829 if (RSTRING_LEN(str) > 1) {
8830 if (single_byte_optimizable(str)) {
8831 while (s < e) {
8832 *--p = *s++;
8833 }
8834 }
8835 else if (cr == ENC_CODERANGE_VALID) {
8836 while (s < e) {
8837 int clen = rb_enc_fast_mbclen(s, e, enc);
8838
8839 p -= clen;
8840 memcpy(p, s, clen);
8841 s += clen;
8842 }
8843 }
8844 else {
8845 cr = rb_enc_asciicompat(enc) ?
8847 while (s < e) {
8848 int clen = rb_enc_mbclen(s, e, enc);
8849
8850 if (clen > 1 || (*s & 0x80)) cr = ENC_CODERANGE_UNKNOWN;
8851 p -= clen;
8852 memcpy(p, s, clen);
8853 s += clen;
8854 }
8855 }
8856 }
8857 STR_SET_LEN(rev, RSTRING_LEN(str));
8858 ENC_CODERANGE_SET(rev, cr);
8859
8860 return rev;
8861}
8862
8863
8864/*
8865 * call-seq:
8866 * reverse! -> self
8867 *
8868 * Returns +self+ with its characters reversed:
8869 *
8870 * 'drawer'.reverse! # => "reward"
8871 * 'reviled'.reverse! # => "deliver"
8872 * 'stressed'.reverse! # => "desserts"
8873 * 'semordnilaps'.reverse! # => "spalindromes"
8874 *
8875 * Related: see {Modifying}[rdoc-ref:String@Modifying].
8876 */
8877
8878static VALUE
8879rb_str_reverse_bang(VALUE str)
8880{
8881 if (RSTRING_LEN(str) > 1) {
8882 if (single_byte_optimizable(str)) {
8883 char *s, *e, c;
8884
8885 str_modify_keep_cr(str);
8886 s = RSTRING_PTR(str);
8887 e = RSTRING_END(str) - 1;
8888 while (s < e) {
8889 c = *s;
8890 *s++ = *e;
8891 *e-- = c;
8892 }
8893 }
8894 else {
8895 str_shared_replace(str, rb_str_reverse(str));
8896 }
8897 }
8898 else {
8899 str_modify_keep_cr(str);
8900 }
8901 return str;
8902}
8903
8904
8905/*
8906 * call-seq:
8907 * include?(other_string) -> true or false
8908 *
8909 * Returns whether +self+ contains +other_string+:
8910 *
8911 * s = 'bar'
8912 * s.include?('ba') # => true
8913 * s.include?('ar') # => true
8914 * s.include?('bar') # => true
8915 * s.include?('a') # => true
8916 * s.include?('') # => true
8917 * s.include?('foo') # => false
8918 *
8919 * Related: see {Querying}[rdoc-ref:String@Querying].
8920 */
8921
8922VALUE
8923rb_str_include(VALUE str, VALUE arg)
8924{
8925 rb_len_t i;
8926
8927 StringValue(arg);
8928 i = rb_str_index(str, arg, 0);
8929
8930 return RBOOL(i != -1);
8931}
8932
8933
8934/*
8935 * call-seq:
8936 * to_i(base = 10) -> integer
8937 *
8938 * Returns the result of interpreting leading characters in +self+
8939 * as an integer in the given +base+;
8940 * +base+ must be either +0+ or in range <tt>(2..36)</tt>:
8941 *
8942 * '123456'.to_i # => 123456
8943 * '123def'.to_i(16) # => 1195503
8944 *
8945 * With +base+ zero given, string +object+ may contain leading characters
8946 * to specify the actual base:
8947 *
8948 * '123def'.to_i(0) # => 123
8949 * '0123def'.to_i(0) # => 83
8950 * '0b123def'.to_i(0) # => 1
8951 * '0o123def'.to_i(0) # => 83
8952 * '0d123def'.to_i(0) # => 123
8953 * '0x123def'.to_i(0) # => 1195503
8954 *
8955 * Characters past a leading valid number (in the given +base+) are ignored:
8956 *
8957 * '12.345'.to_i # => 12
8958 * '12345'.to_i(2) # => 1
8959 *
8960 * Returns zero if there is no leading valid number:
8961 *
8962 * 'abcdef'.to_i # => 0
8963 * '2'.to_i(2) # => 0
8964 *
8965 * Related: see {Converting to Non-String}[rdoc-ref:String@Converting+to+Non-String].
8966 */
8967
8968static VALUE
8969rb_str_to_i(int argc, VALUE *argv, VALUE str)
8970{
8971 int base = 10;
8972
8973 if (rb_check_arity(argc, 0, 1) && (base = NUM2INT(argv[0])) < 0) {
8974 rb_raise(rb_eArgError, "invalid radix %d", base);
8975 }
8976 return rb_str_to_inum(str, base, FALSE);
8977}
8978
8979
8980/*
8981 * call-seq:
8982 * to_f -> float
8983 *
8984 * Returns the result of interpreting leading characters in +self+ as a Float:
8985 *
8986 * '3.14159'.to_f # => 3.14159
8987 * '1.234e-2'.to_f # => 0.01234
8988 *
8989 * Characters past a leading valid number are ignored:
8990 *
8991 * '3.14 (pi to two places)'.to_f # => 3.14
8992 *
8993 * Returns zero if there is no leading valid number:
8994 *
8995 * 'abcdef'.to_f # => 0.0
8996 *
8997 * See {Converting to Non-String}[rdoc-ref:String@Converting+to+Non-String].
8998 */
8999
9000static VALUE
9001rb_str_to_f(VALUE str)
9002{
9003 return DBL2NUM(rb_str_to_dbl(str, FALSE));
9004}
9005
9006
9007/*
9008 * call-seq:
9009 * to_s -> self or new_string
9010 *
9011 * Returns +self+ if +self+ is a +String+,
9012 * or +self+ converted to a +String+ if +self+ is a subclass of +String+.
9013 *
9014 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
9015 */
9016
9017static VALUE
9018rb_str_to_s(VALUE str)
9019{
9020 if (rb_obj_class(str) != rb_cString) {
9021 return str_duplicate(rb_cString, str);
9022 }
9023 return str;
9024}
9025
9026#if 0
9027static void
9028str_cat_char(VALUE str, unsigned int c, rb_encoding *enc)
9029{
9030 char s[RUBY_MAX_CHAR_LEN];
9031 int n = rb_enc_codelen(c, enc);
9032
9033 rb_enc_mbcput(c, s, enc);
9034 rb_enc_str_buf_cat(str, s, n, enc);
9035}
9036#endif
9037
9038#define CHAR_ESC_LEN 13 /* sizeof(\x{ hex of 32bit unsigned int } \0) */
9039
9040int
9041rb_str_buf_cat_escaped_char(VALUE result, unsigned int c, int unicode_p)
9042{
9043 char buf[CHAR_ESC_LEN + 1];
9044 int l;
9045
9046#if SIZEOF_INT > 4
9047 c &= 0xffffffff;
9048#endif
9049 if (unicode_p) {
9050 if (c < 0x7F && ISPRINT(c)) {
9051 snprintf(buf, CHAR_ESC_LEN, "%c", c);
9052 }
9053 else if (c < 0x10000) {
9054 snprintf(buf, CHAR_ESC_LEN, "\\u%04X", c);
9055 }
9056 else {
9057 snprintf(buf, CHAR_ESC_LEN, "\\u{%X}", c);
9058 }
9059 }
9060 else {
9061 if (c < 0x100) {
9062 snprintf(buf, CHAR_ESC_LEN, "\\x%02X", c);
9063 }
9064 else {
9065 snprintf(buf, CHAR_ESC_LEN, "\\x{%X}", c);
9066 }
9067 }
9068 l = (int)strlen(buf); /* CHAR_ESC_LEN cannot exceed INT_MAX */
9069 rb_str_buf_cat(result, buf, l);
9070 return l;
9071}
9072
9073const char *
9074ruby_escaped_char(int c)
9075{
9076 switch (c) {
9077 case '\0': return "\\0";
9078 case '\n': return "\\n";
9079 case '\r': return "\\r";
9080 case '\t': return "\\t";
9081 case '\f': return "\\f";
9082 case '\013': return "\\v";
9083 case '\010': return "\\b";
9084 case '\007': return "\\a";
9085 case '\033': return "\\e";
9086 case '\x7f': return "\\c?";
9087 }
9088 return NULL;
9089}
9090
9091VALUE
9092rb_str_escape(VALUE str)
9093{
9094 int encidx = ENCODING_GET(str);
9095 rb_encoding *enc = rb_enc_from_index(encidx);
9096 const char *p = RSTRING_PTR(str);
9097 const char *pend = RSTRING_END(str);
9098 const char *prev = p;
9099 char buf[CHAR_ESC_LEN + 1];
9100 VALUE result = rb_str_buf_new(0);
9101 int unicode_p = rb_enc_unicode_p(enc);
9102 int asciicompat = rb_enc_asciicompat(enc);
9103
9104 while (p < pend) {
9105 unsigned int c;
9106 const char *cc;
9107 int n = rb_enc_precise_mbclen(p, pend, enc);
9108 if (!MBCLEN_CHARFOUND_P(n)) {
9109 if (p > prev) str_buf_cat(result, prev, p - prev);
9110 n = rb_enc_mbminlen(enc);
9111 if (pend < p + n)
9112 n = (int)(pend - p);
9113 while (n--) {
9114 snprintf(buf, CHAR_ESC_LEN, "\\x%02X", *p & 0377);
9115 str_buf_cat(result, buf, strlen(buf));
9116 prev = ++p;
9117 }
9118 continue;
9119 }
9120 n = MBCLEN_CHARFOUND_LEN(n);
9121 c = rb_enc_mbc_to_codepoint(p, pend, enc);
9122 p += n;
9123 cc = ruby_escaped_char(c);
9124 if (cc) {
9125 if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
9126 str_buf_cat(result, cc, strlen(cc));
9127 prev = p;
9128 }
9129 else if (asciicompat && rb_enc_isascii(c, enc) && ISPRINT(c)) {
9130 }
9131 else {
9132 if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
9133 rb_str_buf_cat_escaped_char(result, c, unicode_p);
9134 prev = p;
9135 }
9136 }
9137 if (p > prev) str_buf_cat(result, prev, p - prev);
9139
9140 return result;
9141}
9142
9143/* Lookup table for the inspect fast path. 1 marks bytes that need
9144 * no escaping. 0 marks bytes that need escape inspection: 0x00-0x1F
9145 * (control), 0x22 ("), 0x23 (#), 0x5C (\‍), 0x7F (DEL), 0x80-0xFF
9146 * (non-ASCII). */
9147static const bool inspect_no_escape[256] = {
9148 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00-0x0F */
9149 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10-0x1F */
9150 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x20-0x2F */
9151 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x30-0x3F */
9152 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40-0x4F */
9153 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, /* 0x50-0x5F */
9154 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60-0x6F */
9155 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, /* 0x70-0x7F */
9156};
9157
9158/*
9159 * call-seq:
9160 * inspect -> string
9161 *
9162 * Returns a printable version of +self+, enclosed in double-quotes.
9163 *
9164 * Most printable characters are rendered simply as themselves:
9165 *
9166 * 'abc'.inspect # => "\"abc\""
9167 * '012'.inspect # => "\"012\""
9168 * ''.inspect # => "\"\""
9169 * "\u000012".inspect # => "\"\\u000012\""
9170 * 'こんにちは'.inspect # => "\"こんにちは\""
9171 *
9172 * But printable characters double-quote (<tt>'"'</tt>) and backslash (<tt>'\\'</tt>) are escaped:
9173 *
9174 * '"'.inspect # => "\"\\\"\""
9175 * '\\'.inspect # => "\"\\\\\""
9176 *
9177 * Unprintable characters are the {ASCII characters}[https://en.wikipedia.org/wiki/ASCII]
9178 * whose values are in range <tt>0..31</tt>,
9179 * along with the character whose value is +127+.
9180 *
9181 * Most of these characters are rendered thus:
9182 *
9183 * 0.chr.inspect # => "\"\\x00\""
9184 * 1.chr.inspect # => "\"\\x01\""
9185 * 2.chr.inspect # => "\"\\x02\""
9186 * # ...
9187 *
9188 * A few, however, have special renderings:
9189 *
9190 * 7.chr.inspect # => "\"\\a\"" # BEL
9191 * 8.chr.inspect # => "\"\\b\"" # BS
9192 * 9.chr.inspect # => "\"\\t\"" # TAB
9193 * 10.chr.inspect # => "\"\\n\"" # LF
9194 * 11.chr.inspect # => "\"\\v\"" # VT
9195 * 12.chr.inspect # => "\"\\f\"" # FF
9196 * 13.chr.inspect # => "\"\\r\"" # CR
9197 * 27.chr.inspect # => "\"\\e\"" # ESC
9198 *
9199 * Related: see {Converting to Non-String}[rdoc-ref:String@Converting+to+Non-String].
9200 *
9201 */
9202
9203VALUE
9205{
9206 int encidx = ENCODING_GET(str);
9207 rb_encoding *enc = rb_enc_from_index(encidx);
9208 const char *p, *pend, *prev;
9209 char buf[CHAR_ESC_LEN + 1];
9210 VALUE result = rb_str_buf_new(RSTRING_LEN(str) + 2); /* string content + surrounding quotes */
9211 rb_encoding *resenc = rb_default_internal_encoding();
9212 int unicode_p = rb_enc_unicode_p(enc);
9213 int asciicompat = rb_enc_asciicompat(enc);
9214 int cr = rb_enc_str_coderange(str);
9215
9216 if (resenc == NULL) resenc = rb_default_external_encoding();
9217 if (!rb_enc_asciicompat(resenc)) resenc = rb_usascii_encoding();
9218 rb_enc_associate(result, resenc);
9219 str_buf_cat2(result, "\"");
9220
9221 p = RSTRING_PTR(str); pend = RSTRING_END(str);
9222 prev = p;
9223 while (p < pend) {
9224 unsigned int c, cc;
9225 int n;
9226
9227 /* Fast path: bulk-skip runs of safe ASCII bytes via a lookup table.
9228 * Only well-formed strings (CR=7BIT for any encoding, or UTF-8 VALID)
9229 * are eligible. */
9230 if (cr == ENC_CODERANGE_7BIT ||
9231 (encidx == ENCINDEX_UTF_8 && cr == ENC_CODERANGE_VALID)) {
9232 while (p < pend && inspect_no_escape[(unsigned char)*p]) p++;
9233 if (p >= pend) break;
9234 }
9235
9236 n = rb_enc_precise_mbclen(p, pend, enc);
9237 if (!MBCLEN_CHARFOUND_P(n)) {
9238 if (p > prev) str_buf_cat(result, prev, p - prev);
9239 n = rb_enc_mbminlen(enc);
9240 if (pend < p + n)
9241 n = (int)(pend - p);
9242 while (n--) {
9243 snprintf(buf, CHAR_ESC_LEN, "\\x%02X", *p & 0377);
9244 str_buf_cat(result, buf, strlen(buf));
9245 prev = ++p;
9246 }
9247 continue;
9248 }
9249 n = MBCLEN_CHARFOUND_LEN(n);
9250 c = rb_enc_mbc_to_codepoint(p, pend, enc);
9251 p += n;
9252 if ((asciicompat || unicode_p) &&
9253 (c == '"'|| c == '\\' ||
9254 (c == '#' &&
9255 p < pend &&
9256 MBCLEN_CHARFOUND_P(rb_enc_precise_mbclen(p,pend,enc)) &&
9257 (cc = rb_enc_codepoint(p,pend,enc),
9258 (cc == '$' || cc == '@' || cc == '{'))))) {
9259 if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
9260 str_buf_cat2(result, "\\");
9261 if (asciicompat || enc == resenc) {
9262 prev = p - n;
9263 continue;
9264 }
9265 }
9266 switch (c) {
9267 case '\n': cc = 'n'; break;
9268 case '\r': cc = 'r'; break;
9269 case '\t': cc = 't'; break;
9270 case '\f': cc = 'f'; break;
9271 case '\013': cc = 'v'; break;
9272 case '\010': cc = 'b'; break;
9273 case '\007': cc = 'a'; break;
9274 case 033: cc = 'e'; break;
9275 default: cc = 0; break;
9276 }
9277 if (cc) {
9278 if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
9279 buf[0] = '\\';
9280 buf[1] = (char)cc;
9281 str_buf_cat(result, buf, 2);
9282 prev = p;
9283 continue;
9284 }
9285 /* The special casing of 0x85 (NEXT_LINE) here is because
9286 * Oniguruma historically treats it as printable, but it
9287 * doesn't match the print POSIX bracket class or character
9288 * property in regexps.
9289 *
9290 * See Ruby Bug #16842 for details:
9291 * https://bugs.ruby-lang.org/issues/16842
9292 */
9293 if ((enc == resenc && rb_enc_isprint(c, enc) && c != 0x85) ||
9294 (asciicompat && rb_enc_isascii(c, enc) && ISPRINT(c))) {
9295 continue;
9296 }
9297 else {
9298 if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
9299 rb_str_buf_cat_escaped_char(result, c, unicode_p);
9300 prev = p;
9301 continue;
9302 }
9303 }
9304 if (p > prev) str_buf_cat(result, prev, p - prev);
9305 str_buf_cat2(result, "\"");
9306
9307 return result;
9308}
9309
9310#define IS_EVSTR(p,e) ((p) < (e) && (*(p) == '$' || *(p) == '@' || *(p) == '{'))
9311
9312/*
9313 * call-seq:
9314 * dump -> new_string
9315 *
9316 * For an ordinary string, this method, +String#dump+,
9317 * returns a printable ASCII-only version of +self+, enclosed in double-quotes.
9318 *
9319 * For a dumped string, method String#undump is the inverse of +String#dump+;
9320 * it returns a "restored" version of +self+,
9321 * where all the dumping changes have been undone.
9322 *
9323 * In the simplest case, the dumped string contains the original string,
9324 * enclosed in double-quotes;
9325 * this example is done in +irb+ (interactive Ruby), which uses method `inspect` to render the results:
9326 *
9327 * s = 'hello' # => "hello"
9328 * s.dump # => "\"hello\""
9329 * s.dump.undump # => "hello"
9330 *
9331 * Keep in mind that in the second line above:
9332 *
9333 * - The outer double-quotes are put on by +inspect+,
9334 * and _are_ _not_ part of the output of #dump.
9335 * - The inner double-quotes _are_ part of the output of +dump+,
9336 * and are escaped by +inspect+ because they are within the outer double-quotes.
9337 *
9338 * To avoid confusion, we'll use this helper method to omit the outer double-quotes:
9339 *
9340 * def dump(s)
9341 * print "String: ", s, "\n"
9342 * print "Dumped: ", s.dump, "\n"
9343 * print "Undumped: ", s.dump.undump, "\n"
9344 * end
9345 *
9346 * So that for string <tt>'hello'</tt>, we'll see:
9347 *
9348 * String: hello
9349 * Dumped: "hello"
9350 * Undumped: hello
9351 *
9352 * In a dump, certain special characters are escaped:
9353 *
9354 * String: "
9355 * Dumped: "\""
9356 * Undumped: "
9357 *
9358 * String: \
9359 * Dumped: "\\"
9360 * Undumped: \
9361 *
9362 * In a dump, unprintable characters are replaced by printable ones;
9363 * the unprintable characters are the whitespace characters (other than space itself);
9364 * here we see the ordinals for those characters, together with explanatory text:
9365 *
9366 * h = {
9367 * 7 => 'Alert (BEL)',
9368 * 8 => 'Backspace (BS)',
9369 * 9 => 'Horizontal tab (HT)',
9370 * 10 => 'Linefeed (LF)',
9371 * 11 => 'Vertical tab (VT)',
9372 * 12 => 'Formfeed (FF)',
9373 * 13 => 'Carriage return (CR)'
9374 * }
9375 *
9376 * In this example, the dumped output is printed by method #inspect,
9377 * and so contains both outer double-quotes and escaped inner double-quotes:
9378 *
9379 * s = ''
9380 * h.keys.each {|i| s << i } # => [7, 8, 9, 10, 11, 12, 13]
9381 * s # => "\a\b\t\n\v\f\r"
9382 * s.dump # => "\"\\a\\b\\t\\n\\v\\f\\r\""
9383 *
9384 * If +self+ is encoded in UTF-8 and contains Unicode characters,
9385 * each Unicode character is dumped as a Unicode escape sequence:
9386 *
9387 * String: こんにちは
9388 * Dumped: "\u3053\u3093\u306B\u3061\u306F"
9389 * Undumped: こんにちは
9390 *
9391 * If the encoding of +self+ is not ASCII-compatible
9392 * (i.e., if <tt>self.encoding.ascii_compatible?</tt> returns +false+),
9393 * each ASCII-compatible byte is dumped as an ASCII character,
9394 * and all other bytes are dumped as hexadecimal;
9395 * also appends <tt>.dup.force_encoding(\"encoding\")</tt>,
9396 * where <tt><encoding></tt> is <tt>self.encoding.name</tt>:
9397 *
9398 * String: hello
9399 * Dumped: "\xFE\xFF\x00h\x00e\x00l\x00l\x00o".dup.force_encoding("UTF-16")
9400 * Undumped: hello
9401 *
9402 * String: こんにちは
9403 * Dumped: "\xFE\xFF0S0\x930k0a0o".dup.force_encoding("UTF-16")
9404 * Undumped: こんにちは
9405 *
9406 */
9407
9408VALUE
9410{
9411 int encidx = rb_enc_get_index(str);
9412 rb_encoding *enc = rb_enc_from_index(encidx);
9413 rb_len_t len;
9414 const char *p, *pend;
9415 char *q, *qend;
9416 VALUE result;
9417 int u8 = (encidx == rb_utf8_encindex());
9418 static const char nonascii_suffix[] = ".dup.force_encoding(\"%s\")";
9419
9420 len = 2; /* "" */
9421 if (!rb_enc_asciicompat(enc)) {
9422 len += strlen(nonascii_suffix) - rb_strlen_lit("%s");
9423 len += strlen(enc->name);
9424 }
9425
9426 p = RSTRING_PTR(str); pend = p + RSTRING_LEN(str);
9427 while (p < pend) {
9428 int clen;
9429 unsigned char c = *p++;
9430
9431 switch (c) {
9432 case '"': case '\\':
9433 case '\n': case '\r':
9434 case '\t': case '\f':
9435 case '\013': case '\010': case '\007': case '\033':
9436 clen = 2;
9437 break;
9438
9439 case '#':
9440 clen = IS_EVSTR(p, pend) ? 2 : 1;
9441 break;
9442
9443 default:
9444 if (ISPRINT(c)) {
9445 clen = 1;
9446 }
9447 else {
9448 if (u8 && c > 0x7F) { /* \u notation */
9449 int n = rb_enc_precise_mbclen(p-1, pend, enc);
9450 if (MBCLEN_CHARFOUND_P(n)) {
9451 unsigned int cc = rb_enc_mbc_to_codepoint(p-1, pend, enc);
9452 if (cc <= 0xFFFF)
9453 clen = 6; /* \uXXXX */
9454 else if (cc <= 0xFFFFF)
9455 clen = 9; /* \u{XXXXX} */
9456 else
9457 clen = 10; /* \u{XXXXXX} */
9458 p += MBCLEN_CHARFOUND_LEN(n)-1;
9459 break;
9460 }
9461 }
9462 clen = 4; /* \xNN */
9463 }
9464 break;
9465 }
9466
9467 if (clen > RB_LEN_MAX - len) {
9468 rb_raise(rb_eRuntimeError, "string size too big");
9469 }
9470 len += clen;
9471 }
9472
9473 result = rb_str_new(0, len);
9474 p = RSTRING_PTR(str); pend = p + RSTRING_LEN(str);
9475 q = RSTRING_PTR(result); qend = q + len + 1;
9476
9477 *q++ = '"';
9478 while (p < pend) {
9479 unsigned char c = *p++;
9480
9481 if (c == '"' || c == '\\') {
9482 *q++ = '\\';
9483 *q++ = c;
9484 }
9485 else if (c == '#') {
9486 if (IS_EVSTR(p, pend)) *q++ = '\\';
9487 *q++ = '#';
9488 }
9489 else if (c == '\n') {
9490 *q++ = '\\';
9491 *q++ = 'n';
9492 }
9493 else if (c == '\r') {
9494 *q++ = '\\';
9495 *q++ = 'r';
9496 }
9497 else if (c == '\t') {
9498 *q++ = '\\';
9499 *q++ = 't';
9500 }
9501 else if (c == '\f') {
9502 *q++ = '\\';
9503 *q++ = 'f';
9504 }
9505 else if (c == '\013') {
9506 *q++ = '\\';
9507 *q++ = 'v';
9508 }
9509 else if (c == '\010') {
9510 *q++ = '\\';
9511 *q++ = 'b';
9512 }
9513 else if (c == '\007') {
9514 *q++ = '\\';
9515 *q++ = 'a';
9516 }
9517 else if (c == '\033') {
9518 *q++ = '\\';
9519 *q++ = 'e';
9520 }
9521 else if (ISPRINT(c)) {
9522 *q++ = c;
9523 }
9524 else {
9525 *q++ = '\\';
9526 if (u8) {
9527 int n = rb_enc_precise_mbclen(p-1, pend, enc) - 1;
9528 if (MBCLEN_CHARFOUND_P(n)) {
9529 int cc = rb_enc_mbc_to_codepoint(p-1, pend, enc);
9530 p += n;
9531 if (cc <= 0xFFFF)
9532 snprintf(q, qend-q, "u%04X", cc); /* \uXXXX */
9533 else
9534 snprintf(q, qend-q, "u{%X}", cc); /* \u{XXXXX} or \u{XXXXXX} */
9535 q += strlen(q);
9536 continue;
9537 }
9538 }
9539 snprintf(q, qend-q, "x%02X", c);
9540 q += 3;
9541 }
9542 }
9543 *q++ = '"';
9544 *q = '\0';
9545 if (!rb_enc_asciicompat(enc)) {
9546 snprintf(q, qend-q, nonascii_suffix, enc->name);
9547 encidx = rb_ascii8bit_encindex();
9548 }
9549 /* result from dump is ASCII */
9550 rb_enc_associate_index(result, encidx);
9552 return result;
9553}
9554
9555static int
9556unescape_ascii(unsigned int c)
9557{
9558 switch (c) {
9559 case 'n':
9560 return '\n';
9561 case 'r':
9562 return '\r';
9563 case 't':
9564 return '\t';
9565 case 'f':
9566 return '\f';
9567 case 'v':
9568 return '\13';
9569 case 'b':
9570 return '\010';
9571 case 'a':
9572 return '\007';
9573 case 'e':
9574 return 033;
9575 }
9577}
9578
9579static void
9580undump_after_backslash(VALUE undumped, const char **ss, const char *s_end, rb_encoding **penc, bool *utf8, bool *binary)
9581{
9582 const char *s = *ss;
9583 unsigned int c;
9584 int codelen;
9585 size_t hexlen;
9586 unsigned char buf[6];
9587 static rb_encoding *enc_utf8 = NULL;
9588
9589 switch (*s) {
9590 case '\\':
9591 case '"':
9592 case '#':
9593 rb_str_cat(undumped, s, 1); /* cat itself */
9594 s++;
9595 break;
9596 case 'n':
9597 case 'r':
9598 case 't':
9599 case 'f':
9600 case 'v':
9601 case 'b':
9602 case 'a':
9603 case 'e':
9604 *buf = unescape_ascii(*s);
9605 rb_str_cat(undumped, (char *)buf, 1);
9606 s++;
9607 break;
9608 case 'u':
9609 if (*binary) {
9610 rb_raise(rb_eRuntimeError, "hex escape and Unicode escape are mixed");
9611 }
9612 *utf8 = true;
9613 if (++s >= s_end) {
9614 rb_raise(rb_eRuntimeError, "invalid Unicode escape");
9615 }
9616 if (enc_utf8 == NULL) enc_utf8 = rb_utf8_encoding();
9617 if (*penc != enc_utf8) {
9618 *penc = enc_utf8;
9619 rb_enc_associate(undumped, enc_utf8);
9620 }
9621 if (*s == '{') { /* handle \u{...} form */
9622 s++;
9623 for (;;) {
9624 if (s >= s_end) {
9625 rb_raise(rb_eRuntimeError, "unterminated Unicode escape");
9626 }
9627 if (*s == '}') {
9628 s++;
9629 break;
9630 }
9631 if (ISSPACE(*s)) {
9632 s++;
9633 continue;
9634 }
9635 c = scan_hex(s, s_end-s, &hexlen);
9636 if (hexlen == 0 || hexlen > 6) {
9637 rb_raise(rb_eRuntimeError, "invalid Unicode escape");
9638 }
9639 if (c > 0x10ffff) {
9640 rb_raise(rb_eRuntimeError, "invalid Unicode codepoint (too large)");
9641 }
9642 if (0xd800 <= c && c <= 0xdfff) {
9643 rb_raise(rb_eRuntimeError, "invalid Unicode codepoint");
9644 }
9645 codelen = rb_enc_mbcput(c, (char *)buf, *penc);
9646 rb_str_cat(undumped, (char *)buf, codelen);
9647 s += hexlen;
9648 }
9649 }
9650 else { /* handle \uXXXX form */
9651 c = scan_hex(s, 4, &hexlen);
9652 if (hexlen != 4) {
9653 rb_raise(rb_eRuntimeError, "invalid Unicode escape");
9654 }
9655 if (0xd800 <= c && c <= 0xdfff) {
9656 rb_raise(rb_eRuntimeError, "invalid Unicode codepoint");
9657 }
9658 codelen = rb_enc_mbcput(c, (char *)buf, *penc);
9659 rb_str_cat(undumped, (char *)buf, codelen);
9660 s += hexlen;
9661 }
9662 break;
9663 case 'x':
9664 if (++s >= s_end) {
9665 rb_raise(rb_eRuntimeError, "invalid hex escape");
9666 }
9667 *buf = scan_hex(s, 2, &hexlen);
9668 if (hexlen != 2) {
9669 rb_raise(rb_eRuntimeError, "invalid hex escape");
9670 }
9671 if (!ISASCII(*buf)) {
9672 if (*utf8) {
9673 rb_raise(rb_eRuntimeError, "hex escape and Unicode escape are mixed");
9674 }
9675 *binary = true;
9676 }
9677 rb_str_cat(undumped, (char *)buf, 1);
9678 s += hexlen;
9679 break;
9680 default:
9681 rb_str_cat(undumped, s-1, 2);
9682 s++;
9683 }
9684
9685 *ss = s;
9686}
9687
9688static VALUE rb_str_is_ascii_only_p(VALUE str);
9689
9690/*
9691 * call-seq:
9692 * undump -> new_string
9693 *
9694 * Inverse of String#dump; returns a copy of +self+ with changes of the kinds made by String#dump "undone."
9695 *
9696 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
9697 */
9698
9699static VALUE
9700str_undump(VALUE str)
9701{
9702 const char *s = RSTRING_PTR(str);
9703 const char *s_end = RSTRING_END(str);
9704 rb_encoding *enc = rb_enc_get(str);
9705 VALUE undumped = rb_enc_str_new(s, 0L, enc);
9706 bool utf8 = false;
9707 bool binary = false;
9708 int w;
9709
9711 if (rb_str_is_ascii_only_p(str) == Qfalse) {
9712 rb_raise(rb_eRuntimeError, "non-ASCII character detected");
9713 }
9714 if (!str_null_check(str, &w)) {
9715 rb_raise(rb_eRuntimeError, "string contains null byte");
9716 }
9717 if (RSTRING_LEN(str) < 2) goto invalid_format;
9718 if (*s != '"') goto invalid_format;
9719
9720 /* strip '"' at the start */
9721 s++;
9722
9723 for (;;) {
9724 if (s >= s_end) {
9725 rb_raise(rb_eRuntimeError, "unterminated dumped string");
9726 }
9727
9728 if (*s == '"') {
9729 /* epilogue */
9730 s++;
9731 if (s == s_end) {
9732 /* ascii compatible dumped string */
9733 break;
9734 }
9735 else {
9736 static const char force_encoding_suffix[] = ".force_encoding(\""; /* "\")" */
9737 static const char dup_suffix[] = ".dup";
9738 const char *encname;
9739 int encidx;
9740 ptrdiff_t size;
9741
9742 /* check separately for strings dumped by older versions */
9743 size = sizeof(dup_suffix) - 1;
9744 if (s_end - s > size && memcmp(s, dup_suffix, size) == 0) s += size;
9745
9746 size = sizeof(force_encoding_suffix) - 1;
9747 if (s_end - s <= size) goto invalid_format;
9748 if (memcmp(s, force_encoding_suffix, size) != 0) goto invalid_format;
9749 s += size;
9750
9751 if (utf8) {
9752 rb_raise(rb_eRuntimeError, "dumped string contained Unicode escape but used force_encoding");
9753 }
9754
9755 encname = s;
9756 s = memchr(s, '"', s_end-s);
9757 size = s - encname;
9758 if (!s) goto invalid_format;
9759 if (s_end - s != 2) goto invalid_format;
9760 if (s[0] != '"' || s[1] != ')') goto invalid_format;
9761
9762 encidx = rb_enc_find_index2(encname, (rb_len_t)size);
9763 if (encidx < 0) {
9764 rb_raise(rb_eRuntimeError, "dumped string has unknown encoding name");
9765 }
9766 rb_enc_associate_index(undumped, encidx);
9767 }
9768 break;
9769 }
9770
9771 if (*s == '\\') {
9772 s++;
9773 if (s >= s_end) {
9774 rb_raise(rb_eRuntimeError, "invalid escape");
9775 }
9776 undump_after_backslash(undumped, &s, s_end, &enc, &utf8, &binary);
9777 }
9778 else {
9779 rb_str_cat(undumped, s++, 1);
9780 }
9781 }
9782
9783 RB_GC_GUARD(str);
9784
9785 return undumped;
9786invalid_format:
9787 rb_raise(rb_eRuntimeError, "invalid dumped string; not wrapped with '\"' nor '\"...\".force_encoding(\"...\")' form");
9788}
9789
9790static void
9791rb_str_check_dummy_enc(rb_encoding *enc)
9792{
9793 if (rb_enc_dummy_p(enc)) {
9794 rb_raise(rb_eEncCompatError, "incompatible encoding with this operation: %s",
9795 rb_enc_name(enc));
9796 }
9797}
9798
9799static rb_encoding *
9800str_true_enc(VALUE str)
9801{
9802 rb_encoding *enc = STR_ENC_GET(str);
9803 rb_str_check_dummy_enc(enc);
9804 return enc;
9805}
9806
9807static OnigCaseFoldType
9808check_case_options(int argc, VALUE *argv, OnigCaseFoldType flags)
9809{
9810 if (argc==0)
9811 return flags;
9812 if (argc>2)
9813 rb_raise(rb_eArgError, "too many options");
9814 if (argv[0]==sym_turkic) {
9815 flags |= ONIGENC_CASE_FOLD_TURKISH_AZERI;
9816 if (argc==2) {
9817 if (argv[1]==sym_lithuanian)
9818 flags |= ONIGENC_CASE_FOLD_LITHUANIAN;
9819 else
9820 rb_raise(rb_eArgError, "invalid second option");
9821 }
9822 }
9823 else if (argv[0]==sym_lithuanian) {
9824 flags |= ONIGENC_CASE_FOLD_LITHUANIAN;
9825 if (argc==2) {
9826 if (argv[1]==sym_turkic)
9827 flags |= ONIGENC_CASE_FOLD_TURKISH_AZERI;
9828 else
9829 rb_raise(rb_eArgError, "invalid second option");
9830 }
9831 }
9832 else if (argc>1)
9833 rb_raise(rb_eArgError, "too many options");
9834 else if (argv[0]==sym_ascii)
9835 flags |= ONIGENC_CASE_ASCII_ONLY;
9836 else if (argv[0]==sym_fold) {
9837 if ((flags & (ONIGENC_CASE_UPCASE|ONIGENC_CASE_DOWNCASE)) == ONIGENC_CASE_DOWNCASE)
9838 flags ^= ONIGENC_CASE_FOLD|ONIGENC_CASE_DOWNCASE;
9839 else
9840 rb_raise(rb_eArgError, "option :fold only allowed for downcasing");
9841 }
9842 else
9843 rb_raise(rb_eArgError, "invalid option");
9844 return flags;
9845}
9846
9847static inline bool
9848case_option_single_p(OnigCaseFoldType flags, rb_encoding *enc, VALUE str)
9849{
9850 if ((flags & ONIGENC_CASE_ASCII_ONLY) && (enc==rb_utf8_encoding() || rb_enc_mbmaxlen(enc) == 1))
9851 return true;
9852 return !(flags & ONIGENC_CASE_FOLD_TURKISH_AZERI) &&
9853 (ENC_CODERANGE(str) == ENC_CODERANGE_7BIT || rb_is_ascii8bit_enc(enc));
9854}
9855
9856/* 16 should be long enough to absorb any kind of single character length increase */
9857#define CASE_MAPPING_ADDITIONAL_LENGTH 20
9858#ifndef CASEMAP_DEBUG
9859# define CASEMAP_DEBUG 0
9860#endif
9861
9862struct mapping_buffer;
9863typedef struct mapping_buffer {
9864 size_t capa;
9865 size_t used;
9866 struct mapping_buffer *next;
9867 OnigUChar space[FLEX_ARY_LEN];
9869
9870static void
9871mapping_buffer_free(void *p)
9872{
9873 mapping_buffer *previous_buffer;
9874 mapping_buffer *current_buffer = p;
9875 while (current_buffer) {
9876 previous_buffer = current_buffer;
9877 current_buffer = current_buffer->next;
9878 ruby_xfree_sized(previous_buffer, offsetof(mapping_buffer, space) + previous_buffer->capa);
9879 }
9880}
9881
9882static const rb_data_type_t mapping_buffer_type = {
9883 "mapping_buffer",
9884 {0, mapping_buffer_free,},
9885 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED
9886};
9887
9888static VALUE
9889rb_str_casemap(VALUE source, OnigCaseFoldType *flags, rb_encoding *enc)
9890{
9891 VALUE target;
9892
9893 const OnigUChar *source_current, *source_end;
9894 rb_len_t target_length = 0;
9895 VALUE buffer_anchor;
9896 mapping_buffer *current_buffer = 0;
9897 mapping_buffer **pre_buffer;
9898 size_t buffer_count = 0;
9899 int buffer_length_or_invalid;
9900
9901 if (RSTRING_LEN(source) == 0) return str_duplicate(rb_cString, source);
9902 if (RSTRING_LEN(source) > INT_MAX - CASE_MAPPING_ADDITIONAL_LENGTH)
9903 rb_raise(rb_eArgError, "string size too big");
9904
9905 source_current = (OnigUChar*)RSTRING_PTR(source);
9906 source_end = (OnigUChar*)RSTRING_END(source);
9907
9908 buffer_anchor = TypedData_Wrap_Struct(0, &mapping_buffer_type, 0);
9909 pre_buffer = (mapping_buffer **)&DATA_PTR(buffer_anchor);
9910 while (source_current < source_end) {
9911 /* increase multiplier using buffer count to converge quickly */
9912 size_t capa = (size_t)(source_end-source_current);
9913 if (capa > (INT_MAX - CASE_MAPPING_ADDITIONAL_LENGTH) / ++buffer_count)
9914 capa = INT_MAX;
9915 else
9916 capa = capa * buffer_count + CASE_MAPPING_ADDITIONAL_LENGTH;
9917 if (CASEMAP_DEBUG) {
9918 fprintf(stderr, "Buffer allocation, capa is %"PRIuSIZE"\n", capa); /* for tuning */
9919 }
9920 current_buffer = xmalloc(offsetof(mapping_buffer, space) + capa);
9921 *pre_buffer = current_buffer;
9922 pre_buffer = &current_buffer->next;
9923 current_buffer->next = NULL;
9924 current_buffer->capa = capa;
9925 buffer_length_or_invalid = enc->case_map(flags,
9926 &source_current, source_end,
9927 current_buffer->space,
9928 current_buffer->space+current_buffer->capa,
9929 enc);
9930 if (buffer_length_or_invalid < 0 || buffer_length_or_invalid > RB_LEN_MAX - target_length) {
9931 current_buffer = DATA_PTR(buffer_anchor);
9932 DATA_PTR(buffer_anchor) = 0;
9933 mapping_buffer_free(current_buffer);
9934 if (buffer_length_or_invalid < 0)
9935 rb_raise(rb_eArgError, "input string invalid");
9936 rb_raise(rb_eArgError, "string size too big");
9937 }
9938 target_length += current_buffer->used = buffer_length_or_invalid;
9939 }
9940 if (CASEMAP_DEBUG) {
9941 fprintf(stderr, "Buffer count is %"PRIuSIZE"\n", buffer_count); /* for tuning */
9942 }
9943
9944 if (buffer_count==1) {
9945 target = str_enc_new(rb_cString, (const char*)current_buffer->space, target_length, enc);
9946 }
9947 else {
9948 char *target_current;
9949
9950 target = str_enc_new(rb_cString, 0, target_length, enc);
9951 target_current = RSTRING_PTR(target);
9952 current_buffer = DATA_PTR(buffer_anchor);
9953 while (current_buffer) {
9954 memcpy(target_current, current_buffer->space, current_buffer->used);
9955 target_current += current_buffer->used;
9956 current_buffer = current_buffer->next;
9957 }
9958 }
9959 current_buffer = DATA_PTR(buffer_anchor);
9960 DATA_PTR(buffer_anchor) = 0;
9961 mapping_buffer_free(current_buffer);
9962
9963 RB_GC_GUARD(buffer_anchor);
9964
9965 /*ENC_CODERANGE_SET(mapped, cr);*/
9966
9967 return target;
9968}
9969
9970static VALUE
9971rb_str_ascii_casemap(VALUE source, VALUE target, OnigCaseFoldType *flags, rb_encoding *enc)
9972{
9973 const OnigUChar *source_current, *source_end;
9974 OnigUChar *target_current, *target_end;
9975 rb_len_t old_length = RSTRING_LEN(source);
9976 int length_or_invalid;
9977
9978 if (old_length == 0) return Qnil;
9979
9980 source_current = (OnigUChar*)RSTRING_PTR(source);
9981 source_end = (OnigUChar*)RSTRING_END(source);
9982 if (source == target) {
9983 target_current = (OnigUChar*)source_current;
9984 target_end = (OnigUChar*)source_end;
9985 }
9986 else {
9987 target_current = (OnigUChar*)RSTRING_PTR(target);
9988 target_end = (OnigUChar*)RSTRING_END(target);
9989 }
9990
9991 length_or_invalid = onigenc_ascii_only_case_map(flags,
9992 &source_current, source_end,
9993 target_current, target_end, enc);
9994 if (length_or_invalid < 0)
9995 rb_raise(rb_eArgError, "input string invalid");
9996 if (CASEMAP_DEBUG && length_or_invalid != old_length) {
9997 fprintf(stderr, "problem with rb_str_ascii_casemap"
9998 "; old_length=%"PRIdLEN", new_length=%d\n", old_length, length_or_invalid);
9999 rb_raise(rb_eArgError, "internal problem with rb_str_ascii_casemap"
10000 "; old_length=%"PRIdLEN", new_length=%d\n", old_length, length_or_invalid);
10001 }
10002
10003 rb_enc_associate_index(target, ENCODING_GET(source));
10004
10005 return target;
10006}
10007
10008static bool
10009upcase_single(VALUE str)
10010{
10011 char *s = RSTRING_PTR(str), *send = RSTRING_END(str);
10012 bool modified = false;
10013
10014 while (s < send) {
10015 unsigned int c = *(unsigned char*)s;
10016
10017 if ('a' <= c && c <= 'z') {
10018 *s = 'A' + (c - 'a');
10019 modified = true;
10020 }
10021 s++;
10022 }
10023 return modified;
10024}
10025
10026/*
10027 * call-seq:
10028 * upcase!(mapping) -> self or nil
10029 *
10030 * Like String#upcase, except that:
10031 *
10032 * - Changes character casings in +self+ (not in a copy of +self+).
10033 * - Returns +self+ if any changes are made, +nil+ otherwise.
10034 *
10035 * Related: See {Modifying}[rdoc-ref:String@Modifying].
10036 */
10037
10038static VALUE
10039rb_str_upcase_bang(int argc, VALUE *argv, VALUE str)
10040{
10041 rb_encoding *enc;
10042 OnigCaseFoldType flags = ONIGENC_CASE_UPCASE;
10043
10044 flags = check_case_options(argc, argv, flags);
10045 str_modify_keep_cr(str);
10046 enc = str_true_enc(str);
10047 if (case_option_single_p(flags, enc, str)) {
10048 if (upcase_single(str))
10049 flags |= ONIGENC_CASE_MODIFIED;
10050 }
10051 else if (flags&ONIGENC_CASE_ASCII_ONLY)
10052 rb_str_ascii_casemap(str, str, &flags, enc);
10053 else
10054 str_shared_replace(str, rb_str_casemap(str, &flags, enc));
10055
10056 if (ONIGENC_CASE_MODIFIED&flags) return str;
10057 return Qnil;
10058}
10059
10060
10061/*
10062 * call-seq:
10063 * upcase(mapping) -> new_string
10064 *
10065 * Returns a new string containing the upcased characters in +self+:
10066 *
10067 * 'hello'.upcase # => "HELLO"
10068 * 'straße'.upcase # => "STRASSE"
10069 * 'привет'.upcase # => "ПРИВЕТ"
10070 * 'RubyGems.org'.upcase # => "RUBYGEMS.ORG"
10071 *
10072 * The sizes of +self+ and the upcased result may differ:
10073 *
10074 * s = 'Straße'
10075 * s.size # => 6
10076 * s.upcase # => "STRASSE"
10077 * s.upcase.size # => 7
10078 *
10079 * Some characters (and some character sets) do not have upcase and downcase versions;
10080 * see {Case Mapping}[rdoc-ref:case_mapping.rdoc]:
10081 *
10082 * s = '1, 2, 3, ...'
10083 * s.upcase == s # => true
10084 * s = 'こんにちは'
10085 * s.upcase == s # => true
10086 *
10087 * The casing is affected by the given +mapping+,
10088 * which may be +:ascii+, +:fold+, or +:turkic+;
10089 * see {Case Mappings}[rdoc-ref:case_mapping.rdoc@Case+Mappings].
10090 *
10091 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
10092 */
10093
10094static VALUE
10095rb_str_upcase(int argc, VALUE *argv, VALUE str)
10096{
10097 rb_encoding *enc;
10098 OnigCaseFoldType flags = ONIGENC_CASE_UPCASE;
10099 VALUE ret;
10100
10101 flags = check_case_options(argc, argv, flags);
10102 enc = str_true_enc(str);
10103 if (case_option_single_p(flags, enc, str)) {
10104 ret = rb_str_new(RSTRING_PTR(str), RSTRING_LEN(str));
10105 str_enc_copy_direct(ret, str);
10106 upcase_single(ret);
10107 }
10108 else if (flags&ONIGENC_CASE_ASCII_ONLY) {
10109 ret = rb_str_new(0, RSTRING_LEN(str));
10110 rb_str_ascii_casemap(str, ret, &flags, enc);
10111 }
10112 else {
10113 ret = rb_str_casemap(str, &flags, enc);
10114 }
10115
10116 return ret;
10117}
10118
10119static bool
10120downcase_single(VALUE str)
10121{
10122 char *s = RSTRING_PTR(str), *send = RSTRING_END(str);
10123 bool modified = false;
10124
10125 while (s < send) {
10126 unsigned int c = *(unsigned char*)s;
10127
10128 if ('A' <= c && c <= 'Z') {
10129 *s = 'a' + (c - 'A');
10130 modified = true;
10131 }
10132 s++;
10133 }
10134
10135 return modified;
10136}
10137
10138/*
10139 * call-seq:
10140 * downcase!(mapping) -> self or nil
10141 *
10142 * Like String#downcase, except that:
10143 *
10144 * - Changes character casings in +self+ (not in a copy of +self+).
10145 * - Returns +self+ if any changes are made, +nil+ otherwise.
10146 *
10147 * Related: See {Modifying}[rdoc-ref:String@Modifying].
10148 */
10149
10150static VALUE
10151rb_str_downcase_bang(int argc, VALUE *argv, VALUE str)
10152{
10153 rb_encoding *enc;
10154 OnigCaseFoldType flags = ONIGENC_CASE_DOWNCASE;
10155
10156 flags = check_case_options(argc, argv, flags);
10157 str_modify_keep_cr(str);
10158 enc = str_true_enc(str);
10159 if (case_option_single_p(flags, enc, str)) {
10160 if (downcase_single(str))
10161 flags |= ONIGENC_CASE_MODIFIED;
10162 }
10163 else if (flags&ONIGENC_CASE_ASCII_ONLY)
10164 rb_str_ascii_casemap(str, str, &flags, enc);
10165 else
10166 str_shared_replace(str, rb_str_casemap(str, &flags, enc));
10167
10168 if (ONIGENC_CASE_MODIFIED&flags) return str;
10169 return Qnil;
10170}
10171
10172
10173/*
10174 * call-seq:
10175 * downcase(mapping) -> new_string
10176 *
10177 * Returns a new string containing the downcased characters in +self+:
10178 *
10179 * 'HELLO'.downcase # => "hello"
10180 * 'STRAẞE'.downcase # => "straße"
10181 * 'ПРИВЕТ'.downcase # => "привет"
10182 * 'RubyGems.org'.downcase # => "rubygems.org"
10183 *
10184 * Some characters (and some character sets) do not have upcase and downcase versions;
10185 * see {Case Mapping}[rdoc-ref:case_mapping.rdoc]:
10186 *
10187 * s = '1, 2, 3, ...'
10188 * s.downcase == s # => true
10189 * s = 'こんにちは'
10190 * s.downcase == s # => true
10191 *
10192 * The casing is affected by the given +mapping+,
10193 * which may be +:ascii+, +:fold+, or +:turkic+;
10194 * see {Case Mappings}[rdoc-ref:case_mapping.rdoc@Case+Mappings].
10195 *
10196 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
10197 *
10198 */
10199
10200static VALUE
10201rb_str_downcase(int argc, VALUE *argv, VALUE str)
10202{
10203 rb_encoding *enc;
10204 OnigCaseFoldType flags = ONIGENC_CASE_DOWNCASE;
10205 VALUE ret;
10206
10207 flags = check_case_options(argc, argv, flags);
10208 enc = str_true_enc(str);
10209 if (case_option_single_p(flags, enc, str)) {
10210 ret = rb_str_new(RSTRING_PTR(str), RSTRING_LEN(str));
10211 str_enc_copy_direct(ret, str);
10212 downcase_single(ret);
10213 }
10214 else if (flags&ONIGENC_CASE_ASCII_ONLY) {
10215 ret = rb_str_new(0, RSTRING_LEN(str));
10216 rb_str_ascii_casemap(str, ret, &flags, enc);
10217 }
10218 else {
10219 ret = rb_str_casemap(str, &flags, enc);
10220 }
10221
10222 return ret;
10223}
10224
10225static bool
10226capitalize_single(VALUE str)
10227{
10228 char *s = RSTRING_PTR(str), *send = RSTRING_END(str);
10229 bool modified = false;
10230
10231 if (s < send) {
10232 unsigned int c = (unsigned char)*s;
10233
10234 if ('a' <= c && c <= 'z') {
10235 *s = 'A' + (c - 'a');
10236 modified = true;
10237 }
10238 s++;
10239 }
10240 while (s < send) {
10241 unsigned int c = (unsigned char)*s;
10242
10243 if ('A' <= c && c <= 'Z') {
10244 *s = 'a' + (c - 'A');
10245 modified = true;
10246 }
10247 s++;
10248 }
10249
10250 return modified;
10251}
10252
10253/*
10254 * call-seq:
10255 * capitalize!(mapping) -> self or nil
10256 *
10257 * Like String#capitalize, except that:
10258 *
10259 * - Changes character casings in +self+ (not in a copy of +self+).
10260 * - Returns +self+ if any changes are made, +nil+ otherwise.
10261 *
10262 * Related: See {Modifying}[rdoc-ref:String@Modifying].
10263 */
10264
10265static VALUE
10266rb_str_capitalize_bang(int argc, VALUE *argv, VALUE str)
10267{
10268 rb_encoding *enc;
10269 OnigCaseFoldType flags = ONIGENC_CASE_UPCASE | ONIGENC_CASE_TITLECASE;
10270
10271 flags = check_case_options(argc, argv, flags);
10272 str_modify_keep_cr(str);
10273 enc = str_true_enc(str);
10274 if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return Qnil;
10275 if (case_option_single_p(flags, enc, str)) {
10276 if (capitalize_single(str))
10277 flags |= ONIGENC_CASE_MODIFIED;
10278 }
10279 else if (flags&ONIGENC_CASE_ASCII_ONLY)
10280 rb_str_ascii_casemap(str, str, &flags, enc);
10281 else
10282 str_shared_replace(str, rb_str_casemap(str, &flags, enc));
10283
10284 if (ONIGENC_CASE_MODIFIED&flags) return str;
10285 return Qnil;
10286}
10287
10288
10289/*
10290 * call-seq:
10291 * capitalize(mapping) -> new_string
10292 *
10293 * Returns a string containing the characters in +self+,
10294 * each with possibly changed case:
10295 *
10296 * - The first character made uppercase.
10297 * - All other characters are made lowercase.
10298 *
10299 * Examples:
10300 *
10301 * 'hello'.capitalize # => "Hello"
10302 * 'HELLO'.capitalize # => "Hello"
10303 * 'straße'.capitalize # => "Straße" # Lowercase 'ß' not changed.
10304 * 'STRAẞE'.capitalize # => "Straße" # Uppercase 'ẞ' downcased to 'ß'.
10305 *
10306 * Some characters (and some character sets) do not have upcase and downcase versions;
10307 * see {Case Mapping}[rdoc-ref:case_mapping.rdoc]:
10308 *
10309 * s = '1, 2, 3, ...'
10310 * s.capitalize == s # => true
10311 * s = 'こんにちは'
10312 * s.capitalize == s # => true
10313 *
10314 * The casing is affected by the given +mapping+,
10315 * which may be +:ascii+, +:fold+, or +:turkic+;
10316 * see {Case Mappings}[rdoc-ref:case_mapping.rdoc@Case+Mappings].
10317 *
10318 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
10319 *
10320 */
10321
10322static VALUE
10323rb_str_capitalize(int argc, VALUE *argv, VALUE str)
10324{
10325 rb_encoding *enc;
10326 OnigCaseFoldType flags = ONIGENC_CASE_UPCASE | ONIGENC_CASE_TITLECASE;
10327 VALUE ret;
10328
10329 flags = check_case_options(argc, argv, flags);
10330 enc = str_true_enc(str);
10331 if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return str;
10332 if (case_option_single_p(flags, enc, str)) {
10333 ret = rb_str_new(RSTRING_PTR(str), RSTRING_LEN(str));
10334 str_enc_copy_direct(ret, str);
10335 capitalize_single(ret);
10336 }
10337 else if (flags&ONIGENC_CASE_ASCII_ONLY) {
10338 ret = rb_str_new(0, RSTRING_LEN(str));
10339 rb_str_ascii_casemap(str, ret, &flags, enc);
10340 }
10341 else {
10342 ret = rb_str_casemap(str, &flags, enc);
10343 }
10344 return ret;
10345}
10346
10347
10348/*
10349 * call-seq:
10350 * swapcase!(mapping) -> self or nil
10351 *
10352 * Like String#swapcase, except that:
10353 *
10354 * - Changes are made to +self+, not to copy of +self+.
10355 * - Returns +self+ if any changes are made, +nil+ otherwise.
10356 *
10357 * Related: see {Modifying}[rdoc-ref:String@Modifying].
10358 */
10359
10360static VALUE
10361rb_str_swapcase_bang(int argc, VALUE *argv, VALUE str)
10362{
10363 rb_encoding *enc;
10364 OnigCaseFoldType flags = ONIGENC_CASE_UPCASE | ONIGENC_CASE_DOWNCASE;
10365
10366 flags = check_case_options(argc, argv, flags);
10367 str_modify_keep_cr(str);
10368 enc = str_true_enc(str);
10369 if (flags&ONIGENC_CASE_ASCII_ONLY)
10370 rb_str_ascii_casemap(str, str, &flags, enc);
10371 else
10372 str_shared_replace(str, rb_str_casemap(str, &flags, enc));
10373
10374 if (ONIGENC_CASE_MODIFIED&flags) return str;
10375 return Qnil;
10376}
10377
10378
10379/*
10380 * call-seq:
10381 * swapcase(mapping) -> new_string
10382 *
10383 * Returns a string containing the characters in +self+, with cases reversed:
10384 *
10385 * - Each uppercase character is downcased.
10386 * - Each lowercase character is upcased.
10387 *
10388 * Examples:
10389 *
10390 * 'Hello'.swapcase # => "hELLO"
10391 * 'Straße'.swapcase # => "sTRASSE"
10392 * 'RubyGems.org'.swapcase # => "rUBYgEMS.ORG"
10393 *
10394 * The sizes of +self+ and the upcased result may differ:
10395 *
10396 * s = 'Straße'
10397 * s.size # => 6
10398 * s.swapcase # => "sTRASSE"
10399 * s.swapcase.size # => 7
10400 *
10401 * Some characters (and some character sets) do not have upcase and downcase versions;
10402 * see {Case Mapping}[rdoc-ref:case_mapping.rdoc]:
10403 *
10404 * s = '1, 2, 3, ...'
10405 * s.swapcase == s # => true
10406 * s = 'こんにちは'
10407 * s.swapcase == s # => true
10408 *
10409 * The casing is affected by the given +mapping+,
10410 * which may be +:ascii+, +:fold+, or +:turkic+;
10411 * see {Case Mappings}[rdoc-ref:case_mapping.rdoc@Case+Mappings].
10412 *
10413 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
10414 *
10415 */
10416
10417static VALUE
10418rb_str_swapcase(int argc, VALUE *argv, VALUE str)
10419{
10420 rb_encoding *enc;
10421 OnigCaseFoldType flags = ONIGENC_CASE_UPCASE | ONIGENC_CASE_DOWNCASE;
10422 VALUE ret;
10423
10424 flags = check_case_options(argc, argv, flags);
10425 enc = str_true_enc(str);
10426 if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return str_duplicate(rb_cString, str);
10427 if (flags&ONIGENC_CASE_ASCII_ONLY) {
10428 ret = rb_str_new(0, RSTRING_LEN(str));
10429 rb_str_ascii_casemap(str, ret, &flags, enc);
10430 }
10431 else {
10432 ret = rb_str_casemap(str, &flags, enc);
10433 }
10434 return ret;
10435}
10436
10437typedef unsigned char *USTR;
10438
10439struct tr {
10440 int gen;
10441 unsigned int now, max;
10442 const char *p, *pend;
10443};
10444
10445static unsigned int
10446trnext(struct tr *t, rb_encoding *enc)
10447{
10448 int n;
10449
10450 for (;;) {
10451 nextpart:
10452 if (!t->gen) {
10453 if (t->p == t->pend) return -1;
10454 if (rb_enc_ascget(t->p, t->pend, &n, enc) == '\\' && t->p + n < t->pend) {
10455 t->p += n;
10456 }
10457 t->now = rb_enc_codepoint_len(t->p, t->pend, &n, enc);
10458 t->p += n;
10459 if (rb_enc_ascget(t->p, t->pend, &n, enc) == '-' && t->p + n < t->pend) {
10460 t->p += n;
10461 if (t->p < t->pend) {
10462 unsigned int c = rb_enc_codepoint_len(t->p, t->pend, &n, enc);
10463 t->p += n;
10464 if (t->now > c) {
10465 if (t->now < 0x80 && c < 0x80) {
10466 rb_raise(rb_eArgError,
10467 "invalid range \"%c-%c\" in string transliteration",
10468 t->now, c);
10469 }
10470 else {
10471 rb_raise(rb_eArgError, "invalid range in string transliteration");
10472 }
10473 continue; /* not reached */
10474 }
10475 else if (t->now < c) {
10476 t->gen = 1;
10477 t->max = c;
10478 }
10479 }
10480 }
10481 return t->now;
10482 }
10483 else {
10484 while (ONIGENC_CODE_TO_MBCLEN(enc, ++t->now) <= 0) {
10485 if (t->now == t->max) {
10486 t->gen = 0;
10487 goto nextpart;
10488 }
10489 }
10490 if (t->now < t->max) {
10491 return t->now;
10492 }
10493 else {
10494 t->gen = 0;
10495 return t->max;
10496 }
10497 }
10498 }
10499}
10500
10501static VALUE rb_str_delete_bang(int,VALUE*,VALUE);
10502
10503static VALUE
10504tr_trans(VALUE str, VALUE src, VALUE repl, int sflag)
10505{
10506 const unsigned int errc = -1;
10507 unsigned int trans[256];
10508 rb_encoding *enc, *e1, *e2;
10509 struct tr trsrc, trrepl;
10510 int cflag = 0;
10511 unsigned int c, c0, last = 0;
10512 int modify = 0, i, l;
10513 unsigned char *s, *send;
10514 VALUE hash = 0;
10515 int singlebyte = single_byte_optimizable(str);
10516 int termlen;
10517 int cr;
10518
10519#define CHECK_IF_ASCII(c) \
10520 (void)((cr == ENC_CODERANGE_7BIT && !rb_isascii(c)) ? \
10521 (cr = ENC_CODERANGE_VALID) : 0)
10522
10523 StringValue(src);
10524 StringValue(repl);
10525 if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return Qnil;
10526 if (RSTRING_LEN(repl) == 0) {
10527 return rb_str_delete_bang(1, &src, str);
10528 }
10529
10530 cr = ENC_CODERANGE(str);
10531 e1 = rb_enc_check(str, src);
10532 e2 = rb_enc_check(str, repl);
10533 if (e1 == e2) {
10534 enc = e1;
10535 }
10536 else {
10537 enc = rb_enc_check(src, repl);
10538 }
10539 trsrc.p = RSTRING_PTR(src); trsrc.pend = trsrc.p + RSTRING_LEN(src);
10540 if (RSTRING_LEN(src) > 1 &&
10541 rb_enc_ascget(trsrc.p, trsrc.pend, &l, enc) == '^' &&
10542 trsrc.p + l < trsrc.pend) {
10543 cflag = 1;
10544 trsrc.p += l;
10545 }
10546 trrepl.p = RSTRING_PTR(repl);
10547 trrepl.pend = trrepl.p + RSTRING_LEN(repl);
10548 trsrc.gen = trrepl.gen = 0;
10549 trsrc.now = trrepl.now = 0;
10550 trsrc.max = trrepl.max = 0;
10551
10552 if (cflag) {
10553 for (i=0; i<256; i++) {
10554 trans[i] = 1;
10555 }
10556 while ((c = trnext(&trsrc, enc)) != errc) {
10557 if (c < 256) {
10558 trans[c] = errc;
10559 }
10560 else {
10561 if (!hash) hash = rb_hash_new();
10562 rb_hash_aset(hash, UINT2NUM(c), Qtrue);
10563 }
10564 }
10565 while ((c = trnext(&trrepl, enc)) != errc)
10566 /* retrieve last replacer */;
10567 last = trrepl.now;
10568 for (i=0; i<256; i++) {
10569 if (trans[i] != errc) {
10570 trans[i] = last;
10571 }
10572 }
10573 }
10574 else {
10575 unsigned int r;
10576
10577 for (i=0; i<256; i++) {
10578 trans[i] = errc;
10579 }
10580 while ((c = trnext(&trsrc, enc)) != errc) {
10581 r = trnext(&trrepl, enc);
10582 if (r == errc) r = trrepl.now;
10583 if (c < 256) {
10584 trans[c] = r;
10585 if (rb_enc_codelen(r, enc) != 1) singlebyte = 0;
10586 }
10587 else {
10588 if (!hash) hash = rb_hash_new();
10589 rb_hash_aset(hash, UINT2NUM(c), UINT2NUM(r));
10590 }
10591 }
10592 }
10593
10594 if (cr == ENC_CODERANGE_VALID && rb_enc_asciicompat(e1))
10595 cr = ENC_CODERANGE_7BIT;
10596 str_modify_keep_cr(str);
10597 s = (unsigned char *)RSTRING_PTR(str); send = (unsigned char *)RSTRING_END(str);
10598 termlen = rb_enc_mbminlen(enc);
10599 if (sflag) {
10600 int clen, tlen;
10601 rb_len_t offset, max = RSTRING_LEN(str);
10602 unsigned int save = -1;
10603 unsigned char *buf = ALLOC_N(unsigned char, max + termlen), *t = buf;
10604
10605 while (s < send) {
10606 int may_modify = 0;
10607
10608 int r = rb_enc_precise_mbclen((char *)s, (char *)send, e1);
10609 if (!MBCLEN_CHARFOUND_P(r)) {
10610 SIZED_FREE_N(buf, max + termlen);
10611 rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(e1));
10612 }
10613 clen = MBCLEN_CHARFOUND_LEN(r);
10614 c0 = c = rb_enc_mbc_to_codepoint((char *)s, (char *)send, e1);
10615
10616 tlen = enc == e1 ? clen : rb_enc_codelen(c, enc);
10617
10618 s += clen;
10619 if (c < 256) {
10620 c = trans[c];
10621 }
10622 else if (hash) {
10623 VALUE tmp = rb_hash_lookup(hash, UINT2NUM(c));
10624 if (NIL_P(tmp)) {
10625 if (cflag) c = last;
10626 else c = errc;
10627 }
10628 else if (cflag) c = errc;
10629 else c = NUM2INT(tmp);
10630 }
10631 else {
10632 c = errc;
10633 }
10634 if (c != (unsigned int)-1) {
10635 if (save == c) {
10636 CHECK_IF_ASCII(c);
10637 continue;
10638 }
10639 save = c;
10640 tlen = rb_enc_codelen(c, enc);
10641 modify = 1;
10642 }
10643 else {
10644 save = -1;
10645 c = c0;
10646 if (enc != e1) may_modify = 1;
10647 }
10648 if ((offset = t - buf) + tlen > max) {
10649 size_t MAYBE_UNUSED(old) = max + termlen;
10650 max = offset + tlen + (send - s);
10651 SIZED_REALLOC_N(buf, unsigned char, max + termlen, old);
10652 t = buf + offset;
10653 }
10654 rb_enc_mbcput(c, t, enc);
10655 if (may_modify && memcmp(s, t, tlen) != 0) {
10656 modify = 1;
10657 }
10658 CHECK_IF_ASCII(c);
10659 t += tlen;
10660 }
10661 if (!STR_EMBED_P(str)) {
10662 SIZED_FREE_N(STR_HEAP_PTR(str), STR_HEAP_SIZE(str));
10663 }
10664 TERM_FILL((char *)t, termlen);
10665 RSTRING(str)->as.heap.ptr = (char *)buf;
10666 STR_SET_LEN(str, t - buf);
10667 STR_SET_NOEMBED(str);
10668 RSTRING(str)->as.heap.aux.capa = max;
10669 }
10670 else if (rb_enc_mbmaxlen(enc) == 1 || (singlebyte && !hash)) {
10671 while (s < send) {
10672 c = (unsigned char)*s;
10673 if (trans[c] != errc) {
10674 if (!cflag) {
10675 c = trans[c];
10676 *s = c;
10677 modify = 1;
10678 }
10679 else {
10680 *s = last;
10681 modify = 1;
10682 }
10683 }
10684 CHECK_IF_ASCII(c);
10685 s++;
10686 }
10687 }
10688 else {
10689 int clen, tlen;
10690 rb_len_t offset, max = (rb_len_t)((send - s) * 1.2);
10691 unsigned char *buf = ALLOC_N(unsigned char, max + termlen), *t = buf;
10692
10693 while (s < send) {
10694 int may_modify = 0;
10695
10696 int r = rb_enc_precise_mbclen((char *)s, (char *)send, e1);
10697 if (!MBCLEN_CHARFOUND_P(r)) {
10698 SIZED_FREE_N(buf, max + termlen);
10699 rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(e1));
10700 }
10701 clen = MBCLEN_CHARFOUND_LEN(r);
10702 c0 = c = rb_enc_mbc_to_codepoint((char *)s, (char *)send, e1);
10703
10704 tlen = enc == e1 ? clen : rb_enc_codelen(c, enc);
10705
10706 if (c < 256) {
10707 c = trans[c];
10708 }
10709 else if (hash) {
10710 VALUE tmp = rb_hash_lookup(hash, UINT2NUM(c));
10711 if (NIL_P(tmp)) {
10712 if (cflag) c = last;
10713 else c = errc;
10714 }
10715 else if (cflag) c = errc;
10716 else c = NUM2INT(tmp);
10717 }
10718 else {
10719 c = cflag ? last : errc;
10720 }
10721 if (c != errc) {
10722 tlen = rb_enc_codelen(c, enc);
10723 modify = 1;
10724 }
10725 else {
10726 c = c0;
10727 if (enc != e1) may_modify = 1;
10728 }
10729 if ((offset = t - buf) + tlen > max) {
10730 size_t MAYBE_UNUSED(old) = max + termlen;
10731 max = offset + tlen + (rb_len_t)((send - s) * 1.2);
10732 SIZED_REALLOC_N(buf, unsigned char, max + termlen, old);
10733 t = buf + offset;
10734 }
10735
10736 rb_enc_mbcput(c, t, enc);
10737 if (may_modify && memcmp(s, t, tlen) != 0) {
10738 modify = 1;
10739 }
10740 CHECK_IF_ASCII(c);
10741 s += clen;
10742 t += tlen;
10743 }
10744 if (!STR_EMBED_P(str)) {
10745 SIZED_FREE_N(STR_HEAP_PTR(str), STR_HEAP_SIZE(str));
10746 }
10747 TERM_FILL((char *)t, termlen);
10748 RSTRING(str)->as.heap.ptr = (char *)buf;
10749 STR_SET_LEN(str, t - buf);
10750 STR_SET_NOEMBED(str);
10751 RSTRING(str)->as.heap.aux.capa = max;
10752 }
10753
10754 if (modify) {
10755 if (cr != ENC_CODERANGE_BROKEN)
10756 ENC_CODERANGE_SET(str, cr);
10757 rb_enc_associate(str, enc);
10758 return str;
10759 }
10760 return Qnil;
10761}
10762
10764 unsigned char *buf;
10765 unsigned char *ptr;
10766 size_t capa;
10767 size_t initial_capa;
10768};
10769
10770static inline void
10771tr_buffer_init(struct tr_buffer *buffer, size_t initial_capa)
10772{
10773 if (initial_capa < 32) {
10774 initial_capa = 32;
10775 }
10776 *buffer = (struct tr_buffer){ .initial_capa = initial_capa };
10777}
10778
10779static inline void
10780tr_buffer_ensure_capa(struct tr_buffer *buffer, size_t extra_capa)
10781{
10782 size_t offset = buffer->ptr - buffer->buf;
10783 size_t required_capa = offset + extra_capa;
10784 if (UNLIKELY(buffer->capa < required_capa)) {
10785 size_t new_capa = buffer->capa ? buffer->capa : buffer->initial_capa;
10786 RUBY_ASSERT(new_capa >= 32); // Lower would cause infinite loop
10787 while (new_capa < required_capa) {
10788 new_capa = (size_t)(new_capa * 1.2);
10789 }
10790 SIZED_REALLOC_N(buffer->buf, unsigned char, new_capa, buffer->capa);
10791 buffer->ptr = buffer->buf + offset;
10792 buffer->capa = new_capa;
10793 }
10794}
10795
10796static inline void
10797tr_buffer_append(struct tr_buffer *buffer, const unsigned char *ptr, size_t len)
10798{
10799 if (len) {
10800 tr_buffer_ensure_capa(buffer, len);
10801 memcpy(buffer->ptr, ptr, len);
10802 buffer->ptr += len;
10803 }
10804}
10805
10806static inline void
10807tr_buffer_append_str(struct tr_buffer *buffer, VALUE str)
10808{
10809 tr_buffer_append(buffer, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str));
10810}
10811
10812static inline void
10813tr_buffer_mbcput(struct tr_buffer *buffer, int codepoint, rb_encoding *enc)
10814{
10815 tr_buffer_ensure_capa(buffer, rb_enc_mbmaxlen(enc));
10816 buffer->ptr += rb_enc_mbcput(codepoint, buffer->ptr, enc);
10817}
10818
10819static inline void
10820tr_buffer_free(struct tr_buffer *buffer)
10821{
10822 if (buffer->buf) {
10823 SIZED_FREE_N(buffer->buf, buffer->capa);
10824 }
10825}
10826
10827struct tr_pair {
10828 VALUE search;
10829 VALUE replace;
10830};
10831
10833 struct tr_pair *pairs;
10834 size_t index;
10835 rb_encoding *enc;
10836 int cr;
10837};
10838
10839static int
10840tr_trans_pairs_coerce_i(st_data_t key, st_data_t value, st_data_t _args)
10841{
10842 struct tr_trans_pairs_coerce_args *args = (struct tr_trans_pairs_coerce_args *)_args;
10843 struct tr_pair *pair = &args->pairs[args->index];
10844 args->index++;
10845
10846 VALUE search = (VALUE)key;
10847 VALUE replace = (VALUE)value;
10848 StringValue(search);
10849 StringValue(replace);
10850
10851 if (RSTRING_LEN(search) != 1 && str_strlen(search, NULL) != 1) {
10852 rb_raise(rb_eArgError, "keys must be of size 1"); // TODO: better error message
10853 }
10854
10855 args->enc = rb_enc_check_multi_str(args->enc, &args->cr, search);
10856 args->enc = rb_enc_check_multi_str(args->enc, &args->cr, replace);
10857
10858 pair->search = search;
10859 pair->replace = replace;
10860 return ST_CONTINUE;
10861}
10862
10863#define TR_TRANS_PAIRS_SIMD_MAX_NEEDLES 16
10864
10866 const unsigned char *s;
10867 const unsigned char *send;
10868
10869#ifdef HAVE_SIMD
10870 unsigned int needles_count;
10871#ifdef HAVE_SIMD_NEON
10872 uint64_t matches_bitmap;
10873 uint8x16_t needle_masks[TR_TRANS_PAIRS_SIMD_MAX_NEEDLES];
10874#endif
10875#ifdef HAVE_SIMD_SSE2
10876 int matches_bitmap;
10877 __m128i needle_masks[TR_TRANS_PAIRS_SIMD_MAX_NEEDLES];
10878#endif
10879#endif
10880
10881 VALUE trans_table[256];
10882};
10883
10884static inline VALUE
10885tr_trans_pairs_search_basic(struct tr_trans_pairs_search *search)
10886{
10887 while (search->s < search->send) {
10888 VALUE repl = search->trans_table[*search->s];
10889 if (UNLIKELY(repl)) {
10890 return repl;
10891 }
10892
10893 search->s++;
10894 }
10895
10896 return 0;
10897}
10898
10899#ifdef HAVE_SIMD_SSE2
10900static inline VALUE
10901tr_trans_pairs_next_match_sse2(struct tr_trans_pairs_search *search)
10902{
10903 RUBY_ASSERT(search->matches_bitmap > 0);
10904 size_t trailing_zeros = (size_t)ntz_int32(search->matches_bitmap);
10905
10906 RUBY_ASSERT(trailing_zeros < (sizeof(search->matches_bitmap) * CHAR_BIT));
10907 search->matches_bitmap >>= trailing_zeros;
10908 search->s += trailing_zeros;
10909
10910 RUBY_ASSERT(search->s <= search->send);
10911 return search->trans_table[*search->s];
10912}
10913
10914static inline VALUE
10915tr_trans_pairs_search_sse2(struct tr_trans_pairs_search *search)
10916{
10917 const unsigned int needles_count = search->needles_count;
10918 if (needles_count) {
10919 RBIMPL_ASSERT_OR_ASSUME(needles_count <= TR_TRANS_PAIRS_SIMD_MAX_NEEDLES);
10920
10921 if (search->matches_bitmap) {
10922 return tr_trans_pairs_next_match_sse2(search);
10923 }
10924
10925 if ((size_t)(search->send - search->s) >= sizeof(__m128i)) {
10926 unsigned int i;
10927
10928 do {
10929 const __m128i bytes = _mm_loadu_si128((__m128i const *)search->s);
10930
10931 __m128i matches[TR_TRANS_PAIRS_SIMD_MAX_NEEDLES];
10932 for (i = 0; i < needles_count; i++) {
10933 matches[i] = _mm_cmpeq_epi8(bytes, search->needle_masks[i]);
10934 }
10935
10936 for (i = 1; i < needles_count; i++) {
10937 matches[0] = _mm_or_si128(matches[0], matches[i]);
10938 }
10939
10940 const int bitmap = _mm_movemask_epi8(matches[0]);
10941
10942 if (bitmap) {
10943 search->matches_bitmap = bitmap;
10944 return tr_trans_pairs_next_match_sse2(search);
10945 }
10946 search->s += sizeof(__m128i);
10947 } while ((size_t)(search->send - search->s) >= sizeof(__m128i));
10948 }
10949 }
10950 return tr_trans_pairs_search_basic(search);
10951}
10952
10953#define tr_trans_pairs_search_impl tr_trans_pairs_search_sse2
10954#endif
10955
10956#ifdef HAVE_SIMD_NEON
10957static inline VALUE
10958tr_trans_pairs_next_match_neon(struct tr_trans_pairs_search *search)
10959{
10960 RUBY_ASSERT(search->matches_bitmap > 0);
10961 size_t trailing_zeros = (size_t)ntz_int64(search->matches_bitmap);
10962
10963 // uint64_t >>= 64 would be undefined behaviour
10964 RUBY_ASSERT(trailing_zeros < (sizeof(search->matches_bitmap) * CHAR_BIT));
10965 search->matches_bitmap >>= trailing_zeros;
10966 search->s += trailing_zeros / 4;
10967
10968 RUBY_ASSERT(search->s <= search->send);
10969 return search->trans_table[*search->s];
10970}
10971
10972static inline VALUE
10973tr_trans_pairs_search_neon(struct tr_trans_pairs_search *search)
10974{
10975 const unsigned int needles_count = search->needles_count;
10976 if (needles_count) {
10977 RBIMPL_ASSERT_OR_ASSUME(needles_count <= TR_TRANS_PAIRS_SIMD_MAX_NEEDLES);
10978
10979 if (search->matches_bitmap) {
10980 return tr_trans_pairs_next_match_neon(search);
10981 }
10982
10983 if ((size_t)(search->send - search->s) >= sizeof(uint8x16_t)) {
10984 unsigned int i;
10985
10986 do {
10987 const uint8x16_t bytes = vld1q_u8(search->s);
10988
10989 uint8x16_t matches[TR_TRANS_PAIRS_SIMD_MAX_NEEDLES];
10990 for (i = 0; i < needles_count; i++) {
10991 matches[i] = vceqq_u8(bytes, search->needle_masks[i]);
10992 }
10993
10994 for (i = 1; i < needles_count; i++) {
10995 matches[0] = vorrq_u8(matches[0], matches[i]);
10996 }
10997
10998 const uint8x8_t res = vshrn_n_u16(vreinterpretq_u16_u8(matches[0]), 4);
10999 const uint64_t bitmap = vget_lane_u64(vreinterpret_u64_u8(res), 0);
11000
11001 if (bitmap) {
11002 search->matches_bitmap = bitmap & 0x8888888888888888ull;
11003 return tr_trans_pairs_next_match_neon(search);
11004 }
11005 search->s += sizeof(uint8x16_t);
11006 } while ((size_t)(search->send - search->s) >= sizeof(uint8x16_t));
11007 }
11008 }
11009 return tr_trans_pairs_search_basic(search);
11010}
11011
11012#define tr_trans_pairs_search_impl tr_trans_pairs_search_neon
11013#endif
11014
11015#ifndef tr_trans_pairs_search_impl
11016#define tr_trans_pairs_search_impl tr_trans_pairs_search_basic
11017#endif
11018
11019static inline void
11020tr_trans_pairs_consume_match(struct tr_trans_pairs_search *search)
11021{
11022 search->s++;
11023#ifdef HAVE_SIMD
11024 search->matches_bitmap >>= 1;
11025#endif
11026}
11027
11028static VALUE
11029tr_trans_pairs_search_prepare(struct tr_trans_pairs_search *search, size_t pairs_count, struct tr_pair *pairs, rb_encoding *enc)
11030{
11031 VALUE hash = 0;
11032 for (size_t index = 0; index < pairs_count; index++) {
11033 struct tr_pair *pair = &pairs[index];
11034
11035 char *ptr = RSTRING_PTR(pair->search);
11036 unsigned int codepoint = rb_enc_mbc_to_codepoint(ptr, RSTRING_END(pair->search), enc);
11037
11038 const unsigned char first_byte = (unsigned char)*ptr;
11039
11040#ifdef HAVE_SIMD
11041 if (pairs_count <= TR_TRANS_PAIRS_SIMD_MAX_NEEDLES) {
11042#ifdef HAVE_SIMD_NEON
11043 search->needle_masks[index] = vdupq_n_u8(first_byte);
11044#endif
11045#ifdef HAVE_SIMD_SSE2
11046 search->needle_masks[index] = _mm_set1_epi8(first_byte);
11047#endif
11048 search->needles_count++;
11049 }
11050#endif
11051
11052 if (rb_enc_codelen(codepoint, enc) == 1) {
11053 search->trans_table[first_byte] = pair->replace;
11054 }
11055 else {
11056 search->trans_table[first_byte] = Qundef;
11057 if (!hash) {
11058 hash = rb_obj_hide(rb_hash_new_capa(pairs_count));
11059 }
11060 rb_hash_aset(hash, UINT2NUM(codepoint), pair->replace);
11061 }
11062 }
11063 return hash;
11064}
11065
11066static VALUE
11067tr_trans_pairs(VALUE str, VALUE pairs_val)
11068{
11069 Check_Type(pairs_val, T_HASH);
11070 size_t pairs_count = RHASH_SIZE(pairs_val);
11071 mustnot_broken(str);
11072 rb_str_modify(str);
11073
11074 if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str) || pairs_count == 0) return Qnil;
11075
11076 VALUE pairs_handle;
11077 struct tr_pair *pairs = ALLOCV_N(struct tr_pair, pairs_handle, pairs_count);
11078
11079 int cr = rb_enc_str_coderange(str);
11080 rb_encoding *enc = rb_str_enc_get(str);
11081
11082 struct tr_trans_pairs_coerce_args coerce_args = {
11083 .pairs = pairs,
11084 .enc = enc,
11085 .cr = cr,
11086 };
11087 rb_hash_foreach(pairs_val, tr_trans_pairs_coerce_i, (VALUE)&coerce_args);
11088 rb_encoding *e1 = coerce_args.enc;
11089
11090 /* Keys could be deleted from pairs_val during rb_hash_foreach when coercing
11091 * the keys/values, so we need to update pairs_count to the number of pairs we
11092 * were actually able to extract from pairs_val. */
11093 pairs_count = coerce_args.index;
11094
11095 VALUE hash = 0;
11096
11097 const unsigned char *sstart = (unsigned char *)RSTRING_PTR(str);
11098 rb_len_t str_len = RSTRING_LEN(str);
11099 int termlen = rb_enc_mbminlen(e1);
11100
11101 struct tr_buffer buffer;
11102 tr_buffer_init(&buffer, str_len);
11103 bool modify = false;
11104
11105 if (RB_LIKELY(rb_str_encindex_fastpath(rb_enc_to_index(e1)))) {
11106
11107 struct tr_trans_pairs_search search = {
11108 .s = sstart,
11109 .send = sstart + str_len,
11110 };
11111
11112 hash = tr_trans_pairs_search_prepare(&search, pairs_count, pairs, e1);
11113
11114 const unsigned char *checkpoint = search.s;
11115 VALUE repl;
11116 while ((repl = tr_trans_pairs_search_impl(&search))) {
11117 int clen = 1;
11118
11119 if (UNLIKELY(repl == Qundef)) {
11120 unsigned int c = rb_enc_mbc_to_codepoint((char *)search.s, (char *)search.send, e1);
11121 clen = rb_enc_codelen(c, e1);
11122 repl = rb_hash_lookup2(hash, UINT2NUM(c), 0);
11123 if (!repl) {
11124 tr_trans_pairs_consume_match(&search);
11125 continue;
11126 }
11127 }
11129
11130 modify = true;
11131
11132 if (checkpoint < search.s) {
11133 tr_buffer_append(&buffer, checkpoint, search.s - checkpoint);
11134 }
11135 tr_buffer_append_str(&buffer, repl);
11136 checkpoint = search.s + clen;
11137 tr_trans_pairs_consume_match(&search);
11138
11139 if (cr == ENC_CODERANGE_7BIT && rb_enc_str_coderange(repl) != ENC_CODERANGE_7BIT) {
11141 }
11142 }
11143
11144 if (modify && checkpoint < search.s) {
11145 tr_buffer_append(&buffer, checkpoint, search.s - checkpoint);
11146 }
11147 }
11148 else {
11149 const unsigned char *s = sstart;
11150 const unsigned char *send = sstart + str_len;
11151
11152 hash = rb_obj_hide(rb_hash_new_capa(pairs_count));
11153
11154 for (size_t index = 0; index < pairs_count; index++) {
11155 struct tr_pair *pair = &pairs[index];
11156
11157 unsigned int codepoint = rb_enc_mbc_to_codepoint(RSTRING_PTR(pair->search), RSTRING_END(pair->search), e1);
11158 rb_hash_aset(hash, UINT2NUM(codepoint), pair->replace);
11159 }
11160
11161 while (s < send) {
11162 bool may_modify = false;
11163
11164 int r = rb_enc_precise_mbclen((char *)s, (char *)send, e1);
11165 if (!MBCLEN_CHARFOUND_P(r)) {
11166 tr_buffer_free(&buffer);
11167 rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(e1));
11168 }
11169 int clen = MBCLEN_CHARFOUND_LEN(r);
11170 unsigned int c = rb_enc_mbc_to_codepoint((char *)s, (char *)send, e1);
11171 unsigned int c0 = c;
11172
11173 rb_len_t tlen = enc == e1 ? clen : rb_enc_codelen(c, e1);
11174
11175 VALUE replacement = rb_hash_lookup(hash, UINT2NUM(c));
11176 if (NIL_P(replacement)) {
11177 tlen = enc == e1 ? clen : rb_enc_codelen(c, enc);
11178 c = c0;
11179 if (enc != e1) may_modify = true;
11180 }
11181 else {
11182 tlen = RSTRING_LEN(replacement);
11183 modify = true;
11184 }
11185
11186 if (NIL_P(replacement)) {
11187 tr_buffer_mbcput(&buffer, c, enc);
11188 }
11189 else {
11190 tr_buffer_append_str(&buffer, replacement);
11191 }
11192
11193 if (may_modify && memcmp(s, buffer.ptr - tlen, tlen) != 0) {
11194 modify = true;
11195 }
11196
11197 if (cr == ENC_CODERANGE_7BIT && !rb_isascii(c)) {
11199 }
11200
11201 s += clen;
11202 }
11203 }
11204
11205 ALLOCV_END(pairs_handle);
11206
11207 if (!modify) {
11208 tr_buffer_free(&buffer);
11209 return Qnil;
11210 }
11211
11212 if (!STR_EMBED_P(str)) {
11213 SIZED_FREE_N(STR_HEAP_PTR(str), STR_HEAP_SIZE(str));
11214 }
11215 tr_buffer_ensure_capa(&buffer, termlen);
11216 TERM_FILL((char *)buffer.ptr, termlen);
11217 RSTRING(str)->as.heap.ptr = (char *)buffer.buf;
11218 STR_SET_LEN(str, buffer.ptr - buffer.buf);
11219 STR_SET_NOEMBED(str);
11220 RSTRING(str)->as.heap.aux.capa = buffer.capa - termlen;
11221
11222 RB_GC_GUARD(hash);
11223
11224 if (cr != ENC_CODERANGE_BROKEN)
11225 ENC_CODERANGE_SET(str, cr);
11226 rb_enc_associate(str, e1);
11227 return str;
11228}
11229
11230/*
11231 * call-seq:
11232 * tr!(selector, replacements) -> self or nil
11233 * tr!(pairs) -> self or nil
11234 *
11235 * Like String#tr, except:
11236 *
11237 * - Performs substitutions in +self+ (not in a copy of +self+).
11238 * - Returns +self+ if any modifications were made, +nil+ otherwise.
11239 *
11240 * Related: {Modifying}[rdoc-ref:String@Modifying].
11241 */
11242
11243static VALUE
11244rb_str_tr_bang(int argc, VALUE *argv, VALUE str)
11245{
11246 rb_check_arity(argc, 1, 2);
11247
11248 if (argc == 1) {
11249 VALUE pairs = argv[0];
11250 return tr_trans_pairs(str, pairs);
11251 }
11252
11253 VALUE src = argv[0], repl = argv[1];
11254 return tr_trans(str, src, repl, 0);
11255}
11256
11257
11258/*
11259 * call-seq:
11260 * tr(pairs) -> new_string
11261 * tr(selector, replacements) -> new_string
11262 *
11263 * With hash argument +pairs+ given,
11264 * returns a copy of +self+ with some codepoints replaced:
11265 *
11266 * - Each key in +pairs+ is a string contains a single codepoint.
11267 * - Each value in +pairs+ is a string of any length (even length zero).
11268 *
11269 * Each codepoint in +self+ is examined;
11270 * when the codepoint in +self+ matches a key codepoint in +pairs+,
11271 * the string value for that key replaces the codepoint in +self+.
11272 *
11273 * Examples:
11274 *
11275 * h = {'h' => 'H', 'e' => 'er', 'l' => '', 'o' => 'o!'}
11276 * 'hello'.tr(h) # => "Hero!"
11277 *
11278 * With string arguments +selector+ and +replacements+ given,
11279 * returns a copy of +self+
11280 * with each character specified by string +selector+ translated to the
11281 * corresponding character in string +replacements+.
11282 * The correspondence is _positional_:
11283 *
11284 * - Each occurrence of the first character specified by +selector+
11285 * is translated to the first character in +replacements+.
11286 * - Each occurrence of the second character specified by +selector+
11287 * is translated to the second character in +replacements+.
11288 * - And so on.
11289 *
11290 * Example:
11291 *
11292 * 'hello'.tr('el', 'ip') #=> "hippo"
11293 *
11294 * If +replacements+ is shorter than +selector+,
11295 * it is implicitly padded with its own last character:
11296 *
11297 * 'hello'.tr('aeiou', '-') # => "h-ll-"
11298 * 'hello'.tr('aeiou', 'AA-') # => "hAll-"
11299 *
11300 * Arguments +selector+ and +replacements+ must be valid character selectors
11301 * (see {Character Selectors}[rdoc-ref:character_selectors.rdoc]),
11302 * and may use any of its valid forms, including negation, ranges, and escapes:
11303 *
11304 * 'hello'.tr('^aeiou', '-') # => "-e--o" # Negation.
11305 * 'ibm'.tr('b-z', 'a-z') # => "hal" # Range.
11306 * 'hel^lo'.tr('\^aeiou', '-') # => "h-l-l-" # Escaped leading caret.
11307 * 'i-b-m'.tr('b\-z', 'a-z') # => "ibabm" # Escaped embedded hyphen.
11308 * 'foo\\bar'.tr('ab\\', 'XYZ') # => "fooZYXr" # Escaped backslash.
11309 *
11310 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
11311 */
11312
11313static VALUE
11314rb_str_tr(int argc, VALUE *argv, VALUE str)
11315{
11316 rb_check_arity(argc, 1, 2);
11317
11318 str = str_duplicate(rb_cString, str);
11319
11320 if (argc == 1) {
11321 VALUE pairs = argv[0];
11322 VALUE result = tr_trans_pairs(str, pairs);
11323 if (NIL_P(result)) result = str;
11324 return str;
11325 }
11326
11327 VALUE src = argv[0], repl = argv[1];
11328 tr_trans(str, src, repl, 0);
11329 return str;
11330}
11331
11332#define TR_TABLE_MAX (UCHAR_MAX+1)
11333#define TR_TABLE_SIZE (TR_TABLE_MAX+1)
11334static void
11335tr_setup_table(VALUE str, char stable[TR_TABLE_SIZE], int first,
11336 VALUE *tablep, VALUE *ctablep, rb_encoding *enc)
11337{
11338 const unsigned int errc = -1;
11339 char buf[TR_TABLE_MAX];
11340 struct tr tr;
11341 unsigned int c;
11342 VALUE table = 0, ptable = 0;
11343 int i, l, cflag = 0;
11344
11345 tr.p = RSTRING_PTR(str); tr.pend = tr.p + RSTRING_LEN(str);
11346 tr.gen = tr.now = tr.max = 0;
11347
11348 if (RSTRING_LEN(str) > 1 && rb_enc_ascget(tr.p, tr.pend, &l, enc) == '^') {
11349 cflag = 1;
11350 tr.p += l;
11351 }
11352 if (first) {
11353 for (i=0; i<TR_TABLE_MAX; i++) {
11354 stable[i] = 1;
11355 }
11356 stable[TR_TABLE_MAX] = cflag;
11357 }
11358 else if (stable[TR_TABLE_MAX] && !cflag) {
11359 stable[TR_TABLE_MAX] = 0;
11360 }
11361 for (i=0; i<TR_TABLE_MAX; i++) {
11362 buf[i] = cflag;
11363 }
11364
11365 while ((c = trnext(&tr, enc)) != errc) {
11366 if (c < TR_TABLE_MAX) {
11367 buf[(unsigned char)c] = !cflag;
11368 }
11369 else {
11370 VALUE key = UINT2NUM(c);
11371
11372 if (!table && (first || *tablep || stable[TR_TABLE_MAX])) {
11373 if (cflag) {
11374 ptable = *ctablep;
11375 table = ptable ? ptable : rb_hash_new();
11376 *ctablep = table;
11377 }
11378 else {
11379 table = rb_hash_new();
11380 ptable = *tablep;
11381 *tablep = table;
11382 }
11383 }
11384 if (table && (!ptable || (cflag ^ !NIL_P(rb_hash_aref(ptable, key))))) {
11385 rb_hash_aset(table, key, Qtrue);
11386 }
11387 }
11388 }
11389 for (i=0; i<TR_TABLE_MAX; i++) {
11390 stable[i] = stable[i] && buf[i];
11391 }
11392 if (!table && !cflag) {
11393 *tablep = 0;
11394 }
11395}
11396
11397
11398static int
11399tr_find(unsigned int c, const char table[TR_TABLE_SIZE], VALUE del, VALUE nodel)
11400{
11401 if (c < TR_TABLE_MAX) {
11402 return table[c] != 0;
11403 }
11404 else {
11405 VALUE v = UINT2NUM(c);
11406
11407 if (del) {
11408 if (!NIL_P(rb_hash_lookup(del, v)) &&
11409 (!nodel || NIL_P(rb_hash_lookup(nodel, v)))) {
11410 return TRUE;
11411 }
11412 }
11413 else if (nodel && !NIL_P(rb_hash_lookup(nodel, v))) {
11414 return FALSE;
11415 }
11416 return table[TR_TABLE_MAX] ? TRUE : FALSE;
11417 }
11418}
11419
11420/*
11421 * call-seq:
11422 * delete!(*selectors) -> self or nil
11423 *
11424 * Like String#delete, but modifies +self+ in place;
11425 * returns +self+ if any characters were deleted, +nil+ otherwise.
11426 *
11427 * Related: see {Modifying}[rdoc-ref:String@Modifying].
11428 */
11429
11430static VALUE
11431rb_str_delete_bang(int argc, VALUE *argv, VALUE str)
11432{
11433 char squeez[TR_TABLE_SIZE];
11434 rb_encoding *enc = 0;
11435 char *s, *send, *t;
11436 VALUE del = 0, nodel = 0;
11437 int modify = 0;
11438 int i, ascompat, cr;
11439
11440 if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return Qnil;
11442 for (i=0; i<argc; i++) {
11443 VALUE s = argv[i];
11444
11445 StringValue(s);
11446 enc = rb_enc_check(str, s);
11447 tr_setup_table(s, squeez, i==0, &del, &nodel, enc);
11448 }
11449
11450 str_modify_keep_cr(str);
11451 ascompat = rb_enc_asciicompat(enc);
11452 s = t = RSTRING_PTR(str);
11453 send = RSTRING_END(str);
11454 cr = ascompat ? ENC_CODERANGE_7BIT : ENC_CODERANGE_VALID;
11455 while (s < send) {
11456 unsigned int c;
11457 int clen;
11458
11459 if (ascompat && (c = *(unsigned char*)s) < 0x80) {
11460 if (squeez[c]) {
11461 modify = 1;
11462 }
11463 else {
11464 if (t != s) *t = c;
11465 t++;
11466 }
11467 s++;
11468 }
11469 else {
11470 c = rb_enc_codepoint_len(s, send, &clen, enc);
11471
11472 if (tr_find(c, squeez, del, nodel)) {
11473 modify = 1;
11474 }
11475 else {
11476 if (t != s) rb_enc_mbcput(c, t, enc);
11477 t += clen;
11479 }
11480 s += clen;
11481 }
11482 }
11483 TERM_FILL(t, TERM_LEN(str));
11484 STR_SET_LEN(str, t - RSTRING_PTR(str));
11485 ENC_CODERANGE_SET(str, cr);
11486
11487 if (modify) return str;
11488 return Qnil;
11489}
11490
11491
11492/*
11493 * call-seq:
11494 * delete(*selectors) -> new_string
11495 *
11496 * Returns a new string that is a copy of +self+ with certain characters removed;
11497 * the removed characters are all instances of those specified by the given string +selectors+.
11498 *
11499 * For one 1-character selector,
11500 * removes all instances of that character:
11501 *
11502 * s = 'abracadabra'
11503 * s.delete('a') # => "brcdbr"
11504 * s.delete('b') # => "aracadara"
11505 * s.delete('x') # => "abracadabra"
11506 * s.delete('') # => "abracadabra"
11507 *
11508 * s = 'よろしくお願いします'
11509 * s.delete('よ') # => "ろしくお願いします"
11510 * s.delete('し') # => "よろくお願います"
11511 *
11512 * For one multi-character selector,
11513 * removes all instances of the specified characters:
11514 *
11515 * s = 'abracadabra'
11516 * s.delete('ab') # => "rcdr"
11517 * s.delete('abc') # => "rdr"
11518 * s.delete('abcd') # => "rr"
11519 * s.delete('abcdr') # => ""
11520 * s.delete('abcdrx') # => ""
11521 *
11522 * Order and repetition do not matter:
11523 *
11524 * s.delete('ba') == s.delete('ab') # => true
11525 * s.delete('baab') == s.delete('ab') # => true
11526 *
11527 * For multiple selectors,
11528 * forms a single selector that is the intersection of characters in all selectors
11529 * and removes all instances of characters specified by that selector:
11530 *
11531 * s = 'abcdefg'
11532 * s.delete('abcde', 'dcbfg') == s.delete('bcd') # => true
11533 * s.delete('abc', 'def') == s.delete('') # => true
11534 *
11535 * In a character selector, three characters get special treatment:
11536 *
11537 * - A caret (<tt>'^'</tt>) functions as a _negation_ operator
11538 * for the immediately following characters:
11539 *
11540 * s = 'abracadabra'
11541 * s.delete('^bc') # => "bcb" # Deletes all except 'b' and 'c'.
11542 *
11543 * - A hyphen (<tt>'-'</tt>) between two other characters defines a _range_ of characters:
11544 *
11545 * s = 'abracadabra'
11546 * s.delete('a-c') # => "rdr" # Deletes all 'a', 'b', and 'c'.
11547 *
11548 * - A backslash (<tt>'\'</tt>) acts as an escape for a caret, a hyphen,
11549 * or another backslash:
11550 *
11551 * s = 'abracadabra'
11552 * s.delete('\^bc') # => "araadara" # Deletes all '^', 'b', and 'c'.
11553 * s.delete('a\-c') # => "brdbr" # Deletes all 'a', '-', and 'c'.
11554 * 'foo\bar\baz'.delete('\\') # => "foobarbaz" # Deletes all '\'.
11555 *
11556 * These usages may be mixed:
11557 *
11558 * s = 'abracadabra'
11559 * s.delete('a-cq-t') # => "d" # Multiple ranges.
11560 * s.delete('ac-d') # => "brbr" # Range mixed with plain characters.
11561 * s.delete('^a-c') # => "abacaaba" # Range mixed with negation.
11562 *
11563 * For multiple selectors, all forms may be used, including negations, ranges, and escapes.
11564 *
11565 * s = 'abracadabra'
11566 * s.delete('^abc', '^def') == s.delete('^abcdef') # => true
11567 * s.delete('a-e', 'c-g') == s.delete('cde') # => true
11568 * s.delete('^abc', 'c-g') == s.delete('defg') # => true
11569 *
11570 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
11571 *
11572 */
11573
11574static VALUE
11575rb_str_delete(int argc, VALUE *argv, VALUE str)
11576{
11577 str = str_duplicate(rb_cString, str);
11578 rb_str_delete_bang(argc, argv, str);
11579 return str;
11580}
11581
11582
11583/*
11584 * call-seq:
11585 * squeeze!(*selectors) -> self or nil
11586 *
11587 * Like String#squeeze, except that:
11588 *
11589 * - Characters are squeezed in +self+ (not in a copy of +self+).
11590 * - Returns +self+ if any changes are made, +nil+ otherwise.
11591 *
11592 * Related: See {Modifying}[rdoc-ref:String@Modifying].
11593 */
11594
11595static VALUE
11596rb_str_squeeze_bang(int argc, VALUE *argv, VALUE str)
11597{
11598 char squeez[TR_TABLE_SIZE];
11599 rb_encoding *enc = 0;
11600 VALUE del = 0, nodel = 0;
11601 unsigned char *s, *send, *t;
11602 int i, modify = 0;
11603 int ascompat, singlebyte = single_byte_optimizable(str);
11604 unsigned int save;
11605
11606 if (argc == 0) {
11607 enc = STR_ENC_GET(str);
11608 }
11609 else {
11610 for (i=0; i<argc; i++) {
11611 VALUE s = argv[i];
11612
11613 StringValue(s);
11614 enc = rb_enc_check(str, s);
11615 if (singlebyte && !single_byte_optimizable(s))
11616 singlebyte = 0;
11617 tr_setup_table(s, squeez, i==0, &del, &nodel, enc);
11618 }
11619 }
11620
11621 str_modify_keep_cr(str);
11622 s = t = (unsigned char *)RSTRING_PTR(str);
11623 if (!s || RSTRING_LEN(str) == 0) return Qnil;
11624 send = (unsigned char *)RSTRING_END(str);
11625 save = -1;
11626 ascompat = rb_enc_asciicompat(enc);
11627
11628 if (singlebyte) {
11629 while (s < send) {
11630 unsigned int c = *s++;
11631 if (c != save || (argc > 0 && !squeez[c])) {
11632 *t++ = save = c;
11633 }
11634 }
11635 }
11636 else {
11637 while (s < send) {
11638 unsigned int c;
11639 int clen;
11640
11641 if (ascompat && (c = *s) < 0x80) {
11642 if (c != save || (argc > 0 && !squeez[c])) {
11643 *t++ = save = c;
11644 }
11645 s++;
11646 }
11647 else {
11648 c = rb_enc_codepoint_len((char *)s, (char *)send, &clen, enc);
11649
11650 if (c != save || (argc > 0 && !tr_find(c, squeez, del, nodel))) {
11651 if (t != s) rb_enc_mbcput(c, t, enc);
11652 save = c;
11653 t += clen;
11654 }
11655 s += clen;
11656 }
11657 }
11658 }
11659
11660 TERM_FILL((char *)t, TERM_LEN(str));
11661 if ((char *)t - RSTRING_PTR(str) != RSTRING_LEN(str)) {
11662 STR_SET_LEN(str, (char *)t - RSTRING_PTR(str));
11663 modify = 1;
11664 }
11665
11666 if (modify) return str;
11667 return Qnil;
11668}
11669
11670
11671/*
11672 * call-seq:
11673 * squeeze(*selectors) -> new_string
11674 *
11675 * Returns a copy of +self+ with each tuple (doubling, tripling, etc.) of specified characters
11676 * "squeezed" down to a single character.
11677 *
11678 * The tuples to be squeezed are specified by arguments +selectors+,
11679 * each of which is a string;
11680 * see {Character Selectors}[rdoc-ref:character_selectors.rdoc@Character+Selectors].
11681 *
11682 * A single argument may be a single character:
11683 *
11684 * 'Noooooo!'.squeeze('o') # => "No!"
11685 * 'foo bar baz'.squeeze(' ') # => "foo bar baz"
11686 * 'Mississippi'.squeeze('s') # => "Misisippi"
11687 * 'Mississippi'.squeeze('p') # => "Mississipi"
11688 * 'Mississippi'.squeeze('x') # => "Mississippi" # Unused selector character is ignored.
11689 * 'бессонница'.squeeze('с') # => "бесонница"
11690 * 'бессонница'.squeeze('н') # => "бессоница"
11691 *
11692 * A single argument may be a string of characters:
11693 *
11694 * 'Mississippi'.squeeze('sp') # => "Misisipi"
11695 * 'Mississippi'.squeeze('ps') # => "Misisipi" # Order doesn't matter.
11696 * 'Mississippi'.squeeze('nonsense') # => "Misisippi" # Unused selector characters are ignored.
11697 *
11698 * A single argument may be a range of characters:
11699 *
11700 * 'Mississippi'.squeeze('a-p') # => "Mississipi"
11701 * 'Mississippi'.squeeze('q-z') # => "Misisippi"
11702 * 'Mississippi'.squeeze('a-z') # => "Misisipi"
11703 *
11704 * Multiple arguments are allowed;
11705 * see {Multiple Character Selectors}[rdoc-ref:character_selectors.rdoc@Multiple+Character+Selectors].
11706 *
11707 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
11708 *
11709 */
11710
11711static VALUE
11712rb_str_squeeze(int argc, VALUE *argv, VALUE str)
11713{
11714 str = str_duplicate(rb_cString, str);
11715 rb_str_squeeze_bang(argc, argv, str);
11716 return str;
11717}
11718
11719
11720/*
11721 * call-seq:
11722 * tr_s!(selector, replacements) -> self or nil
11723 *
11724 * Like String#tr_s, except:
11725 *
11726 * - Modifies +self+ in place (not a copy of +self+).
11727 * - Returns +self+ if any changes were made, +nil+ otherwise.
11728 *
11729 * Related: {Modifying}[rdoc-ref:String@Modifying].
11730 */
11731
11732static VALUE
11733rb_str_tr_s_bang(VALUE str, VALUE src, VALUE repl)
11734{
11735 return tr_trans(str, src, repl, 1);
11736}
11737
11738
11739/*
11740 * call-seq:
11741 * tr_s(selector, replacements) -> new_string
11742 *
11743 * Like String#tr, except:
11744 *
11745 * - Also squeezes the modified portions of the translated string;
11746 * see String#squeeze.
11747 * - Returns the translated and squeezed string.
11748 *
11749 * Examples:
11750 *
11751 * 'hello'.tr_s('l', 'r') #=> "hero"
11752 * 'hello'.tr_s('el', '-') #=> "h-o"
11753 * 'hello'.tr_s('el', 'hx') #=> "hhxo"
11754 *
11755 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
11756 *
11757 */
11758
11759static VALUE
11760rb_str_tr_s(VALUE str, VALUE src, VALUE repl)
11761{
11762 str = str_duplicate(rb_cString, str);
11763 tr_trans(str, src, repl, 1);
11764 return str;
11765}
11766
11767
11768/*
11769 * call-seq:
11770 * count(*selectors) -> integer
11771 *
11772 * Returns the total number of characters in +self+ that are specified by the given selectors.
11773 *
11774 * For one 1-character selector,
11775 * returns the count of instances of that character:
11776 *
11777 * s = 'abracadabra'
11778 * s.count('a') # => 5
11779 * s.count('b') # => 2
11780 * s.count('x') # => 0
11781 * s.count('') # => 0
11782 *
11783 * s = 'よろしくお願いします'
11784 * s.count('よ') # => 1
11785 * s.count('し') # => 2
11786 *
11787 * For one multi-character selector,
11788 * returns the count of instances for all specified characters:
11789 *
11790 * s = 'abracadabra'
11791 * s.count('ab') # => 7
11792 * s.count('abc') # => 8
11793 * s.count('abcd') # => 9
11794 * s.count('abcdr') # => 11
11795 * s.count('abcdrx') # => 11
11796 *
11797 * Order and repetition do not matter:
11798 *
11799 * s.count('ba') == s.count('ab') # => true
11800 * s.count('baab') == s.count('ab') # => true
11801 *
11802 * For multiple selectors,
11803 * forms a single selector that is the intersection of characters in all selectors
11804 * and returns the count of instances for that selector:
11805 *
11806 * s = 'abcdefg'
11807 * s.count('abcde', 'dcbfg') == s.count('bcd') # => true
11808 * s.count('abc', 'def') == s.count('') # => true
11809 *
11810 * In a character selector, three characters get special treatment:
11811 *
11812 * - A caret (<tt>'^'</tt>) functions as a _negation_ operator
11813 * for the immediately following characters:
11814 *
11815 * s = 'abracadabra'
11816 * s.count('^bc') # => 8 # Count of all except 'b' and 'c'.
11817 *
11818 * - A hyphen (<tt>'-'</tt>) between two other characters defines a _range_ of characters:
11819 *
11820 * s = 'abracadabra'
11821 * s.count('a-c') # => 8 # Count of all 'a', 'b', and 'c'.
11822 *
11823 * - A backslash (<tt>'\'</tt>) acts as an escape for a caret, a hyphen,
11824 * or another backslash:
11825 *
11826 * s = 'abracadabra'
11827 * s.count('\^bc') # => 3 # Count of '^', 'b', and 'c'.
11828 * s.count('a\-c') # => 6 # Count of 'a', '-', and 'c'.
11829 * 'foo\bar\baz'.count('\\') # => 2 # Count of '\'.
11830 *
11831 * These usages may be mixed:
11832 *
11833 * s = 'abracadabra'
11834 * s.count('a-cq-t') # => 10 # Multiple ranges.
11835 * s.count('ac-d') # => 7 # Range mixed with plain characters.
11836 * s.count('^a-c') # => 3 # Range mixed with negation.
11837 *
11838 * For multiple selectors, all forms may be used, including negations, ranges, and escapes.
11839 *
11840 * s = 'abracadabra'
11841 * s.count('^abc', '^def') == s.count('^abcdef') # => true
11842 * s.count('a-e', 'c-g') == s.count('cde') # => true
11843 * s.count('^abc', 'c-g') == s.count('defg') # => true
11844 *
11845 * Related: see {Querying}[rdoc-ref:String@Querying].
11846 */
11847
11848static VALUE
11849rb_str_count(int argc, VALUE *argv, VALUE str)
11850{
11851 char table[TR_TABLE_SIZE];
11852 rb_encoding *enc = 0;
11853 VALUE del = 0, nodel = 0, tstr;
11854 const char *s, *send;
11855 int i;
11856 int ascompat;
11857 size_t n = 0;
11858
11860
11861 tstr = argv[0];
11862 StringValue(tstr);
11863 enc = rb_enc_check(str, tstr);
11864 if (argc == 1) {
11865 const char *ptstr;
11866 if (RSTRING_LEN(tstr) == 1 && rb_enc_asciicompat(enc) &&
11867 (ptstr = RSTRING_PTR(tstr),
11868 ONIGENC_IS_ALLOWED_REVERSE_MATCH(enc, (const unsigned char *)ptstr, (const unsigned char *)ptstr+1)) &&
11869 !is_broken_string(str)) {
11870 int clen;
11871 unsigned char c = rb_enc_codepoint_len(ptstr, ptstr+1, &clen, enc);
11872
11873 s = RSTRING_PTR(str);
11874 if (!s || RSTRING_LEN(str) == 0) return INT2FIX(0);
11875 send = RSTRING_END(str);
11876 while (s < send) {
11877 if (*(unsigned char*)s++ == c) n++;
11878 }
11879 return SIZET2NUM(n);
11880 }
11881 }
11882
11883 tr_setup_table(tstr, table, TRUE, &del, &nodel, enc);
11884 for (i=1; i<argc; i++) {
11885 tstr = argv[i];
11886 StringValue(tstr);
11887 enc = rb_enc_check(str, tstr);
11888 tr_setup_table(tstr, table, FALSE, &del, &nodel, enc);
11889 }
11890
11891 s = RSTRING_PTR(str);
11892 if (!s || RSTRING_LEN(str) == 0) return INT2FIX(0);
11893 send = RSTRING_END(str);
11894 ascompat = rb_enc_asciicompat(enc);
11895 while (s < send) {
11896 unsigned int c;
11897
11898 if (ascompat && (c = *(unsigned char*)s) < 0x80) {
11899 if (table[c]) {
11900 n++;
11901 }
11902 s++;
11903 }
11904 else {
11905 int clen;
11906 c = rb_enc_codepoint_len(s, send, &clen, enc);
11907 if (tr_find(c, table, del, nodel)) {
11908 n++;
11909 }
11910 s += clen;
11911 }
11912 }
11913
11914 return SIZET2NUM(n);
11915}
11916
11917static VALUE
11918rb_fs_check(VALUE val)
11919{
11920 if (!NIL_P(val) && !RB_TYPE_P(val, T_STRING) && !RB_TYPE_P(val, T_REGEXP)) {
11921 val = rb_check_string_type(val);
11922 if (NIL_P(val)) return 0;
11923 }
11924 return val;
11925}
11926
11927static const char isspacetable[256] = {
11928 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0,
11929 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11930 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11931 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11932 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11933 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11934 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11935 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11936 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11937 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11938 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11939 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11940 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11941 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11942 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11943 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
11944};
11945
11946#define ascii_isspace(c) isspacetable[(unsigned char)(c)]
11947
11948static rb_len_t
11949split_string(VALUE result, VALUE str, rb_len_t beg, rb_len_t len, rb_len_t empty_count)
11950{
11951 if (empty_count >= 0 && len == 0) {
11952 return empty_count + 1;
11953 }
11954 if (empty_count > 0) {
11955 /* make different substrings */
11956 if (result) {
11957 do {
11958 rb_ary_push(result, str_new_empty_String(str));
11959 } while (--empty_count > 0);
11960 }
11961 else {
11962 do {
11963 rb_yield(str_new_empty_String(str));
11964 } while (--empty_count > 0);
11965 }
11966 }
11967 str = rb_str_subseq(str, beg, len);
11968 if (result) {
11969 rb_ary_push(result, str);
11970 }
11971 else {
11972 rb_yield(str);
11973 }
11974 return empty_count;
11975}
11976
11977typedef enum {
11978 SPLIT_TYPE_AWK, SPLIT_TYPE_STRING, SPLIT_TYPE_REGEXP, SPLIT_TYPE_CHARS
11979} split_type_t;
11980
11981static split_type_t
11982literal_split_pattern(VALUE spat, split_type_t default_type)
11983{
11984 rb_encoding *enc = STR_ENC_GET(spat);
11985 const char *ptr;
11986 rb_len_t len;
11987 RSTRING_GETMEM(spat, ptr, len);
11988 if (len == 0) {
11989 /* Special case - split into chars */
11990 return SPLIT_TYPE_CHARS;
11991 }
11992 else if (rb_enc_asciicompat(enc)) {
11993 if (len == 1 && ptr[0] == ' ') {
11994 return SPLIT_TYPE_AWK;
11995 }
11996 }
11997 else {
11998 int l;
11999 if (rb_enc_ascget(ptr, ptr + len, &l, enc) == ' ' && len == l) {
12000 return SPLIT_TYPE_AWK;
12001 }
12002 }
12003 return default_type;
12004}
12005
12006/*
12007 * call-seq:
12008 * split(field_sep = $;, limit = 0) -> array_of_substrings
12009 * split(field_sep = $;, limit = 0) {|substring| ... } -> self
12010 *
12011 * Creates an array of substrings by splitting +self+
12012 * at each occurrence of the given field separator +field_sep+.
12013 *
12014 * With no arguments given,
12015 * splits using the field separator <tt>$;</tt>,
12016 * whose default value is +nil+.
12017 *
12018 * With no block given, returns the array of substrings:
12019 *
12020 * 'abracadabra'.split('a') # => ["", "br", "c", "d", "br"]
12021 *
12022 * When +field_sep+ is +nil+ or <tt>' '</tt> (a single space),
12023 * splits at each sequence of whitespace:
12024 *
12025 * 'foo bar baz'.split(nil) # => ["foo", "bar", "baz"]
12026 * 'foo bar baz'.split(' ') # => ["foo", "bar", "baz"]
12027 * "foo \n\tbar\t\n baz".split(' ') # => ["foo", "bar", "baz"]
12028 * 'foo bar baz'.split(' ') # => ["foo", "bar", "baz"]
12029 * ''.split(' ') # => []
12030 *
12031 * When +field_sep+ is an empty string,
12032 * splits at every character:
12033 *
12034 * 'abracadabra'.split('') # => ["a", "b", "r", "a", "c", "a", "d", "a", "b", "r", "a"]
12035 * ''.split('') # => []
12036 * 'こんにちは'.split('') # => ["こ", "ん", "に", "ち", "は"]
12037 *
12038 * When +field_sep+ is a non-empty string and different from <tt>' '</tt> (a single space),
12039 * uses that string as the separator:
12040 *
12041 * 'abracadabra'.split('a') # => ["", "br", "c", "d", "br"]
12042 * 'abracadabra'.split('ab') # => ["", "racad", "ra"]
12043 * ''.split('a') # => []
12044 * 'こんにちは'.split('に') # => ["こん", "ちは"]
12045 *
12046 * When +field_sep+ is a Regexp,
12047 * splits at each occurrence of a matching substring:
12048 *
12049 * 'abracadabra'.split(/ab/) # => ["", "racad", "ra"]
12050 * '1 + 1 == 2'.split(/\W+/) # => ["1", "1", "2"]
12051 * 'abracadabra'.split(//) # => ["a", "b", "r", "a", "c", "a", "d", "a", "b", "r", "a"]
12052 *
12053 * If the \Regexp contains groups, their matches are included
12054 * in the returned array:
12055 *
12056 * '1:2:3'.split(/(:)()()/, 2) # => ["1", ":", "", "", "2:3"]
12057 *
12058 * Argument +limit+ sets a limit on the size of the returned array;
12059 * it also determines whether trailing empty strings are included in the returned array.
12060 *
12061 * When +limit+ is zero,
12062 * there is no limit on the size of the array,
12063 * but trailing empty strings are omitted:
12064 *
12065 * 'abracadabra'.split('', 0) # => ["a", "b", "r", "a", "c", "a", "d", "a", "b", "r", "a"]
12066 * 'abracadabra'.split('a', 0) # => ["", "br", "c", "d", "br"] # Empty string after last 'a' omitted.
12067 *
12068 * When +limit+ is a positive integer,
12069 * there is a limit on the size of the array (no more than <tt>n - 1</tt> splits occur),
12070 * and trailing empty strings are included:
12071 *
12072 * 'abracadabra'.split('', 3) # => ["a", "b", "racadabra"]
12073 * 'abracadabra'.split('a', 3) # => ["", "br", "cadabra"]
12074 * 'abracadabra'.split('', 30) # => ["a", "b", "r", "a", "c", "a", "d", "a", "b", "r", "a", ""]
12075 * 'abracadabra'.split('a', 30) # => ["", "br", "c", "d", "br", ""]
12076 * 'abracadabra'.split('', 1) # => ["abracadabra"]
12077 * 'abracadabra'.split('a', 1) # => ["abracadabra"]
12078 *
12079 * When +limit+ is negative,
12080 * there is no limit on the size of the array,
12081 * and trailing empty strings are included:
12082 *
12083 * 'abracadabra'.split('', -1) # => ["a", "b", "r", "a", "c", "a", "d", "a", "b", "r", "a", ""]
12084 * 'abracadabra'.split('a', -1) # => ["", "br", "c", "d", "br", ""]
12085 *
12086 * If a block is given, it is called with each substring and returns +self+:
12087 *
12088 * 'foo bar baz'.split(' ') {|substring| p substring }
12089 *
12090 * Output :
12091 *
12092 * "foo"
12093 * "bar"
12094 * "baz"
12095 *
12096 * Note that the above example is functionally equivalent to:
12097 *
12098 * 'foo bar baz'.split(' ').each {|substring| p substring }
12099 *
12100 * Output :
12101 *
12102 * "foo"
12103 * "bar"
12104 * "baz"
12105 *
12106 * But the latter:
12107 *
12108 * - Has poorer performance because it creates an intermediate array.
12109 * - Returns an array (instead of +self+).
12110 *
12111 * Related: see {Converting to Non-String}[rdoc-ref:String@Converting+to+Non-String].
12112 *
12113 */
12114
12115static VALUE
12116rb_str_split_m(int argc, VALUE *argv, VALUE str)
12117{
12118 rb_encoding *enc;
12119 VALUE spat;
12120 VALUE limit;
12121 split_type_t split_type;
12122 rb_len_t beg, end, i = 0, empty_count = -1;
12123 int lim = 0;
12124 VALUE result, tmp;
12125
12126 result = rb_block_given_p() ? Qfalse : Qnil;
12127 if (rb_scan_args(argc, argv, "02", &spat, &limit) == 2) {
12128 lim = NUM2INT(limit);
12129 if (lim <= 0) limit = Qnil;
12130 else if (lim == 1) {
12131 if (RSTRING_LEN(str) == 0)
12132 return result ? rb_ary_new2(0) : str;
12133 tmp = str_duplicate(rb_cString, str);
12134 if (!result) {
12135 rb_yield(tmp);
12136 return str;
12137 }
12138 return rb_ary_new3(1, tmp);
12139 }
12140 i = 1;
12141 }
12142 if (NIL_P(limit) && !lim) empty_count = 0;
12143
12144 enc = STR_ENC_GET(str);
12145 split_type = SPLIT_TYPE_REGEXP;
12146 if (!NIL_P(spat)) {
12147 spat = get_pat_quoted(spat, 0);
12148 }
12149 else if (NIL_P(spat = rb_fs)) {
12150 split_type = SPLIT_TYPE_AWK;
12151 }
12152 else if (!(spat = rb_fs_check(spat))) {
12153 rb_raise(rb_eTypeError, "value of $; must be String or Regexp");
12154 }
12155 else {
12156 rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "$; is set to non-nil value");
12157 }
12158 if (split_type != SPLIT_TYPE_AWK) {
12159 switch (BUILTIN_TYPE(spat)) {
12160 case T_REGEXP:
12161 rb_reg_options(spat); /* check if uninitialized */
12162 tmp = RREGEXP_SRC(spat);
12163 split_type = literal_split_pattern(tmp, SPLIT_TYPE_REGEXP);
12164 if (split_type == SPLIT_TYPE_AWK) {
12165 spat = tmp;
12166 split_type = SPLIT_TYPE_STRING;
12167 }
12168 break;
12169
12170 case T_STRING:
12171 mustnot_broken(spat);
12172 split_type = literal_split_pattern(spat, SPLIT_TYPE_STRING);
12173 break;
12174
12175 default:
12177 }
12178 }
12179
12180#define SPLIT_STR(beg, len) ( \
12181 empty_count = split_string(result, str, beg, len, empty_count), \
12182 str_mod_check(str, str_start, str_len))
12183
12184 beg = 0;
12185 const char *ptr = RSTRING_PTR(str);
12186 const char *const str_start = ptr;
12187 const rb_len_t str_len = RSTRING_LEN(str);
12188 const char *const eptr = str_start + str_len;
12189 if (split_type == SPLIT_TYPE_AWK) {
12190 const char *bptr = ptr;
12191 int skip = 1;
12192 unsigned int c;
12193
12194 if (result) result = rb_ary_new();
12195 end = beg;
12196 if (is_ascii_string(str)) {
12197 while (ptr < eptr) {
12198 c = (unsigned char)*ptr++;
12199 if (skip) {
12200 if (ascii_isspace(c)) {
12201 beg = ptr - bptr;
12202 }
12203 else {
12204 end = ptr - bptr;
12205 skip = 0;
12206 if (!NIL_P(limit) && lim <= i) break;
12207 }
12208 }
12209 else if (ascii_isspace(c)) {
12210 SPLIT_STR(beg, end-beg);
12211 skip = 1;
12212 beg = ptr - bptr;
12213 if (!NIL_P(limit)) ++i;
12214 }
12215 else {
12216 end = ptr - bptr;
12217 }
12218 }
12219 }
12220 else {
12221 while (ptr < eptr) {
12222 int n;
12223
12224 c = rb_enc_codepoint_len(ptr, eptr, &n, enc);
12225 ptr += n;
12226 if (skip) {
12227 if (rb_isspace(c)) {
12228 beg = ptr - bptr;
12229 }
12230 else {
12231 end = ptr - bptr;
12232 skip = 0;
12233 if (!NIL_P(limit) && lim <= i) break;
12234 }
12235 }
12236 else if (rb_isspace(c)) {
12237 SPLIT_STR(beg, end-beg);
12238 skip = 1;
12239 beg = ptr - bptr;
12240 if (!NIL_P(limit)) ++i;
12241 }
12242 else {
12243 end = ptr - bptr;
12244 }
12245 }
12246 }
12247 }
12248 else if (split_type == SPLIT_TYPE_STRING) {
12249 const char *substr_start = ptr;
12250 const char *sptr = RSTRING_PTR(spat);
12251 rb_len_t slen = RSTRING_LEN(spat);
12252
12253 if (result) result = rb_ary_new();
12254 mustnot_broken(str);
12255 enc = rb_enc_check(str, spat);
12256 while (ptr < eptr &&
12257 (end = rb_memsearch(sptr, slen, ptr, eptr - ptr, enc)) >= 0) {
12258 /* Check we are at the start of a char */
12259 const char *t = rb_enc_right_char_head(ptr, ptr + end, eptr, enc);
12260 if (t != ptr + end) {
12261 ptr = t;
12262 continue;
12263 }
12264 SPLIT_STR(substr_start - str_start, (ptr+end) - substr_start);
12265 str_mod_check(spat, sptr, slen);
12266 ptr += end + slen;
12267 substr_start = ptr;
12268 if (!NIL_P(limit) && lim <= ++i) break;
12269 }
12270 beg = substr_start - str_start;
12271 }
12272 else if (split_type == SPLIT_TYPE_CHARS) {
12273 int n;
12274
12275 if (result) result = rb_ary_new_capa(RSTRING_LEN(str));
12276 mustnot_broken(str);
12277 enc = rb_enc_get(str);
12278 while (ptr < eptr &&
12279 (n = rb_enc_precise_mbclen(ptr, eptr, enc)) > 0) {
12280 SPLIT_STR(ptr - str_start, n);
12281 ptr += n;
12282 if (!NIL_P(limit) && lim <= ++i) break;
12283 }
12284 beg = ptr - str_start;
12285 }
12286 else {
12287 if (result) result = rb_ary_new();
12288 rb_len_t len = RSTRING_LEN(str);
12289 rb_len_t start = beg;
12290 int idx;
12291 int last_null = 0;
12292 VALUE match = 0;
12293 /* Search the frozen copy made by the first match, so that every
12294 * following match does not copy an embedded str again. */
12295 VALUE search = str;
12296
12297 for (; rb_reg_search(spat, search, start, 0) >= 0;
12298 (match ? (rb_match_unbusy(match), rb_backref_set(match)) : (void)0)) {
12299 match = rb_backref_get();
12300 search = RMATCH(match)->str;
12301 if (!result) rb_match_busy(match);
12302 end = RMATCH_BEG(match, 0);
12303 if (start == end && RMATCH_BEG(match, 0) == RMATCH_END(match, 0)) {
12304 if (!ptr) {
12305 SPLIT_STR(0, 0);
12306 break;
12307 }
12308 else if (last_null == 1) {
12309 SPLIT_STR(beg, rb_enc_fast_mbclen(ptr+beg, eptr, enc));
12310 beg = start;
12311 }
12312 else {
12313 if (start == len)
12314 start++;
12315 else
12316 start += rb_enc_fast_mbclen(ptr+start,eptr,enc);
12317 last_null = 1;
12318 continue;
12319 }
12320 }
12321 else {
12322 SPLIT_STR(beg, end-beg);
12323 beg = start = RMATCH_END(match, 0);
12324 }
12325 last_null = 0;
12326
12327 for (idx = 1; idx < RMATCH_NREGS(match); idx++) {
12328 if (RMATCH_BEG(match, idx) == -1) continue;
12329 SPLIT_STR(RMATCH_BEG(match, idx), RMATCH_END(match, idx) - RMATCH_BEG(match, idx));
12330 }
12331 if (!NIL_P(limit) && lim <= ++i) break;
12332 }
12333 if (match) rb_match_unbusy(match);
12334 }
12335 if (RSTRING_LEN(str) > 0 && (!NIL_P(limit) || RSTRING_LEN(str) > beg || lim < 0)) {
12336 SPLIT_STR(beg, RSTRING_LEN(str)-beg);
12337 }
12338
12339 return result ? result : str;
12340}
12341
12342VALUE
12343rb_str_split(VALUE str, const char *sep0)
12344{
12345 VALUE sep;
12346
12347 StringValue(str);
12348 sep = rb_str_new_cstr(sep0);
12349 return rb_str_split_m(1, &sep, str);
12350}
12351
12352#define WANTARRAY(m, size) (!rb_block_given_p() ? rb_ary_new_capa(size) : 0)
12353
12354static inline int
12355enumerator_element(VALUE ary, VALUE e)
12356{
12357 if (ary) {
12358 rb_ary_push(ary, e);
12359 return 0;
12360 }
12361 else {
12362 rb_yield(e);
12363 return 1;
12364 }
12365}
12366
12367#define ENUM_ELEM(ary, e) enumerator_element(ary, e)
12368
12369static const char *
12370chomp_newline(const char *p, const char *e, rb_encoding *enc)
12371{
12372 const char *prev = rb_enc_prev_char(p, e, e, enc);
12373 if (rb_enc_is_newline(prev, e, enc)) {
12374 e = prev;
12375 prev = rb_enc_prev_char(p, e, e, enc);
12376 if (prev && rb_enc_ascget(prev, e, NULL, enc) == '\r')
12377 e = prev;
12378 }
12379 return e;
12380}
12381
12382static VALUE
12383get_rs(void)
12384{
12385 VALUE rs = rb_rs;
12386 if (!NIL_P(rs) &&
12387 (!RB_TYPE_P(rs, T_STRING) ||
12388 RSTRING_LEN(rs) != 1 ||
12389 RSTRING_PTR(rs)[0] != '\n')) {
12390 rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "$/ is set to non-default value");
12391 }
12392 return rs;
12393}
12394
12395#define rb_rs get_rs()
12396
12397static VALUE
12398rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, VALUE ary)
12399{
12400 rb_encoding *enc;
12401 VALUE line, rs, orig = str, opts = Qnil, chomp = Qfalse;
12402 const char *pend, *subptr, *subend, *rsptr, *hit, *adjusted;
12403 rb_len_t pos, rslen;
12404 int rsnewline = 0;
12405
12406 if (rb_scan_args(argc, argv, "01:", &rs, &opts) == 0)
12407 rs = rb_rs;
12408 if (!NIL_P(opts)) {
12409 static ID keywords[1];
12410 if (!keywords[0]) {
12411 keywords[0] = rb_intern_const("chomp");
12412 }
12413 rb_get_kwargs(opts, keywords, 0, 1, &chomp);
12414 chomp = (!UNDEF_P(chomp) && RTEST(chomp));
12415 }
12416
12417 if (NIL_P(rs)) {
12418 if (!ENUM_ELEM(ary, str)) {
12419 return ary;
12420 }
12421 else {
12422 return orig;
12423 }
12424 }
12425
12426 if (!RSTRING_LEN(str)) goto end;
12427 str = rb_str_new_frozen(str);
12428 const char *const ptr = subptr = RSTRING_PTR(str);
12429 const rb_len_t len = RSTRING_LEN(str);
12430 pend = RSTRING_END(str);
12431 StringValue(rs);
12432 rslen = RSTRING_LEN(rs);
12433
12434 if (rs == rb_default_rs)
12435 enc = rb_enc_get(str);
12436 else
12437 enc = rb_enc_check(str, rs);
12438
12439 if (rslen == 0) {
12440 /* paragraph mode */
12441 int n;
12442 const char *eol = NULL;
12443 subend = subptr;
12444 while (subend < pend) {
12445 rb_len_t chomp_rslen = 0;
12446 do {
12447 if (rb_enc_ascget(subend, pend, &n, enc) != '\r')
12448 n = 0;
12449 rslen = n + rb_enc_mbclen(subend + n, pend, enc);
12450 if (rb_enc_is_newline(subend + n, pend, enc)) {
12451 if (eol == subend) break;
12452 subend += rslen;
12453 if (subptr) {
12454 eol = subend;
12455 chomp_rslen = -rslen;
12456 }
12457 }
12458 else {
12459 if (!subptr) subptr = subend;
12460 subend += rslen;
12461 }
12462 rslen = 0;
12463 } while (subend < pend);
12464 if (!subptr) break;
12465 if (rslen == 0) chomp_rslen = 0;
12466 line = rb_str_subseq(str, subptr - ptr,
12467 subend - subptr + (chomp ? chomp_rslen : rslen));
12468 if (ENUM_ELEM(ary, line)) {
12469 str_mod_check(str, ptr, len);
12470 }
12471 subptr = eol = NULL;
12472 }
12473 goto end;
12474 }
12475 else {
12476 rsptr = RSTRING_PTR(rs);
12477 if (RSTRING_LEN(rs) == rb_enc_mbminlen(enc) &&
12478 rb_enc_is_newline(rsptr, rsptr + RSTRING_LEN(rs), enc)) {
12479 rsnewline = 1;
12480 }
12481 }
12482
12483 if ((rs == rb_default_rs) && !rb_enc_asciicompat(enc)) {
12484 rs = rb_str_new(rsptr, rslen);
12485 rs = rb_str_encode(rs, rb_enc_from_encoding(enc), 0, Qnil);
12486 rsptr = RSTRING_PTR(rs);
12487 rslen = RSTRING_LEN(rs);
12488 }
12489
12490 while (subptr < pend) {
12491 pos = rb_memsearch(rsptr, rslen, subptr, pend - subptr, enc);
12492 if (pos < 0) break;
12493 hit = subptr + pos;
12494 adjusted = rb_enc_right_char_head(subptr, hit, pend, enc);
12495 if (hit != adjusted) {
12496 subptr = adjusted;
12497 continue;
12498 }
12499 subend = hit += rslen;
12500 if (chomp) {
12501 if (rsnewline) {
12502 subend = chomp_newline(subptr, subend, enc);
12503 }
12504 else {
12505 subend -= rslen;
12506 }
12507 }
12508 line = rb_str_subseq(str, subptr - ptr, subend - subptr);
12509 if (ENUM_ELEM(ary, line)) {
12510 str_mod_check(str, ptr, len);
12511 str_mod_check(rs, rsptr, rslen);
12512 }
12513 subptr = hit;
12514 }
12515
12516 if (subptr < pend) {
12517 if (chomp) {
12518 if (rsnewline) {
12519 pend = chomp_newline(subptr, pend, enc);
12520 }
12521 else if (pend - subptr >= rslen &&
12522 memcmp(pend - rslen, rsptr, rslen) == 0) {
12523 pend -= rslen;
12524 }
12525 }
12526 line = rb_str_subseq(str, subptr - ptr, pend - subptr);
12527 ENUM_ELEM(ary, line);
12528 RB_GC_GUARD(str);
12529 }
12530
12531 end:
12532 if (ary)
12533 return ary;
12534 else
12535 return orig;
12536}
12537
12538/*
12539 * call-seq:
12540 * each_line(record_separator = $/, chomp: false) {|substring| ... } -> self
12541 * each_line(record_separator = $/, chomp: false) -> enumerator
12542 *
12543 * With a block given, forms the substrings (lines)
12544 * that are the result of splitting +self+
12545 * at each occurrence of the given +record_separator+;
12546 * passes each line to the block;
12547 * returns +self+.
12548 *
12549 * With the default +record_separator+:
12550 *
12551 * $/ # => "\n"
12552 * s = <<~EOT
12553 * This is the first line.
12554 * This is line two.
12555 *
12556 * This is line four.
12557 * This is line five.
12558 * EOT
12559 * s.each_line {|line| p line }
12560 *
12561 * Output:
12562 *
12563 * "This is the first line.\n"
12564 * "This is line two.\n"
12565 * "\n"
12566 * "This is line four.\n"
12567 * "This is line five.\n"
12568 *
12569 * With a different +record_separator+:
12570 *
12571 * record_separator = ' is '
12572 * s.each_line(record_separator) {|line| p line }
12573 *
12574 * Output:
12575 *
12576 * "This is "
12577 * "the first line.\nThis is "
12578 * "line two.\n\nThis is "
12579 * "line four.\nThis is "
12580 * "line five.\n"
12581 *
12582 * With +chomp+ as +true+, removes the trailing +record_separator+ from each line:
12583 *
12584 * s.each_line(chomp: true) {|line| p line }
12585 *
12586 * Output:
12587 *
12588 * "This is the first line."
12589 * "This is line two."
12590 * ""
12591 * "This is line four."
12592 * "This is line five."
12593 *
12594 * With an empty string as +record_separator+,
12595 * forms and passes "paragraphs" by splitting at each occurrence
12596 * of two or more newlines:
12597 *
12598 * record_separator = ''
12599 * s.each_line(record_separator) {|line| p line }
12600 *
12601 * Output:
12602 *
12603 * "This is the first line.\nThis is line two.\n\n"
12604 * "This is line four.\nThis is line five.\n"
12605 *
12606 * With no block given, returns an enumerator.
12607 *
12608 * Related: see {Iterating}[rdoc-ref:String@Iterating].
12609 *
12610 */
12611
12612static VALUE
12613rb_str_each_line(int argc, VALUE *argv, VALUE str)
12614{
12615 RETURN_SIZED_ENUMERATOR(str, argc, argv, 0);
12616 return rb_str_enumerate_lines(argc, argv, str, 0);
12617}
12618
12619/*
12620 * call-seq:
12621 * lines(record_separator = $/, chomp: false) -> array_of_strings
12622 *
12623 * Returns substrings ("lines") of +self+
12624 * according to the given arguments:
12625 *
12626 * s = <<~EOT
12627 * This is the first line.
12628 * This is line two.
12629 *
12630 * This is line four.
12631 * This is line five.
12632 * EOT
12633 *
12634 * With the default argument values:
12635 *
12636 * $/ # => "\n"
12637 * s.lines
12638 * # =>
12639 * ["This is the first line.\n",
12640 * "This is line two.\n",
12641 * "\n",
12642 * "This is line four.\n",
12643 * "This is line five.\n"]
12644 *
12645 * With a different +record_separator+:
12646 *
12647 * record_separator = ' is '
12648 * s.lines(record_separator)
12649 * # =>
12650 * ["This is ",
12651 * "the first line.\nThis is ",
12652 * "line two.\n\nThis is ",
12653 * "line four.\nThis is ",
12654 * "line five.\n"]
12655 *
12656 * With keyword argument +chomp+ as +true+,
12657 * removes the trailing newline from each line:
12658 *
12659 * s.lines(chomp: true)
12660 * # =>
12661 * ["This is the first line.",
12662 * "This is line two.",
12663 * "",
12664 * "This is line four.",
12665 * "This is line five."]
12666 *
12667 * Related: see {Converting to Non-String}[rdoc-ref:String@Converting+to+Non-String].
12668 */
12669
12670static VALUE
12671rb_str_lines(int argc, VALUE *argv, VALUE str)
12672{
12673 VALUE ary = WANTARRAY("lines", 0);
12674 return rb_str_enumerate_lines(argc, argv, str, ary);
12675}
12676
12677static VALUE
12678rb_str_each_byte_size(VALUE str, VALUE args, VALUE eobj)
12679{
12680 return LEN2NUM(RSTRING_LEN(str));
12681}
12682
12683static VALUE
12684rb_str_enumerate_bytes(VALUE str, VALUE ary)
12685{
12686 rb_len_t i;
12687
12688 for (i=0; i<RSTRING_LEN(str); i++) {
12689 ENUM_ELEM(ary, INT2FIX((unsigned char)RSTRING_PTR(str)[i]));
12690 }
12691 if (ary)
12692 return ary;
12693 else
12694 return str;
12695}
12696
12697/*
12698 * call-seq:
12699 * each_byte {|byte| ... } -> self
12700 * each_byte -> enumerator
12701 *
12702 * With a block given, calls the block with each successive byte from +self+;
12703 * returns +self+:
12704 *
12705 * a = []
12706 * 'hello'.each_byte {|byte| a.push(byte) } # Five 1-byte characters.
12707 * a # => [104, 101, 108, 108, 111]
12708 * a = []
12709 * 'こんにちは'.each_byte {|byte| a.push(byte) } # Five 3-byte characters.
12710 * a # => [227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]
12711 *
12712 * With no block given, returns an enumerator.
12713 *
12714 * Related: see {Iterating}[rdoc-ref:String@Iterating].
12715 *
12716 */
12717
12718static VALUE
12719rb_str_each_byte(VALUE str)
12720{
12721 RETURN_SIZED_ENUMERATOR(str, 0, 0, rb_str_each_byte_size);
12722 return rb_str_enumerate_bytes(str, 0);
12723}
12724
12725/*
12726 * call-seq:
12727 * bytes -> array_of_bytes
12728 *
12729 * Returns an array of the bytes in +self+:
12730 *
12731 * 'hello'.bytes # => [104, 101, 108, 108, 111]
12732 * 'こんにちは'.bytes
12733 * # => [227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]
12734 *
12735 * Related: see {Converting to Non-String}[rdoc-ref:String@Converting+to+Non-String].
12736 *
12737 */
12738
12739static VALUE
12740rb_str_bytes(VALUE str)
12741{
12742 VALUE ary = WANTARRAY("bytes", RSTRING_LEN(str));
12743 return rb_str_enumerate_bytes(str, ary);
12744}
12745
12746static VALUE
12747rb_str_each_char_size(VALUE str, VALUE args, VALUE eobj)
12748{
12749 return rb_str_length(str);
12750}
12751
12752static VALUE
12753rb_str_enumerate_chars(VALUE str, VALUE ary)
12754{
12755 VALUE orig = str;
12756 rb_len_t i, len, n;
12757 const char *ptr;
12758 rb_encoding *enc;
12759
12760 str = rb_str_new_frozen(str);
12761 ptr = RSTRING_PTR(str);
12762 len = RSTRING_LEN(str);
12763 enc = rb_enc_get(str);
12764
12766 for (i = 0; i < len; i += n) {
12767 n = rb_enc_fast_mbclen(ptr + i, ptr + len, enc);
12768 ENUM_ELEM(ary, rb_str_subseq(str, i, n));
12769 }
12770 }
12771 else {
12772 for (i = 0; i < len; i += n) {
12773 n = rb_enc_mbclen(ptr + i, ptr + len, enc);
12774 ENUM_ELEM(ary, rb_str_subseq(str, i, n));
12775 }
12776 }
12777 RB_GC_GUARD(str);
12778 if (ary)
12779 return ary;
12780 else
12781 return orig;
12782}
12783
12784/*
12785 * call-seq:
12786 * each_char {|char| ... } -> self
12787 * each_char -> enumerator
12788 *
12789 * With a block given, calls the block with each successive character from +self+;
12790 * returns +self+:
12791 *
12792 * a = []
12793 * 'hello'.each_char do |char|
12794 * a.push(char)
12795 * end
12796 * a # => ["h", "e", "l", "l", "o"]
12797 * a = []
12798 * 'こんにちは'.each_char do |char|
12799 * a.push(char)
12800 * end
12801 * a # => ["こ", "ん", "に", "ち", "は"]
12802 *
12803 * With no block given, returns an enumerator.
12804 *
12805 * Related: see {Iterating}[rdoc-ref:String@Iterating].
12806 *
12807 */
12808
12809static VALUE
12810rb_str_each_char(VALUE str)
12811{
12812 RETURN_SIZED_ENUMERATOR(str, 0, 0, rb_str_each_char_size);
12813 return rb_str_enumerate_chars(str, 0);
12814}
12815
12816/*
12817 * call-seq:
12818 * chars -> array_of_characters
12819 *
12820 * Returns an array of the characters in +self+:
12821 *
12822 * 'hello'.chars # => ["h", "e", "l", "l", "o"]
12823 * 'こんにちは'.chars # => ["こ", "ん", "に", "ち", "は"]
12824 * ''.chars # => []
12825 *
12826 * Related: see {Converting to Non-String}[rdoc-ref:String@Converting+to+Non-String].
12827 *
12828 */
12829
12830static VALUE
12831rb_str_chars(VALUE str)
12832{
12833 VALUE ary = WANTARRAY("chars", rb_str_strlen(str));
12834 return rb_str_enumerate_chars(str, ary);
12835}
12836
12837static VALUE
12838rb_str_enumerate_codepoints(VALUE str, VALUE ary)
12839{
12840 VALUE orig = str;
12841 int n;
12842 unsigned int c;
12843 const char *ptr, *end;
12844 rb_encoding *enc;
12845 int enc_asciicompat;
12846
12847 if (single_byte_optimizable(str))
12848 return rb_str_enumerate_bytes(str, ary);
12849
12850 str = rb_str_new_frozen(str);
12851 ptr = RSTRING_PTR(str);
12852 end = RSTRING_END(str);
12853 enc = STR_ENC_GET(str);
12854 enc_asciicompat = rb_enc_asciicompat(enc);
12855
12856 while (ptr < end) {
12857 /* Fast path: ASCII byte in an ASCII-compatible encoding is its own codepoint;
12858 * skip rb_enc_codepoint_len and return the byte directly.
12859 */
12860 n = 1;
12861 c = (enc_asciicompat && ISASCII(*ptr)) ?
12862 (unsigned char)*ptr : rb_enc_codepoint_len(ptr, end, &n, enc);
12863 ENUM_ELEM(ary, UINT2NUM(c));
12864 ptr += n;
12865 }
12866 RB_GC_GUARD(str);
12867 if (ary)
12868 return ary;
12869 else
12870 return orig;
12871}
12872
12873/*
12874 * call-seq:
12875 * each_codepoint {|codepoint| ... } -> self
12876 * each_codepoint -> enumerator
12877 *
12878 * With a block given, calls the block with each successive codepoint from +self+;
12879 * each {codepoint}[https://en.wikipedia.org/wiki/Code_point] is the integer value for a character;
12880 * returns +self+:
12881 *
12882 * a = []
12883 * 'hello'.each_codepoint do |codepoint|
12884 * a.push(codepoint)
12885 * end
12886 * a # => [104, 101, 108, 108, 111]
12887 * a = []
12888 * 'こんにちは'.each_codepoint do |codepoint|
12889 * a.push(codepoint)
12890 * end
12891 * a # => [12371, 12435, 12395, 12385, 12399]
12892 *
12893 * With no block given, returns an enumerator.
12894 *
12895 * Related: see {Iterating}[rdoc-ref:String@Iterating].
12896 *
12897 */
12898
12899static VALUE
12900rb_str_each_codepoint(VALUE str)
12901{
12902 RETURN_SIZED_ENUMERATOR(str, 0, 0, rb_str_each_char_size);
12903 return rb_str_enumerate_codepoints(str, 0);
12904}
12905
12906/*
12907 * call-seq:
12908 * codepoints -> array_of_integers
12909 *
12910 * Returns an array of the codepoints in +self+;
12911 * each codepoint is the integer value for a character:
12912 *
12913 * 'hello'.codepoints # => [104, 101, 108, 108, 111]
12914 * 'こんにちは'.codepoints # => [12371, 12435, 12395, 12385, 12399]
12915 * ''.codepoints # => []
12916 *
12917 * Related: see {Converting to Non-String}[rdoc-ref:String@Converting+to+Non-String].
12918 *
12919 */
12920
12921static VALUE
12922rb_str_codepoints(VALUE str)
12923{
12924 VALUE ary = WANTARRAY("codepoints", rb_str_strlen(str));
12925 return rb_str_enumerate_codepoints(str, ary);
12926}
12927
12928static regex_t *
12929get_reg_grapheme_cluster(rb_encoding *enc)
12930{
12931 int encidx = rb_enc_to_index(enc);
12932
12933 const OnigUChar source_ascii[] = "\\X";
12934 const OnigUChar *source = source_ascii;
12935 size_t source_len = sizeof(source_ascii) - 1;
12936
12937 switch (encidx) {
12938#define CHARS_16BE(x) (OnigUChar)((x)>>8), (OnigUChar)(x)
12939#define CHARS_16LE(x) (OnigUChar)(x), (OnigUChar)((x)>>8)
12940#define CHARS_32BE(x) CHARS_16BE((x)>>16), CHARS_16BE(x)
12941#define CHARS_32LE(x) CHARS_16LE(x), CHARS_16LE((x)>>16)
12942#define CASE_UTF(e) \
12943 case ENCINDEX_UTF_##e: { \
12944 static const OnigUChar source_UTF_##e[] = {CHARS_##e('\\'), CHARS_##e('X')}; \
12945 source = source_UTF_##e; \
12946 source_len = sizeof(source_UTF_##e); \
12947 break; \
12948 }
12949 CASE_UTF(16BE); CASE_UTF(16LE); CASE_UTF(32BE); CASE_UTF(32LE);
12950#undef CASE_UTF
12951#undef CHARS_16BE
12952#undef CHARS_16LE
12953#undef CHARS_32BE
12954#undef CHARS_32LE
12955 }
12956
12957 regex_t *reg_grapheme_cluster;
12958 OnigErrorInfo einfo;
12959 int r = onig_new(&reg_grapheme_cluster, source, source + source_len,
12960 ONIG_OPTION_DEFAULT, enc, OnigDefaultSyntax, &einfo);
12961 if (r) {
12962 UChar message[ONIG_MAX_ERROR_MESSAGE_LEN];
12963 onig_error_code_to_str(message, r, &einfo);
12964 rb_fatal("cannot compile grapheme cluster regexp: %s", (char *)message);
12965 }
12966
12967 return reg_grapheme_cluster;
12968}
12969
12970static regex_t *
12971get_cached_reg_grapheme_cluster(rb_encoding *enc)
12972{
12973 int encidx = rb_enc_to_index(enc);
12974 static regex_t *reg_grapheme_cluster_utf8 = NULL;
12975
12976 if (encidx == rb_utf8_encindex()) {
12977 if (!reg_grapheme_cluster_utf8) {
12978 reg_grapheme_cluster_utf8 = get_reg_grapheme_cluster(enc);
12979 }
12980
12981 return reg_grapheme_cluster_utf8;
12982 }
12983
12984 return NULL;
12985}
12986
12987static VALUE
12988rb_str_each_grapheme_cluster_size(VALUE str, VALUE args, VALUE eobj)
12989{
12990 size_t grapheme_cluster_count = 0;
12991 rb_encoding *enc = get_encoding(str);
12992 const char *ptr, *end;
12993
12994 if (!rb_enc_unicode_p(enc)) {
12995 return rb_str_length(str);
12996 }
12997
12998 bool cached_reg_grapheme_cluster = true;
12999 regex_t *reg_grapheme_cluster = get_cached_reg_grapheme_cluster(enc);
13000 if (!reg_grapheme_cluster) {
13001 reg_grapheme_cluster = get_reg_grapheme_cluster(enc);
13002 cached_reg_grapheme_cluster = false;
13003 }
13004
13005 ptr = RSTRING_PTR(str);
13006 end = RSTRING_END(str);
13007
13008 while (ptr < end) {
13009 OnigPosition len = onig_match(reg_grapheme_cluster,
13010 (const OnigUChar *)ptr, (const OnigUChar *)end,
13011 (const OnigUChar *)ptr, NULL, 0);
13012 if (len <= 0) break;
13013 grapheme_cluster_count++;
13014 ptr += len;
13015 }
13016
13017 if (!cached_reg_grapheme_cluster) {
13018 onig_free(reg_grapheme_cluster);
13019 }
13020
13021 return SIZET2NUM(grapheme_cluster_count);
13022}
13023
13024static VALUE
13025rb_str_enumerate_grapheme_clusters(VALUE str, VALUE ary)
13026{
13027 VALUE orig = str;
13028 rb_encoding *enc = get_encoding(str);
13029 const char *ptr0, *ptr, *end;
13030
13031 if (!rb_enc_unicode_p(enc)) {
13032 return rb_str_enumerate_chars(str, ary);
13033 }
13034
13035 if (!ary) str = rb_str_new_frozen(str);
13036
13037 bool cached_reg_grapheme_cluster = true;
13038 regex_t *reg_grapheme_cluster = get_cached_reg_grapheme_cluster(enc);
13039 if (!reg_grapheme_cluster) {
13040 reg_grapheme_cluster = get_reg_grapheme_cluster(enc);
13041 cached_reg_grapheme_cluster = false;
13042 }
13043
13044 ptr0 = ptr = RSTRING_PTR(str);
13045 end = RSTRING_END(str);
13046
13047 while (ptr < end) {
13048 OnigPosition len = onig_match(reg_grapheme_cluster,
13049 (const OnigUChar *)ptr, (const OnigUChar *)end,
13050 (const OnigUChar *)ptr, NULL, 0);
13051 if (len <= 0) break;
13052 ENUM_ELEM(ary, rb_str_subseq(str, ptr-ptr0, len));
13053 ptr += len;
13054 }
13055
13056 if (!cached_reg_grapheme_cluster) {
13057 onig_free(reg_grapheme_cluster);
13058 }
13059
13060 RB_GC_GUARD(str);
13061 if (ary)
13062 return ary;
13063 else
13064 return orig;
13065}
13066
13067/*
13068 * call-seq:
13069 * each_grapheme_cluster {|grapheme_cluster| ... } -> self
13070 * each_grapheme_cluster -> enumerator
13071 *
13072 * With a block given, calls the given block with each successive grapheme cluster from +self+
13073 * (see {Unicode Grapheme Cluster Boundaries}[https://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries]);
13074 * returns +self+:
13075 *
13076 * a = []
13077 * 'hello'.each_grapheme_cluster do |grapheme_cluster|
13078 * a.push(grapheme_cluster)
13079 * end
13080 * a # => ["h", "e", "l", "l", "o"]
13081 *
13082 * a = []
13083 * 'こんにちは'.each_grapheme_cluster do |grapheme_cluster|
13084 * a.push(grapheme_cluster)
13085 * end
13086 * a # => ["こ", "ん", "に", "ち", "は"]
13087 *
13088 * With no block given, returns an enumerator.
13089 *
13090 * Related: see {Iterating}[rdoc-ref:String@Iterating].
13091 *
13092 */
13093
13094static VALUE
13095rb_str_each_grapheme_cluster(VALUE str)
13096{
13097 RETURN_SIZED_ENUMERATOR(str, 0, 0, rb_str_each_grapheme_cluster_size);
13098 return rb_str_enumerate_grapheme_clusters(str, 0);
13099}
13100
13101/*
13102 * call-seq:
13103 * grapheme_clusters -> array_of_grapheme_clusters
13104 *
13105 * Returns an array of the grapheme clusters in +self+
13106 * (see {Unicode Grapheme Cluster Boundaries}[https://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries]):
13107 *
13108 * s = "ä-pqr-b̈-xyz-c̈"
13109 * s.size # => 16
13110 * s.bytesize # => 19
13111 * s.grapheme_clusters.size # => 13
13112 * s.grapheme_clusters
13113 * # => ["ä", "-", "p", "q", "r", "-", "b̈", "-", "x", "y", "z", "-", "c̈"]
13114 *
13115 * Details:
13116 *
13117 * s = "ä"
13118 * s.grapheme_clusters # => ["ä"] # One grapheme cluster.
13119 * s.bytes # => [97, 204, 136] # Three bytes.
13120 * s.chars # => ["a", "̈"] # Two characters.
13121 * s.chars.map {|char| char.ord } # => [97, 776] # Their values.
13122 *
13123 * Related: see {Converting to Non-String}[rdoc-ref:String@Converting+to+Non-String].
13124 *
13125 */
13126
13127static VALUE
13128rb_str_grapheme_clusters(VALUE str)
13129{
13130 VALUE ary = WANTARRAY("grapheme_clusters", rb_str_strlen(str));
13131 return rb_str_enumerate_grapheme_clusters(str, ary);
13132}
13133
13134static rb_len_t
13135chopped_length(VALUE str)
13136{
13137 rb_encoding *enc = STR_ENC_GET(str);
13138 const char *p, *p2, *beg, *end;
13139
13140 beg = RSTRING_PTR(str);
13141 end = beg + RSTRING_LEN(str);
13142 if (beg >= end) return 0;
13143 p = rb_enc_prev_char(beg, end, end, enc);
13144 if (!p) return 0;
13145 if (p > beg && rb_enc_ascget(p, end, 0, enc) == '\n') {
13146 p2 = rb_enc_prev_char(beg, p, end, enc);
13147 if (p2 && rb_enc_ascget(p2, end, 0, enc) == '\r') p = p2;
13148 }
13149 return p - beg;
13150}
13151
13152/*
13153 * call-seq:
13154 * chop! -> self or nil
13155 *
13156 * Like String#chop, except that:
13157 *
13158 * - Removes trailing characters from +self+ (not from a copy of +self+).
13159 * - Returns +self+ if any characters are removed, +nil+ otherwise.
13160 *
13161 * Related: see {Modifying}[rdoc-ref:String@Modifying].
13162 */
13163
13164static VALUE
13165rb_str_chop_bang(VALUE str)
13166{
13167 str_modify_keep_cr(str);
13168 if (RSTRING_LEN(str) > 0) {
13169 rb_len_t len;
13170 len = chopped_length(str);
13171 STR_SET_LEN(str, len);
13172 TERM_FILL(&RSTRING_PTR(str)[len], TERM_LEN(str));
13173 if (ENC_CODERANGE(str) != ENC_CODERANGE_7BIT) {
13175 }
13176 return str;
13177 }
13178 return Qnil;
13179}
13180
13181
13182/*
13183 * call-seq:
13184 * chop -> new_string
13185 *
13186 * Returns a new string copied from +self+, with trailing characters possibly removed.
13187 *
13188 * Removes <tt>"\r\n"</tt> if those are the last two characters.
13189 *
13190 * "abc\r\n".chop # => "abc"
13191 * "こんにちは\r\n".chop # => "こんにちは"
13192 *
13193 * Otherwise removes the last character if it exists.
13194 *
13195 * 'abcd'.chop # => "abc"
13196 * 'こんにちは'.chop # => "こんにち"
13197 * ''.chop # => ""
13198 *
13199 * If you only need to remove the newline separator at the end of the string,
13200 * String#chomp is a better alternative.
13201 *
13202 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
13203 *
13204 */
13205
13206static VALUE
13207rb_str_chop(VALUE str)
13208{
13209 return rb_str_subseq(str, 0, chopped_length(str));
13210}
13211
13212static rb_len_t
13213smart_chomp(VALUE str, const char *e, const char *p)
13214{
13215 rb_encoding *enc = rb_enc_get(str);
13216 if (rb_enc_mbminlen(enc) > 1) {
13217 /* a receiver shorter than one character has nothing to chomp */
13218 if (e - p < rb_enc_mbminlen(enc)) return e - p;
13219 const char *pp = rb_enc_left_char_head(p, e-rb_enc_mbminlen(enc), e, enc);
13220 if (rb_enc_is_newline(pp, e, enc)) {
13221 e = pp;
13222 }
13223 pp = e - rb_enc_mbminlen(enc);
13224 if (pp >= p) {
13225 pp = rb_enc_left_char_head(p, pp, e, enc);
13226 if (rb_enc_ascget(pp, e, 0, enc) == '\r') {
13227 e = pp;
13228 }
13229 }
13230 }
13231 else {
13232 switch (*(e-1)) { /* not e[-1] to get rid of VC bug */
13233 case '\n':
13234 if (--e > p && *(e-1) == '\r') {
13235 --e;
13236 }
13237 break;
13238 case '\r':
13239 --e;
13240 break;
13241 }
13242 }
13243 return e - p;
13244}
13245
13246static rb_len_t
13247chompped_length(VALUE str, VALUE rs)
13248{
13249 rb_encoding *enc;
13250 int newline;
13251 const char *pp, *e, *rsptr;
13252 rb_len_t rslen;
13253 const char *const p = RSTRING_PTR(str);
13254 rb_len_t len = RSTRING_LEN(str);
13255
13256 if (len == 0) return 0;
13257 e = p + len;
13258 if (rs == rb_default_rs) {
13259 return smart_chomp(str, e, p);
13260 }
13261
13262 enc = rb_enc_get(str);
13263 RSTRING_GETMEM(rs, rsptr, rslen);
13264 if (rslen == 0) {
13265 if (rb_enc_mbminlen(enc) > 1) {
13266 while (e - p >= rb_enc_mbminlen(enc)) {
13267 pp = rb_enc_left_char_head(p, e-rb_enc_mbminlen(enc), e, enc);
13268 if (!rb_enc_is_newline(pp, e, enc)) break;
13269 e = pp;
13270 pp -= rb_enc_mbminlen(enc);
13271 if (pp >= p) {
13272 pp = rb_enc_left_char_head(p, pp, e, enc);
13273 if (rb_enc_ascget(pp, e, 0, enc) == '\r') {
13274 e = pp;
13275 }
13276 }
13277 }
13278 }
13279 else {
13280 while (e > p && *(e-1) == '\n') {
13281 --e;
13282 if (e > p && *(e-1) == '\r')
13283 --e;
13284 }
13285 }
13286 return e - p;
13287 }
13288 if (rslen > len) return len;
13289
13290 enc = rb_enc_get(rs);
13291 newline = rsptr[rslen-1];
13292 if (rslen == rb_enc_mbminlen(enc)) {
13293 if (rslen == 1) {
13294 if (newline == '\n')
13295 return smart_chomp(str, e, p);
13296 }
13297 else {
13298 if (rb_enc_is_newline(rsptr, rsptr+rslen, enc))
13299 return smart_chomp(str, e, p);
13300 }
13301 }
13302
13303 enc = rb_enc_check(str, rs);
13304 if (is_broken_string(rs)) {
13305 return len;
13306 }
13307 pp = e - rslen;
13308 if (p[len-1] == newline &&
13309 (rslen <= 1 ||
13310 memcmp(rsptr, pp, rslen) == 0)) {
13311 if (at_char_boundary(p, pp, e, enc))
13312 return len - rslen;
13313 RB_GC_GUARD(rs);
13314 }
13315 return len;
13316}
13317
13323static VALUE
13324chomp_rs(int argc, const VALUE *argv)
13325{
13326 rb_check_arity(argc, 0, 1);
13327 if (argc > 0) {
13328 VALUE rs = argv[0];
13329 if (!NIL_P(rs)) StringValue(rs);
13330 return rs;
13331 }
13332 else {
13333 return rb_rs;
13334 }
13335}
13336
13337static VALUE
13338str_shrink(VALUE str, rb_len_t len)
13339{
13340 str_modify_keep_cr(str);
13341 STR_SET_LEN(str, len);
13342 TERM_FILL(&RSTRING_PTR(str)[len], TERM_LEN(str));
13343 if (ENC_CODERANGE(str) != ENC_CODERANGE_7BIT) {
13345 }
13346 return str;
13347}
13348
13349VALUE
13350rb_str_chomp_string(VALUE str, VALUE rs)
13351{
13352 rb_len_t olen = RSTRING_LEN(str);
13353 rb_len_t len = chompped_length(str, rs);
13354 if (len >= olen) return Qnil;
13355 return str_shrink(str, len);
13356}
13357
13358/*
13359 * call-seq:
13360 * chomp!(line_sep = $/) -> self or nil
13361 *
13362 * Like String#chomp, except that:
13363 *
13364 * - Removes trailing characters from +self+ (not from a copy of +self+).
13365 * - Returns +self+ if any characters are removed, +nil+ otherwise.
13366 *
13367 * Related: see {Modifying}[rdoc-ref:String@Modifying].
13368 */
13369
13370static VALUE
13371rb_str_chomp_bang(int argc, VALUE *argv, VALUE str)
13372{
13373 VALUE rs;
13374 str_modifiable(str);
13375 if (RSTRING_LEN(str) == 0 && argc < 2) return Qnil;
13376 rs = chomp_rs(argc, argv);
13377 if (NIL_P(rs)) return Qnil;
13378 return rb_str_chomp_string(str, rs);
13379}
13380
13381
13382/*
13383 * call-seq:
13384 * chomp(line_sep = $/) -> new_string
13385 *
13386 * Returns a new string copied from +self+, with trailing characters possibly removed:
13387 *
13388 * When +line_sep+ is <tt>"\n"</tt>, removes the last one or two characters
13389 * if they are <tt>"\r"</tt>, <tt>"\n"</tt>, or <tt>"\r\n"</tt>
13390 * (but not <tt>"\n\r"</tt>):
13391 *
13392 * $/ # => "\n"
13393 * "abc\r".chomp # => "abc"
13394 * "abc\n".chomp # => "abc"
13395 * "abc\r\n".chomp # => "abc"
13396 * "abc\n\r".chomp # => "abc\n"
13397 * "こんにちは\r\n".chomp # => "こんにちは"
13398 *
13399 * When +line_sep+ is <tt>''</tt> (an empty string),
13400 * removes multiple trailing occurrences of <tt>"\n"</tt> or <tt>"\r\n"</tt>
13401 * (but not <tt>"\r"</tt> or <tt>"\n\r"</tt>):
13402 *
13403 * "abc\n\n\n".chomp('') # => "abc"
13404 * "abc\r\n\r\n\r\n".chomp('') # => "abc"
13405 * "abc\n\n\r\n\r\n\n\n".chomp('') # => "abc"
13406 * "abc\n\r\n\r\n\r".chomp('') # => "abc\n\r\n\r\n\r"
13407 * "abc\r\r\r".chomp('') # => "abc\r\r\r"
13408 *
13409 * When +line_sep+ is neither <tt>"\n"</tt> nor <tt>''</tt>,
13410 * removes a single trailing line separator if there is one:
13411 *
13412 * 'abcd'.chomp('cd') # => "ab"
13413 * 'abcdcd'.chomp('cd') # => "abcd"
13414 * 'abcd'.chomp('xx') # => "abcd"
13415 *
13416 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
13417 *
13418 */
13419
13420static VALUE
13421rb_str_chomp(int argc, VALUE *argv, VALUE str)
13422{
13423 VALUE rs = chomp_rs(argc, argv);
13424 if (NIL_P(rs)) return str_duplicate(rb_cString, str);
13425 return rb_str_subseq(str, 0, chompped_length(str, rs));
13426}
13427
13428static void
13429tr_setup_table_multi(char table[TR_TABLE_SIZE], VALUE *tablep, VALUE *ctablep,
13430 VALUE str, int num_selectors, VALUE *selectors)
13431{
13432 int i;
13433
13434 for (i=0; i<num_selectors; i++) {
13435 VALUE selector = selectors[i];
13436 rb_encoding *enc;
13437
13438 StringValue(selector);
13439 enc = rb_enc_check(str, selector);
13440 tr_setup_table(selector, table, i==0, tablep, ctablep, enc);
13441 }
13442}
13443
13444static rb_len_t
13445lstrip_offset(VALUE str, const char *s, const char *e, rb_encoding *enc)
13446{
13447 const char *const start = s;
13448
13449 if (!s || s >= e) return 0;
13450
13451 /* remove spaces at head */
13452 if (single_byte_optimizable(str)) {
13453 while (s < e && (*s == '\0' || ascii_isspace(*s))) s++;
13454 }
13455 else {
13456 while (s < e) {
13457 int n;
13458 unsigned int cc = rb_enc_codepoint_len(s, e, &n, enc);
13459
13460 if (cc && !rb_isspace(cc)) break;
13461 s += n;
13462 }
13463 }
13464 return s - start;
13465}
13466
13467static rb_len_t
13468lstrip_offset_table(VALUE str, const char *s, const char *e, rb_encoding *enc,
13469 char table[TR_TABLE_SIZE], VALUE del, VALUE nodel)
13470{
13471 const char *const start = s;
13472
13473 if (!s || s >= e) return 0;
13474
13475 /* remove leading characters in the table */
13476 while (s < e) {
13477 int n;
13478 unsigned int cc = rb_enc_codepoint_len(s, e, &n, enc);
13479
13480 if (!tr_find(cc, table, del, nodel)) break;
13481 s += n;
13482 }
13483 return s - start;
13484}
13485
13486/*
13487 * call-seq:
13488 * lstrip!(*selectors) -> self or nil
13489 *
13490 * Like String#lstrip, except that:
13491 *
13492 * - Performs stripping in +self+ (not in a copy of +self+).
13493 * - Returns +self+ if any characters are stripped, +nil+ otherwise.
13494 *
13495 * Related: see {Modifying}[rdoc-ref:String@Modifying].
13496 */
13497
13498static VALUE
13499rb_str_lstrip_bang(int argc, VALUE *argv, VALUE str)
13500{
13501 rb_encoding *enc;
13502 char *start;
13503 rb_len_t olen, loffset;
13504
13505 str_modify_keep_cr(str);
13506 enc = STR_ENC_GET(str);
13507 RSTRING_GETMEM(str, start, olen);
13508 if (argc > 0) {
13509 char table[TR_TABLE_SIZE];
13510 VALUE del = 0, nodel = 0;
13511
13512 tr_setup_table_multi(table, &del, &nodel, str, argc, argv);
13513
13514 /* the selector conversion may have modified str */
13515 str_modify_keep_cr(str);
13516 enc = STR_ENC_GET(str);
13517 RSTRING_GETMEM(str, start, olen);
13518
13519 loffset = lstrip_offset_table(str, start, start+olen, enc, table, del, nodel);
13520 }
13521 else {
13522 loffset = lstrip_offset(str, start, start+olen, enc);
13523 }
13524
13525 if (loffset > 0) {
13526 rb_len_t len = olen-loffset;
13527 memmove(start, start + loffset, len);
13528 STR_SET_LEN(str, len);
13529 TERM_FILL(start+len, rb_enc_mbminlen(enc));
13530 return str;
13531 }
13532 return Qnil;
13533}
13534
13535
13536/*
13537 * call-seq:
13538 * lstrip(*selectors) -> new_string
13539 *
13540 * Returns a copy of +self+ with leading whitespace removed;
13541 * see {Whitespace in Strings}[rdoc-ref:String@Whitespace+in+Strings]:
13542 *
13543 * whitespace = "\x00\t\n\v\f\r "
13544 * s = whitespace + 'abc' + whitespace
13545 * # => "\u0000\t\n\v\f\r abc\u0000\t\n\v\f\r "
13546 * s.lstrip
13547 * # => "abc\u0000\t\n\v\f\r "
13548 *
13549 * If +selectors+ are given, removes characters of +selectors+ from the beginning of +self+:
13550 *
13551 * s = "---abc+++"
13552 * s.lstrip("-") # => "abc+++"
13553 *
13554 * +selectors+ must be valid character selectors (see {Character Selectors}[rdoc-ref:character_selectors.rdoc]),
13555 * and may use any of its valid forms, including negation, ranges, and escapes:
13556 *
13557 * "01234abc56789".lstrip("0-9") # "abc56789"
13558 * "01234abc56789".lstrip("0-9", "^4-6") # "4abc56789"
13559 *
13560 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
13561 */
13562
13563static VALUE
13564rb_str_lstrip(int argc, VALUE *argv, VALUE str)
13565{
13566 const char *start;
13567 rb_len_t len, loffset;
13568
13569 RSTRING_GETMEM(str, start, len);
13570 if (argc > 0) {
13571 char table[TR_TABLE_SIZE];
13572 VALUE del = 0, nodel = 0;
13573
13574 tr_setup_table_multi(table, &del, &nodel, str, argc, argv);
13575
13576 /* the selector conversion may have modified str */
13577 RSTRING_GETMEM(str, start, len);
13578
13579 loffset = lstrip_offset_table(str, start, start+len, STR_ENC_GET(str), table, del, nodel);
13580 }
13581 else {
13582 loffset = lstrip_offset(str, start, start+len, STR_ENC_GET(str));
13583 }
13584 if (loffset <= 0) return str_duplicate(rb_cString, str);
13585 return rb_str_subseq(str, loffset, len - loffset);
13586}
13587
13588static rb_len_t
13589rstrip_offset(VALUE str, const char *s, const char *e, rb_encoding *enc)
13590{
13591 const char *t;
13592
13593 rb_str_check_dummy_enc(enc);
13594 if (rb_enc_str_coderange(str) == ENC_CODERANGE_BROKEN) {
13595 rb_raise(rb_eEncCompatError, "invalid byte sequence in %s", rb_enc_name(enc));
13596 }
13597 if (!s || s >= e) return 0;
13598 t = e;
13599
13600 /* remove trailing spaces or '\0's */
13601 if (single_byte_optimizable(str)) {
13602 unsigned char c;
13603 while (s < t && ((c = *(t-1)) == '\0' || ascii_isspace(c))) t--;
13604 }
13605 else {
13606 const char *tp;
13607
13608 while ((tp = rb_enc_prev_char(s, t, e, enc)) != NULL) {
13609 unsigned int c = rb_enc_codepoint(tp, e, enc);
13610 if (c && !rb_isspace(c)) break;
13611 t = tp;
13612 }
13613 }
13614 return e - t;
13615}
13616
13617static rb_len_t
13618rstrip_offset_table(VALUE str, const char *s, const char *e, rb_encoding *enc,
13619 char table[TR_TABLE_SIZE], VALUE del, VALUE nodel)
13620{
13621 const char *t, *tp;
13622
13623 rb_str_check_dummy_enc(enc);
13624 if (rb_enc_str_coderange(str) == ENC_CODERANGE_BROKEN) {
13625 rb_raise(rb_eEncCompatError, "invalid byte sequence in %s", rb_enc_name(enc));
13626 }
13627 if (!s || s >= e) return 0;
13628 t = e;
13629
13630 /* remove trailing characters in the table */
13631 while ((tp = rb_enc_prev_char(s, t, e, enc)) != NULL) {
13632 unsigned int c = rb_enc_codepoint(tp, e, enc);
13633 if (!tr_find(c, table, del, nodel)) break;
13634 t = tp;
13635 }
13636
13637 return e - t;
13638}
13639
13640/*
13641 * call-seq:
13642 * rstrip!(*selectors) -> self or nil
13643 *
13644 * Like String#rstrip, except that:
13645 *
13646 * - Performs stripping in +self+ (not in a copy of +self+).
13647 * - Returns +self+ if any characters are stripped, +nil+ otherwise.
13648 *
13649 * Related: see {Modifying}[rdoc-ref:String@Modifying].
13650 */
13651
13652static VALUE
13653rb_str_rstrip_bang(int argc, VALUE *argv, VALUE str)
13654{
13655 rb_encoding *enc;
13656 char *start;
13657 rb_len_t olen, roffset;
13658
13659 str_modify_keep_cr(str);
13660 enc = STR_ENC_GET(str);
13661 RSTRING_GETMEM(str, start, olen);
13662 if (argc > 0) {
13663 char table[TR_TABLE_SIZE];
13664 VALUE del = 0, nodel = 0;
13665
13666 tr_setup_table_multi(table, &del, &nodel, str, argc, argv);
13667
13668 /* the selector conversion may have modified str */
13669 str_modify_keep_cr(str);
13670 enc = STR_ENC_GET(str);
13671 RSTRING_GETMEM(str, start, olen);
13672
13673 roffset = rstrip_offset_table(str, start, start+olen, enc, table, del, nodel);
13674 }
13675 else {
13676 roffset = rstrip_offset(str, start, start+olen, enc);
13677 }
13678 if (roffset > 0) {
13679 rb_len_t len = olen - roffset;
13680
13681 STR_SET_LEN(str, len);
13682 TERM_FILL(start+len, rb_enc_mbminlen(enc));
13683 return str;
13684 }
13685 return Qnil;
13686}
13687
13688
13689/*
13690 * call-seq:
13691 * rstrip(*selectors) -> new_string
13692 *
13693 * Returns a copy of +self+ with trailing whitespace removed;
13694 * see {Whitespace in Strings}[rdoc-ref:String@Whitespace+in+Strings]:
13695 *
13696 * whitespace = "\x00\t\n\v\f\r "
13697 * s = whitespace + 'abc' + whitespace
13698 * s # => "\u0000\t\n\v\f\r abc\u0000\t\n\v\f\r "
13699 * s.rstrip # => "\u0000\t\n\v\f\r abc"
13700 *
13701 * If +selectors+ are given, removes characters of +selectors+ from the end of +self+:
13702 *
13703 * s = "---abc+++"
13704 * s.rstrip("+") # => "---abc"
13705 *
13706 * +selectors+ must be valid character selectors (see {Character Selectors}[rdoc-ref:character_selectors.rdoc]),
13707 * and may use any of its valid forms, including negation, ranges, and escapes:
13708 *
13709 * "01234abc56789".rstrip("0-9") # "01234abc"
13710 * "01234abc56789".rstrip("0-9", "^4-6") # "01234abc56"
13711 *
13712 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
13713 */
13714
13715static VALUE
13716rb_str_rstrip(int argc, VALUE *argv, VALUE str)
13717{
13718 rb_encoding *enc;
13719 const char *start;
13720 rb_len_t olen, roffset;
13721
13722 enc = STR_ENC_GET(str);
13723 RSTRING_GETMEM(str, start, olen);
13724 if (argc > 0) {
13725 char table[TR_TABLE_SIZE];
13726 VALUE del = 0, nodel = 0;
13727
13728 tr_setup_table_multi(table, &del, &nodel, str, argc, argv);
13729
13730 /* the selector conversion may have modified str */
13731 enc = STR_ENC_GET(str);
13732
13733 RSTRING_GETMEM(str, start, olen);
13734 roffset = rstrip_offset_table(str, start, start+olen, enc, table, del, nodel);
13735 }
13736 else {
13737 roffset = rstrip_offset(str, start, start+olen, enc);
13738 }
13739 if (roffset <= 0) return str_duplicate(rb_cString, str);
13740 return rb_str_subseq(str, 0, olen-roffset);
13741}
13742
13743
13744/*
13745 * call-seq:
13746 * strip!(*selectors) -> self or nil
13747 *
13748 * Like String#strip, except that:
13749 *
13750 * - Any modifications are made to +self+.
13751 * - Returns +self+ if any modification are made, +nil+ otherwise.
13752 *
13753 * Related: see {Modifying}[rdoc-ref:String@Modifying].
13754 */
13755
13756static VALUE
13757rb_str_strip_bang(int argc, VALUE *argv, VALUE str)
13758{
13759 char *start;
13760 rb_len_t olen, loffset, roffset;
13761 rb_encoding *enc;
13762
13763 str_modify_keep_cr(str);
13764 enc = STR_ENC_GET(str);
13765 RSTRING_GETMEM(str, start, olen);
13766
13767 if (argc > 0) {
13768 char table[TR_TABLE_SIZE];
13769 VALUE del = 0, nodel = 0;
13770
13771 tr_setup_table_multi(table, &del, &nodel, str, argc, argv);
13772
13773 /* the selector conversion may have modified str */
13774 str_modify_keep_cr(str);
13775 enc = STR_ENC_GET(str);
13776 RSTRING_GETMEM(str, start, olen);
13777
13778 loffset = lstrip_offset_table(str, start, start+olen, enc, table, del, nodel);
13779 roffset = rstrip_offset_table(str, start+loffset, start+olen, enc, table, del, nodel);
13780 }
13781 else {
13782 loffset = lstrip_offset(str, start, start+olen, enc);
13783 roffset = rstrip_offset(str, start+loffset, start+olen, enc);
13784 }
13785
13786 if (loffset > 0 || roffset > 0) {
13787 rb_len_t len = olen-roffset;
13788 if (loffset > 0) {
13789 len -= loffset;
13790 memmove(start, start + loffset, len);
13791 }
13792 STR_SET_LEN(str, len);
13793 TERM_FILL(start+len, rb_enc_mbminlen(enc));
13794 return str;
13795 }
13796 return Qnil;
13797}
13798
13799
13800/*
13801 * call-seq:
13802 * strip(*selectors) -> new_string
13803 *
13804 * Returns a copy of +self+ with leading and trailing whitespace removed;
13805 * see {Whitespace in Strings}[rdoc-ref:String@Whitespace+in+Strings]:
13806 *
13807 * whitespace = "\x00\t\n\v\f\r "
13808 * s = whitespace + 'abc' + whitespace
13809 * # => "\u0000\t\n\v\f\r abc\u0000\t\n\v\f\r "
13810 * s.strip # => "abc"
13811 *
13812 * If +selectors+ are given, removes characters of +selectors+ from both ends of +self+:
13813 *
13814 * s = "---abc+++"
13815 * s.strip("-+") # => "abc"
13816 * s.strip("+-") # => "abc"
13817 *
13818 * +selectors+ must be valid character selectors (see {Character Selectors}[rdoc-ref:character_selectors.rdoc]),
13819 * and may use any of its valid forms, including negation, ranges, and escapes:
13820 *
13821 * "01234abc56789".strip("0-9") # "abc"
13822 * "01234abc56789".strip("0-9", "^4-6") # "4abc56"
13823 *
13824 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
13825 */
13826
13827static VALUE
13828rb_str_strip(int argc, VALUE *argv, VALUE str)
13829{
13830 const char *start;
13831 rb_len_t olen, loffset, roffset;
13832 rb_encoding *enc = STR_ENC_GET(str);
13833
13834 RSTRING_GETMEM(str, start, olen);
13835
13836 if (argc > 0) {
13837 char table[TR_TABLE_SIZE];
13838 VALUE del = 0, nodel = 0;
13839
13840 tr_setup_table_multi(table, &del, &nodel, str, argc, argv);
13841
13842 /* the selector conversion may have modified str */
13843 enc = STR_ENC_GET(str);
13844 RSTRING_GETMEM(str, start, olen);
13845
13846 loffset = lstrip_offset_table(str, start, start+olen, enc, table, del, nodel);
13847 roffset = rstrip_offset_table(str, start+loffset, start+olen, enc, table, del, nodel);
13848 }
13849 else {
13850 loffset = lstrip_offset(str, start, start+olen, enc);
13851 roffset = rstrip_offset(str, start+loffset, start+olen, enc);
13852 }
13853
13854 if (loffset <= 0 && roffset <= 0) return str_duplicate(rb_cString, str);
13855 return rb_str_subseq(str, loffset, olen-loffset-roffset);
13856}
13857
13858static VALUE
13859scan_once(VALUE str, VALUE pat, rb_len_t *start, int set_backref_str)
13860{
13861 VALUE result = Qnil;
13862 rb_len_t end, pos = rb_pat_search(pat, str, *start, set_backref_str);
13863 if (pos >= 0) {
13864 VALUE match = Qnil;
13865 if (BUILTIN_TYPE(pat) == T_STRING) {
13866 end = pos + RSTRING_LEN(pat);
13867 }
13868 else {
13869 match = rb_backref_get();
13870 pos = RMATCH_BEG(match, 0);
13871 end = RMATCH_END(match, 0);
13872 }
13873
13874 if (pos == end) {
13875 rb_encoding *enc = STR_ENC_GET(str);
13876 /*
13877 * Always consume at least one character of the input string
13878 */
13879 if (RSTRING_LEN(str) > end)
13880 *start = end + rb_enc_fast_mbclen(RSTRING_PTR(str) + end,
13881 RSTRING_END(str), enc);
13882 else
13883 *start = end + 1;
13884 }
13885 else {
13886 *start = end;
13887 }
13888
13889 if (NIL_P(match) || RMATCH_NREGS(match) == 1) {
13890 result = rb_str_subseq(str, pos, end - pos);
13891 return result;
13892 }
13893 else {
13894 int num_regs = RMATCH_NREGS(match);
13895 result = rb_ary_new2(num_regs);
13896 for (int i = 1; i < num_regs; i++) {
13897 VALUE s = Qnil;
13898 if (RMATCH_BEG(match, i) >= 0) {
13899 s = rb_str_subseq(str, RMATCH_BEG(match, i), RMATCH_END(match, i) - RMATCH_BEG(match, i));
13900 }
13901
13902 rb_ary_push(result, s);
13903 }
13904 }
13905
13906 RB_GC_GUARD(match);
13907 }
13908
13909 return result;
13910}
13911
13912
13913/*
13914 * call-seq:
13915 * scan(pattern) -> array_of_results
13916 * scan(pattern) {|result| ... } -> self
13917 *
13918 * Matches a pattern against +self+:
13919 *
13920 * - If +pattern+ is a Regexp, the pattern used is +pattern+ itself.
13921 * - If +pattern+ is a string, the pattern used is <tt>Regexp.quote(pattern)</tt>.
13922 *
13923 * Generates a collection of matching results
13924 * and updates {regexp-related global variables}[rdoc-ref:Regexp@Global+Variables]:
13925 *
13926 * - If the pattern contains no groups, each result is a matched substring.
13927 * - If the pattern contains groups, each result is an array
13928 * containing a matched substring for each group.
13929 *
13930 * With no block given, returns an array of the results:
13931 *
13932 * 'cruel world'.scan(/\w+/) # => ["cruel", "world"]
13933 * 'cruel world'.scan(/.../) # => ["cru", "el ", "wor"]
13934 * 'cruel world'.scan(/(...)/) # => [["cru"], ["el "], ["wor"]]
13935 * 'cruel world'.scan(/(..)(..)/) # => [["cr", "ue"], ["l ", "wo"]]
13936 * 'こんにちは'.scan(/../) # => ["こん", "にち"]
13937 * 'abracadabra'.scan('ab') # => ["ab", "ab"]
13938 * 'abracadabra'.scan('nosuch') # => []
13939 *
13940 * With a block given, calls the block with each result; returns +self+:
13941 *
13942 * 'cruel world'.scan(/\w+/) {|w| p w }
13943 * # => "cruel"
13944 * # => "world"
13945 * 'cruel world'.scan(/(.)(.)/) {|x, y| p [x, y] }
13946 * # => ["c", "r"]
13947 * # => ["u", "e"]
13948 * # => ["l", " "]
13949 * # => ["w", "o"]
13950 * # => ["r", "l"]
13951 *
13952 * Related: see {Converting to Non-String}[rdoc-ref:String@Converting+to+Non-String].
13953 *
13954 */
13955
13956static VALUE
13957rb_str_scan(VALUE str, VALUE pat)
13958{
13959 VALUE result;
13960 rb_len_t start = 0;
13961 rb_len_t last = -1, prev = 0;
13962 const char *p = RSTRING_PTR(str);
13963 rb_len_t len = RSTRING_LEN(str);
13964
13965 pat = get_pat_quoted(pat, 1);
13966 mustnot_broken(str);
13967 if (!rb_block_given_p()) {
13968 VALUE ary = rb_ary_new();
13969
13970 while (!NIL_P(result = scan_once(str, pat, &start, 0))) {
13971 last = prev;
13972 prev = start;
13973 rb_ary_push(ary, result);
13974 }
13975 if (last >= 0) rb_pat_search(pat, str, last, 1);
13976 else rb_backref_set(Qnil);
13977 return ary;
13978 }
13979
13980 while (!NIL_P(result = scan_once(str, pat, &start, 1))) {
13981 last = prev;
13982 prev = start;
13983 rb_yield(result);
13984 str_mod_check(str, p, len);
13985 }
13986 if (last >= 0) rb_pat_search(pat, str, last, 1);
13987 return str;
13988}
13989
13990
13991/*
13992 * call-seq:
13993 * hex -> integer
13994 *
13995 * Interprets the leading substring of +self+ as hexadecimal, possibly signed;
13996 * returns its value as an integer.
13997 *
13998 * The leading substring is interpreted as hexadecimal when it begins with:
13999 *
14000 * - One or more character representing hexadecimal digits
14001 * (each in one of the ranges <tt>'0'..'9'</tt>, <tt>'a'..'f'</tt>, or <tt>'A'..'F'</tt>);
14002 * the string to be interpreted ends at the first character that does not represent a hexadecimal digit:
14003 *
14004 * 'f'.hex # => 15
14005 * '11'.hex # => 17
14006 * 'FFF'.hex # => 4095
14007 * 'fffg'.hex # => 4095
14008 * 'foo'.hex # => 15 # 'f' hexadecimal, 'oo' not.
14009 * 'bar'.hex # => 186 # 'ba' hexadecimal, 'r' not.
14010 * 'deadbeef'.hex # => 3735928559
14011 *
14012 * - <tt>'0x'</tt> or <tt>'0X'</tt>, followed by one or more hexadecimal digits:
14013 *
14014 * '0xfff'.hex # => 4095
14015 * '0xfffg'.hex # => 4095
14016 *
14017 * Any of the above may prefixed with <tt>'-'</tt>, which negates the interpreted value:
14018 *
14019 * '-fff'.hex # => -4095
14020 * '-0xFFF'.hex # => -4095
14021 *
14022 * For any substring not described above, returns zero:
14023 *
14024 * 'xxx'.hex # => 0
14025 * ''.hex # => 0
14026 *
14027 * Note that, unlike #oct, this method interprets only hexadecimal,
14028 * and not binary, octal, or decimal notations:
14029 *
14030 * '0b111'.hex # => 45329
14031 * '0o777'.hex # => 0
14032 * '0d999'.hex # => 55705
14033 *
14034 * Related: See {Converting to Non-String}[rdoc-ref:String@Converting+to+Non-String].
14035 */
14036
14037static VALUE
14038rb_str_hex(VALUE str)
14039{
14040 return rb_str_to_inum(str, 16, FALSE);
14041}
14042
14043
14044/*
14045 * call-seq:
14046 * oct -> integer
14047 *
14048 * Interprets the leading substring of +self+ as octal, binary, decimal, or hexadecimal, possibly signed;
14049 * returns their value as an integer.
14050 *
14051 * In brief:
14052 *
14053 * # Interpreted as octal.
14054 * '777'.oct # => 511
14055 * '777x'.oct # => 511
14056 * '0777'.oct # => 511
14057 * '0o777'.oct # => 511
14058 * '-777'.oct # => -511
14059 * # Not interpreted as octal.
14060 * '0b111'.oct # => 7 # Interpreted as binary.
14061 * '0d999'.oct # => 999 # Interpreted as decimal.
14062 * '0xfff'.oct # => 4095 # Interpreted as hexadecimal.
14063 *
14064 * The leading substring is interpreted as octal when it begins with:
14065 *
14066 * - One or more character representing octal digits
14067 * (each in the range <tt>'0'..'7'</tt>);
14068 * the string to be interpreted ends at the first character that does not represent an octal digit:
14069 *
14070 * '7'.oct @ => 7
14071 * '11'.oct # => 9
14072 * '777'.oct # => 511
14073 * '0777'.oct # => 511
14074 * '7778'.oct # => 511
14075 * '777x'.oct # => 511
14076 *
14077 * - <tt>'0o'</tt>, followed by one or more octal digits:
14078 *
14079 * '0o777'.oct # => 511
14080 * '0o7778'.oct # => 511
14081 *
14082 * The leading substring is _not_ interpreted as octal when it begins with:
14083 *
14084 * - <tt>'0b'</tt>, followed by one or more characters representing binary digits
14085 * (each in the range <tt>'0'..'1'</tt>);
14086 * the string to be interpreted ends at the first character that does not represent a binary digit.
14087 * the string is interpreted as binary digits (base 2):
14088 *
14089 * '0b111'.oct # => 7
14090 * '0b1112'.oct # => 7
14091 *
14092 * - <tt>'0d'</tt>, followed by one or more characters representing decimal digits
14093 * (each in the range <tt>'0'..'9'</tt>);
14094 * the string to be interpreted ends at the first character that does not represent a decimal digit.
14095 * the string is interpreted as decimal digits (base 10):
14096 *
14097 * '0d999'.oct # => 999
14098 * '0d999x'.oct # => 999
14099 *
14100 * - <tt>'0x'</tt>, followed by one or more characters representing hexadecimal digits
14101 * (each in one of the ranges <tt>'0'..'9'</tt>, <tt>'a'..'f'</tt>, or <tt>'A'..'F'</tt>);
14102 * the string to be interpreted ends at the first character that does not represent a hexadecimal digit.
14103 * the string is interpreted as hexadecimal digits (base 16):
14104 *
14105 * '0xfff'.oct # => 4095
14106 * '0xfffg'.oct # => 4095
14107 *
14108 * Any of the above may prefixed with <tt>'-'</tt>, which negates the interpreted value:
14109 *
14110 * '-777'.oct # => -511
14111 * '-0777'.oct # => -511
14112 * '-0b111'.oct # => -7
14113 * '-0xfff'.oct # => -4095
14114 *
14115 * For any substring not described above, returns zero:
14116 *
14117 * 'foo'.oct # => 0
14118 * ''.oct # => 0
14119 *
14120 * Related: see {Converting to Non-String}[rdoc-ref:String@Converting+to+Non-String].
14121 */
14122
14123static VALUE
14124rb_str_oct(VALUE str)
14125{
14126 return rb_str_to_inum(str, -8, FALSE);
14127}
14128
14129#ifndef HAVE_CRYPT_R
14130# include "ruby/thread_native.h"
14131# include "ruby/atomic.h"
14132
14133static struct {
14134 rb_nativethread_lock_t lock;
14135} crypt_mutex = {PTHREAD_MUTEX_INITIALIZER};
14136#endif
14137
14138/*
14139 * call-seq:
14140 * crypt(salt_str) -> new_string
14141 *
14142 * Returns the string generated by calling <code>crypt(3)</code>
14143 * standard library function with <code>str</code> and
14144 * <code>salt_str</code>, in this order, as its arguments. Please do
14145 * not use this method any longer. It is legacy; provided only for
14146 * backward compatibility with ruby scripts in earlier days. It is
14147 * bad to use in contemporary programs for several reasons:
14148 *
14149 * * Behaviour of C's <code>crypt(3)</code> depends on the OS it is
14150 * run. The generated string lacks data portability.
14151 *
14152 * * On some OSes such as Mac OS, <code>crypt(3)</code> never fails
14153 * (i.e. silently ends up in unexpected results).
14154 *
14155 * * On some OSes such as Mac OS, <code>crypt(3)</code> is not
14156 * thread safe.
14157 *
14158 * * So-called "traditional" usage of <code>crypt(3)</code> is very
14159 * very very weak. According to its manpage, Linux's traditional
14160 * <code>crypt(3)</code> output has only 2**56 variations; too
14161 * easy to brute force today. And this is the default behaviour.
14162 *
14163 * * In order to make things robust some OSes implement so-called
14164 * "modular" usage. To go through, you have to do a complex
14165 * build-up of the <code>salt_str</code> parameter, by hand.
14166 * Failure in generation of a proper salt string tends not to
14167 * yield any errors; typos in parameters are normally not
14168 * detectable.
14169 *
14170 * * For instance, in the following example, the second invocation
14171 * of String#crypt is wrong; it has a typo in "round=" (lacks
14172 * "s"). However the call does not fail and something unexpected
14173 * is generated.
14174 *
14175 * "foo".crypt("$5$rounds=1000$salt$") # OK, proper usage
14176 * "foo".crypt("$5$round=1000$salt$") # Typo not detected
14177 *
14178 * * Even in the "modular" mode, some hash functions are considered
14179 * archaic and no longer recommended at all; for instance module
14180 * <code>$1$</code> is officially abandoned by its author: see
14181 * http://phk.freebsd.dk/sagas/md5crypt_eol/ . For another
14182 * instance module <code>$3$</code> is considered completely
14183 * broken: see the manpage of FreeBSD.
14184 *
14185 * * On some OS such as Mac OS, there is no modular mode. Yet, as
14186 * written above, <code>crypt(3)</code> on Mac OS never fails.
14187 * This means even if you build up a proper salt string it
14188 * generates a traditional DES hash anyways, and there is no way
14189 * for you to be aware of.
14190 *
14191 * "foo".crypt("$5$rounds=1000$salt$") # => "$5fNPQMxC5j6."
14192 *
14193 * If for some reason you cannot migrate to other secure contemporary
14194 * password hashing algorithms, install the string-crypt gem and
14195 * <code>require 'string/crypt'</code> to continue using it.
14196 */
14197
14198static VALUE
14199rb_str_crypt(VALUE str, VALUE salt)
14200{
14201#ifdef HAVE_CRYPT_R
14202 VALUE databuf;
14203 struct crypt_data *data;
14204# define CRYPT_END() ALLOCV_END(databuf)
14205#else
14206 char *tmp_buf;
14207 extern char *crypt(const char *, const char *);
14208# define CRYPT_END() rb_nativethread_lock_unlock(&crypt_mutex.lock)
14209#endif
14210 VALUE result;
14211 const char *s, *saltp, *res;
14212#ifdef BROKEN_CRYPT
14213 char salt_8bit_clean[3];
14214#endif
14215
14216 StringValue(salt);
14217 mustnot_wchar(str);
14218 mustnot_wchar(salt);
14219 s = StringValueCStr(str);
14220 saltp = RSTRING_PTR(salt);
14221 if (RSTRING_LEN(salt) < 2 || !saltp[0] || !saltp[1]) {
14222 rb_raise(rb_eArgError, "salt too short (need >=2 bytes)");
14223 }
14224
14225#ifdef BROKEN_CRYPT
14226 if (!ISASCII((unsigned char)saltp[0]) || !ISASCII((unsigned char)saltp[1])) {
14227 salt_8bit_clean[0] = saltp[0] & 0x7f;
14228 salt_8bit_clean[1] = saltp[1] & 0x7f;
14229 salt_8bit_clean[2] = '\0';
14230 saltp = salt_8bit_clean;
14231 }
14232#endif
14233#ifdef HAVE_CRYPT_R
14234 data = ALLOCV(databuf, sizeof(struct crypt_data));
14235# ifdef HAVE_STRUCT_CRYPT_DATA_INITIALIZED
14236 data->initialized = 0;
14237# endif
14238 res = crypt_r(s, saltp, data);
14239#else
14240 rb_nativethread_lock_lock(&crypt_mutex.lock);
14241 res = crypt(s, saltp);
14242#endif
14243 if (!res) {
14244 int err = errno;
14245 CRYPT_END();
14246 rb_syserr_fail(err, "crypt");
14247 }
14248#ifdef HAVE_CRYPT_R
14249 result = rb_str_new_cstr(res);
14250 CRYPT_END();
14251#else
14252 // We need to copy this buffer because it's static and we need to unlock the mutex
14253 // before allocating a new object (the string to be returned). If we allocate while
14254 // holding the lock, we could run GC which fires the VM barrier and causes a deadlock
14255 // if other ractors are waiting on this lock.
14256 size_t res_size = strlen(res);
14257 tmp_buf = ALLOCA_N(char, res_size); // should be small enough to alloca
14258 memcpy(tmp_buf, res, res_size);
14259 CRYPT_END();
14260 result = rb_str_new(tmp_buf, res_size);
14261#endif
14262 return result;
14263}
14264
14265
14266/*
14267 * call-seq:
14268 * ord -> integer
14269 *
14270 * Returns the integer ordinal of the first character of +self+:
14271 *
14272 * 'h'.ord # => 104
14273 * 'hello'.ord # => 104
14274 * 'こんにちは'.ord # => 12371
14275 *
14276 * Related: see {Converting to Non-String}[rdoc-ref:String@Converting+to+Non-String].
14277 *
14278 */
14279
14280static VALUE
14281rb_str_ord(VALUE s)
14282{
14283 unsigned int c;
14284
14285 c = rb_enc_codepoint(RSTRING_PTR(s), RSTRING_END(s), STR_ENC_GET(s));
14286 return UINT2NUM(c);
14287}
14288/*
14289 * call-seq:
14290 * sum(n = 16) -> integer
14291 *
14292 * Returns a basic +n+-bit {checksum}[https://en.wikipedia.org/wiki/Checksum] of the characters in +self+;
14293 * the checksum is the sum of the binary value of each byte in +self+,
14294 * modulo <tt>2**n - 1</tt>:
14295 *
14296 * 'hello'.sum # => 532
14297 * 'hello'.sum(4) # => 4
14298 * 'hello'.sum(64) # => 532
14299 * 'こんにちは'.sum # => 2582
14300 *
14301 * This is not a particularly strong checksum.
14302 *
14303 * Related: see {Querying}[rdoc-ref:String@Querying].
14304 *
14305 */
14306
14307static VALUE
14308rb_str_sum(int argc, VALUE *argv, VALUE str)
14309{
14310 int bits = 16;
14311 char *ptr, *p, *pend;
14312 rb_len_t len;
14313 VALUE sum = INT2FIX(0);
14314 unsigned long sum0 = 0;
14315
14316 if (rb_check_arity(argc, 0, 1) && (bits = NUM2INT(argv[0])) < 0) {
14317 bits = 0;
14318 }
14319 ptr = p = RSTRING_PTR(str);
14320 len = RSTRING_LEN(str);
14321 pend = p + len;
14322
14323 while (p < pend) {
14324 if (FIXNUM_MAX - UCHAR_MAX < sum0) {
14325 sum = rb_funcall(sum, '+', 1, LONG2FIX(sum0));
14326 str_mod_check(str, ptr, len);
14327 sum0 = 0;
14328 }
14329 sum0 += (unsigned char)*p;
14330 p++;
14331 }
14332
14333 if (bits == 0) {
14334 if (sum0) {
14335 sum = rb_funcall(sum, '+', 1, LONG2FIX(sum0));
14336 }
14337 }
14338 else {
14339 if (sum == INT2FIX(0)) {
14340 if (bits < (int)sizeof(long)*CHAR_BIT) {
14341 sum0 &= (((unsigned long)1)<<bits)-1;
14342 }
14343 sum = LONG2FIX(sum0);
14344 }
14345 else {
14346 VALUE mod;
14347
14348 if (sum0) {
14349 sum = rb_funcall(sum, '+', 1, LONG2FIX(sum0));
14350 }
14351
14352 mod = rb_funcall(INT2FIX(1), idLTLT, 1, INT2FIX(bits));
14353 mod = rb_funcall(mod, '-', 1, INT2FIX(1));
14354 sum = rb_funcall(sum, '&', 1, mod);
14355 }
14356 }
14357 return sum;
14358}
14359
14360static VALUE
14361rb_str_justify(int argc, VALUE *argv, VALUE str, char jflag)
14362{
14363 rb_encoding *enc;
14364 VALUE w;
14365 rb_len_t width, len, flen = 1, fclen = 1;
14366 VALUE res;
14367 char *p;
14368 const char *f = " ";
14369 rb_len_t n, size, llen, rlen, llen2 = 0, rlen2 = 0;
14370 VALUE pad;
14371 int singlebyte = 1, cr;
14372 int termlen;
14373
14374 rb_scan_args(argc, argv, "11", &w, &pad);
14375 enc = STR_ENC_GET(str);
14376 width = NUM2LEN(w);
14377 if (argc == 2) {
14378 StringValue(pad);
14379 enc = rb_enc_check(str, pad);
14380 f = RSTRING_PTR(pad);
14381 flen = RSTRING_LEN(pad);
14382 fclen = str_strlen(pad, enc); /* rb_enc_check */
14383 singlebyte = single_byte_optimizable(pad);
14384 if (flen == 0 || fclen == 0) {
14385 rb_raise(rb_eArgError, "zero width padding");
14386 }
14387 }
14388 termlen = rb_enc_mbminlen(enc);
14389 len = str_strlen(str, enc); /* rb_enc_check */
14390 if (width < 0 || len >= width) return str_duplicate(rb_cString, str);
14391 n = width - len;
14392 llen = (jflag == 'l') ? 0 : ((jflag == 'r') ? n : n/2);
14393 rlen = n - llen;
14394 cr = ENC_CODERANGE(str);
14395 if (flen > 1) {
14396 llen2 = str_offset(f, f + flen, llen % fclen, enc, singlebyte);
14397 rlen2 = str_offset(f, f + flen, rlen % fclen, enc, singlebyte);
14398 }
14399 size = RSTRING_LEN(str);
14400 if ((len = llen / fclen + rlen / fclen) >= RB_LEN_MAX / flen ||
14401 (len *= flen) >= RB_LEN_MAX - llen2 - rlen2 ||
14402 (len += llen2 + rlen2) >= RB_LEN_MAX - size) {
14403 rb_raise(rb_eArgError, "argument too big");
14404 }
14405 len += size;
14406 res = str_enc_new(rb_cString, 0, len, enc);
14407 p = RSTRING_PTR(res);
14408 if (flen <= 1) {
14409 memset(p, *f, llen);
14410 p += llen;
14411 }
14412 else {
14413 while (llen >= fclen) {
14414 memcpy(p,f,flen);
14415 p += flen;
14416 llen -= fclen;
14417 }
14418 if (llen > 0) {
14419 memcpy(p, f, llen2);
14420 p += llen2;
14421 }
14422 }
14423 memcpy(p, RSTRING_PTR(str), size);
14424 p += size;
14425 if (flen <= 1) {
14426 memset(p, *f, rlen);
14427 p += rlen;
14428 }
14429 else {
14430 while (rlen >= fclen) {
14431 memcpy(p,f,flen);
14432 p += flen;
14433 rlen -= fclen;
14434 }
14435 if (rlen > 0) {
14436 memcpy(p, f, rlen2);
14437 p += rlen2;
14438 }
14439 }
14440 TERM_FILL(p, termlen);
14441 STR_SET_LEN(res, p-RSTRING_PTR(res));
14442
14443 if (argc == 2)
14444 cr = ENC_CODERANGE_AND(cr, ENC_CODERANGE(pad));
14445 if (cr != ENC_CODERANGE_BROKEN)
14446 ENC_CODERANGE_SET(res, cr);
14447
14448 RB_GC_GUARD(pad);
14449 return res;
14450}
14451
14452
14453/*
14454 * call-seq:
14455 * ljust(width, pad_string = ' ') -> new_string
14456 *
14457 * Returns a copy of +self+, left-justified and, if necessary, right-padded with the +pad_string+:
14458 *
14459 * 'hello'.ljust(10) # => "hello "
14460 * ' hello'.ljust(10) # => " hello "
14461 * 'hello'.ljust(10, 'ab') # => "helloababa"
14462 * 'こんにちは'.ljust(10) # => "こんにちは "
14463 *
14464 * If <tt>width <= self.length</tt>, returns a copy of +self+:
14465 *
14466 * 'hello'.ljust(5) # => "hello"
14467 * 'hello'.ljust(1) # => "hello" # Does not truncate to width.
14468 *
14469 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
14470 *
14471 */
14472
14473static VALUE
14474rb_str_ljust(int argc, VALUE *argv, VALUE str)
14475{
14476 return rb_str_justify(argc, argv, str, 'l');
14477}
14478
14479/*
14480 * call-seq:
14481 * rjust(width, pad_string = ' ') -> new_string
14482 *
14483 * Returns a right-justified copy of +self+.
14484 *
14485 * If integer argument +width+ is greater than the size (in characters) of +self+,
14486 * returns a new string of length +width+ that is a copy of +self+,
14487 * right justified and padded on the left with +pad_string+:
14488 *
14489 * 'hello'.rjust(10) # => " hello"
14490 * 'hello '.rjust(10) # => " hello "
14491 * 'hello'.rjust(10, 'ab') # => "ababahello"
14492 * 'こんにちは'.rjust(10) # => " こんにちは"
14493 *
14494 * If <tt>width <= self.size</tt>, returns a copy of +self+:
14495 *
14496 * 'hello'.rjust(5, 'ab') # => "hello"
14497 * 'hello'.rjust(1, 'ab') # => "hello"
14498 *
14499 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
14500 *
14501 */
14502
14503static VALUE
14504rb_str_rjust(int argc, VALUE *argv, VALUE str)
14505{
14506 return rb_str_justify(argc, argv, str, 'r');
14507}
14508
14509
14510/*
14511 * call-seq:
14512 * center(size, pad_string = ' ') -> new_string
14513 *
14514 * Returns a centered copy of +self+.
14515 *
14516 * If integer argument +size+ is greater than the size (in characters) of +self+,
14517 * returns a new string of length +size+ that is a copy of +self+,
14518 * centered and padded on one or both ends with +pad_string+:
14519 *
14520 * 'hello'.center(6) # => "hello " # Padded on one end.
14521 * 'hello'.center(10) # => " hello " # Padded on both ends.
14522 * 'hello'.center(20, '-|') # => "-|-|-|-hello-|-|-|-|" # Some padding repeated.
14523 * 'hello'.center(10, 'abcdefg') # => "abhelloabc" # Some padding not used.
14524 * ' hello '.center(13) # => " hello "
14525 * 'こんにちは'.center(10) # => " こんにちは " # Multi-byte characters.
14526 *
14527 * If +size+ is less than or equal to the size of +self+, returns an unpadded copy of +self+:
14528 *
14529 * 'hello'.center(5) # => "hello"
14530 * 'hello'.center(-10) # => "hello"
14531 *
14532 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
14533 *
14534 */
14535
14536static VALUE
14537rb_str_center(int argc, VALUE *argv, VALUE str)
14538{
14539 return rb_str_justify(argc, argv, str, 'c');
14540}
14541
14542/*
14543 * call-seq:
14544 * partition(pattern) -> [pre_match, first_match, post_match]
14545 *
14546 * Returns a 3-element array of substrings of +self+.
14547 *
14548 * If +pattern+ is matched, returns the array:
14549 *
14550 * [pre_match, first_match, post_match]
14551 *
14552 * where:
14553 *
14554 * - +first_match+ is the first-found matching substring.
14555 * - +pre_match+ and +post_match+ are the preceding and following substrings.
14556 *
14557 * If +pattern+ is not matched, returns the array:
14558 *
14559 * [self.dup, "", ""]
14560 *
14561 * Note that in the examples below, a returned string <tt>'hello'</tt>
14562 * is a copy of +self+, not +self+.
14563 *
14564 * If +pattern+ is a Regexp, performs the equivalent of <tt>self.match(pattern)</tt>
14565 * (also setting {matched-data variables}[rdoc-ref:language/globals.md@Matched+Data]):
14566 *
14567 * 'hello'.partition(/h/) # => ["", "h", "ello"]
14568 * 'hello'.partition(/l/) # => ["he", "l", "lo"]
14569 * 'hello'.partition(/l+/) # => ["he", "ll", "o"]
14570 * 'hello'.partition(/o/) # => ["hell", "o", ""]
14571 * 'hello'.partition(/^/) # => ["", "", "hello"]
14572 * 'hello'.partition(//) # => ["", "", "hello"]
14573 * 'hello'.partition(/$/) # => ["hello", "", ""]
14574 * 'hello'.partition(/x/) # => ["hello", "", ""]
14575 *
14576 * If +pattern+ is not a Regexp, converts it to a string (if it is not already one),
14577 * then performs the equivalent of <tt>self.index(pattern)</tt>
14578 * (and does _not_ set {matched-data global variables}[rdoc-ref:language/globals.md@Matched+Data]):
14579 *
14580 * 'hello'.partition('h') # => ["", "h", "ello"]
14581 * 'hello'.partition('l') # => ["he", "l", "lo"]
14582 * 'hello'.partition('ll') # => ["he", "ll", "o"]
14583 * 'hello'.partition('o') # => ["hell", "o", ""]
14584 * 'hello'.partition('') # => ["", "", "hello"]
14585 * 'hello'.partition('x') # => ["hello", "", ""]
14586 * 'こんにちは'.partition('に') # => ["こん", "に", "ちは"]
14587 *
14588 * Related: see {Converting to Non-String}[rdoc-ref:String@Converting+to+Non-String].
14589 *
14590 */
14591
14592static VALUE
14593rb_str_partition(VALUE str, VALUE sep)
14594{
14595 rb_len_t pos;
14596
14597 sep = get_pat_quoted(sep, 0);
14598 if (RB_TYPE_P(sep, T_REGEXP)) {
14599 if (rb_reg_search(sep, str, 0, 0) < 0) {
14600 goto failed;
14601 }
14602 VALUE match = rb_backref_get();
14603
14604 pos = RMATCH_BEG(match, 0);
14605 sep = rb_str_subseq(str, pos, RMATCH_END(match, 0) - pos);
14606 }
14607 else {
14608 pos = rb_str_index(str, sep, 0);
14609 if (pos < 0) goto failed;
14610 }
14611
14612 rb_len_t rpos = pos + RSTRING_LEN(sep);
14613 if (rpos > RSTRING_LEN(str)) goto failed;
14614 return rb_ary_new3(3, rb_str_subseq(str, 0, pos),
14615 sep,
14616 rb_str_subseq(str, rpos, RSTRING_LEN(str)-rpos));
14617
14618 failed:
14619 return rb_ary_new3(3, str_duplicate(rb_cString, str), str_new_empty_String(str), str_new_empty_String(str));
14620}
14621
14622/*
14623 * call-seq:
14624 * rpartition(pattern) -> [pre_match, last_match, post_match]
14625 *
14626 * Returns a 3-element array of substrings of +self+.
14627 *
14628 * Searches +self+ for a match of +pattern+, seeking the _last_ match.
14629 *
14630 * If +pattern+ is not matched, returns the array:
14631 *
14632 * ["", "", self.dup]
14633 *
14634 * If +pattern+ is matched, returns the array:
14635 *
14636 * [pre_match, last_match, post_match]
14637 *
14638 * where:
14639 *
14640 * - +last_match+ is the last-found matching substring.
14641 * - +pre_match+ and +post_match+ are the preceding and following substrings.
14642 *
14643 * The pattern used is:
14644 *
14645 * - +pattern+ itself, if it is a Regexp.
14646 * - <tt>Regexp.quote(pattern)</tt>, if +pattern+ is a string.
14647 *
14648 * Note that in the examples below, a returned string <tt>'hello'</tt> is a copy of +self+, not +self+.
14649 *
14650 * If +pattern+ is a Regexp, searches for the last matching substring
14651 * (also setting {matched-data global variables}[rdoc-ref:language/globals.md@Matched+Data]):
14652 *
14653 * 'hello'.rpartition(/l/) # => ["hel", "l", "o"]
14654 * 'hello'.rpartition(/ll/) # => ["he", "ll", "o"]
14655 * 'hello'.rpartition(/h/) # => ["", "h", "ello"]
14656 * 'hello'.rpartition(/o/) # => ["hell", "o", ""]
14657 * 'hello'.rpartition(//) # => ["hello", "", ""]
14658 * 'hello'.rpartition(/x/) # => ["", "", "hello"]
14659 * 'こんにちは'.rpartition(/に/) # => ["こん", "に", "ちは"]
14660 *
14661 * If +pattern+ is not a Regexp, converts it to a string (if it is not already one),
14662 * then searches for the last matching substring
14663 * (and does _not_ set {matched-data global variables}[rdoc-ref:language/globals.md@Matched+Data]):
14664 *
14665 * 'hello'.rpartition('l') # => ["hel", "l", "o"]
14666 * 'hello'.rpartition('ll') # => ["he", "ll", "o"]
14667 * 'hello'.rpartition('h') # => ["", "h", "ello"]
14668 * 'hello'.rpartition('o') # => ["hell", "o", ""]
14669 * 'hello'.rpartition('') # => ["hello", "", ""]
14670 * 'こんにちは'.rpartition('に') # => ["こん", "に", "ちは"]
14671 *
14672 * Related: see {Converting to Non-String}[rdoc-ref:String@Converting+to+Non-String].
14673 *
14674 */
14675
14676static VALUE
14677rb_str_rpartition(VALUE str, VALUE sep)
14678{
14679 rb_len_t pos;
14680
14681 sep = get_pat_quoted(sep, 0);
14682 if (RB_TYPE_P(sep, T_REGEXP)) {
14683 pos = RSTRING_LEN(str);
14684 if (rb_reg_search(sep, str, pos, 1) < 0) {
14685 goto failed;
14686 }
14687 VALUE match = rb_backref_get();
14688
14689 pos = RMATCH_BEG(match, 0);
14690 sep = rb_str_subseq(str, pos, RMATCH_END(match, 0) - pos);
14691 }
14692 else {
14693 /* str may have been modified by #to_str above */
14694 pos = rb_str_sublen(str, RSTRING_LEN(str));
14695 pos = rb_str_rindex(str, sep, pos);
14696 if (pos < 0) {
14697 goto failed;
14698 }
14699 }
14700
14701 rb_len_t rpos = pos + RSTRING_LEN(sep);
14702 if (rpos > RSTRING_LEN(str)) goto failed;
14703 return rb_ary_new3(3, rb_str_subseq(str, 0, pos),
14704 sep,
14705 rb_str_subseq(str, rpos, RSTRING_LEN(str)-rpos));
14706 failed:
14707 return rb_ary_new3(3, str_new_empty_String(str), str_new_empty_String(str), str_duplicate(rb_cString, str));
14708}
14709
14710/*
14711 * call-seq:
14712 * start_with?(*patterns) -> true or false
14713 *
14714 * Returns whether +self+ starts with any of the given +patterns+.
14715 *
14716 * For each argument, the pattern used is:
14717 *
14718 * - The pattern itself, if it is a Regexp.
14719 * - <tt>Regexp.quote(pattern)</tt>, if it is a string.
14720 *
14721 * Returns +true+ if any pattern matches the beginning, +false+ otherwise:
14722 *
14723 * 'hello'.start_with?('hell') # => true
14724 * 'hello'.start_with?(/H/i) # => true
14725 * 'hello'.start_with?('heaven', 'hell') # => true
14726 * 'hello'.start_with?('heaven', 'paradise') # => false
14727 * 'こんにちは'.start_with?('こ') # => true
14728 *
14729 * Related: see {Querying}[rdoc-ref:String@Querying].
14730 *
14731 */
14732
14733static VALUE
14734rb_str_start_with(int argc, VALUE *argv, VALUE str)
14735{
14736 int i;
14737
14738 for (i=0; i<argc; i++) {
14739 VALUE tmp = argv[i];
14740 if (RB_TYPE_P(tmp, T_REGEXP)) {
14741 if (rb_reg_start_with_p(tmp, str))
14742 return Qtrue;
14743 }
14744 else {
14745 const char *p, *s, *e;
14746 rb_len_t slen, tlen;
14747 rb_encoding *enc;
14748
14749 StringValue(tmp);
14750 enc = rb_enc_check(str, tmp);
14751 if ((tlen = RSTRING_LEN(tmp)) == 0) return Qtrue;
14752 if ((slen = RSTRING_LEN(str)) < tlen) continue;
14753 p = RSTRING_PTR(str);
14754 e = p + slen;
14755 s = p + tlen;
14756 if (!at_char_right_boundary(p, s, e, enc))
14757 continue;
14758 if (memcmp(p, RSTRING_PTR(tmp), tlen) == 0)
14759 return Qtrue;
14760 }
14761 }
14762 return Qfalse;
14763}
14764
14765/*
14766 * call-seq:
14767 * end_with?(*strings) -> true or false
14768 *
14769 * Returns whether +self+ ends with any of the given +strings+:
14770 *
14771 * 'foo'.end_with?('oo') # => true
14772 * 'foo'.end_with?('bar', 'oo') # => true
14773 * 'foo'.end_with?('bar', 'baz') # => false
14774 * 'foo'.end_with?('') # => true
14775 * 'こんにちは'.end_with?('は') # => true
14776 *
14777 * Related: see {Querying}[rdoc-ref:String@Querying].
14778 *
14779 */
14780
14781static VALUE
14782rb_str_end_with(int argc, VALUE *argv, VALUE str)
14783{
14784 int i;
14785
14786 for (i=0; i<argc; i++) {
14787 VALUE tmp = argv[i];
14788 const char *p, *s, *e;
14789 rb_len_t slen, tlen;
14790 rb_encoding *enc;
14791
14792 StringValue(tmp);
14793 enc = rb_enc_check(str, tmp);
14794 if ((tlen = RSTRING_LEN(tmp)) == 0) return Qtrue;
14795 if ((slen = RSTRING_LEN(str)) < tlen) continue;
14796 p = RSTRING_PTR(str);
14797 e = p + slen;
14798 s = e - tlen;
14799 if (!at_char_boundary(p, s, e, enc))
14800 continue;
14801 if (memcmp(s, RSTRING_PTR(tmp), tlen) == 0)
14802 return Qtrue;
14803 }
14804 return Qfalse;
14805}
14806
14816static rb_len_t
14817deleted_prefix_length(VALUE str, VALUE prefix)
14818{
14819 const char *strptr, *prefixptr;
14820 rb_len_t olen, prefixlen;
14821 rb_encoding *enc = rb_enc_get(str);
14822
14823 StringValue(prefix);
14824
14825 if (!is_broken_string(prefix) ||
14826 !rb_enc_asciicompat(enc) ||
14827 !rb_enc_asciicompat(rb_enc_get(prefix))) {
14828 enc = rb_enc_check(str, prefix);
14829 }
14830
14831 /* return 0 if not start with prefix */
14832 prefixlen = RSTRING_LEN(prefix);
14833 if (prefixlen <= 0) return 0;
14834 olen = RSTRING_LEN(str);
14835 if (olen < prefixlen) return 0;
14836 strptr = RSTRING_PTR(str);
14837 prefixptr = RSTRING_PTR(prefix);
14838 if (memcmp(strptr, prefixptr, prefixlen) != 0) return 0;
14839 if (is_broken_string(prefix)) {
14840 if (!is_broken_string(str)) {
14841 /* prefix in a valid string cannot be broken */
14842 return 0;
14843 }
14844 const char *strend = strptr + olen;
14845 const char *after_prefix = strptr + prefixlen;
14846 if (!at_char_right_boundary(strptr, after_prefix, strend, enc)) {
14847 /* prefix does not end at char-boundary */
14848 return 0;
14849 }
14850 }
14851 /* prefix part in `str` also should be valid. */
14852
14853 return prefixlen;
14854}
14855
14856/*
14857 * call-seq:
14858 * delete_prefix!(prefix) -> self or nil
14859 *
14860 * Like String#delete_prefix, except that +self+ is modified in place;
14861 * returns +self+ if the prefix is removed, +nil+ otherwise.
14862 *
14863 * Related: see {Modifying}[rdoc-ref:String@Modifying].
14864 */
14865
14866static VALUE
14867rb_str_delete_prefix_bang(VALUE str, VALUE prefix)
14868{
14869 rb_len_t prefixlen;
14870 str_modify_keep_cr(str);
14871
14872 prefixlen = deleted_prefix_length(str, prefix);
14873 if (prefixlen <= 0) return Qnil;
14874
14875 return rb_str_drop_bytes(str, prefixlen);
14876}
14877
14878/*
14879 * call-seq:
14880 * delete_prefix(prefix) -> new_string
14881 *
14882 * Returns a copy of +self+ with leading substring +prefix+ removed:
14883 *
14884 * 'oof'.delete_prefix('o') # => "of"
14885 * 'oof'.delete_prefix('oo') # => "f"
14886 * 'oof'.delete_prefix('oof') # => ""
14887 * 'oof'.delete_prefix('x') # => "oof"
14888 * 'こんにちは'.delete_prefix('こん') # => "にちは"
14889 *
14890 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
14891 *
14892 */
14893
14894static VALUE
14895rb_str_delete_prefix(VALUE str, VALUE prefix)
14896{
14897 rb_len_t prefixlen;
14898
14899 prefixlen = deleted_prefix_length(str, prefix);
14900 if (prefixlen <= 0) return str_duplicate(rb_cString, str);
14901
14902 return rb_str_subseq(str, prefixlen, RSTRING_LEN(str) - prefixlen);
14903}
14904
14914static rb_len_t
14915deleted_suffix_length(VALUE str, VALUE suffix)
14916{
14917 const char *strptr, *suffixptr;
14918 rb_len_t olen, suffixlen;
14919 rb_encoding *enc;
14920
14921 StringValue(suffix);
14922 if (is_broken_string(suffix)) return 0;
14923 enc = rb_enc_check(str, suffix);
14924
14925 /* return 0 if not start with suffix */
14926 suffixlen = RSTRING_LEN(suffix);
14927 if (suffixlen <= 0) return 0;
14928 olen = RSTRING_LEN(str);
14929 if (olen < suffixlen) return 0;
14930 strptr = RSTRING_PTR(str);
14931 suffixptr = RSTRING_PTR(suffix);
14932 const char *strend = strptr + olen;
14933 const char *before_suffix = strend - suffixlen;
14934 if (memcmp(before_suffix, suffixptr, suffixlen) != 0) return 0;
14935 if (!at_char_boundary(strptr, before_suffix, strend, enc)) return 0;
14936
14937 return suffixlen;
14938}
14939
14940/*
14941 * call-seq:
14942 * delete_suffix!(suffix) -> self or nil
14943 *
14944 * Like String#delete_suffix, except that +self+ is modified in place;
14945 * returns +self+ if the suffix is removed, +nil+ otherwise.
14946 *
14947 * Related: see {Modifying}[rdoc-ref:String@Modifying].
14948 */
14949
14950static VALUE
14951rb_str_delete_suffix_bang(VALUE str, VALUE suffix)
14952{
14953 rb_len_t suffixlen;
14954 str_modifiable(str);
14955
14956 suffixlen = deleted_suffix_length(str, suffix);
14957 if (suffixlen <= 0) return Qnil;
14958
14959 return str_shrink(str, RSTRING_LEN(str) - suffixlen);
14960}
14961
14962/*
14963 * call-seq:
14964 * delete_suffix(suffix) -> new_string
14965 *
14966 * Returns a copy of +self+ with trailing substring <tt>suffix</tt> removed:
14967 *
14968 * 'foo'.delete_suffix('o') # => "fo"
14969 * 'foo'.delete_suffix('oo') # => "f"
14970 * 'foo'.delete_suffix('foo') # => ""
14971 * 'foo'.delete_suffix('f') # => "foo"
14972 * 'foo'.delete_suffix('x') # => "foo"
14973 * 'こんにちは'.delete_suffix('ちは') # => "こんに"
14974 *
14975 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
14976 *
14977 */
14978
14979static VALUE
14980rb_str_delete_suffix(VALUE str, VALUE suffix)
14981{
14982 rb_len_t suffixlen;
14983
14984 suffixlen = deleted_suffix_length(str, suffix);
14985 if (suffixlen <= 0) return str_duplicate(rb_cString, str);
14986
14987 return rb_str_subseq(str, 0, RSTRING_LEN(str) - suffixlen);
14988}
14989
14990void
14991rb_str_setter(VALUE val, ID id, VALUE *var)
14992{
14993 if (!NIL_P(val) && !RB_TYPE_P(val, T_STRING)) {
14994 rb_raise(rb_eTypeError, "value of %"PRIsVALUE" must be String", rb_id2str(id));
14995 }
14996 *var = val;
14997}
14998
14999static void
15000nil_setter_warning(ID id)
15001{
15002 rb_warn_deprecated("non-nil '%"PRIsVALUE"'", NULL, rb_id2str(id));
15003}
15004
15005void
15006rb_deprecated_str_setter(VALUE val, ID id, VALUE *var)
15007{
15008 rb_str_setter(val, id, var);
15009 if (!NIL_P(*var)) {
15010 nil_setter_warning(id);
15011 }
15012}
15013
15014static void
15015rb_fs_setter(VALUE val, ID id, VALUE *var)
15016{
15017 val = rb_fs_check(val);
15018 if (!val) {
15019 rb_raise(rb_eTypeError,
15020 "value of %"PRIsVALUE" must be String or Regexp",
15021 rb_id2str(id));
15022 }
15023 if (!NIL_P(val)) {
15024 nil_setter_warning(id);
15025 }
15026 *var = val;
15027}
15028
15029
15030/*
15031 * call-seq:
15032 * force_encoding(encoding) -> self
15033 *
15034 * Changes the encoding of +self+ to the given +encoding+,
15035 * which may be a string encoding name or an Encoding object;
15036 * does not change the underlying bytes;
15037 * returns self:
15038 *
15039 * s = 'łał'
15040 * s.bytes # => [197, 130, 97, 197, 130]
15041 * s.encoding # => #<Encoding:UTF-8>
15042 * s.force_encoding('ascii') # => "\xC5\x82a\xC5\x82"
15043 * s.encoding # => #<Encoding:US-ASCII>
15044 * s.valid_encoding? # => true
15045 * s.bytes # => [197, 130, 97, 197, 130]
15046 *
15047 * Makes the change even if the given +encoding+ is invalid
15048 * for +self+ (as is the change above):
15049 *
15050 * s.valid_encoding? # => false
15051 *
15052 * See {Encodings}[rdoc-ref:encodings.rdoc].
15053 *
15054 * Related: see {Modifying}[rdoc-ref:String@Modifying].
15055 *
15056 */
15057
15058static VALUE
15059rb_str_force_encoding(VALUE str, VALUE enc)
15060{
15061 str_modifiable(str);
15062
15063 rb_encoding *encoding = rb_to_encoding(enc);
15064 int idx = rb_enc_to_index(encoding);
15065
15066 // If the encoding is unchanged, we do nothing.
15067 if (ENCODING_GET(str) == idx) {
15068 return str;
15069 }
15070
15071 rb_enc_associate_index(str, idx);
15072
15073 // If the coderange was 7bit and the new encoding is ASCII-compatible
15074 // we can keep the coderange.
15075 if (ENC_CODERANGE(str) == ENC_CODERANGE_7BIT && encoding && rb_enc_asciicompat(encoding)) {
15076 return str;
15077 }
15078
15080 return str;
15081}
15082
15083/*
15084 * call-seq:
15085 * b -> new_string
15086 *
15087 * Returns a copy of +self+ that has ASCII-8BIT encoding;
15088 * the underlying bytes are not modified:
15089 *
15090 * s = "\x99"
15091 * s.encoding # => #<Encoding:UTF-8>
15092 * t = s.b # => "\x99"
15093 * t.encoding # => #<Encoding:ASCII-8BIT>
15094 *
15095 * s = "\u4095" # => "䂕"
15096 * s.encoding # => #<Encoding:UTF-8>
15097 * s.bytes # => [228, 130, 149]
15098 * t = s.b # => "\xE4\x82\x95"
15099 * t.encoding # => #<Encoding:ASCII-8BIT>
15100 * t.bytes # => [228, 130, 149]
15101 *
15102 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
15103 *
15104 */
15105
15106static VALUE
15107rb_str_b(VALUE str)
15108{
15109 VALUE str2;
15110 if (STR_EMBED_P(str)) {
15111 str2 = str_alloc_embed(rb_cString, RSTRING_LEN(str) + TERM_LEN(str));
15112 }
15113 else {
15114 str2 = str_alloc_heap(rb_cString);
15115 }
15116 str_replace_shared_without_enc(str2, str);
15117
15118 if (rb_enc_asciicompat(STR_ENC_GET(str))) {
15119 // BINARY strings can never be broken; they're either 7-bit ASCII or VALID.
15120 // If we know the receiver's code range then we know the result's code range.
15121 int cr = ENC_CODERANGE(str);
15122 switch (cr) {
15123 case ENC_CODERANGE_7BIT:
15125 break;
15129 break;
15130 default:
15131 ENC_CODERANGE_CLEAR(str2);
15132 break;
15133 }
15134 }
15135
15136 return str2;
15137}
15138
15139/* Defined as a leaf builtin in string.rb, so this must never raise or call into Ruby. */
15140static VALUE
15141rb_str_valid_encoding_p(VALUE str)
15142{
15143 int cr = rb_enc_str_coderange(str);
15144
15145 return RBOOL(cr != ENC_CODERANGE_BROKEN);
15146}
15147
15148/* Defined as a leaf builtin in string.rb, so this must never raise or call into Ruby. */
15149static VALUE
15150rb_str_is_ascii_only_p(VALUE str)
15151{
15152 int cr = rb_enc_str_coderange(str);
15153
15154 return RBOOL(cr == ENC_CODERANGE_7BIT);
15155}
15156
15157VALUE
15159{
15160 static const char ellipsis[] = "...";
15161 const rb_len_t ellipsislen = sizeof(ellipsis) - 1;
15162 rb_encoding *const enc = rb_enc_get(str);
15163 const rb_len_t blen = RSTRING_LEN(str);
15164 const char *const p = RSTRING_PTR(str), *e = p + blen;
15165 VALUE estr, ret = 0;
15166
15167 if (len < 0) rb_raise(rb_eIndexError, "negative length %"PRIdLEN, len);
15168 if (len * rb_enc_mbminlen(enc) >= blen ||
15169 (e = rb_enc_nth(p, e, len, enc)) - p == blen) {
15170 ret = str;
15171 }
15172 else if (len <= ellipsislen ||
15173 !(e = rb_enc_step_back(p, e, e, len = ellipsislen, enc))) {
15174 if (rb_enc_asciicompat(enc)) {
15175 ret = rb_str_new(ellipsis, len);
15176 rb_enc_associate(ret, enc);
15177 }
15178 else {
15179 estr = rb_usascii_str_new(ellipsis, len);
15180 ret = rb_str_encode(estr, rb_enc_from_encoding(enc), 0, Qnil);
15181 }
15182 }
15183 else if (ret = rb_str_subseq(str, 0, e - p), rb_enc_asciicompat(enc)) {
15184 rb_str_cat(ret, ellipsis, ellipsislen);
15185 }
15186 else {
15187 estr = rb_str_encode(rb_usascii_str_new(ellipsis, ellipsislen),
15188 rb_enc_from_encoding(enc), 0, Qnil);
15189 rb_str_append(ret, estr);
15190 }
15191 return ret;
15192}
15193
15194static VALUE
15195str_compat_and_valid(VALUE str, rb_encoding *enc)
15196{
15197 int cr;
15198 str = StringValue(str);
15199 cr = rb_enc_str_coderange(str);
15200 if (cr == ENC_CODERANGE_BROKEN) {
15201 rb_raise(rb_eArgError, "replacement must be valid byte sequence '%+"PRIsVALUE"'", str);
15202 }
15203 else {
15204 rb_encoding *e = STR_ENC_GET(str);
15205 if (cr == ENC_CODERANGE_7BIT ? rb_enc_mbminlen(enc) != 1 : enc != e) {
15206 rb_raise(rb_eEncCompatError, "incompatible character encodings: %s and %s",
15207 rb_enc_inspect_name(enc), rb_enc_inspect_name(e));
15208 }
15209 }
15210 return str;
15211}
15212
15213static VALUE enc_str_scrub(rb_encoding *enc, VALUE str, VALUE repl, int cr);
15214
15215VALUE
15217{
15218 rb_encoding *enc = STR_ENC_GET(str);
15219 return enc_str_scrub(enc, str, repl, ENC_CODERANGE(str));
15220}
15221
15222VALUE
15223rb_enc_str_scrub(rb_encoding *enc, VALUE str, VALUE repl)
15224{
15225 int cr = ENC_CODERANGE_UNKNOWN;
15226 if (enc == STR_ENC_GET(str)) {
15227 /* cached coderange makes sense only when enc equals the
15228 * actual encoding of str */
15229 cr = ENC_CODERANGE(str);
15230 }
15231 return enc_str_scrub(enc, str, repl, cr);
15232}
15233
15234static VALUE
15235enc_str_scrub(rb_encoding *enc, VALUE str, VALUE repl, int cr)
15236{
15237 int encidx;
15238 VALUE buf = Qnil;
15239 const char *rep, *p, *e, *p1, *sp;
15240 rb_len_t replen = -1;
15241 rb_len_t slen;
15242
15243 if (rb_block_given_p()) {
15244 if (!NIL_P(repl))
15245 rb_raise(rb_eArgError, "both of block and replacement given");
15246 replen = 0;
15247 }
15248
15249 if (ENC_CODERANGE_CLEAN_P(cr))
15250 return Qnil;
15251
15252 if (!NIL_P(repl)) {
15253 repl = str_compat_and_valid(repl, enc);
15254 }
15255
15256 if (rb_enc_dummy_p(enc)) {
15257 return Qnil;
15258 }
15259 encidx = rb_enc_to_index(enc);
15260
15261#define DEFAULT_REPLACE_CHAR(str) do { \
15262 RBIMPL_ATTR_NONSTRING() static const char replace[sizeof(str)-1] = str; \
15263 rep = replace; replen = (int)sizeof(replace); \
15264 } while (0)
15265
15266 slen = RSTRING_LEN(str);
15267 p = RSTRING_PTR(str);
15268 e = RSTRING_END(str);
15269 p1 = p;
15270 sp = p;
15271
15272 if (rb_enc_asciicompat(enc)) {
15273 int rep7bit_p;
15274 if (!replen) {
15275 rep = NULL;
15276 rep7bit_p = FALSE;
15277 }
15278 else if (!NIL_P(repl)) {
15279 rep = RSTRING_PTR(repl);
15280 replen = RSTRING_LEN(repl);
15281 rep7bit_p = (ENC_CODERANGE(repl) == ENC_CODERANGE_7BIT);
15282 }
15283 else if (encidx == rb_utf8_encindex()) {
15284 DEFAULT_REPLACE_CHAR("\xEF\xBF\xBD");
15285 rep7bit_p = FALSE;
15286 }
15287 else {
15288 DEFAULT_REPLACE_CHAR("?");
15289 rep7bit_p = TRUE;
15290 }
15291 cr = ENC_CODERANGE_7BIT;
15292
15293 p = search_nonascii(p, e);
15294 if (!p) {
15295 p = e;
15296 }
15297 while (p < e) {
15298 int ret = rb_enc_precise_mbclen(p, e, enc);
15299 if (MBCLEN_NEEDMORE_P(ret)) {
15300 break;
15301 }
15302 else if (MBCLEN_CHARFOUND_P(ret)) {
15304 p += MBCLEN_CHARFOUND_LEN(ret);
15305 /* After a multibyte character, fast-skip the following ASCII run. */
15306 p = search_nonascii(p, e);
15307 if (!p) {
15308 p = e;
15309 break;
15310 }
15311 }
15312 else if (MBCLEN_INVALID_P(ret)) {
15313 /*
15314 * p1~p: valid ascii/multibyte chars
15315 * p ~e: invalid bytes + unknown bytes
15316 */
15317 rb_len_t clen = rb_enc_mbmaxlen(enc);
15318 if (NIL_P(buf)) buf = rb_str_buf_new(RSTRING_LEN(str));
15319 if (p > p1) {
15320 rb_str_buf_cat(buf, p1, p - p1);
15321 }
15322
15323 if (e - p < clen) clen = e - p;
15324 if (clen <= 2) {
15325 clen = 1;
15326 }
15327 else {
15328 const char *q = p;
15329 clen--;
15330 for (; clen > 1; clen--) {
15331 ret = rb_enc_precise_mbclen(q, q + clen, enc);
15332 if (MBCLEN_NEEDMORE_P(ret)) break;
15333 if (MBCLEN_INVALID_P(ret)) continue;
15335 }
15336 }
15337 if (rep) {
15338 rb_str_buf_cat(buf, rep, replen);
15339 if (!rep7bit_p) cr = ENC_CODERANGE_VALID;
15340 }
15341 else {
15342 repl = rb_yield(rb_enc_str_new(p, clen, enc));
15343 str_mod_check(str, sp, slen);
15344 repl = str_compat_and_valid(repl, enc);
15345 rb_str_buf_cat(buf, RSTRING_PTR(repl), RSTRING_LEN(repl));
15348 }
15349 p += clen;
15350 p1 = p;
15351 p = search_nonascii(p, e);
15352 if (!p) {
15353 p = e;
15354 break;
15355 }
15356 }
15357 else {
15359 }
15360 }
15361 if (NIL_P(buf)) {
15362 if (p == e) {
15363 ENC_CODERANGE_SET(str, cr);
15364 return Qnil;
15365 }
15366 buf = rb_str_buf_new(RSTRING_LEN(str));
15367 }
15368 if (p1 < p) {
15369 rb_str_buf_cat(buf, p1, p - p1);
15370 }
15371 if (p < e) {
15372 if (rep) {
15373 rb_str_buf_cat(buf, rep, replen);
15374 if (!rep7bit_p) cr = ENC_CODERANGE_VALID;
15375 }
15376 else {
15377 repl = rb_yield(rb_enc_str_new(p, e-p, enc));
15378 str_mod_check(str, sp, slen);
15379 repl = str_compat_and_valid(repl, enc);
15380 rb_str_buf_cat(buf, RSTRING_PTR(repl), RSTRING_LEN(repl));
15383 }
15384 }
15385 }
15386 else {
15387 /* ASCII incompatible */
15388 rb_len_t mbminlen = rb_enc_mbminlen(enc);
15389 if (!replen) {
15390 rep = NULL;
15391 }
15392 else if (!NIL_P(repl)) {
15393 rep = RSTRING_PTR(repl);
15394 replen = RSTRING_LEN(repl);
15395 }
15396 else if (encidx == ENCINDEX_UTF_16BE) {
15397 DEFAULT_REPLACE_CHAR("\xFF\xFD");
15398 }
15399 else if (encidx == ENCINDEX_UTF_16LE) {
15400 DEFAULT_REPLACE_CHAR("\xFD\xFF");
15401 }
15402 else if (encidx == ENCINDEX_UTF_32BE) {
15403 DEFAULT_REPLACE_CHAR("\x00\x00\xFF\xFD");
15404 }
15405 else if (encidx == ENCINDEX_UTF_32LE) {
15406 DEFAULT_REPLACE_CHAR("\xFD\xFF\x00\x00");
15407 }
15408 else {
15409 DEFAULT_REPLACE_CHAR("?");
15410 }
15411
15412 while (p < e) {
15413 int ret = rb_enc_precise_mbclen(p, e, enc);
15414 if (MBCLEN_NEEDMORE_P(ret)) {
15415 break;
15416 }
15417 else if (MBCLEN_CHARFOUND_P(ret)) {
15418 p += MBCLEN_CHARFOUND_LEN(ret);
15419 }
15420 else if (MBCLEN_INVALID_P(ret)) {
15421 const char *q = p;
15422 rb_len_t clen = rb_enc_mbmaxlen(enc);
15423 if (NIL_P(buf)) buf = rb_str_buf_new(RSTRING_LEN(str));
15424 if (p > p1) rb_str_buf_cat(buf, p1, p - p1);
15425
15426 if (e - p < clen) clen = e - p;
15427 if (clen <= mbminlen * 2) {
15428 clen = mbminlen;
15429 }
15430 else {
15431 clen -= mbminlen;
15432 for (; clen > mbminlen; clen-=mbminlen) {
15433 ret = rb_enc_precise_mbclen(q, q + clen, enc);
15434 if (MBCLEN_NEEDMORE_P(ret)) break;
15435 if (MBCLEN_INVALID_P(ret)) continue;
15437 }
15438 }
15439 if (rep) {
15440 rb_str_buf_cat(buf, rep, replen);
15441 }
15442 else {
15443 repl = rb_yield(rb_enc_str_new(p, clen, enc));
15444 str_mod_check(str, sp, slen);
15445 repl = str_compat_and_valid(repl, enc);
15446 rb_str_buf_cat(buf, RSTRING_PTR(repl), RSTRING_LEN(repl));
15447 }
15448 p += clen;
15449 p1 = p;
15450 }
15451 else {
15453 }
15454 }
15455 if (NIL_P(buf)) {
15456 if (p == e) {
15458 return Qnil;
15459 }
15460 buf = rb_str_buf_new(RSTRING_LEN(str));
15461 }
15462 if (p1 < p) {
15463 rb_str_buf_cat(buf, p1, p - p1);
15464 }
15465 if (p < e) {
15466 if (rep) {
15467 rb_str_buf_cat(buf, rep, replen);
15468 }
15469 else {
15470 repl = rb_yield(rb_enc_str_new(p, e-p, enc));
15471 str_mod_check(str, sp, slen);
15472 repl = str_compat_and_valid(repl, enc);
15473 rb_str_buf_cat(buf, RSTRING_PTR(repl), RSTRING_LEN(repl));
15474 }
15475 }
15477 }
15478 rb_enc_associate(buf, enc);
15479 ENC_CODERANGE_SET(buf, cr);
15480 return buf;
15481}
15482
15483/*
15484 * call-seq:
15485 * scrub(replacement_string = default_replacement_string) -> new_string
15486 * scrub{|sequence| ... } -> new_string
15487 *
15488 * Returns a copy of +self+ with each invalid byte sequence replaced
15489 * by the given +replacement_string+.
15490 *
15491 * With no block given, replaces each invalid sequence
15492 * with the given +default_replacement_string+
15493 * (by default, <tt>"�"</tt> for a Unicode encoding, <tt>'?'</tt> otherwise):
15494 *
15495 * "foo\x81\x81bar".scrub # => "foo��bar"
15496 * "foo\x81\x81bar".force_encoding('US-ASCII').scrub # => "foo??bar"
15497 * "foo\x81\x81bar".scrub('xyzzy') # => "fooxyzzyxyzzybar"
15498 *
15499 * With a block given, calls the block with each invalid sequence,
15500 * and replaces that sequence with the return value of the block:
15501 *
15502 * "foo\x81\x81bar".scrub {|sequence| p sequence; 'XYZZY' } # => "fooXYZZYXYZZYbar"
15503 *
15504 * Output :
15505 *
15506 * "\x81"
15507 * "\x81"
15508 *
15509 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
15510 *
15511 */
15512static VALUE
15513str_scrub(int argc, VALUE *argv, VALUE str)
15514{
15515 VALUE repl = argc ? (rb_check_arity(argc, 0, 1), argv[0]) : Qnil;
15516 VALUE new = rb_str_scrub(str, repl);
15517 return NIL_P(new) ? str_duplicate(rb_cString, str): new;
15518}
15519
15520/*
15521 * call-seq:
15522 * scrub!(replacement_string = default_replacement_string) -> self
15523 * scrub!{|sequence| ... } -> self
15524 *
15525 * Like String#scrub, except that:
15526 *
15527 * - Any replacements are made in +self+.
15528 * - Returns +self+.
15529 *
15530 * Related: see {Modifying}[rdoc-ref:String@Modifying].
15531 *
15532 */
15533static VALUE
15534str_scrub_bang(int argc, VALUE *argv, VALUE str)
15535{
15536 VALUE repl = argc ? (rb_check_arity(argc, 0, 1), argv[0]) : Qnil;
15537 VALUE new = rb_str_scrub(str, repl);
15538 if (!NIL_P(new)) rb_str_replace(str, new);
15539 return str;
15540}
15541
15542static ID id_normalize;
15543static ID id_normalized_p;
15544static VALUE mUnicodeNormalize;
15545
15546static VALUE
15547unicode_normalize_common(int argc, VALUE *argv, VALUE str, ID id)
15548{
15549 static int UnicodeNormalizeRequired = 0;
15550 VALUE argv2[2];
15551
15552 if (!UnicodeNormalizeRequired) {
15553 rb_require("unicode_normalize/normalize.rb");
15554 UnicodeNormalizeRequired = 1;
15555 }
15556 argv2[0] = str;
15557 if (rb_check_arity(argc, 0, 1)) argv2[1] = argv[0];
15558 return rb_funcallv(mUnicodeNormalize, id, argc+1, argv2);
15559}
15560
15561/*
15562 * call-seq:
15563 * unicode_normalize(form = :nfc) -> string
15564 *
15565 * Returns a copy of +self+ with
15566 * {Unicode normalization}[https://unicode.org/reports/tr15] applied.
15567 *
15568 * Argument +form+ must be one of the following symbols
15569 * (see {Unicode normalization forms}[https://unicode.org/reports/tr15/#Norm_Forms]):
15570 *
15571 * - +:nfc+: Canonical decomposition, followed by canonical composition.
15572 * - +:nfd+: Canonical decomposition.
15573 * - +:nfkc+: Compatibility decomposition, followed by canonical composition.
15574 * - +:nfkd+: Compatibility decomposition.
15575 *
15576 * The encoding of +self+ must be one of:
15577 *
15578 * - <tt>Encoding::UTF_8</tt>.
15579 * - <tt>Encoding::UTF_16BE</tt>.
15580 * - <tt>Encoding::UTF_16LE</tt>.
15581 * - <tt>Encoding::UTF_32BE</tt>.
15582 * - <tt>Encoding::UTF_32LE</tt>.
15583 * - <tt>Encoding::GB18030</tt>.
15584 * - <tt>Encoding::UCS_2BE</tt>.
15585 * - <tt>Encoding::UCS_4BE</tt>.
15586 *
15587 * Examples:
15588 *
15589 * "a\u0300".unicode_normalize # => "à" # Lowercase 'a' with grave accens.
15590 * "a\u0300".unicode_normalize(:nfd) # => "à" # Same.
15591 *
15592 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
15593 *
15594 */
15595static VALUE
15596rb_str_unicode_normalize(int argc, VALUE *argv, VALUE str)
15597{
15598 return unicode_normalize_common(argc, argv, str, id_normalize);
15599}
15600
15601/*
15602 * call-seq:
15603 * unicode_normalize!(form = :nfc) -> self
15604 *
15605 * Like String#unicode_normalize, except that the normalization
15606 * is performed on +self+ (not on a copy of +self+).
15607 *
15608 * Related: see {Modifying}[rdoc-ref:String@Modifying].
15609 *
15610 */
15611static VALUE
15612rb_str_unicode_normalize_bang(int argc, VALUE *argv, VALUE str)
15613{
15614 return rb_str_replace(str, unicode_normalize_common(argc, argv, str, id_normalize));
15615}
15616
15617/* call-seq:
15618 * unicode_normalized?(form = :nfc) -> true or false
15619 *
15620 * Returns whether +self+ is in the given +form+ of Unicode normalization;
15621 * see String#unicode_normalize.
15622 *
15623 * The +form+ must be one of +:nfc+, +:nfd+, +:nfkc+, or +:nfkd+.
15624 *
15625 * Examples:
15626 *
15627 * "a\u0300".unicode_normalized? # => false
15628 * "a\u0300".unicode_normalized?(:nfd) # => true
15629 * "\u00E0".unicode_normalized? # => true
15630 * "\u00E0".unicode_normalized?(:nfd) # => false
15631 *
15632 *
15633 * Raises an exception if +self+ is not in a Unicode encoding:
15634 *
15635 * s = "\xE0".force_encoding(Encoding::ISO_8859_1)
15636 * s.unicode_normalized? # Raises Encoding::CompatibilityError
15637 *
15638 * Related: see {Querying}[rdoc-ref:String@Querying].
15639 */
15640static VALUE
15641rb_str_unicode_normalized_p(int argc, VALUE *argv, VALUE str)
15642{
15643 return unicode_normalize_common(argc, argv, str, id_normalized_p);
15644}
15645
15646/**********************************************************************
15647 * Document-class: Symbol
15648 *
15649 * A +Symbol+ object represents a named identifier inside the Ruby interpreter.
15650 *
15651 * You can create a +Symbol+ object explicitly with:
15652 *
15653 * - A {symbol literal}[rdoc-ref:syntax/literals.rdoc@Symbol+Literals].
15654 *
15655 * The same +Symbol+ object will be
15656 * created for a given name or string for the duration of a program's
15657 * execution, regardless of the context or meaning of that name. Thus
15658 * if <code>Fred</code> is a constant in one context, a method in
15659 * another, and a class in a third, the +Symbol+ <code>:Fred</code>
15660 * will be the same object in all three contexts.
15661 *
15662 * module One
15663 * class Fred
15664 * end
15665 * $f1 = :Fred
15666 * end
15667 * module Two
15668 * Fred = 1
15669 * $f2 = :Fred
15670 * end
15671 * def Fred()
15672 * end
15673 * $f3 = :Fred
15674 * $f1.object_id #=> 2514190
15675 * $f2.object_id #=> 2514190
15676 * $f3.object_id #=> 2514190
15677 *
15678 * Constant, method, and variable names are returned as symbols:
15679 *
15680 * module One
15681 * Two = 2
15682 * def three; 3 end
15683 * @four = 4
15684 * @@five = 5
15685 * $six = 6
15686 * end
15687 * seven = 7
15688 *
15689 * One.constants
15690 * # => [:Two]
15691 * One.instance_methods(true)
15692 * # => [:three]
15693 * One.instance_variables
15694 * # => [:@four]
15695 * One.class_variables
15696 * # => [:@@five]
15697 * global_variables.grep(/six/)
15698 * # => [:$six]
15699 * local_variables
15700 * # => [:seven]
15701 *
15702 * A +Symbol+ object differs from a String object in that
15703 * a +Symbol+ object represents an identifier, while a String object
15704 * represents text or data.
15705 *
15706 * == What's Here
15707 *
15708 * First, what's elsewhere. Class +Symbol+:
15709 *
15710 * - Inherits from {class Object}[rdoc-ref:Object@Whats+Here].
15711 * - Includes {module Comparable}[rdoc-ref:Comparable@Whats+Here].
15712 *
15713 * Here, class +Symbol+ provides methods that are useful for:
15714 *
15715 * - {Querying}[rdoc-ref:Symbol@Methods+for+Querying]
15716 * - {Comparing}[rdoc-ref:Symbol@Methods+for+Comparing]
15717 * - {Converting}[rdoc-ref:Symbol@Methods+for+Converting]
15718 *
15719 * === Methods for Querying
15720 *
15721 * - ::all_symbols: Returns an array of the symbols currently in Ruby's symbol table.
15722 * - #=~: Returns the index of the first substring in symbol that matches a
15723 * given Regexp or other object; returns +nil+ if no match is found.
15724 * - #[], #slice : Returns a substring of symbol
15725 * determined by a given index, start/length, or range, or string.
15726 * - #empty?: Returns +true+ if +self.length+ is zero; +false+ otherwise.
15727 * - #encoding: Returns the Encoding object that represents the encoding
15728 * of symbol.
15729 * - #end_with?: Returns +true+ if symbol ends with
15730 * any of the given strings.
15731 * - #match: Returns a MatchData object if symbol
15732 * matches a given Regexp; +nil+ otherwise.
15733 * - #match?: Returns +true+ if symbol
15734 * matches a given Regexp; +false+ otherwise.
15735 * - #length, #size: Returns the number of characters in symbol.
15736 * - #start_with?: Returns +true+ if symbol starts with
15737 * any of the given strings.
15738 *
15739 * === Methods for Comparing
15740 *
15741 * - #<=>: Returns -1, 0, or 1 as a given symbol is smaller than, equal to,
15742 * or larger than symbol.
15743 * - #==, #===: Returns +true+ if a given symbol has the same content and
15744 * encoding.
15745 * - #casecmp: Ignoring case, returns -1, 0, or 1 as a given
15746 * symbol is smaller than, equal to, or larger than symbol.
15747 * - #casecmp?: Returns +true+ if symbol is equal to a given symbol
15748 * after Unicode case folding; +false+ otherwise.
15749 *
15750 * === Methods for Converting
15751 *
15752 * - #capitalize: Returns symbol with the first character upcased
15753 * and all other characters downcased.
15754 * - #downcase: Returns symbol with all characters downcased.
15755 * - #inspect: Returns the string representation of +self+ as a symbol literal.
15756 * - #name: Returns the frozen string corresponding to symbol.
15757 * - #succ, #next: Returns the symbol that is the successor to symbol.
15758 * - #swapcase: Returns symbol with all upcase characters downcased
15759 * and all downcase characters upcased.
15760 * - #to_proc: Returns a Proc object which responds to the method named by symbol.
15761 * - #to_s, #id2name: Returns the string corresponding to +self+.
15762 * - #to_sym, #intern: Returns +self+.
15763 * - #upcase: Returns symbol with all characters upcased.
15764 *
15765 */
15766
15767
15768/*
15769 * call-seq:
15770 * self == other -> true or false
15771 *
15772 * Returns whether +other+ is the same object as +self+.
15773 */
15774
15775#define sym_equal rb_obj_equal
15776
15777static int
15778sym_printable(const char *s, const char *send, rb_encoding *enc)
15779{
15780 while (s < send) {
15781 int n;
15782 int c = rb_enc_precise_mbclen(s, send, enc);
15783
15784 if (!MBCLEN_CHARFOUND_P(c)) return FALSE;
15785 n = MBCLEN_CHARFOUND_LEN(c);
15786 c = rb_enc_mbc_to_codepoint(s, send, enc);
15787 if (!rb_enc_isprint(c, enc)) return FALSE;
15788 s += n;
15789 }
15790 return TRUE;
15791}
15792
15793int
15794rb_str_symname_p(VALUE sym)
15795{
15796 rb_encoding *enc;
15797 const char *ptr;
15798 rb_len_t len;
15799 rb_encoding *resenc = rb_default_internal_encoding();
15800
15801 if (resenc == NULL) resenc = rb_default_external_encoding();
15802 enc = STR_ENC_GET(sym);
15803 ptr = RSTRING_PTR(sym);
15804 len = RSTRING_LEN(sym);
15805 if ((resenc != enc && !rb_str_is_ascii_only_p(sym)) || len != (rb_len_t)strlen(ptr) ||
15806 !rb_enc_symname2_p(ptr, len, enc) || !sym_printable(ptr, ptr + len, enc)) {
15807 return FALSE;
15808 }
15809 return TRUE;
15810}
15811
15812VALUE
15813rb_str_quote_unprintable(VALUE str)
15814{
15815 rb_encoding *enc;
15816 const char *ptr;
15817 rb_len_t len;
15818 rb_encoding *resenc;
15819
15820 Check_Type(str, T_STRING);
15822 if (resenc == NULL) resenc = rb_default_external_encoding();
15823 enc = STR_ENC_GET(str);
15824 ptr = RSTRING_PTR(str);
15825 len = RSTRING_LEN(str);
15826 if ((resenc != enc && !rb_str_is_ascii_only_p(str)) ||
15827 !sym_printable(ptr, ptr + len, enc)) {
15828 return rb_str_escape(str);
15829 }
15830 return str;
15831}
15832
15833VALUE
15834rb_id_quote_unprintable(ID id)
15835{
15836 VALUE str = rb_id2str(id);
15837 if (!rb_str_symname_p(str)) {
15838 return rb_str_escape(str);
15839 }
15840 return str;
15841}
15842
15843/*
15844 * call-seq:
15845 * inspect -> string
15846 *
15847 * Returns a string representation of +self+ (including the leading colon):
15848 *
15849 * :foo.inspect # => ":foo"
15850 *
15851 * Related: Symbol#to_s, Symbol#name.
15852 *
15853 */
15854
15855static VALUE
15856sym_inspect(VALUE sym)
15857{
15858 VALUE str = rb_sym2str(sym);
15859 const char *ptr;
15860 rb_len_t len;
15861 char *dest;
15862
15863 if (!rb_str_symname_p(str)) {
15864 str = rb_str_inspect(str);
15865 len = RSTRING_LEN(str);
15866 rb_str_resize(str, len + 1);
15867 dest = RSTRING_PTR(str);
15868 memmove(dest + 1, dest, len);
15869 }
15870 else {
15871 rb_encoding *enc = STR_ENC_GET(str);
15872 VALUE orig_str = str;
15873
15874 len = RSTRING_LEN(orig_str);
15875 str = rb_enc_str_new(0, len + 1, enc);
15876
15877 // Get data pointer after allocation
15878 ptr = RSTRING_PTR(orig_str);
15879 dest = RSTRING_PTR(str);
15880 memcpy(dest + 1, ptr, len);
15881
15882 RB_GC_GUARD(orig_str);
15883 }
15884 dest[0] = ':';
15885
15887
15888 return str;
15889}
15890
15891VALUE
15893{
15894 return rb_sym2str(sym);
15895}
15896
15897VALUE
15898rb_sym_proc_call(ID mid, int argc, const VALUE *argv, int kw_splat, VALUE passed_proc)
15899{
15900 VALUE obj;
15901
15902 if (argc < 1) {
15903 rb_raise(rb_eArgError, "no receiver given");
15904 }
15905 obj = argv[0];
15906 return rb_funcall_with_block_kw(obj, mid, argc - 1, argv + 1, passed_proc, kw_splat);
15907}
15908
15909/*
15910 * call-seq:
15911 * succ
15912 *
15913 * Equivalent to <tt>self.to_s.succ.to_sym</tt>:
15914 *
15915 * :foo.succ # => :fop
15916 *
15917 * Related: String#succ.
15918 */
15919
15920static VALUE
15921sym_succ(VALUE sym)
15922{
15923 return rb_str_intern(rb_str_succ(rb_sym2str(sym)));
15924}
15925
15926/*
15927 * call-seq:
15928 * self <=> other -> -1, 0, 1, or nil
15929 *
15930 * Compares +self+ and +other+, using String#<=>.
15931 *
15932 * Returns:
15933 *
15934 * - <tt>self.to_s <=> other.to_s</tt>, if +other+ is a symbol.
15935 * - +nil+, otherwise.
15936 *
15937 * Examples:
15938 *
15939 * :bar <=> :foo # => -1
15940 * :foo <=> :foo # => 0
15941 * :foo <=> :bar # => 1
15942 * :foo <=> 'bar' # => nil
15943 *
15944 * \Class \Symbol includes module Comparable,
15945 * each of whose methods uses Symbol#<=> for comparison.
15946 *
15947 * Related: String#<=>.
15948 */
15949
15950static VALUE
15951sym_cmp(VALUE sym, VALUE other)
15952{
15953 if (!SYMBOL_P(other)) {
15954 return Qnil;
15955 }
15956 return rb_str_cmp_m(rb_sym2str(sym), rb_sym2str(other));
15957}
15958
15959/*
15960 * call-seq:
15961 * casecmp(object) -> -1, 0, 1, or nil
15962 *
15963 * Like Symbol#<=>, but case-insensitive;
15964 * equivalent to <tt>self.to_s.casecmp(object.to_s)</tt>:
15965 *
15966 * lower = :abc
15967 * upper = :ABC
15968 * upper.casecmp(lower) # => 0
15969 * lower.casecmp(lower) # => 0
15970 * lower.casecmp(upper) # => 0
15971 *
15972 * Returns nil if +self+ and +object+ have incompatible encodings,
15973 * or if +object+ is not a symbol:
15974 *
15975 * sym = 'äöü'.encode("ISO-8859-1").to_sym
15976 * other_sym = 'ÄÖÜ'
15977 * sym.casecmp(other_sym) # => nil
15978 * :foo.casecmp(2) # => nil
15979 *
15980 * Unlike Symbol#casecmp?,
15981 * case-insensitivity does not work for characters outside of 'A'..'Z' and 'a'..'z':
15982 *
15983 * lower = :äöü
15984 * upper = :ÄÖÜ
15985 * upper.casecmp(lower) # => -1
15986 * lower.casecmp(lower) # => 0
15987 * lower.casecmp(upper) # => 1
15988 *
15989 * Related: Symbol#casecmp?, String#casecmp.
15990 *
15991 */
15992
15993static VALUE
15994sym_casecmp(VALUE sym, VALUE other)
15995{
15996 if (!SYMBOL_P(other)) {
15997 return Qnil;
15998 }
15999 return str_casecmp(rb_sym2str(sym), rb_sym2str(other));
16000}
16001
16002/*
16003 * call-seq:
16004 * casecmp?(object) -> true, false, or nil
16005 *
16006 * Returns +true+ if +self+ and +object+ are equal after Unicode case folding,
16007 * otherwise +false+:
16008 *
16009 * lower = :abc
16010 * upper = :ABC
16011 * upper.casecmp?(lower) # => true
16012 * lower.casecmp?(lower) # => true
16013 * lower.casecmp?(upper) # => true
16014 *
16015 * Returns nil if +self+ and +object+ have incompatible encodings,
16016 * or if +object+ is not a symbol:
16017 *
16018 * sym = 'äöü'.encode("ISO-8859-1").to_sym
16019 * other_sym = 'ÄÖÜ'
16020 * sym.casecmp?(other_sym) # => nil
16021 * :foo.casecmp?(2) # => nil
16022 *
16023 * Unlike Symbol#casecmp, works for characters outside of 'A'..'Z' and 'a'..'z':
16024 *
16025 * lower = :äöü
16026 * upper = :ÄÖÜ
16027 * upper.casecmp?(lower) # => true
16028 * lower.casecmp?(lower) # => true
16029 * lower.casecmp?(upper) # => true
16030 *
16031 * Related: Symbol#casecmp, String#casecmp?.
16032 *
16033 */
16034
16035static VALUE
16036sym_casecmp_p(VALUE sym, VALUE other)
16037{
16038 if (!SYMBOL_P(other)) {
16039 return Qnil;
16040 }
16041 return str_casecmp_p(rb_sym2str(sym), rb_sym2str(other));
16042}
16043
16044/*
16045 * call-seq:
16046 * self =~ other -> integer or nil
16047 *
16048 * Equivalent to <tt>self.to_s =~ other</tt>,
16049 * including possible updates to global variables;
16050 * see String#=~.
16051 *
16052 */
16053
16054static VALUE
16055sym_match(VALUE sym, VALUE other)
16056{
16057 return rb_str_match(rb_sym2str(sym), other);
16058}
16059
16060/*
16061 * call-seq:
16062 * match(pattern, offset = 0) -> matchdata or nil
16063 * match(pattern, offset = 0) {|matchdata| } -> object
16064 *
16065 * Equivalent to <tt>self.to_s.match</tt>,
16066 * including possible updates to global variables;
16067 * see String#match.
16068 *
16069 */
16070
16071static VALUE
16072sym_match_m(int argc, VALUE *argv, VALUE sym)
16073{
16074 return rb_str_match_m(argc, argv, rb_sym2str(sym));
16075}
16076
16077/*
16078 * call-seq:
16079 * match?(pattern, offset) -> true or false
16080 *
16081 * Equivalent to <tt>sym.to_s.match?</tt>;
16082 * see String#match.
16083 *
16084 */
16085
16086static VALUE
16087sym_match_m_p(int argc, VALUE *argv, VALUE sym)
16088{
16089 return rb_str_match_m_p(argc, argv, sym);
16090}
16091
16092/*
16093 * call-seq:
16094 * self[offset] -> string or nil
16095 * self[offset, size] -> string or nil
16096 * self[range] -> string or nil
16097 * self[regexp, capture = 0] -> string or nil
16098 * self[substring] -> string or nil
16099 *
16100 * Equivalent to <tt>symbol.to_s[]</tt>; see String#[].
16101 *
16102 */
16103
16104static VALUE
16105sym_aref(int argc, VALUE *argv, VALUE sym)
16106{
16107 return rb_str_aref_m(argc, argv, rb_sym2str(sym));
16108}
16109
16110/*
16111 * call-seq:
16112 * length -> integer
16113 *
16114 * Equivalent to <tt>self.to_s.length</tt>; see String#length.
16115 */
16116
16117static VALUE
16118sym_length(VALUE sym)
16119{
16120 return rb_str_length(rb_sym2str(sym));
16121}
16122
16123/*
16124 * call-seq:
16125 * upcase(mapping) -> symbol
16126 *
16127 * Equivalent to <tt>sym.to_s.upcase.to_sym</tt>.
16128 *
16129 * See String#upcase.
16130 *
16131 */
16132
16133static VALUE
16134sym_upcase(int argc, VALUE *argv, VALUE sym)
16135{
16136 return rb_str_intern(rb_str_upcase(argc, argv, rb_sym2str(sym)));
16137}
16138
16139/*
16140 * call-seq:
16141 * downcase(mapping) -> symbol
16142 *
16143 * Equivalent to <tt>sym.to_s.downcase.to_sym</tt>.
16144 *
16145 * See String#downcase.
16146 *
16147 * Related: Symbol#upcase.
16148 *
16149 */
16150
16151static VALUE
16152sym_downcase(int argc, VALUE *argv, VALUE sym)
16153{
16154 return rb_str_intern(rb_str_downcase(argc, argv, rb_sym2str(sym)));
16155}
16156
16157/*
16158 * call-seq:
16159 * capitalize(mapping) -> symbol
16160 *
16161 * Equivalent to <tt>sym.to_s.capitalize.to_sym</tt>.
16162 *
16163 * See String#capitalize.
16164 *
16165 */
16166
16167static VALUE
16168sym_capitalize(int argc, VALUE *argv, VALUE sym)
16169{
16170 return rb_str_intern(rb_str_capitalize(argc, argv, rb_sym2str(sym)));
16171}
16172
16173/*
16174 * call-seq:
16175 * swapcase(mapping) -> symbol
16176 *
16177 * Equivalent to <tt>sym.to_s.swapcase.to_sym</tt>.
16178 *
16179 * See String#swapcase.
16180 *
16181 */
16182
16183static VALUE
16184sym_swapcase(int argc, VALUE *argv, VALUE sym)
16185{
16186 return rb_str_intern(rb_str_swapcase(argc, argv, rb_sym2str(sym)));
16187}
16188
16189/*
16190 * call-seq:
16191 * start_with?(*string_or_regexp) -> true or false
16192 *
16193 * Equivalent to <tt>self.to_s.start_with?</tt>; see String#start_with?.
16194 *
16195 */
16196
16197static VALUE
16198sym_start_with(int argc, VALUE *argv, VALUE sym)
16199{
16200 return rb_str_start_with(argc, argv, rb_sym2str(sym));
16201}
16202
16203/*
16204 * call-seq:
16205 * end_with?(*strings) -> true or false
16206 *
16207 *
16208 * Equivalent to <tt>self.to_s.end_with?</tt>; see String#end_with?.
16209 *
16210 */
16211
16212static VALUE
16213sym_end_with(int argc, VALUE *argv, VALUE sym)
16214{
16215 return rb_str_end_with(argc, argv, rb_sym2str(sym));
16216}
16217
16218/*
16219 * call-seq:
16220 * encoding -> encoding
16221 *
16222 * Equivalent to <tt>self.to_s.encoding</tt>; see String#encoding.
16223 *
16224 */
16225
16226static VALUE
16227sym_encoding(VALUE sym)
16228{
16229 return rb_obj_encoding(rb_sym2str(sym));
16230}
16231
16232static VALUE
16233string_for_symbol(VALUE name)
16234{
16235 if (!RB_TYPE_P(name, T_STRING)) {
16236 VALUE tmp = rb_check_string_type(name);
16237 if (NIL_P(tmp)) {
16238 rb_raise(rb_eTypeError, "%+"PRIsVALUE" is not a symbol nor a string",
16239 name);
16240 }
16241 name = tmp;
16242 }
16243 return name;
16244}
16245
16246ID
16248{
16249 if (SYMBOL_P(name)) {
16250 return SYM2ID(name);
16251 }
16252 name = string_for_symbol(name);
16253 return rb_intern_str(name);
16254}
16255
16256VALUE
16258{
16259 if (SYMBOL_P(name)) {
16260 return name;
16261 }
16262 name = string_for_symbol(name);
16263 return rb_str_intern(name);
16264}
16265
16266/*
16267 * call-seq:
16268 * Symbol.all_symbols -> array_of_symbols
16269 *
16270 * Returns an array of all symbols currently in Ruby's symbol table:
16271 *
16272 * Symbol.all_symbols.size # => 9334
16273 * Symbol.all_symbols.take(3) # => [:!, :"\"", :"#"]
16274 *
16275 */
16276
16277static VALUE
16278sym_all_symbols(VALUE _)
16279{
16280 return rb_sym_all_symbols();
16281}
16282
16283VALUE
16284rb_str_to_interned_str(VALUE str)
16285{
16286 return rb_fstring(str);
16287}
16288
16289VALUE
16291{
16292 struct RString fake_str = {RBASIC_INIT};
16293 int encidx = ENCINDEX_US_ASCII;
16294 int coderange = ENC_CODERANGE_7BIT;
16295 if (len > 0 && search_nonascii(ptr, ptr + len)) {
16296 encidx = ENCINDEX_ASCII_8BIT;
16297 coderange = ENC_CODERANGE_VALID;
16298 }
16299 VALUE str = setup_fake_str(&fake_str, ptr, len, encidx);
16300 ENC_CODERANGE_SET(str, coderange);
16301 return register_fstring(str, true, false);
16302}
16303
16304VALUE
16305rb_interned_str_cstr(const char *ptr)
16306{
16307 return rb_interned_str(ptr, strlen(ptr));
16308}
16309
16310VALUE
16311rb_enc_interned_str(const char *ptr, rb_len_t len, rb_encoding *enc)
16312{
16313 if (enc != NULL && UNLIKELY(rb_enc_autoload_p(enc))) {
16314 rb_enc_autoload(enc);
16315 }
16316
16317 struct RString fake_str = {RBASIC_INIT};
16318 return register_fstring(rb_setup_fake_str(&fake_str, ptr, len, enc), true, false);
16319}
16320
16321VALUE
16322rb_enc_literal_str(const char *ptr, rb_len_t len, rb_encoding *enc)
16323{
16324 if (enc != NULL && UNLIKELY(rb_enc_autoload_p(enc))) {
16325 rb_enc_autoload(enc);
16326 }
16327
16328 struct RString fake_str = {RBASIC_INIT};
16329 VALUE str = register_fstring(rb_setup_fake_str(&fake_str, ptr, len, enc), true, true);
16330 RUBY_ASSERT(RB_OBJ_SHAREABLE_P(str) && (rb_gc_verify_shareable(str), 1));
16331 return str;
16332}
16333
16334VALUE
16335rb_enc_interned_str_cstr(const char *ptr, rb_encoding *enc)
16336{
16337 return rb_enc_interned_str(ptr, strlen(ptr), enc);
16338}
16339
16340#if USE_YJIT || USE_ZJIT
16341void
16342rb_jit_str_concat_codepoint(VALUE str, VALUE codepoint)
16343{
16344 if (RB_LIKELY(ENCODING_GET_INLINED(str) == rb_ascii8bit_encindex())) {
16345 ssize_t code = RB_NUM2SSIZE(codepoint);
16346
16347 if (RB_LIKELY(code >= 0 && code < 0xff)) {
16348 rb_str_buf_cat_byte(str, (char) code);
16349 return;
16350 }
16351 }
16352
16353 rb_str_concat(str, codepoint);
16354}
16355#endif
16356
16357static int
16358fstring_set_class_i(VALUE *str, void *data)
16359{
16360 RBASIC_SET_CLASS(*str, rb_cString);
16361
16362 return ST_CONTINUE;
16363}
16364
16365void
16366Init_String(void)
16367{
16368 rb_cString = rb_define_class("String", rb_cObject);
16369
16370 rb_concurrent_set_foreach_with_replace(fstring_table_obj, fstring_set_class_i, NULL);
16371
16373 rb_define_alloc_func(rb_cString, empty_str_alloc);
16374 rb_define_singleton_method(rb_cString, "new", rb_str_s_new, -1);
16375 rb_define_singleton_method(rb_cString, "try_convert", rb_str_s_try_convert, 1);
16377 rb_define_method(rb_cString, "initialize_copy", rb_str_replace, 1);
16378 rb_define_method(rb_cString, "<=>", rb_str_cmp_m, 1);
16381 rb_define_method(rb_cString, "eql?", rb_str_eql, 1);
16382 rb_define_method(rb_cString, "hash", rb_str_hash_m, 0);
16383 rb_define_method(rb_cString, "casecmp", rb_str_casecmp, 1);
16384 rb_define_method(rb_cString, "casecmp?", rb_str_casecmp_p, 1);
16387 rb_define_method(rb_cString, "%", rb_str_format_m, 1);
16388 rb_define_method(rb_cString, "[]", rb_str_aref_m, -1);
16389 rb_define_method(rb_cString, "[]=", rb_str_aset_m, -1);
16390 rb_define_method(rb_cString, "insert", rb_str_insert, 2);
16393 rb_define_method(rb_cString, "bytesize", rb_str_bytesize, 0);
16394 rb_define_method(rb_cString, "empty?", rb_str_empty, 0);
16395 rb_define_method(rb_cString, "=~", rb_str_match, 1);
16396 rb_define_method(rb_cString, "match", rb_str_match_m, -1);
16397 rb_define_method(rb_cString, "match?", rb_str_match_m_p, -1);
16399 rb_define_method(rb_cString, "succ!", rb_str_succ_bang, 0);
16401 rb_define_method(rb_cString, "next!", rb_str_succ_bang, 0);
16402 rb_define_method(rb_cString, "upto", rb_str_upto, -1);
16403 rb_define_method(rb_cString, "index", rb_str_index_m, -1);
16404 rb_define_method(rb_cString, "byteindex", rb_str_byteindex_m, -1);
16405 rb_define_method(rb_cString, "rindex", rb_str_rindex_m, -1);
16406 rb_define_method(rb_cString, "byterindex", rb_str_byterindex_m, -1);
16407 rb_define_method(rb_cString, "clear", rb_str_clear, 0);
16408 rb_define_method(rb_cString, "chr", rb_str_chr, 0);
16409 rb_define_method(rb_cString, "getbyte", rb_str_getbyte, 1);
16410 rb_define_method(rb_cString, "setbyte", rb_str_setbyte, 2);
16411 rb_define_method(rb_cString, "bit_get", rb_str_bit_get, -1);
16412 rb_define_method(rb_cString, "bit_set?", rb_str_bit_set_p, -1);
16413 rb_define_method(rb_cString, "bit_set", rb_str_bit_set, -1);
16414 rb_define_method(rb_cString, "bit_clear", rb_str_bit_clear, -1);
16415 rb_define_method(rb_cString, "bit_flip", rb_str_bit_flip, -1);
16416 rb_define_method(rb_cString, "bit_count", rb_str_bit_count, -1);
16417 rb_define_method(rb_cString, "bitwise_not", rb_str_bitwise_not, 0);
16418 rb_define_method(rb_cString, "bitwise_not!", rb_str_bitwise_not_bang, 0);
16419 rb_define_method(rb_cString, "bitwise_and", rb_str_bitwise_and, 1);
16420 rb_define_method(rb_cString, "bitwise_and!", rb_str_bitwise_and_bang, 1);
16421 rb_define_method(rb_cString, "bitwise_or", rb_str_bitwise_or, 1);
16422 rb_define_method(rb_cString, "bitwise_or!", rb_str_bitwise_or_bang, 1);
16423 rb_define_method(rb_cString, "bitwise_xor", rb_str_bitwise_xor, 1);
16424 rb_define_method(rb_cString, "bitwise_xor!", rb_str_bitwise_xor_bang, 1);
16425 rb_define_method(rb_cString, "byteslice", rb_str_byteslice, -1);
16426 rb_define_method(rb_cString, "bytesplice", rb_str_bytesplice, -1);
16427 rb_define_method(rb_cString, "scrub", str_scrub, -1);
16428 rb_define_method(rb_cString, "scrub!", str_scrub_bang, -1);
16430 rb_define_method(rb_cString, "+@", str_uplus, 0);
16431 rb_define_method(rb_cString, "-@", str_uminus, 0);
16432 rb_define_method(rb_cString, "dup", rb_str_dup_m, 0);
16433 rb_define_alias(rb_cString, "dedup", "-@");
16434
16435 rb_define_method(rb_cString, "to_i", rb_str_to_i, -1);
16436 rb_define_method(rb_cString, "to_f", rb_str_to_f, 0);
16437 rb_define_method(rb_cString, "to_s", rb_str_to_s, 0);
16438 rb_define_method(rb_cString, "to_str", rb_str_to_s, 0);
16441 rb_define_method(rb_cString, "undump", str_undump, 0);
16442
16443 sym_ascii = ID2SYM(rb_intern_const("ascii"));
16444 sym_turkic = ID2SYM(rb_intern_const("turkic"));
16445 sym_lithuanian = ID2SYM(rb_intern_const("lithuanian"));
16446 sym_fold = ID2SYM(rb_intern_const("fold"));
16447
16448 rb_define_method(rb_cString, "upcase", rb_str_upcase, -1);
16449 rb_define_method(rb_cString, "downcase", rb_str_downcase, -1);
16450 rb_define_method(rb_cString, "capitalize", rb_str_capitalize, -1);
16451 rb_define_method(rb_cString, "swapcase", rb_str_swapcase, -1);
16452
16453 rb_define_method(rb_cString, "upcase!", rb_str_upcase_bang, -1);
16454 rb_define_method(rb_cString, "downcase!", rb_str_downcase_bang, -1);
16455 rb_define_method(rb_cString, "capitalize!", rb_str_capitalize_bang, -1);
16456 rb_define_method(rb_cString, "swapcase!", rb_str_swapcase_bang, -1);
16457
16458 rb_define_method(rb_cString, "hex", rb_str_hex, 0);
16459 rb_define_method(rb_cString, "oct", rb_str_oct, 0);
16460 rb_define_method(rb_cString, "split", rb_str_split_m, -1);
16461 rb_define_method(rb_cString, "lines", rb_str_lines, -1);
16462 rb_define_method(rb_cString, "bytes", rb_str_bytes, 0);
16463 rb_define_method(rb_cString, "chars", rb_str_chars, 0);
16464 rb_define_method(rb_cString, "codepoints", rb_str_codepoints, 0);
16465 rb_define_method(rb_cString, "grapheme_clusters", rb_str_grapheme_clusters, 0);
16466 rb_define_method(rb_cString, "reverse", rb_str_reverse, 0);
16467 rb_define_method(rb_cString, "reverse!", rb_str_reverse_bang, 0);
16468 rb_define_method(rb_cString, "concat", rb_str_concat_multi, -1);
16469 rb_define_method(rb_cString, "append_as_bytes", rb_str_append_as_bytes, -1);
16471 rb_define_method(rb_cString, "prepend", rb_str_prepend_multi, -1);
16472 rb_define_method(rb_cString, "crypt", rb_str_crypt, 1);
16473 rb_define_method(rb_cString, "intern", rb_str_intern, 0); /* in symbol.c */
16474 rb_define_method(rb_cString, "to_sym", rb_str_intern, 0); /* in symbol.c */
16475 rb_define_method(rb_cString, "ord", rb_str_ord, 0);
16476
16477 rb_define_method(rb_cString, "include?", rb_str_include, 1);
16478 rb_define_method(rb_cString, "start_with?", rb_str_start_with, -1);
16479 rb_define_method(rb_cString, "end_with?", rb_str_end_with, -1);
16480
16481 rb_define_method(rb_cString, "scan", rb_str_scan, 1);
16482
16483 rb_define_method(rb_cString, "ljust", rb_str_ljust, -1);
16484 rb_define_method(rb_cString, "rjust", rb_str_rjust, -1);
16485 rb_define_method(rb_cString, "center", rb_str_center, -1);
16486
16487 rb_define_method(rb_cString, "sub", rb_str_sub, -1);
16488 rb_define_method(rb_cString, "gsub", rb_str_gsub, -1);
16489 rb_define_method(rb_cString, "chop", rb_str_chop, 0);
16490 rb_define_method(rb_cString, "chomp", rb_str_chomp, -1);
16491 rb_define_method(rb_cString, "strip", rb_str_strip, -1);
16492 rb_define_method(rb_cString, "lstrip", rb_str_lstrip, -1);
16493 rb_define_method(rb_cString, "rstrip", rb_str_rstrip, -1);
16494 rb_define_method(rb_cString, "delete_prefix", rb_str_delete_prefix, 1);
16495 rb_define_method(rb_cString, "delete_suffix", rb_str_delete_suffix, 1);
16496
16497 rb_define_method(rb_cString, "sub!", rb_str_sub_bang, -1);
16498 rb_define_method(rb_cString, "gsub!", rb_str_gsub_bang, -1);
16499 rb_define_method(rb_cString, "chop!", rb_str_chop_bang, 0);
16500 rb_define_method(rb_cString, "chomp!", rb_str_chomp_bang, -1);
16501 rb_define_method(rb_cString, "strip!", rb_str_strip_bang, -1);
16502 rb_define_method(rb_cString, "lstrip!", rb_str_lstrip_bang, -1);
16503 rb_define_method(rb_cString, "rstrip!", rb_str_rstrip_bang, -1);
16504 rb_define_method(rb_cString, "delete_prefix!", rb_str_delete_prefix_bang, 1);
16505 rb_define_method(rb_cString, "delete_suffix!", rb_str_delete_suffix_bang, 1);
16506
16507 rb_define_method(rb_cString, "tr", rb_str_tr, -1);
16508 rb_define_method(rb_cString, "tr_s", rb_str_tr_s, 2);
16509 rb_define_method(rb_cString, "delete", rb_str_delete, -1);
16510 rb_define_method(rb_cString, "squeeze", rb_str_squeeze, -1);
16511 rb_define_method(rb_cString, "count", rb_str_count, -1);
16512
16513 rb_define_method(rb_cString, "tr!", rb_str_tr_bang, -1);
16514 rb_define_method(rb_cString, "tr_s!", rb_str_tr_s_bang, 2);
16515 rb_define_method(rb_cString, "delete!", rb_str_delete_bang, -1);
16516 rb_define_method(rb_cString, "squeeze!", rb_str_squeeze_bang, -1);
16517
16518 rb_define_method(rb_cString, "each_line", rb_str_each_line, -1);
16519 rb_define_method(rb_cString, "each_byte", rb_str_each_byte, 0);
16520 rb_define_method(rb_cString, "each_char", rb_str_each_char, 0);
16521 rb_define_method(rb_cString, "each_codepoint", rb_str_each_codepoint, 0);
16522 rb_define_method(rb_cString, "each_grapheme_cluster", rb_str_each_grapheme_cluster, 0);
16523
16524 rb_define_method(rb_cString, "sum", rb_str_sum, -1);
16525
16526 rb_define_method(rb_cString, "slice", rb_str_aref_m, -1);
16527 rb_define_method(rb_cString, "slice!", rb_str_slice_bang, -1);
16528
16529 rb_define_method(rb_cString, "partition", rb_str_partition, 1);
16530 rb_define_method(rb_cString, "rpartition", rb_str_rpartition, 1);
16531
16532 rb_define_method(rb_cString, "encoding", rb_obj_encoding, 0); /* in encoding.c */
16533 rb_define_method(rb_cString, "force_encoding", rb_str_force_encoding, 1);
16534 rb_define_method(rb_cString, "b", rb_str_b, 0);
16535
16536 /* define UnicodeNormalize module here so that we don't have to look it up */
16537 mUnicodeNormalize = rb_define_module("UnicodeNormalize");
16538 id_normalize = rb_intern_const("normalize");
16539 id_normalized_p = rb_intern_const("normalized?");
16540
16541 rb_define_method(rb_cString, "unicode_normalize", rb_str_unicode_normalize, -1);
16542 rb_define_method(rb_cString, "unicode_normalize!", rb_str_unicode_normalize_bang, -1);
16543 rb_define_method(rb_cString, "unicode_normalized?", rb_str_unicode_normalized_p, -1);
16544
16545 rb_fs = Qnil;
16546 rb_define_hooked_variable("$;", &rb_fs, 0, rb_fs_setter);
16547 rb_define_hooked_variable("$-F", &rb_fs, 0, rb_fs_setter);
16548 rb_gc_register_address(&rb_fs);
16549
16550 rb_cSymbol = rb_define_class("Symbol", rb_cObject);
16554 rb_define_singleton_method(rb_cSymbol, "all_symbols", sym_all_symbols, 0);
16555
16556 rb_define_method(rb_cSymbol, "==", sym_equal, 1);
16557 rb_define_method(rb_cSymbol, "===", sym_equal, 1);
16558 rb_define_method(rb_cSymbol, "inspect", sym_inspect, 0);
16559 rb_define_method(rb_cSymbol, "to_proc", rb_sym_to_proc, 0); /* in proc.c */
16560 rb_define_method(rb_cSymbol, "succ", sym_succ, 0);
16561 rb_define_method(rb_cSymbol, "next", sym_succ, 0);
16562
16563 rb_define_method(rb_cSymbol, "<=>", sym_cmp, 1);
16564 rb_define_method(rb_cSymbol, "casecmp", sym_casecmp, 1);
16565 rb_define_method(rb_cSymbol, "casecmp?", sym_casecmp_p, 1);
16566 rb_define_method(rb_cSymbol, "=~", sym_match, 1);
16567
16568 rb_define_method(rb_cSymbol, "[]", sym_aref, -1);
16569 rb_define_method(rb_cSymbol, "slice", sym_aref, -1);
16570 rb_define_method(rb_cSymbol, "length", sym_length, 0);
16571 rb_define_method(rb_cSymbol, "size", sym_length, 0);
16572 rb_define_method(rb_cSymbol, "match", sym_match_m, -1);
16573 rb_define_method(rb_cSymbol, "match?", sym_match_m_p, -1);
16574
16575 rb_define_method(rb_cSymbol, "upcase", sym_upcase, -1);
16576 rb_define_method(rb_cSymbol, "downcase", sym_downcase, -1);
16577 rb_define_method(rb_cSymbol, "capitalize", sym_capitalize, -1);
16578 rb_define_method(rb_cSymbol, "swapcase", sym_swapcase, -1);
16579
16580 rb_define_method(rb_cSymbol, "start_with?", sym_start_with, -1);
16581 rb_define_method(rb_cSymbol, "end_with?", sym_end_with, -1);
16582
16583 rb_define_method(rb_cSymbol, "encoding", sym_encoding, 0);
16584}
16585
16586#include "string.rbinc"
Atomic operations.
@ RUBY_ENC_CODERANGE_7BIT
The object holds 0 to 127 inclusive and nothing else.
Definition coderange.h:39
static enum ruby_coderange_type RB_ENC_CODERANGE_AND(enum ruby_coderange_type a, enum ruby_coderange_type b)
"Mix" two code ranges into one.
Definition coderange.h:162
#define ISSPACE
@old{rb_isspace}
Definition ctype.h:88
static int rb_isspace(int c)
Our own locale-insensitive version of isspace(3).
Definition ctype.h:395
static int rb_isascii(int c)
Our own locale-insensitive version of isascii(3).
Definition ctype.h:209
#define ISDIGIT
@old{rb_isdigit}
Definition ctype.h:93
#define ISALPHA
@old{rb_isalpha}
Definition ctype.h:92
#define TOLOWER
@old{rb_tolower}
Definition ctype.h:101
#define ISPRINT
@old{rb_isprint}
Definition ctype.h:86
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
static bool rb_enc_is_newline(const char *p, const char *e, rb_encoding *enc)
Queries if the passed pointer points to a newline character.
Definition ctype.h:43
static bool rb_enc_isprint(OnigCodePoint c, rb_encoding *enc)
Identical to rb_isprint(), except it additionally takes an encoding.
Definition ctype.h:180
static bool rb_enc_isctype(OnigCodePoint c, OnigCtype t, rb_encoding *enc)
Queries if the passed code point is of passed character type in the passed encoding.
Definition ctype.h:63
VALUE rb_enc_sprintf(rb_encoding *enc, const char *fmt,...)
Identical to rb_sprintf(), except it additionally takes an encoding.
Definition sprintf.c:1249
static VALUE RB_OBJ_FROZEN_RAW(VALUE obj)
This is an implementation detail of RB_OBJ_FROZEN().
Definition fl_type.h:699
static VALUE RB_FL_TEST_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_TEST().
Definition fl_type.h:407
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
Definition class.c:1737
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition class.c:3068
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition class.c:2871
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
Definition class.c:3358
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition eval.c:1041
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
Definition class.c:3147
#define TYPE(_)
Old name of rb_type.
Definition value_type.h:108
#define ENCODING_SET_INLINED(obj, i)
Old name of RB_ENCODING_SET_INLINED.
Definition encoding.h:106
#define RB_INTEGER_TYPE_P
Old name of rb_integer_type_p.
Definition value_type.h:87
#define ENC_CODERANGE_7BIT
Old name of RUBY_ENC_CODERANGE_7BIT.
Definition coderange.h:180
#define ENC_CODERANGE_VALID
Old name of RUBY_ENC_CODERANGE_VALID.
Definition coderange.h:181
#define FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
Definition fl_type.h:130
#define rb_str_buf_cat2
Old name of rb_usascii_str_new_cstr.
Definition string.h:1707
#define ALLOCV
Old name of RB_ALLOCV.
Definition memory.h:407
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define ENC_CODERANGE_CLEAN_P(cr)
Old name of RB_ENC_CODERANGE_CLEAN_P.
Definition coderange.h:183
#define ENC_CODERANGE_AND(a, b)
Old name of RB_ENC_CODERANGE_AND.
Definition coderange.h:188
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define OBJ_FROZEN
Old name of RB_OBJ_FROZEN.
Definition fl_type.h:133
#define LEN2NUM
Old name of RB_LEN2NUM.
Definition len_t.h:33
#define rb_str_cat2
Old name of rb_str_cat_cstr.
Definition string.h:1708
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
Definition assume.h:28
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
Definition value_type.h:57
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition fl_type.h:131
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
Definition value_type.h:63
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition assume.h:29
#define SYM2ID
Old name of RB_SYM2ID.
Definition symbol.h:45
#define ENC_CODERANGE(obj)
Old name of RB_ENC_CODERANGE.
Definition coderange.h:184
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:205
#define ENC_CODERANGE_UNKNOWN
Old name of RUBY_ENC_CODERANGE_UNKNOWN.
Definition coderange.h:179
#define SIZET2NUM
Old name of RB_SIZE2NUM.
Definition size_t.h:62
#define FIXABLE
Old name of RB_FIXABLE.
Definition fixnum.h:25
#define xmalloc
Old name of ruby_xmalloc.
Definition xmalloc.h:53
#define ENCODING_GET(obj)
Old name of RB_ENCODING_GET.
Definition encoding.h:109
#define LONG2FIX
Old name of RB_INT2FIX.
Definition long.h:49
#define ENC_CODERANGE_MASK
Old name of RUBY_ENC_CODERANGE_MASK.
Definition coderange.h:178
#define ZALLOC_N
Old name of RB_ZALLOC_N.
Definition memory.h:404
#define T_HASH
Old name of RUBY_T_HASH.
Definition value_type.h:65
#define ALLOC_N
Old name of RB_ALLOC_N.
Definition memory.h:402
#define MBCLEN_CHARFOUND_LEN(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_LEN.
Definition encoding.h:517
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:128
#define FL_SET
Old name of RB_FL_SET.
Definition fl_type.h:125
#define rb_ary_new3
Old name of rb_ary_new_from_args.
Definition array.h:658
#define ENCODING_INLINE_MAX
Old name of RUBY_ENCODING_INLINE_MAX.
Definition encoding.h:67
#define LONG2NUM
Old name of RB_LONG2NUM.
Definition long.h:50
#define FL_ANY_RAW
Old name of RB_FL_ANY_RAW.
Definition fl_type.h:122
#define MBCLEN_INVALID_P(ret)
Old name of ONIGENC_MBCLEN_INVALID_P.
Definition encoding.h:518
#define ULL2NUM
Old name of RB_ULL2NUM.
Definition long_long.h:31
#define Qtrue
Old name of RUBY_Qtrue.
#define ST2FIX
Old name of RB_ST2FIX.
Definition st_data_t.h:33
#define MBCLEN_NEEDMORE_P(ret)
Old name of ONIGENC_MBCLEN_NEEDMORE_P.
Definition encoding.h:519
#define FIXNUM_MAX
Old name of RUBY_FIXNUM_MAX.
Definition fixnum.h:26
#define NUM2INT
Old name of RB_NUM2INT.
Definition int.h:44
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
Definition long.h:46
#define ENC_CODERANGE_BROKEN
Old name of RUBY_ENC_CODERANGE_BROKEN.
Definition coderange.h:182
#define scan_hex(s, l, e)
Old name of ruby_scan_hex.
Definition util.h:108
#define NIL_P
Old name of RB_NIL_P.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
Definition memory.h:408
#define MBCLEN_CHARFOUND_P(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_P.
Definition encoding.h:516
#define NUM2ULL
Old name of RB_NUM2ULL.
Definition long_long.h:35
#define DBL2NUM
Old name of rb_float_new.
Definition double.h:29
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
#define ENCODING_SHIFT
Old name of RUBY_ENCODING_SHIFT.
Definition encoding.h:68
#define FL_TEST
Old name of RB_FL_TEST.
Definition fl_type.h:127
#define FL_FREEZE
Old name of RUBY_FL_FREEZE.
Definition fl_type.h:65
#define ENCODING_GET_INLINED(obj)
Old name of RB_ENCODING_GET_INLINED.
Definition encoding.h:108
#define ENC_CODERANGE_CLEAR(obj)
Old name of RB_ENC_CODERANGE_CLEAR.
Definition coderange.h:187
#define FL_UNSET
Old name of RB_FL_UNSET.
Definition fl_type.h:129
#define UINT2NUM
Old name of RB_UINT2NUM.
Definition int.h:46
#define ENCODING_IS_ASCII8BIT(obj)
Old name of RB_ENCODING_IS_ASCII8BIT.
Definition encoding.h:110
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define NUM2LEN
Old name of RB_NUM2LEN.
Definition len_t.h:34
#define CONST_ID
Old name of RUBY_CONST_ID.
Definition symbol.h:47
#define rb_ary_new2
Old name of rb_ary_new_capa.
Definition array.h:657
#define ENC_CODERANGE_SET(obj, cr)
Old name of RB_ENC_CODERANGE_SET.
Definition coderange.h:186
#define ENCODING_CODERANGE_SET(obj, encindex, cr)
Old name of RB_ENCODING_CODERANGE_SET.
Definition coderange.h:189
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition fl_type.h:126
#define ALLOCV_END
Old name of RB_ALLOCV_END.
Definition memory.h:409
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
#define OBJ_FROZEN_RAW
Old name of RB_OBJ_FROZEN_RAW.
Definition fl_type.h:134
#define T_REGEXP
Old name of RUBY_T_REGEXP.
Definition value_type.h:77
#define ENCODING_MASK
Old name of RUBY_ENCODING_MASK.
Definition encoding.h:69
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:474
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:678
void rb_syserr_fail(int e, const char *mesg)
Raises appropriate exception that represents a C errno.
Definition error.c:4053
VALUE rb_eRangeError
RangeError exception.
Definition error.c:1461
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1457
VALUE rb_eEncCompatError
Encoding::CompatibilityError exception.
Definition error.c:1464
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1455
VALUE rb_eArgError
ArgumentError exception.
Definition error.c:1458
VALUE rb_eIndexError
IndexError exception.
Definition error.c:1459
@ RB_WARN_CATEGORY_DEPRECATED
Warning is for deprecated features.
Definition error.h:48
VALUE rb_cObject
Object class.
Definition object.c:60
VALUE rb_any_to_s(VALUE obj)
Generates a textual representation of the given object.
Definition object.c:654
VALUE rb_obj_alloc(VALUE klass)
Allocates an instance of the given class.
Definition object.c:2264
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
Definition object.c:94
VALUE rb_class_new_instance_pass_kw(int argc, const VALUE *argv, VALUE klass)
Identical to rb_class_new_instance(), except it passes the passed keywords if any to the #initialize ...
Definition object.c:2282
VALUE rb_obj_frozen_p(VALUE obj)
Same as RB_OBJ_FROZEN(), but returns Qtrue/Qfalse instead of bool.
Definition object.c:1312
double rb_str_to_dbl(VALUE str, int mode)
Identical to rb_cstr_to_dbl(), except it accepts a Ruby's string instead of C's.
Definition object.c:3651
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:230
VALUE rb_obj_dup(VALUE obj)
Duplicates the given object.
Definition object.c:552
VALUE rb_cSymbol
Symbol class.
Definition string.c:86
VALUE rb_cRange
Range class.
Definition range.c:35
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
Definition object.c:902
VALUE rb_obj_freeze(VALUE obj)
Same as RB_OBJ_FREEZE(), but returns the given object.
Definition object.c:1305
VALUE rb_mComparable
Comparable module.
Definition compar.c:19
VALUE rb_cString
String class.
Definition string.c:85
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
Definition object.c:3337
#define RUBY_ASSERT_ALWAYS(expr,...)
A variant of RUBY_ASSERT that does not interface with RUBY_DEBUG.
Definition assert.h:199
#define RBIMPL_ASSERT_OR_ASSUME(...)
This is either RUBY_ASSERT or RBIMPL_ASSUME, depending on RUBY_DEBUG.
Definition assert.h:311
#define RUBY_ASSERT_BUILTIN_TYPE(obj, type)
A variant of RUBY_ASSERT that asserts when either RUBY_DEBUG or built-in type of obj is type.
Definition assert.h:291
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
Encoding relates APIs.
rb_encoding * rb_utf8_encoding(void)
Queries the encoding that represents UTF-8.
Definition encoding.c:1578
rb_encoding * rb_ascii8bit_encoding(void)
Queries the encoding that represents ASCII-8BIT a.k.a.
Definition encoding.c:1566
rb_encoding * rb_filesystem_encoding(void)
Queries the "filesystem" encoding.
Definition encoding.c:1642
rb_encoding * rb_default_internal_encoding(void)
Queries the "default internal" encoding.
Definition encoding.c:1787
static char * rb_enc_left_char_head(const char *s, const char *p, const char *e, rb_encoding *enc)
Queries the left boundary of a character.
Definition encoding.h:683
int rb_utf8_encindex(void)
Identical to rb_utf8_encoding(), except it returns the encoding's index instead of the encoding itsel...
Definition encoding.c:1584
int rb_ascii8bit_encindex(void)
Identical to rb_ascii8bit_encoding(), except it returns the encoding's index instead of the encoding ...
Definition encoding.c:1572
static char * rb_enc_right_char_head(const char *s, const char *p, const char *e, rb_encoding *enc)
Queries the right boundary of a character.
Definition encoding.h:704
static unsigned int rb_enc_codepoint(const char *p, const char *e, rb_encoding *enc)
Queries the code point of character pointed by the passed pointer.
Definition encoding.h:571
rb_encoding * rb_default_external_encoding(void)
Queries the "default external" encoding.
Definition encoding.c:1700
static int rb_enc_mbmaxlen(rb_encoding *enc)
Queries the maximum number of bytes that the passed encoding needs to represent a character.
Definition encoding.h:447
static int RB_ENCODING_GET_INLINED(VALUE obj)
Queries the encoding of the passed object.
Definition encoding.h:99
rb_encoding * rb_locale_encoding(void)
Queries the encoding that represents the current locale.
Definition encoding.c:1630
rb_encoding * rb_usascii_encoding(void)
Queries the encoding that represents US-ASCII.
Definition encoding.c:1590
static int rb_enc_code_to_mbclen(int c, rb_encoding *enc)
Identical to rb_enc_codelen(), except it returns 0 for invalid code points.
Definition encoding.h:619
static char * rb_enc_step_back(const char *s, const char *p, const char *e, int n, rb_encoding *enc)
Scans the string backwards for n characters.
Definition encoding.h:726
int rb_usascii_encindex(void)
Identical to rb_usascii_encoding(), except it returns the encoding's index instead of the encoding it...
Definition encoding.c:1596
rb_len_t rb_enc_strlen(const char *head, const char *tail, rb_encoding *enc)
Counts the number of characters of the passed string, according to the passed encoding.
Definition string.c:2406
VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
Encoding conversion main routine.
Definition string.c:1393
rb_len_t rb_memsearch(const void *x, rb_len_t m, const void *y, rb_len_t n, rb_encoding *enc)
Looks for the passed string in the passed buffer.
Definition re.c:290
VALUE rb_enc_str_new_static(const char *ptr, rb_len_t len, rb_encoding *enc)
Identical to rb_enc_str_new(), except it takes a C string literal.
Definition string.c:1258
VALUE rb_external_str_new_with_enc(const char *ptr, rb_len_t len, rb_encoding *enc)
Identical to rb_external_str_new(), except it additionally takes an encoding.
Definition string.c:1399
VALUE rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags, VALUE ecopts)
Identical to rb_str_conv_enc(), except it additionally takes IO encoder options.
Definition string.c:1277
VALUE rb_enc_str_new_cstr(const char *ptr, rb_encoding *enc)
Identical to rb_enc_str_new(), except it assumes the passed pointer is a pointer to a C string.
Definition string.c:1189
VALUE rb_str_export_to_enc(VALUE obj, rb_encoding *enc)
Identical to rb_str_export(), except it additionally takes an encoding.
Definition string.c:1498
VALUE rb_enc_str_buf_cat(VALUE str, const char *ptr, rb_len_t len, rb_encoding *enc)
Identical to rb_str_cat(), except it additionally takes an encoding.
Definition string.c:3873
int rb_enc_str_asciionly_p(VALUE str)
Queries if the passed string is "ASCII only".
Definition string.c:1002
VALUE rb_enc_interned_str(const char *ptr, rb_len_t len, rb_encoding *enc)
Identical to rb_enc_str_new(), except it returns a "f"string.
Definition string.c:16311
VALUE rb_enc_interned_str_cstr(const char *ptr, rb_encoding *enc)
Identical to rb_enc_str_new_cstr(), except it returns a "f"string.
Definition string.c:16335
char * rb_enc_nth(const char *head, const char *tail, rb_len_t nth, rb_encoding *enc)
Queries the n-th character.
Definition string.c:3145
int rb_enc_symname2_p(const char *name, rb_len_t len, rb_encoding *enc)
Identical to rb_enc_symname_p(), except it additionally takes the passed string's length.
Definition symbol.c:858
rb_econv_result_t rb_econv_convert(rb_econv_t *ec, const unsigned char **source_buffer_ptr, const unsigned char *source_buffer_end, unsigned char **destination_buffer_ptr, unsigned char *destination_buffer_end, int flags)
Converts a string from an encoding to another.
Definition transcode.c:1487
rb_econv_result_t
return value of rb_econv_convert()
Definition transcode.h:30
@ econv_finished
The conversion stopped after converting everything.
Definition transcode.h:57
@ econv_destination_buffer_full
The conversion stopped because there is no destination.
Definition transcode.h:46
rb_econv_t * rb_econv_open_opts(const char *source_encoding, const char *destination_encoding, int ecflags, VALUE ecopts)
Identical to rb_econv_open(), except it additionally takes a hash of optional strings.
Definition transcode.c:2732
VALUE rb_str_encode(VALUE str, VALUE to, int ecflags, VALUE ecopts)
Converts the contents of the passed string from its encoding to the passed one.
Definition transcode.c:3044
void rb_econv_close(rb_econv_t *ec)
Destructs a converter.
Definition transcode.c:1744
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1149
VALUE rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcall(), except it takes the method arguments as a C array.
Definition vm_eval.c:1107
VALUE rb_funcall_with_block_kw(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE procval, int kw_splat)
Identical to rb_funcallv_with_block(), except you can specify how to handle the last element of the g...
Definition vm_eval.c:1236
VALUE rb_ary_new_capa(rb_len_t capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_freeze(VALUE obj)
Freeze an array, preventing further modifications.
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
This roughly resembles return enum_for(__callee__) unless block_given?.
Definition enumerator.h:208
#define RETURN_ENUMERATOR(obj, argc, argv)
Identical to RETURN_SIZED_ENUMERATOR(), except its size is unknown.
Definition enumerator.h:242
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
Definition error.h:35
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:284
VALUE rb_hash_new(void)
Creates a new, empty hash object.
Definition hash.c:1646
VALUE rb_fs
The field separator character for inputs, or the $;.
Definition string.c:734
VALUE rb_default_rs
This is the default value of rb_rs, i.e.
Definition io.c:200
VALUE rb_backref_get(void)
Queries the last match, or Regexp.last_match, or the $~.
Definition vm.c:2143
VALUE rb_sym_all_symbols(void)
Collects every single bits of symbols that have ever interned in the entire history of the current pr...
Definition symbol.c:1222
void rb_backref_set(VALUE md)
Updates $~.
Definition vm.c:2149
int rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
Deconstructs a range into its components.
Definition range.c:1857
VALUE rb_range_beg_len(VALUE range, rb_len_t *begp, rb_len_t *lenp, rb_len_t len, int err)
Deconstructs a numerical range.
Definition range.c:1949
int rb_reg_backref_number(VALUE match, VALUE backref)
Queries the index of the given named capture.
Definition re.c:1518
int rb_reg_options(VALUE re)
Queries the options of the passed regular expression.
Definition re.c:5102
VALUE rb_reg_match(VALUE re, VALUE str)
This is the match operator.
Definition re.c:4596
void rb_match_busy(VALUE md)
Asserts that the given MatchData is "occupied".
Definition re.c:2032
VALUE rb_reg_nth_match(int n, VALUE md)
Queries the nth captured substring.
Definition re.c:2501
VALUE rb_filesystem_str_new(const char *ptr, rb_len_t len)
Identical to rb_str_new(), except it generates a string of "filesystem" encoding.
Definition string.c:1474
void rb_str_modify_expand(VALUE str, rb_len_t capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
Definition string.c:2838
void rb_str_free(VALUE str)
Destroys the given string for no reason.
Definition string.c:1817
rb_len_t rb_str_offset(VALUE str, rb_len_t pos)
"Inverse" of rb_str_sublen().
Definition string.c:3173
VALUE rb_str_subseq(VALUE str, rb_len_t beg, rb_len_t len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
Definition string.c:3288
rb_len_t rb_str_strlen(VALUE str)
Counts the number of characters (not bytes) that are stored inside of the given string.
Definition string.c:2493
VALUE rb_str_new_shared(VALUE str)
Identical to rb_str_new_cstr(), except it takes a Ruby's string instead of C's.
Definition string.c:1563
VALUE rb_str_plus(VALUE lhs, VALUE rhs)
Generates a new string, concatenating the former to the latter.
Definition string.c:2578
#define rb_utf8_str_new_cstr(str)
Identical to rb_str_new_cstr, except it generates a string of "UTF-8" encoding.
Definition string.h:1608
#define rb_hash_end(h)
Just another name of st_hash_end.
Definition string.h:970
#define rb_hash_uint32(h, i)
Just another name of st_hash_uint32.
Definition string.h:964
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
Definition string.c:3940
VALUE rb_str_tmp_new(rb_len_t len)
Allocates a "temporary" string.
Definition string.c:1811
VALUE rb_sym_to_s(VALUE sym)
This is an rb_sym2str() + rb_str_dup() combo.
Definition string.c:15892
VALUE rb_str_times(VALUE str, VALUE num)
Repetition of a string.
Definition string.c:2652
VALUE rb_str_succ(VALUE orig)
Searches for the "successor" of a string.
Definition string.c:5675
int rb_str_hash_cmp(VALUE str1, VALUE str2)
Compares two strings.
Definition string.c:4321
VALUE rb_utf8_str_new_static(const char *ptr, rb_len_t len)
Identical to rb_str_new_static(), except it generates a string of "UTF-8" encoding instead of "binary...
Definition string.c:1252
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1523
void rb_str_shared_replace(VALUE dst, VALUE src)
Replaces the contents of the former with the latter.
Definition string.c:1853
#define rb_str_buf_cat
Just another name of rb_str_cat.
Definition string.h:1706
#define rb_usascii_str_new(str, len)
Identical to rb_str_new, except it generates a string of "US ASCII" encoding.
Definition string.h:1557
size_t rb_str_capacity(VALUE str)
Queries the capacity of the given string.
Definition string.c:1037
rb_len_t rb_str_sublen(VALUE str, rb_len_t pos)
Byte offset to character offset conversion.
Definition string.c:3220
VALUE rb_external_str_new(const char *ptr, rb_len_t len)
Identical to rb_str_new(), except it generates a string of "defaultexternal" encoding.
Definition string.c:1450
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
Definition string.c:1569
void rb_str_set_len(VALUE str, rb_len_t len)
Overwrites the length of the string.
Definition string.c:3522
VALUE rb_str_dup(VALUE str)
Duplicates a string.
Definition string.c:2052
st_index_t rb_str_hash(VALUE str)
Calculates a hash value of a string.
Definition string.c:4307
VALUE rb_str_ellipsize(VALUE str, rb_len_t len)
Shortens str and adds three dots, an ellipsis, if it is longer than len characters.
Definition string.c:15158
VALUE rb_str_cat(VALUE dst, const char *src, rb_len_t srclen)
Destructively appends the passed contents to the string.
Definition string.c:3706
VALUE rb_str_locktmp(VALUE str)
Obtains a "temporary lock" of the string.
VALUE rb_str_resurrect(VALUE str)
Like rb_str_dup(), but always create an instance of rb_cString regardless of the given object's class...
Definition string.c:2070
#define rb_str_buf_new_cstr(str)
Identical to rb_str_new_cstr, except done differently.
Definition string.h:1664
VALUE rb_str_new_static(const char *ptr, rb_len_t len)
Identical to rb_str_new(), except it takes a C string literal.
Definition string.c:1223
#define rb_usascii_str_new_cstr(str)
Identical to rb_str_new_cstr, except it generates a string of "US ASCII" encoding.
Definition string.h:1592
VALUE rb_str_new_with_class(VALUE obj, const char *ptr, rb_len_t len)
Identical to rb_str_new(), except it takes the class of the allocating object.
Definition string.c:1767
VALUE rb_str_replace(VALUE dst, VALUE src)
Replaces the contents of the former object with the stringised contents of the latter.
Definition string.c:7256
VALUE rb_str_no_gvl_safe_acquire(VALUE orig)
Creates a frozen copy of orig that guarantees the RSTRING_PTR is safe to use in operations that relea...
Definition string.c:1598
rb_gvar_setter_t rb_str_setter
This is a rb_gvar_setter_t that refutes non-string assignments.
Definition string.h:1171
VALUE rb_interned_str_cstr(const char *ptr)
Identical to rb_interned_str(), except it assumes the passed pointer is a pointer to a C's string.
Definition string.c:16305
VALUE rb_filesystem_str_new_cstr(const char *ptr)
Identical to rb_filesystem_str_new(), except it assumes the passed pointer is a pointer to a C string...
Definition string.c:1480
VALUE rb_str_drop_bytes(VALUE str, rb_len_t len)
Shrinks the given string for the given number of bytes.
Definition string.c:6209
#define rb_external_str_new_cstr(str)
Identical to rb_str_new_cstr, except it generates a string of "defaultexternal" encoding.
Definition string.h:1629
VALUE rb_str_buf_append(VALUE dst, VALUE src)
Identical to rb_str_cat_cstr(), except it takes Ruby's string instead of C's.
Definition string.c:3904
st_index_t rb_memhash(const void *ptr, rb_len_t len)
This is a universal hash function.
Definition random.c:1720
VALUE rb_str_equal(VALUE str1, VALUE str2)
Equality of two strings.
Definition string.c:4446
void rb_str_no_gvl_safe_release(VALUE orig, VALUE tmp)
Releases a string created from rb_str_no_gvl_safe_acquire.
Definition string.c:1641
void rb_str_update(VALUE dst, rb_len_t beg, rb_len_t len, VALUE src)
Replaces some (or all) of the contents of the given string.
Definition string.c:6300
VALUE rb_str_inspect(VALUE str)
Generates a "readable" version of the receiver.
Definition string.c:9204
void rb_must_asciicompat(VALUE obj)
Asserts that the given string's encoding is (Ruby's definition of) ASCII compatible.
Definition string.c:2884
VALUE rb_str_substr(VALUE str, rb_len_t beg, rb_len_t len)
This is the implementation of two-argumented String#slice.
Definition string.c:3385
int rb_str_cmp(VALUE lhs, VALUE rhs)
Compares two strings, as in strcmp(3).
Definition string.c:4393
VALUE rb_str_concat(VALUE dst, VALUE src)
Identical to rb_str_append(), except it also accepts an integer as a codepoint.
Definition string.c:4189
int rb_str_comparable(VALUE str1, VALUE str2)
Checks if two strings are comparable each other or not.
Definition string.c:4368
VALUE rb_usascii_str_new_static(const char *ptr, rb_len_t len)
Identical to rb_str_new_static(), except it generates a string of "US ASCII" encoding instead of "bin...
Definition string.c:1246
#define rb_strlen_lit(str)
Length of a string literal.
Definition string.h:1717
VALUE rb_interned_str(const char *ptr, rb_len_t len)
Identical to rb_str_new(), except it returns an infamous "f"string.
Definition string.c:16290
VALUE rb_str_buf_cat_ascii(VALUE dst, const char *src)
Identical to rb_str_cat_cstr(), except it additionally assumes the source string be a NUL terminated ...
Definition string.c:3880
VALUE rb_str_freeze(VALUE str)
This is the implementation of String#freeze.
Definition string.c:3413
char * rb_str_subpos(VALUE str, rb_len_t beg, rb_len_t *len)
Identical to rb_str_substr(), except it returns a C's string instead of Ruby's.
Definition string.c:3296
VALUE rb_str_scrub(VALUE str, VALUE repl)
"Cleanses" the string.
Definition string.c:15216
#define rb_locale_str_new_cstr(str)
Identical to rb_external_str_new_cstr, except it generates a string of "locale" encoding instead of "...
Definition string.h:1650
VALUE rb_str_buf_new(rb_len_t capa)
Allocates a "string buffer".
Definition string.c:1783
#define rb_str_dup_frozen
Just another name of rb_str_new_frozen.
Definition string.h:656
VALUE rb_locale_str_new(const char *ptr, rb_len_t len)
Identical to rb_str_new(), except it generates a string of "locale" encoding.
Definition string.c:1462
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
Definition string.c:3069
#define rb_str_cat_cstr(buf, str)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1681
VALUE rb_str_unlocktmp(VALUE str)
Releases a lock formerly obtained by rb_str_locktmp().
Definition string.c:3504
#define rb_utf8_str_new(str, len)
Identical to rb_str_new, except it generates a string of "UTF-8" encoding.
Definition string.h:1574
VALUE rb_str_dump(VALUE str)
"Inverse" of rb_eval_string().
Definition string.c:9409
VALUE rb_str_length(VALUE)
Identical to rb_str_strlen(), except it returns the value in rb_cInteger.
Definition string.c:2517
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1539
VALUE rb_str_split(VALUE str, const char *delim)
Divides the given string based on the given delimiter.
Definition string.c:12343
VALUE rb_str_intern(VALUE str)
Identical to rb_to_symbol(), except it assumes the receiver being an instance of RString.
Definition symbol.c:1091
VALUE rb_obj_as_string(VALUE obj)
Try converting an object to its stringised representation using its to_s method, if any.
Definition string.c:1916
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
Definition variable.c:2168
VALUE rb_ivar_defined(VALUE obj, ID name)
Queries if the instance variable is defined at the object.
Definition variable.c:2228
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
Definition vm_method.c:3752
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition vm_method.c:1893
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition symbol.h:285
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
Definition symbol.c:1155
VALUE rb_to_symbol(VALUE name)
Identical to rb_intern_str(), except it generates a dynamic symbol if necessary.
Definition string.c:16257
ID rb_to_id(VALUE str)
Identical to rb_intern_str(), except it tries to convert the parameter object to an instance of rb_cS...
Definition string.c:16247
int capa
Designed capacity of the buffer.
Definition io.h:11
int off
Offset inside of ptr.
Definition io.h:5
int len
Length of the buffer.
Definition io.h:8
#define RB_OBJ_SET_SHAREABLE(obj)
Wrapper of rb_obj_set_shareable().
Definition ractor.h:290
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
Definition ractor.h:255
rb_len_t rb_reg_search(VALUE re, VALUE str, rb_len_t pos, int dir)
Runs the passed regular expression over the passed string.
Definition re.c:2430
VALUE rb_reg_regcomp(VALUE str)
Creates a new instance of rb_cRegexp.
Definition re.c:4264
int ruby_thread_has_gvl_p(void)
Whether the current thread is holding the GVL.
Definition thread.c:2364
VALUE rb_str_format(int argc, const VALUE *argv, VALUE fmt)
Formats a string.
Definition sprintf.c:985
VALUE rb_yield(VALUE val)
Yields the block.
Definition vm_eval.c:1396
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:375
#define ALLOCA_N(type, n)
Definition memory.h:295
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
Definition memory.h:363
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:170
void rb_define_hooked_variable(const char *q, VALUE *w, type *e, void_type *r)
Define a function-backended global variable.
VALUE type(ANYARGS)
ANYARGS-ed function type.
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
Defines RBIMPL_ATTR_NONSTRING.
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
Definition rarray.h:280
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
Definition rarray.h:51
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
Definition rbasic.h:166
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
#define RHASH_SIZE(h)
Queries the size of the hash.
Definition rhash.h:57
#define RMATCH(obj)
Convenient casting macro.
Definition rmatch.h:37
static VALUE RREGEXP_SRC(VALUE rexp)
Convenient getter function.
Definition rregexp.h:93
#define StringValue(v)
Ensures that the parameter object is a String.
Definition rstring.h:66
VALUE rb_str_export_locale(VALUE obj)
Identical to rb_str_export(), except it converts into the locale encoding instead.
Definition string.c:1492
char * rb_string_value_cstr(volatile VALUE *ptr)
Identical to rb_string_value_ptr(), except it additionally checks for the contents for viability as a...
Definition string.c:3040
static int RSTRING_LENINT(VALUE str)
Identical to RSTRING_LEN(), except it differs for the return type.
Definition rstring.h:438
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
Definition rstring.h:409
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
Definition rstring.h:450
static rb_len_t RSTRING_LEN(VALUE str)
Queries the length of the string.
Definition rstring.h:367
VALUE rb_string_value(volatile VALUE *ptr)
Identical to rb_str_to_str(), except it fills the passed pointer with the converted object.
Definition string.c:2903
#define RSTRING(obj)
Convenient casting macro.
Definition rstring.h:41
VALUE rb_str_export(VALUE obj)
Identical to rb_str_to_str(), except it additionally converts the string into default external encodi...
Definition string.c:1486
char * rb_string_value_ptr(volatile VALUE *ptr)
Identical to rb_str_to_str(), except it returns the converted string's backend memory region.
Definition string.c:2916
VALUE rb_str_to_str(VALUE obj)
Identical to rb_check_string_type(), except it raises exceptions in case of conversion failures.
Definition string.c:1844
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
Definition rstring.h:381
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
Definition rstring.h:89
#define DATA_PTR(obj)
Convenient casting macro for backward compatibility.
Definition rtypeddata.h:439
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
Definition rtypeddata.h:557
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:239
VALUE rb_require(const char *feature)
Identical to rb_require_string(), except it takes C's string instead of Ruby's.
Definition load.c:1781
#define errno
Ractor-aware version of errno.
Definition ruby.h:388
#define RB_NUM2SSIZE
Converts an instance of rb_cInteger into C's ssize_t.
Definition size_t.h:49
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
Definition stdarg.h:35
VALUE flags
Per-object flags.
Definition rbasic.h:81
Ruby's String.
Definition rstring.h:196
rb_len_t len
Length of the string, not including terminating NUL character.
Definition rstring.h:206
struct RBasic basic
Basic part, including flags and class.
Definition rstring.h:199
union RString::@157025041137035241047331270155043025061071337053::@157067065136062356112324002106172053054013023024::@365170260060164113275356137374160141226332013204 aux
Auxiliary info.
struct RString::@157025041137035241047331270155043025061071337053::@153056146250355212360325351117351053336274231135 embed
Embedded contents.
VALUE shared
Parent of the string.
Definition rstring.h:240
char * ptr
Pointer to the contents of the string.
Definition rstring.h:222
rb_len_t capa
Capacity of *ptr.
Definition rstring.h:232
union RString::@157025041137035241047331270155043025061071337053 as
String's specific fields.
struct RString::@157025041137035241047331270155043025061071337053::@157067065136062356112324002106172053054013023024 heap
Strings that use separated memory region for contents use this pattern.
Definition string.c:10439
void rb_nativethread_lock_lock(rb_nativethread_lock_t *lock)
Blocks until the current thread obtains a lock.
Definition thread.c:319
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
Definition value.h:142
#define RB_LEN_MAX
Maximum possible value that a rb_len_t can take.
Definition value.h:156
#define RB_LEN_MIN
Minimum possible value that a rb_len_t can take.
Definition value.h:159
unsigned long rb_ulen_t
An unsigned integer type that has the same width with rb_len_t.
Definition value.h:145
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static enum ruby_value_type rb_type(VALUE obj)
Identical to RB_BUILTIN_TYPE(), except it can also accept special constants.
Definition value_type.h:225
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
Definition value_type.h:425
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376
ruby_value_type
C-level type of an object.
Definition value_type.h:113