Ruby 4.0.10p0 (2026-11-19 revision a704b7cd95a42cc0cf5f307137bf3aac0285b915)
string.c
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/compar.h"
30#include "internal/compilers.h"
31#include "internal/concurrent_set.h"
32#include "internal/encoding.h"
33#include "internal/error.h"
34#include "internal/gc.h"
35#include "internal/hash.h"
36#include "internal/numeric.h"
37#include "internal/object.h"
38#include "internal/proc.h"
39#include "internal/re.h"
40#include "internal/sanitizers.h"
41#include "internal/string.h"
42#include "internal/transcode.h"
43#include "probes.h"
44#include "ruby/encoding.h"
45#include "ruby/re.h"
46#include "ruby/thread.h"
47#include "ruby/util.h"
48#include "ruby/ractor.h"
49#include "ruby_assert.h"
50#include "shape.h"
51#include "vm_sync.h"
53
54#if defined HAVE_CRYPT_R
55# if defined HAVE_CRYPT_H
56# include <crypt.h>
57# endif
58#elif !defined HAVE_CRYPT
59# include "missing/crypt.h"
60# define HAVE_CRYPT_R 1
61#endif
62
63#define BEG(no) (regs->beg[(no)])
64#define END(no) (regs->end[(no)])
65
66#undef rb_str_new
67#undef rb_usascii_str_new
68#undef rb_utf8_str_new
69#undef rb_enc_str_new
70#undef rb_str_new_cstr
71#undef rb_usascii_str_new_cstr
72#undef rb_utf8_str_new_cstr
73#undef rb_enc_str_new_cstr
74#undef rb_external_str_new_cstr
75#undef rb_locale_str_new_cstr
76#undef rb_str_dup_frozen
77#undef rb_str_buf_new_cstr
78#undef rb_str_buf_cat
79#undef rb_str_buf_cat2
80#undef rb_str_cat2
81#undef rb_str_cat_cstr
82#undef rb_fstring_cstr
83
86
87/* Flags of RString
88 *
89 * 0: STR_SHARED (equal to ELTS_SHARED)
90 * The string is shared. The buffer this string points to is owned by
91 * another string (the shared root).
92 * 1: RSTRING_NOEMBED
93 * The string is not embedded. When a string is embedded, the contents
94 * follow the header. When a string is not embedded, the contents is
95 * on a separately allocated buffer.
96 * 2: STR_CHILLED_LITERAL (will be frozen in a future version)
97 * The string was allocated as a literal in a file without an explicit `frozen_string_literal` comment.
98 * It emits a deprecation warning when mutated for the first time.
99 * 3: STR_CHILLED_SYMBOL_TO_S (will be frozen in a future version)
100 * The string was allocated by the `Symbol#to_s` method.
101 * It emits a deprecation warning when mutated for the first time.
102 * 4: STR_PRECOMPUTED_HASH
103 * The string is embedded and has its precomputed hashcode stored
104 * after the terminator.
105 * 5: STR_SHARED_ROOT
106 * Other strings may point to the contents of this string. When this
107 * flag is set, STR_SHARED must not be set.
108 * 6: STR_BORROWED
109 * When RSTRING_NOEMBED is set and klass is 0, this string is unsafe
110 * to be unshared by rb_str_tmp_frozen_release.
111 * 7: STR_TMPLOCK
112 * The pointer to the buffer is passed to a system call such as
113 * read(2). Any modification and realloc is prohibited.
114 * 8-9: ENC_CODERANGE
115 * Stores the coderange of the string.
116 * 10-16: ENCODING
117 * Stores the encoding of the string.
118 * 17: RSTRING_FSTR
119 * The string is a fstring. The string is deduplicated in the fstring
120 * table.
121 * 18: STR_NOFREE
122 * Do not free this string's buffer when the string is reclaimed
123 * by the garbage collector. Used for when the string buffer is a C
124 * string literal.
125 * 19: STR_FAKESTR
126 * The string is not allocated or managed by the garbage collector.
127 * Typically, the string object header (struct RString) is temporarily
128 * allocated on C stack.
129 */
130
131#define RUBY_MAX_CHAR_LEN 16
132#define STR_PRECOMPUTED_HASH FL_USER4
133#define STR_SHARED_ROOT FL_USER5
134#define STR_BORROWED FL_USER6
135#define STR_TMPLOCK FL_USER7
136#define STR_NOFREE FL_USER18
137#define STR_FAKESTR FL_USER19
138
139#define STR_SET_NOEMBED(str) do {\
140 FL_SET((str), STR_NOEMBED);\
141 FL_UNSET((str), STR_SHARED | STR_SHARED_ROOT | STR_BORROWED);\
142} while (0)
143#define STR_SET_EMBED(str) FL_UNSET((str), STR_NOEMBED | STR_SHARED | STR_NOFREE)
144
145#define STR_SET_LEN(str, n) do { \
146 RSTRING(str)->len = (n); \
147} while (0)
148
149static inline bool
150str_encindex_fastpath(int encindex)
151{
152 // The overwhelming majority of strings are in one of these 3 encodings.
153 switch (encindex) {
154 case ENCINDEX_ASCII_8BIT:
155 case ENCINDEX_UTF_8:
156 case ENCINDEX_US_ASCII:
157 return true;
158 default:
159 return false;
160 }
161}
162
163static inline bool
164str_enc_fastpath(VALUE str)
165{
166 return str_encindex_fastpath(ENCODING_GET_INLINED(str));
167}
168
169#define TERM_LEN(str) (str_enc_fastpath(str) ? 1 : rb_enc_mbminlen(rb_enc_from_index(ENCODING_GET(str))))
170#define TERM_FILL(ptr, termlen) do {\
171 char *const term_fill_ptr = (ptr);\
172 const int term_fill_len = (termlen);\
173 *term_fill_ptr = '\0';\
174 if (UNLIKELY(term_fill_len > 1))\
175 memset(term_fill_ptr, 0, term_fill_len);\
176} while (0)
177
178#define RESIZE_CAPA(str,capacity) do {\
179 const int termlen = TERM_LEN(str);\
180 RESIZE_CAPA_TERM(str,capacity,termlen);\
181} while (0)
182#define RESIZE_CAPA_TERM(str,capacity,termlen) do {\
183 if (STR_EMBED_P(str)) {\
184 if (str_embed_capa(str) < capacity + termlen) {\
185 char *const tmp = ALLOC_N(char, (size_t)(capacity) + (termlen));\
186 const long tlen = RSTRING_LEN(str);\
187 memcpy(tmp, RSTRING_PTR(str), str_embed_capa(str));\
188 RSTRING(str)->as.heap.ptr = tmp;\
189 RSTRING(str)->len = tlen;\
190 STR_SET_NOEMBED(str);\
191 RSTRING(str)->as.heap.aux.capa = (capacity);\
192 }\
193 }\
194 else {\
195 RUBY_ASSERT(!FL_TEST((str), STR_SHARED)); \
196 SIZED_REALLOC_N(RSTRING(str)->as.heap.ptr, char, \
197 (size_t)(capacity) + (termlen), STR_HEAP_SIZE(str)); \
198 RSTRING(str)->as.heap.aux.capa = (capacity);\
199 }\
200} while (0)
201
202#define STR_SET_SHARED(str, shared_str) do { \
203 if (!FL_TEST(str, STR_FAKESTR)) { \
204 RUBY_ASSERT(RSTRING_PTR(shared_str) <= RSTRING_PTR(str)); \
205 RUBY_ASSERT(RSTRING_PTR(str) <= RSTRING_PTR(shared_str) + RSTRING_LEN(shared_str)); \
206 RB_OBJ_WRITE((str), &RSTRING(str)->as.heap.aux.shared, (shared_str)); \
207 FL_SET((str), STR_SHARED); \
208 FL_SET((shared_str), STR_SHARED_ROOT); \
209 if (RBASIC_CLASS((shared_str)) == 0) /* for CoW-friendliness */ \
210 FL_SET_RAW((shared_str), STR_BORROWED); \
211 } \
212} while (0)
213
214#define STR_HEAP_PTR(str) (RSTRING(str)->as.heap.ptr)
215#define STR_HEAP_SIZE(str) ((size_t)RSTRING(str)->as.heap.aux.capa + TERM_LEN(str))
216/* TODO: include the terminator size in capa. */
217
218#define STR_ENC_GET(str) get_encoding(str)
219
220#if !defined SHARABLE_MIDDLE_SUBSTRING
221# define SHARABLE_MIDDLE_SUBSTRING 0
222#endif
223#if !SHARABLE_MIDDLE_SUBSTRING
224#define SHARABLE_SUBSTRING_P(beg, len, end) ((beg) + (len) == (end))
225#else
226#define SHARABLE_SUBSTRING_P(beg, len, end) 1
227#endif
228
229
230static inline long
231str_embed_capa(VALUE str)
232{
233 return rb_gc_obj_slot_size(str) - offsetof(struct RString, as.embed.ary);
234}
235
236bool
237rb_str_reembeddable_p(VALUE str)
238{
239 return !FL_TEST(str, STR_NOFREE|STR_SHARED_ROOT|STR_SHARED);
240}
241
242static inline size_t
243rb_str_embed_size(long capa, long termlen)
244{
245 size_t size = offsetof(struct RString, as.embed.ary) + capa + termlen;
246 if (size < sizeof(struct RString)) size = sizeof(struct RString);
247 return size;
248}
249
250size_t
251rb_str_size_as_embedded(VALUE str)
252{
253 size_t real_size;
254 if (STR_EMBED_P(str)) {
255 size_t capa = RSTRING(str)->len;
256 if (FL_TEST_RAW(str, STR_PRECOMPUTED_HASH)) capa += sizeof(st_index_t);
257
258 real_size = rb_str_embed_size(capa, TERM_LEN(str));
259 }
260 /* if the string is not currently embedded, but it can be embedded, how
261 * much space would it require */
262 else if (rb_str_reembeddable_p(str)) {
263 size_t capa = RSTRING(str)->as.heap.aux.capa;
264 if (FL_TEST_RAW(str, STR_PRECOMPUTED_HASH)) capa += sizeof(st_index_t);
265
266 real_size = rb_str_embed_size(capa, TERM_LEN(str));
267 }
268 else {
269 real_size = sizeof(struct RString);
270 }
271
272 return real_size;
273}
274
275static inline bool
276STR_EMBEDDABLE_P(long len, long termlen)
277{
278 return rb_gc_size_allocatable_p(rb_str_embed_size(len, termlen));
279}
280
281static VALUE str_replace_shared_without_enc(VALUE str2, VALUE str);
282static VALUE str_new_frozen(VALUE klass, VALUE orig);
283static VALUE str_new_frozen_buffer(VALUE klass, VALUE orig, int copy_encoding);
284static VALUE str_new_static(VALUE klass, const char *ptr, long len, int encindex);
285static VALUE str_new(VALUE klass, const char *ptr, long len);
286static void str_make_independent_expand(VALUE str, long len, long expand, const int termlen);
287static inline void str_modifiable(VALUE str);
288static VALUE rb_str_downcase(int argc, VALUE *argv, VALUE str);
289static inline VALUE str_alloc_embed(VALUE klass, size_t capa);
290
291static inline void
292str_make_independent(VALUE str)
293{
294 long len = RSTRING_LEN(str);
295 int termlen = TERM_LEN(str);
296 str_make_independent_expand((str), len, 0L, termlen);
297}
298
299static inline int str_dependent_p(VALUE str);
300
301void
302rb_str_make_independent(VALUE str)
303{
304 if (str_dependent_p(str)) {
305 str_make_independent(str);
306 }
307}
308
309void
310rb_str_make_embedded(VALUE str)
311{
312 RUBY_ASSERT(rb_str_reembeddable_p(str));
313 RUBY_ASSERT(!STR_EMBED_P(str));
314
315 char *buf = RSTRING(str)->as.heap.ptr;
316 long len = RSTRING(str)->len;
317
318 STR_SET_EMBED(str);
319 STR_SET_LEN(str, len);
320
321 if (len > 0) {
322 memcpy(RSTRING_PTR(str), buf, len);
323 ruby_xfree(buf);
324 }
325
326 TERM_FILL(RSTRING(str)->as.embed.ary + len, TERM_LEN(str));
327}
328
329/* Converts an embedded string into a heap allocated string. The buffer of an
330 * embedded string lives inside a GC heap slot, which the compactor may mprotect
331 * to implement its read barrier. A pointer to such a buffer must not be given
332 * to a system call without the GVL. */
333void
334rb_str_make_heap(VALUE str)
335{
336 if (STR_EMBED_P(str)) {
337 const int termlen = TERM_LEN(str);
338 long len = RSTRING_LEN(str);
339 str_make_independent_expand(str, len, (long)rb_str_capacity(str) - len, termlen);
340 }
341}
342
343void
344rb_debug_rstring_null_ptr(const char *func)
345{
346 fprintf(stderr, "%s is returning NULL!! "
347 "SIGSEGV is highly expected to follow immediately.\n"
348 "If you could reproduce, attach your debugger here, "
349 "and look at the passed string.\n",
350 func);
351}
352
353/* symbols for [up|down|swap]case/capitalize options */
354static VALUE sym_ascii, sym_turkic, sym_lithuanian, sym_fold;
355
356static rb_encoding *
357get_encoding(VALUE str)
358{
359 return rb_enc_from_index(ENCODING_GET(str));
360}
361
362static void
363mustnot_broken(VALUE str)
364{
365 if (is_broken_string(str)) {
366 rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(STR_ENC_GET(str)));
367 }
368}
369
370static void
371mustnot_wchar(VALUE str)
372{
373 rb_encoding *enc = STR_ENC_GET(str);
374 if (rb_enc_mbminlen(enc) > 1) {
375 rb_raise(rb_eArgError, "wide char encoding: %s", rb_enc_name(enc));
376 }
377}
378
379static VALUE register_fstring(VALUE str, bool copy, bool force_precompute_hash);
380
381#if SIZEOF_LONG == SIZEOF_VOIDP
382#define PRECOMPUTED_FAKESTR_HASH 1
383#else
384#endif
385
386static inline bool
387BARE_STRING_P(VALUE str)
388{
389 return RBASIC_CLASS(str) == rb_cString && !rb_shape_obj_has_ivars(str);
390}
391
392static inline st_index_t
393str_do_hash(VALUE str)
394{
395 st_index_t h = rb_memhash((const void *)RSTRING_PTR(str), RSTRING_LEN(str));
396 int e = RSTRING_LEN(str) ? ENCODING_GET(str) : 0;
397 if (e && !is_ascii_string(str)) {
398 h = rb_hash_end(rb_hash_uint32(h, (uint32_t)e));
399 }
400 return h;
401}
402
403static VALUE
404str_store_precomputed_hash(VALUE str, st_index_t hash)
405{
406 RUBY_ASSERT(!FL_TEST_RAW(str, STR_PRECOMPUTED_HASH));
407 RUBY_ASSERT(STR_EMBED_P(str));
408
409#if RUBY_DEBUG
410 size_t used_bytes = (RSTRING_LEN(str) + TERM_LEN(str));
411 size_t free_bytes = str_embed_capa(str) - used_bytes;
412 RUBY_ASSERT(free_bytes >= sizeof(st_index_t));
413#endif
414
415 memcpy(RSTRING_END(str) + TERM_LEN(str), &hash, sizeof(hash));
416
417 FL_SET(str, STR_PRECOMPUTED_HASH);
418
419 return str;
420}
421
422VALUE
423rb_fstring(VALUE str)
424{
425 VALUE fstr;
426 int bare;
427
428 Check_Type(str, T_STRING);
429
430 if (FL_TEST(str, RSTRING_FSTR))
431 return str;
432
433 bare = BARE_STRING_P(str);
434 if (!bare) {
435 if (STR_EMBED_P(str)) {
436 OBJ_FREEZE(str);
437 return str;
438 }
439
440 if (FL_TEST_RAW(str, STR_SHARED_ROOT | STR_SHARED) == STR_SHARED_ROOT) {
442 return str;
443 }
444 }
445
446 if (!FL_TEST_RAW(str, FL_FREEZE | STR_NOFREE | STR_CHILLED))
447 rb_str_resize(str, RSTRING_LEN(str));
448
449 fstr = register_fstring(str, false, false);
450
451 if (!bare) {
452 str_replace_shared_without_enc(str, fstr);
453 OBJ_FREEZE(str);
454 return str;
455 }
456 return fstr;
457}
458
459static VALUE fstring_table_obj;
460
461static VALUE
462fstring_concurrent_set_hash(VALUE str)
463{
464#ifdef PRECOMPUTED_FAKESTR_HASH
465 st_index_t h;
466 if (FL_TEST_RAW(str, STR_FAKESTR)) {
467 // register_fstring precomputes the hash and stores it in capa for fake strings
468 h = (st_index_t)RSTRING(str)->as.heap.aux.capa;
469 }
470 else {
471 h = rb_str_hash(str);
472 }
473 // rb_str_hash doesn't include the encoding for ascii only strings, so
474 // we add it to avoid common collisions between `:sym.name` (ASCII) and `"sym"` (UTF-8)
475 return (VALUE)rb_hash_end(rb_hash_uint32(h, (uint32_t)ENCODING_GET_INLINED(str)));
476#else
477 return (VALUE)rb_str_hash(str);
478#endif
479}
480
481static bool
482fstring_concurrent_set_cmp(VALUE a, VALUE b)
483{
484 long alen, blen;
485 const char *aptr, *bptr;
486
487 RUBY_ASSERT(RB_TYPE_P(a, T_STRING));
488 RUBY_ASSERT(RB_TYPE_P(b, T_STRING));
489
490 RSTRING_GETMEM(a, aptr, alen);
491 RSTRING_GETMEM(b, bptr, blen);
492 return (alen == blen &&
493 ENCODING_GET(a) == ENCODING_GET(b) &&
494 memcmp(aptr, bptr, alen) == 0);
495}
496
498 bool copy;
499 bool force_precompute_hash;
500};
501
502static VALUE
503fstring_concurrent_set_create(VALUE str, void *data)
504{
505 struct fstr_create_arg *arg = data;
506
507 // Unless the string is empty or binary, its coderange has been precomputed.
508 int coderange = ENC_CODERANGE(str);
509
510 if (FL_TEST_RAW(str, STR_FAKESTR)) {
511 if (arg->copy) {
512 VALUE new_str;
513 long len = RSTRING_LEN(str);
514 long capa = len + sizeof(st_index_t);
515 int term_len = TERM_LEN(str);
516
517 if (arg->force_precompute_hash && STR_EMBEDDABLE_P(capa, term_len)) {
518 new_str = str_alloc_embed(rb_cString, capa + term_len);
519 memcpy(RSTRING_PTR(new_str), RSTRING_PTR(str), len);
520 STR_SET_LEN(new_str, RSTRING_LEN(str));
521 TERM_FILL(RSTRING_END(new_str), TERM_LEN(str));
522 rb_enc_copy(new_str, str);
523 str_store_precomputed_hash(new_str, str_do_hash(str));
524 }
525 else {
526 new_str = str_new(rb_cString, RSTRING(str)->as.heap.ptr, RSTRING(str)->len);
527 rb_enc_copy(new_str, str);
528#ifdef PRECOMPUTED_FAKESTR_HASH
529 if (rb_str_capacity(new_str) >= RSTRING_LEN(str) + term_len + sizeof(st_index_t)) {
530 str_store_precomputed_hash(new_str, (st_index_t)RSTRING(str)->as.heap.aux.capa);
531 }
532#endif
533 }
534 str = new_str;
535 }
536 else {
537 str = str_new_static(rb_cString, RSTRING(str)->as.heap.ptr,
538 RSTRING(str)->len,
539 ENCODING_GET(str));
540 }
541 OBJ_FREEZE(str);
542 }
543 else {
544 if (!OBJ_FROZEN(str) || CHILLED_STRING_P(str)) {
545 str = str_new_frozen(rb_cString, str);
546 }
547 if (STR_SHARED_P(str)) { /* str should not be shared */
548 /* shared substring */
549 str_make_independent(str);
551 }
552 if (!BARE_STRING_P(str)) {
553 str = str_new_frozen(rb_cString, str);
554 }
555 }
556
557 ENC_CODERANGE_SET(str, coderange);
558 RBASIC(str)->flags |= RSTRING_FSTR;
559 if (!RB_OBJ_SHAREABLE_P(str)) {
560 RB_OBJ_SET_SHAREABLE(str);
561 }
562 RUBY_ASSERT((rb_gc_verify_shareable(str), 1));
563 RUBY_ASSERT(RB_TYPE_P(str, T_STRING));
565 RUBY_ASSERT(!FL_TEST_RAW(str, STR_FAKESTR));
566 RUBY_ASSERT(!rb_shape_obj_has_ivars(str));
567 RUBY_ASSERT(RBASIC_CLASS(str) == rb_cString);
568 RUBY_ASSERT(!rb_objspace_garbage_object_p(str));
569
570 return str;
571}
572
573static const struct rb_concurrent_set_funcs fstring_concurrent_set_funcs = {
574 .hash = fstring_concurrent_set_hash,
575 .cmp = fstring_concurrent_set_cmp,
576 .create = fstring_concurrent_set_create,
577 .free = NULL,
578};
579
580void
581Init_fstring_table(void)
582{
583 fstring_table_obj = rb_concurrent_set_new(&fstring_concurrent_set_funcs, 8192);
584 rb_gc_register_address(&fstring_table_obj);
585}
586
587static VALUE
588register_fstring(VALUE str, bool copy, bool force_precompute_hash)
589{
590 struct fstr_create_arg args = {
591 .copy = copy,
592 .force_precompute_hash = force_precompute_hash
593 };
594
595#if SIZEOF_VOIDP == SIZEOF_LONG
596 if (FL_TEST_RAW(str, STR_FAKESTR)) {
597 // if the string hasn't been interned, we'll need the hash twice, so we
598 // compute it once and store it in capa
599 RSTRING(str)->as.heap.aux.capa = (long)str_do_hash(str);
600 }
601#endif
602
603 VALUE result = rb_concurrent_set_find_or_insert(&fstring_table_obj, str, &args);
604
605 RUBY_ASSERT(!rb_objspace_garbage_object_p(result));
606 RUBY_ASSERT(RB_TYPE_P(result, T_STRING));
607 RUBY_ASSERT(OBJ_FROZEN(result));
609 RUBY_ASSERT((rb_gc_verify_shareable(result), 1));
610 RUBY_ASSERT(!FL_TEST_RAW(result, STR_FAKESTR));
611 RUBY_ASSERT(RBASIC_CLASS(result) == rb_cString);
612
613 return result;
614}
615
616bool
617rb_obj_is_fstring_table(VALUE obj)
618{
619 ASSERT_vm_locking();
620
621 return obj == fstring_table_obj;
622}
623
624void
625rb_gc_free_fstring(VALUE obj)
626{
627 ASSERT_vm_locking_with_barrier();
628
629 RUBY_ASSERT(FL_TEST(obj, RSTRING_FSTR));
631 RUBY_ASSERT(!FL_TEST(obj, STR_SHARED));
632
633 rb_concurrent_set_delete_by_identity(fstring_table_obj, obj);
634
635 RB_DEBUG_COUNTER_INC(obj_str_fstr);
636
637 FL_UNSET(obj, RSTRING_FSTR);
638}
639
640void
641rb_fstring_foreach_with_replace(int (*callback)(VALUE *str, void *data), void *data)
642{
643 if (fstring_table_obj) {
644 rb_concurrent_set_foreach_with_replace(fstring_table_obj, callback, data);
645 }
646}
647
648static VALUE
649setup_fake_str(struct RString *fake_str, const char *name, long len, int encidx)
650{
651 fake_str->basic.flags = T_STRING|RSTRING_NOEMBED|STR_NOFREE|STR_FAKESTR;
652 RBASIC_SET_SHAPE_ID((VALUE)fake_str, ROOT_SHAPE_ID);
653
654 if (!name) {
656 name = "";
657 }
658
659 ENCODING_SET_INLINED((VALUE)fake_str, encidx);
660
661 RBASIC_SET_CLASS_RAW((VALUE)fake_str, rb_cString);
662 fake_str->len = len;
663 fake_str->as.heap.ptr = (char *)name;
664 fake_str->as.heap.aux.capa = len;
665 return (VALUE)fake_str;
666}
667
668/*
669 * set up a fake string which refers a static string literal.
670 */
671VALUE
672rb_setup_fake_str(struct RString *fake_str, const char *name, long len, rb_encoding *enc)
673{
674 return setup_fake_str(fake_str, name, len, rb_enc_to_index(enc));
675}
676
677/*
678 * rb_fstring_new and rb_fstring_cstr family create or lookup a frozen
679 * shared string which refers a static string literal. `ptr` must
680 * point a constant string.
681 */
682VALUE
683rb_fstring_new(const char *ptr, long len)
684{
685 struct RString fake_str = {RBASIC_INIT};
686 return register_fstring(setup_fake_str(&fake_str, ptr, len, ENCINDEX_US_ASCII), false, false);
687}
688
689VALUE
690rb_fstring_enc_new(const char *ptr, long len, rb_encoding *enc)
691{
692 struct RString fake_str = {RBASIC_INIT};
693 return register_fstring(rb_setup_fake_str(&fake_str, ptr, len, enc), false, false);
694}
695
696VALUE
697rb_fstring_cstr(const char *ptr)
698{
699 return rb_fstring_new(ptr, strlen(ptr));
700}
701
702static inline bool
703single_byte_optimizable(VALUE str)
704{
705 int encindex = ENCODING_GET(str);
706 switch (encindex) {
707 case ENCINDEX_ASCII_8BIT:
708 case ENCINDEX_US_ASCII:
709 return true;
710 case ENCINDEX_UTF_8:
711 // For UTF-8 it's worth scanning the string coderange when unknown.
713 }
714 /* Conservative. It may be ENC_CODERANGE_UNKNOWN. */
715 if (ENC_CODERANGE(str) == ENC_CODERANGE_7BIT) {
716 return true;
717 }
718
719 if (rb_enc_mbmaxlen(rb_enc_from_index(encindex)) == 1) {
720 return true;
721 }
722
723 /* Conservative. Possibly single byte.
724 * "\xa1" in Shift_JIS for example. */
725 return false;
726}
727
729
730static inline const char *
731search_nonascii(const char *p, const char *e)
732{
733 const char *s, *t;
734
735#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
736# if SIZEOF_UINTPTR_T == 8
737# define NONASCII_MASK UINT64_C(0x8080808080808080)
738# elif SIZEOF_UINTPTR_T == 4
739# define NONASCII_MASK UINT32_C(0x80808080)
740# else
741# error "don't know what to do."
742# endif
743#else
744# if SIZEOF_UINTPTR_T == 8
745# define NONASCII_MASK ((uintptr_t)0x80808080UL << 32 | (uintptr_t)0x80808080UL)
746# elif SIZEOF_UINTPTR_T == 4
747# define NONASCII_MASK 0x80808080UL /* or...? */
748# else
749# error "don't know what to do."
750# endif
751#endif
752
753 if (UNALIGNED_WORD_ACCESS || e - p >= SIZEOF_VOIDP) {
754#if !UNALIGNED_WORD_ACCESS
755 if ((uintptr_t)p % SIZEOF_VOIDP) {
756 int l = SIZEOF_VOIDP - (uintptr_t)p % SIZEOF_VOIDP;
757 p += l;
758 switch (l) {
759 default: UNREACHABLE;
760#if SIZEOF_VOIDP > 4
761 case 7: if (p[-7]&0x80) return p-7;
762 case 6: if (p[-6]&0x80) return p-6;
763 case 5: if (p[-5]&0x80) return p-5;
764 case 4: if (p[-4]&0x80) return p-4;
765#endif
766 case 3: if (p[-3]&0x80) return p-3;
767 case 2: if (p[-2]&0x80) return p-2;
768 case 1: if (p[-1]&0x80) return p-1;
769 case 0: break;
770 }
771 }
772#endif
773#if defined(HAVE_BUILTIN___BUILTIN_ASSUME_ALIGNED) &&! UNALIGNED_WORD_ACCESS
774#define aligned_ptr(value) \
775 __builtin_assume_aligned((value), sizeof(uintptr_t))
776#else
777#define aligned_ptr(value) (value)
778#endif
779 s = aligned_ptr(p);
780 t = (e - (SIZEOF_VOIDP-1));
781#undef aligned_ptr
782 for (;s < t; s += sizeof(uintptr_t)) {
783 uintptr_t word;
784 memcpy(&word, s, sizeof(word));
785 if (word & NONASCII_MASK) {
786#ifdef WORDS_BIGENDIAN
787 return (const char *)s + (nlz_intptr(word&NONASCII_MASK)>>3);
788#else
789 return (const char *)s + (ntz_intptr(word&NONASCII_MASK)>>3);
790#endif
791 }
792 }
793 p = (const char *)s;
794 }
795
796 switch (e - p) {
797 default: UNREACHABLE;
798#if SIZEOF_VOIDP > 4
799 case 7: if (e[-7]&0x80) return e-7;
800 case 6: if (e[-6]&0x80) return e-6;
801 case 5: if (e[-5]&0x80) return e-5;
802 case 4: if (e[-4]&0x80) return e-4;
803#endif
804 case 3: if (e[-3]&0x80) return e-3;
805 case 2: if (e[-2]&0x80) return e-2;
806 case 1: if (e[-1]&0x80) return e-1;
807 case 0: return NULL;
808 }
809}
810
811static int
812coderange_scan(const char *p, long len, rb_encoding *enc)
813{
814 const char *e = p + len;
815
816 if (rb_enc_to_index(enc) == rb_ascii8bit_encindex()) {
817 /* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
818 p = search_nonascii(p, e);
820 }
821
822 if (rb_enc_asciicompat(enc)) {
823 p = search_nonascii(p, e);
824 if (!p) return ENC_CODERANGE_7BIT;
825 for (;;) {
826 int ret = rb_enc_precise_mbclen(p, e, enc);
828 p += MBCLEN_CHARFOUND_LEN(ret);
829 if (p == e) break;
830 p = search_nonascii(p, e);
831 if (!p) break;
832 }
833 }
834 else {
835 while (p < e) {
836 int ret = rb_enc_precise_mbclen(p, e, enc);
838 p += MBCLEN_CHARFOUND_LEN(ret);
839 }
840 }
841 return ENC_CODERANGE_VALID;
842}
843
844long
845rb_str_coderange_scan_restartable(const char *s, const char *e, rb_encoding *enc, int *cr)
846{
847 const char *p = s;
848
849 if (*cr == ENC_CODERANGE_BROKEN)
850 return e - s;
851
852 if (rb_enc_to_index(enc) == rb_ascii8bit_encindex()) {
853 /* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
854 if (*cr == ENC_CODERANGE_VALID) return e - s;
855 p = search_nonascii(p, e);
857 return e - s;
858 }
859 else if (rb_enc_asciicompat(enc)) {
860 p = search_nonascii(p, e);
861 if (!p) {
862 if (*cr != ENC_CODERANGE_VALID) *cr = ENC_CODERANGE_7BIT;
863 return e - s;
864 }
865 for (;;) {
866 int ret = rb_enc_precise_mbclen(p, e, enc);
867 if (!MBCLEN_CHARFOUND_P(ret)) {
869 return p - s;
870 }
871 p += MBCLEN_CHARFOUND_LEN(ret);
872 if (p == e) break;
873 p = search_nonascii(p, e);
874 if (!p) break;
875 }
876 }
877 else {
878 while (p < e) {
879 int ret = rb_enc_precise_mbclen(p, e, enc);
880 if (!MBCLEN_CHARFOUND_P(ret)) {
882 return p - s;
883 }
884 p += MBCLEN_CHARFOUND_LEN(ret);
885 }
886 }
888 return e - s;
889}
890
891static inline void
892str_enc_copy(VALUE str1, VALUE str2)
893{
894 rb_enc_set_index(str1, ENCODING_GET(str2));
895}
896
897/* Like str_enc_copy, but does not check frozen status of str1.
898 * You should use this only if you're certain that str1 is not frozen. */
899static inline void
900str_enc_copy_direct(VALUE str1, VALUE str2)
901{
902 RUBY_ASSERT(TERM_LEN(str2) <= TERM_LEN(str1) || str_dependent_p(str1) ||
903 (STR_EMBED_P(str1) &&
904 str_embed_capa(str1) >= RSTRING_LEN(str1) + TERM_LEN(str2)));
905 int inlined_encoding = RB_ENCODING_GET_INLINED(str2);
906 if (inlined_encoding == ENCODING_INLINE_MAX) {
907 rb_enc_set_index(str1, rb_enc_get_index(str2));
908 }
909 else {
910 ENCODING_SET_INLINED(str1, inlined_encoding);
911 }
912}
913
914static void
915rb_enc_cr_str_copy_for_substr(VALUE dest, VALUE src)
916{
917 /* this function is designed for copying encoding and coderange
918 * from src to new string "dest" which is made from the part of src.
919 */
920 str_enc_copy(dest, src);
921 if (RSTRING_LEN(dest) == 0) {
922 if (!rb_enc_asciicompat(STR_ENC_GET(src)))
924 else
926 return;
927 }
928 switch (ENC_CODERANGE(src)) {
931 break;
933 if (!rb_enc_asciicompat(STR_ENC_GET(src)) ||
934 search_nonascii(RSTRING_PTR(dest), RSTRING_END(dest)))
936 else
938 break;
939 default:
940 break;
941 }
942}
943
944static void
945rb_enc_cr_str_exact_copy(VALUE dest, VALUE src)
946{
947 str_enc_copy(dest, src);
949}
950
951static int
952enc_coderange_scan(VALUE str, rb_encoding *enc)
953{
954 return coderange_scan(RSTRING_PTR(str), RSTRING_LEN(str), enc);
955}
956
957int
958rb_enc_str_coderange_scan(VALUE str, rb_encoding *enc)
959{
960 return enc_coderange_scan(str, enc);
961}
962
963int
965{
966 int cr = ENC_CODERANGE(str);
967
968 if (cr == ENC_CODERANGE_UNKNOWN) {
969 cr = enc_coderange_scan(str, get_encoding(str));
970 ENC_CODERANGE_SET(str, cr);
971 }
972 return cr;
973}
974
975static inline bool
976rb_enc_str_asciicompat(VALUE str)
977{
978 int encindex = ENCODING_GET_INLINED(str);
979 return str_encindex_fastpath(encindex) || rb_enc_asciicompat(rb_enc_get_from_index(encindex));
980}
981
982int
984{
985 switch(ENC_CODERANGE(str)) {
987 return rb_enc_str_asciicompat(str) && is_ascii_string(str);
989 return true;
990 default:
991 return false;
992 }
993}
994
995static inline void
996str_mod_check(VALUE s, const char *p, long len)
997{
998 if (RSTRING_PTR(s) != p || RSTRING_LEN(s) != len){
999 rb_raise(rb_eRuntimeError, "string modified");
1000 }
1001}
1002
1003static size_t
1004str_capacity(VALUE str, const int termlen)
1005{
1006 if (STR_EMBED_P(str)) {
1007 return str_embed_capa(str) - termlen;
1008 }
1009 else if (FL_ANY_RAW(str, STR_SHARED|STR_NOFREE)) {
1010 return RSTRING(str)->len;
1011 }
1012 else {
1013 return RSTRING(str)->as.heap.aux.capa;
1014 }
1015}
1016
1017size_t
1019{
1020 return str_capacity(str, TERM_LEN(str));
1021}
1022
1023static inline void
1024must_not_null(const char *ptr)
1025{
1026 if (!ptr) {
1027 rb_raise(rb_eArgError, "NULL pointer given");
1028 }
1029}
1030
1031static inline VALUE
1032str_alloc_embed(VALUE klass, size_t capa)
1033{
1034 size_t size = rb_str_embed_size(capa, 0);
1035 RUBY_ASSERT(size > 0);
1036 RUBY_ASSERT(rb_gc_size_allocatable_p(size));
1037
1038 NEWOBJ_OF(str, struct RString, klass,
1040
1041 str->len = 0;
1042 str->as.embed.ary[0] = 0;
1043
1044 return (VALUE)str;
1045}
1046
1047static inline VALUE
1048str_alloc_heap(VALUE klass)
1049{
1050 NEWOBJ_OF(str, struct RString, klass,
1051 T_STRING | STR_NOEMBED | (RGENGC_WB_PROTECTED_STRING ? FL_WB_PROTECTED : 0), sizeof(struct RString), 0);
1052
1053 str->len = 0;
1054 str->as.heap.aux.capa = 0;
1055 str->as.heap.ptr = NULL;
1056
1057 return (VALUE)str;
1058}
1059
1060static inline VALUE
1061empty_str_alloc(VALUE klass)
1062{
1063 RUBY_DTRACE_CREATE_HOOK(STRING, 0);
1064 VALUE str = str_alloc_embed(klass, 0);
1065 memset(RSTRING(str)->as.embed.ary, 0, str_embed_capa(str));
1067 return str;
1068}
1069
1070static VALUE
1071str_enc_new(VALUE klass, const char *ptr, long len, rb_encoding *enc)
1072{
1073 VALUE str;
1074
1075 if (len < 0) {
1076 rb_raise(rb_eArgError, "negative string size (or size too big)");
1077 }
1078
1079 if (enc == NULL) {
1080 enc = rb_ascii8bit_encoding();
1081 }
1082
1083 RUBY_DTRACE_CREATE_HOOK(STRING, len);
1084
1085 int termlen = rb_enc_mbminlen(enc);
1086
1087 if (STR_EMBEDDABLE_P(len, termlen)) {
1088 str = str_alloc_embed(klass, len + termlen);
1089 if (len == 0) {
1090 ENC_CODERANGE_SET(str, rb_enc_asciicompat(enc) ? ENC_CODERANGE_7BIT : ENC_CODERANGE_VALID);
1091 }
1092 }
1093 else {
1094 str = str_alloc_heap(klass);
1095 RSTRING(str)->as.heap.aux.capa = len;
1096 /* :FIXME: @shyouhei guesses `len + termlen` is guaranteed to never
1097 * integer overflow. If we can STATIC_ASSERT that, the following
1098 * mul_add_mul can be reverted to a simple ALLOC_N. */
1099 RSTRING(str)->as.heap.ptr =
1100 rb_xmalloc_mul_add_mul(sizeof(char), len, sizeof(char), termlen);
1101 }
1102
1103 rb_enc_raw_set(str, enc);
1104
1105 if (ptr) {
1106 memcpy(RSTRING_PTR(str), ptr, len);
1107 }
1108 else {
1109 memset(RSTRING_PTR(str), 0, len);
1110 }
1111
1112 STR_SET_LEN(str, len);
1113 TERM_FILL(RSTRING_PTR(str) + len, termlen);
1114 return str;
1115}
1116
1117static VALUE
1118str_new(VALUE klass, const char *ptr, long len)
1119{
1120 return str_enc_new(klass, ptr, len, rb_ascii8bit_encoding());
1121}
1122
1123VALUE
1124rb_str_new(const char *ptr, long len)
1125{
1126 return str_new(rb_cString, ptr, len);
1127}
1128
1129VALUE
1130rb_usascii_str_new(const char *ptr, long len)
1131{
1132 return str_enc_new(rb_cString, ptr, len, rb_usascii_encoding());
1133}
1134
1135VALUE
1136rb_utf8_str_new(const char *ptr, long len)
1137{
1138 return str_enc_new(rb_cString, ptr, len, rb_utf8_encoding());
1139}
1140
1141VALUE
1142rb_enc_str_new(const char *ptr, long len, rb_encoding *enc)
1143{
1144 return str_enc_new(rb_cString, ptr, len, enc);
1145}
1146
1147VALUE
1149{
1150 must_not_null(ptr);
1151 /* rb_str_new_cstr() can take pointer from non-malloc-generated
1152 * memory regions, and that cannot be detected by the MSAN. Just
1153 * trust the programmer that the argument passed here is a sane C
1154 * string. */
1155 __msan_unpoison_string(ptr);
1156 return rb_str_new(ptr, strlen(ptr));
1157}
1158
1159VALUE
1161{
1162 return rb_enc_str_new_cstr(ptr, rb_usascii_encoding());
1163}
1164
1165VALUE
1167{
1168 return rb_enc_str_new_cstr(ptr, rb_utf8_encoding());
1169}
1170
1171VALUE
1172rb_enc_str_new_cstr(const char *ptr, rb_encoding *enc)
1173{
1174 must_not_null(ptr);
1175 if (rb_enc_mbminlen(enc) != 1) {
1176 rb_raise(rb_eArgError, "wchar encoding given");
1177 }
1178 return rb_enc_str_new(ptr, strlen(ptr), enc);
1179}
1180
1181static VALUE
1182str_new_static(VALUE klass, const char *ptr, long len, int encindex)
1183{
1184 VALUE str;
1185
1186 if (len < 0) {
1187 rb_raise(rb_eArgError, "negative string size (or size too big)");
1188 }
1189
1190 if (!ptr) {
1191 str = str_enc_new(klass, ptr, len, rb_enc_from_index(encindex));
1192 }
1193 else {
1194 RUBY_DTRACE_CREATE_HOOK(STRING, len);
1195 str = str_alloc_heap(klass);
1196 RSTRING(str)->len = len;
1197 RSTRING(str)->as.heap.ptr = (char *)ptr;
1198 RSTRING(str)->as.heap.aux.capa = len;
1199 RBASIC(str)->flags |= STR_NOFREE;
1200 rb_enc_associate_index(str, encindex);
1201 }
1202 return str;
1203}
1204
1205VALUE
1206rb_str_new_static(const char *ptr, long len)
1207{
1208 return str_new_static(rb_cString, ptr, len, 0);
1209}
1210
1211VALUE
1213{
1214 return str_new_static(rb_cString, ptr, len, ENCINDEX_US_ASCII);
1215}
1216
1217VALUE
1219{
1220 return str_new_static(rb_cString, ptr, len, ENCINDEX_UTF_8);
1221}
1222
1223VALUE
1224rb_enc_str_new_static(const char *ptr, long len, rb_encoding *enc)
1225{
1226 return str_new_static(rb_cString, ptr, len, rb_enc_to_index(enc));
1227}
1228
1229static VALUE str_cat_conv_enc_opts(VALUE newstr, long ofs, const char *ptr, long len,
1230 rb_encoding *from, rb_encoding *to,
1231 int ecflags, VALUE ecopts);
1232
1233static inline bool
1234is_enc_ascii_string(VALUE str, rb_encoding *enc)
1235{
1236 int encidx = rb_enc_to_index(enc);
1237 if (rb_enc_get_index(str) == encidx)
1238 return is_ascii_string(str);
1239 return enc_coderange_scan(str, enc) == ENC_CODERANGE_7BIT;
1240}
1241
1242VALUE
1243rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags, VALUE ecopts)
1244{
1245 long len;
1246 const char *ptr;
1247 VALUE newstr;
1248
1249 if (!to) return str;
1250 if (!from) from = rb_enc_get(str);
1251 if (from == to) return str;
1252 if ((rb_enc_asciicompat(to) && is_enc_ascii_string(str, from)) ||
1253 rb_is_ascii8bit_enc(to)) {
1254 if (STR_ENC_GET(str) != to) {
1255 str = rb_str_dup(str);
1256 rb_enc_associate(str, to);
1257 }
1258 return str;
1259 }
1260
1261 RSTRING_GETMEM(str, ptr, len);
1262 newstr = str_cat_conv_enc_opts(rb_str_buf_new(len), 0, ptr, len,
1263 from, to, ecflags, ecopts);
1264 if (NIL_P(newstr)) {
1265 /* some error, return original */
1266 return str;
1267 }
1268 return newstr;
1269}
1270
1271VALUE
1272rb_str_cat_conv_enc_opts(VALUE newstr, long ofs, const char *ptr, long len,
1273 rb_encoding *from, int ecflags, VALUE ecopts)
1274{
1275 long olen;
1276
1277 olen = RSTRING_LEN(newstr);
1278 if (ofs < -olen || olen < ofs)
1279 rb_raise(rb_eIndexError, "index %ld out of string", ofs);
1280 if (ofs < 0) ofs += olen;
1281 if (!from) {
1282 STR_SET_LEN(newstr, ofs);
1283 return rb_str_cat(newstr, ptr, len);
1284 }
1285
1286 rb_str_modify(newstr);
1287 return str_cat_conv_enc_opts(newstr, ofs, ptr, len, from,
1288 rb_enc_get(newstr),
1289 ecflags, ecopts);
1290}
1291
1292VALUE
1293rb_str_initialize(VALUE str, const char *ptr, long len, rb_encoding *enc)
1294{
1295 STR_SET_LEN(str, 0);
1296 rb_enc_associate(str, enc);
1297 rb_str_cat(str, ptr, len);
1298 return str;
1299}
1300
1301static VALUE
1302str_cat_conv_enc_opts(VALUE newstr, long ofs, const char *ptr, long len,
1303 rb_encoding *from, rb_encoding *to,
1304 int ecflags, VALUE ecopts)
1305{
1306 rb_econv_t *ec;
1308 long olen;
1309 VALUE econv_wrapper;
1310 const unsigned char *start, *sp;
1311 unsigned char *dest, *dp;
1312 size_t converted_output = (size_t)ofs;
1313
1314 olen = rb_str_capacity(newstr);
1315
1316 econv_wrapper = rb_obj_alloc(rb_cEncodingConverter);
1317 RBASIC_CLEAR_CLASS(econv_wrapper);
1318 ec = rb_econv_open_opts(from->name, to->name, ecflags, ecopts);
1319 if (!ec) return Qnil;
1320 DATA_PTR(econv_wrapper) = ec;
1321
1322 sp = (unsigned char*)ptr;
1323 start = sp;
1324 while ((dest = (unsigned char*)RSTRING_PTR(newstr)),
1325 (dp = dest + converted_output),
1326 (ret = rb_econv_convert(ec, &sp, start + len, &dp, dest + olen, 0)),
1328 /* destination buffer short */
1329 size_t converted_input = sp - start;
1330 size_t rest = len - converted_input;
1331 converted_output = dp - dest;
1332 rb_str_set_len(newstr, converted_output);
1333 if (converted_input && converted_output &&
1334 rest < (LONG_MAX / converted_output)) {
1335 rest = (rest * converted_output) / converted_input;
1336 }
1337 else {
1338 rest = olen;
1339 }
1340 olen += rest < 2 ? 2 : rest;
1341 rb_str_resize(newstr, olen);
1342 }
1343 DATA_PTR(econv_wrapper) = 0;
1344 RB_GC_GUARD(econv_wrapper);
1345 rb_econv_close(ec);
1346 switch (ret) {
1347 case econv_finished:
1348 len = dp - (unsigned char*)RSTRING_PTR(newstr);
1349 rb_str_set_len(newstr, len);
1350 rb_enc_associate(newstr, to);
1351 return newstr;
1352
1353 default:
1354 return Qnil;
1355 }
1356}
1357
1358VALUE
1359rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
1360{
1361 return rb_str_conv_enc_opts(str, from, to, 0, Qnil);
1362}
1363
1364VALUE
1365rb_external_str_new_with_enc(const char *ptr, long len, rb_encoding *eenc)
1366{
1367 rb_encoding *ienc;
1368 VALUE str;
1369 const int eidx = rb_enc_to_index(eenc);
1370
1371 if (!ptr) {
1372 return rb_enc_str_new(ptr, len, eenc);
1373 }
1374
1375 /* ASCII-8BIT case, no conversion */
1376 if ((eidx == rb_ascii8bit_encindex()) ||
1377 (eidx == rb_usascii_encindex() && search_nonascii(ptr, ptr + len))) {
1378 return rb_str_new(ptr, len);
1379 }
1380 /* no default_internal or same encoding, no conversion */
1381 ienc = rb_default_internal_encoding();
1382 if (!ienc || eenc == ienc) {
1383 return rb_enc_str_new(ptr, len, eenc);
1384 }
1385 /* ASCII compatible, and ASCII only string, no conversion in
1386 * default_internal */
1387 if ((eidx == rb_ascii8bit_encindex()) ||
1388 (eidx == rb_usascii_encindex()) ||
1389 (rb_enc_asciicompat(eenc) && !search_nonascii(ptr, ptr + len))) {
1390 return rb_enc_str_new(ptr, len, ienc);
1391 }
1392 /* convert from the given encoding to default_internal */
1393 str = rb_enc_str_new(NULL, 0, ienc);
1394 /* when the conversion failed for some reason, just ignore the
1395 * default_internal and result in the given encoding as-is. */
1396 if (NIL_P(rb_str_cat_conv_enc_opts(str, 0, ptr, len, eenc, 0, Qnil))) {
1397 rb_str_initialize(str, ptr, len, eenc);
1398 }
1399 return str;
1400}
1401
1402VALUE
1403rb_external_str_with_enc(VALUE str, rb_encoding *eenc)
1404{
1405 int eidx = rb_enc_to_index(eenc);
1406 if (eidx == rb_usascii_encindex() &&
1407 !is_ascii_string(str)) {
1408 rb_enc_associate_index(str, rb_ascii8bit_encindex());
1409 return str;
1410 }
1411 rb_enc_associate_index(str, eidx);
1412 return rb_str_conv_enc(str, eenc, rb_default_internal_encoding());
1413}
1414
1415VALUE
1416rb_external_str_new(const char *ptr, long len)
1417{
1418 return rb_external_str_new_with_enc(ptr, len, rb_default_external_encoding());
1419}
1420
1421VALUE
1423{
1424 return rb_external_str_new_with_enc(ptr, strlen(ptr), rb_default_external_encoding());
1425}
1426
1427VALUE
1428rb_locale_str_new(const char *ptr, long len)
1429{
1430 return rb_external_str_new_with_enc(ptr, len, rb_locale_encoding());
1431}
1432
1433VALUE
1435{
1436 return rb_external_str_new_with_enc(ptr, strlen(ptr), rb_locale_encoding());
1437}
1438
1439VALUE
1441{
1442 return rb_external_str_new_with_enc(ptr, len, rb_filesystem_encoding());
1443}
1444
1445VALUE
1447{
1448 return rb_external_str_new_with_enc(ptr, strlen(ptr), rb_filesystem_encoding());
1449}
1450
1451VALUE
1453{
1454 return rb_str_export_to_enc(str, rb_default_external_encoding());
1455}
1456
1457VALUE
1459{
1460 return rb_str_export_to_enc(str, rb_locale_encoding());
1461}
1462
1463VALUE
1464rb_str_export_to_enc(VALUE str, rb_encoding *enc)
1465{
1466 return rb_str_conv_enc(str, STR_ENC_GET(str), enc);
1467}
1468
1469static VALUE
1470str_replace_shared_without_enc(VALUE str2, VALUE str)
1471{
1472 const int termlen = TERM_LEN(str);
1473 char *ptr;
1474 long len;
1475
1476 RSTRING_GETMEM(str, ptr, len);
1477 if (str_embed_capa(str2) >= len + termlen) {
1478 char *ptr2 = RSTRING(str2)->as.embed.ary;
1479 STR_SET_EMBED(str2);
1480 memcpy(ptr2, RSTRING_PTR(str), len);
1481 TERM_FILL(ptr2+len, termlen);
1482 }
1483 else {
1484 VALUE root;
1485 if (STR_SHARED_P(str)) {
1486 root = RSTRING(str)->as.heap.aux.shared;
1487 RSTRING_GETMEM(str, ptr, len);
1488 }
1489 else {
1490 root = rb_str_new_frozen(str);
1491 RSTRING_GETMEM(root, ptr, len);
1492 }
1493 RUBY_ASSERT(OBJ_FROZEN(root));
1494
1495 if (!STR_EMBED_P(str2) && !FL_TEST_RAW(str2, STR_SHARED|STR_NOFREE)) {
1496 if (FL_TEST_RAW(str2, STR_SHARED_ROOT)) {
1497 rb_fatal("about to free a possible shared root");
1498 }
1499 char *ptr2 = STR_HEAP_PTR(str2);
1500 if (ptr2 != ptr) {
1501 ruby_sized_xfree(ptr2, STR_HEAP_SIZE(str2));
1502 }
1503 }
1504 FL_SET(str2, STR_NOEMBED);
1505 RSTRING(str2)->as.heap.ptr = ptr;
1506 STR_SET_SHARED(str2, root);
1507 }
1508
1509 STR_SET_LEN(str2, len);
1510
1511 return str2;
1512}
1513
1514static VALUE
1515str_replace_shared(VALUE str2, VALUE str)
1516{
1517 str_replace_shared_without_enc(str2, str);
1518 rb_enc_cr_str_exact_copy(str2, str);
1519 return str2;
1520}
1521
1522static VALUE
1523str_new_shared(VALUE klass, VALUE str)
1524{
1525 return str_replace_shared(str_alloc_heap(klass), str);
1526}
1527
1528VALUE
1530{
1531 return str_new_shared(rb_obj_class(str), str);
1532}
1533
1534VALUE
1536{
1537 if (RB_FL_TEST_RAW(orig, FL_FREEZE | STR_CHILLED) == FL_FREEZE) return orig;
1538 return str_new_frozen(rb_obj_class(orig), orig);
1539}
1540
1541static VALUE
1542rb_str_new_frozen_String(VALUE orig)
1543{
1544 if (OBJ_FROZEN(orig) && rb_obj_class(orig) == rb_cString) return orig;
1545 return str_new_frozen(rb_cString, orig);
1546}
1547
1548
1549VALUE
1550rb_str_frozen_bare_string(VALUE orig)
1551{
1552 if (RB_LIKELY(BARE_STRING_P(orig) && OBJ_FROZEN_RAW(orig))) return orig;
1553 return str_new_frozen(rb_cString, orig);
1554}
1555
1556VALUE
1557rb_str_tmp_frozen_acquire(VALUE orig)
1558{
1559 if (OBJ_FROZEN_RAW(orig)) return orig;
1560 return str_new_frozen_buffer(0, orig, FALSE);
1561}
1562
1563VALUE
1564rb_str_tmp_frozen_no_embed_acquire(VALUE orig)
1565{
1566 if (OBJ_FROZEN_RAW(orig) && !STR_EMBED_P(orig)) return orig;
1567 if (STR_SHARED_P(orig) && !STR_EMBED_P(RSTRING(orig)->as.heap.aux.shared)) return rb_str_tmp_frozen_acquire(orig);
1568
1569 VALUE str = str_alloc_heap(0);
1570 OBJ_FREEZE(str);
1571 /* Always set the STR_SHARED_ROOT to ensure it does not get re-embedded. */
1572 FL_SET(str, STR_SHARED_ROOT);
1573
1574 size_t capa = str_capacity(orig, TERM_LEN(orig));
1575
1576 /* If the string is embedded then we want to create a copy that is heap
1577 * allocated. If the string is shared then the shared root must be
1578 * embedded, so we want to create a copy. If the string is a shared root
1579 * then it must be embedded, so we want to create a copy. */
1580 if (STR_EMBED_P(orig) || FL_TEST_RAW(orig, STR_SHARED | STR_SHARED_ROOT | RSTRING_FSTR)) {
1581 RSTRING(str)->as.heap.ptr = rb_xmalloc_mul_add_mul(sizeof(char), capa, sizeof(char), TERM_LEN(orig));
1582 memcpy(RSTRING(str)->as.heap.ptr, RSTRING_PTR(orig), capa);
1583 }
1584 else {
1585 /* orig must be heap allocated and not shared, so we can safely transfer
1586 * the pointer to str. */
1587 RSTRING(str)->as.heap.ptr = RSTRING(orig)->as.heap.ptr;
1588 RBASIC(str)->flags |= RBASIC(orig)->flags & STR_NOFREE;
1589 RBASIC(orig)->flags &= ~STR_NOFREE;
1590 STR_SET_SHARED(orig, str);
1591 if (RB_OBJ_SHAREABLE_P(orig)) {
1592 RB_OBJ_SET_SHAREABLE(str);
1593 RUBY_ASSERT((rb_gc_verify_shareable(str), 1));
1594 }
1595 }
1596
1597 RSTRING(str)->len = RSTRING(orig)->len;
1598 RSTRING(str)->as.heap.aux.capa = capa;
1599
1600 return str;
1601}
1602
1603void
1604rb_str_tmp_frozen_release(VALUE orig, VALUE tmp)
1605{
1606 if (RBASIC_CLASS(tmp) != 0)
1607 return;
1608
1609 if (STR_EMBED_P(tmp)) {
1611 }
1612 else if (FL_TEST_RAW(orig, STR_SHARED | STR_TMPLOCK) == STR_SHARED &&
1613 !OBJ_FROZEN_RAW(orig)) {
1614 VALUE shared = RSTRING(orig)->as.heap.aux.shared;
1615
1616 if (shared == tmp && !FL_TEST_RAW(tmp, STR_BORROWED)) {
1617 RUBY_ASSERT(RSTRING(orig)->as.heap.ptr == RSTRING(tmp)->as.heap.ptr);
1618 RUBY_ASSERT(RSTRING_LEN(orig) == RSTRING_LEN(tmp));
1619
1620 /* Unshare orig since the root (tmp) only has this one child. */
1621 FL_UNSET_RAW(orig, STR_SHARED);
1622 RSTRING(orig)->as.heap.aux.capa = RSTRING(tmp)->as.heap.aux.capa;
1623 RBASIC(orig)->flags |= RBASIC(tmp)->flags & STR_NOFREE;
1625
1626 /* Make tmp embedded and empty so it is safe for sweeping. */
1627 STR_SET_EMBED(tmp);
1628 STR_SET_LEN(tmp, 0);
1629 }
1630 }
1631}
1632
1633static VALUE
1634str_new_frozen(VALUE klass, VALUE orig)
1635{
1636 return str_new_frozen_buffer(klass, orig, TRUE);
1637}
1638
1639static VALUE
1640heap_str_make_shared(VALUE klass, VALUE orig)
1641{
1642 RUBY_ASSERT(!STR_EMBED_P(orig));
1643 RUBY_ASSERT(!STR_SHARED_P(orig));
1645
1646 VALUE str = str_alloc_heap(klass);
1647 STR_SET_LEN(str, RSTRING_LEN(orig));
1648 RSTRING(str)->as.heap.ptr = RSTRING_PTR(orig);
1649 RSTRING(str)->as.heap.aux.capa = RSTRING(orig)->as.heap.aux.capa;
1650 RBASIC(str)->flags |= RBASIC(orig)->flags & STR_NOFREE;
1651 RBASIC(orig)->flags &= ~STR_NOFREE;
1652 STR_SET_SHARED(orig, str);
1653 if (klass == 0)
1654 FL_UNSET_RAW(str, STR_BORROWED);
1655 return str;
1656}
1657
1658static VALUE
1659str_new_frozen_buffer(VALUE klass, VALUE orig, int copy_encoding)
1660{
1661 VALUE str;
1662
1663 long len = RSTRING_LEN(orig);
1664 rb_encoding *enc = copy_encoding ? STR_ENC_GET(orig) : rb_ascii8bit_encoding();
1665 int termlen = copy_encoding ? TERM_LEN(orig) : 1;
1666
1667 if (STR_EMBED_P(orig) || STR_EMBEDDABLE_P(len, termlen)) {
1668 str = str_enc_new(klass, RSTRING_PTR(orig), len, enc);
1669 RUBY_ASSERT(STR_EMBED_P(str));
1670 }
1671 else {
1672 if (FL_TEST_RAW(orig, STR_SHARED)) {
1673 VALUE shared = RSTRING(orig)->as.heap.aux.shared;
1674 long ofs = RSTRING(orig)->as.heap.ptr - RSTRING_PTR(shared);
1675 long rest = RSTRING_LEN(shared) - ofs - RSTRING_LEN(orig);
1676 RUBY_ASSERT(ofs >= 0);
1677 RUBY_ASSERT(rest >= 0);
1678 RUBY_ASSERT(ofs + rest <= RSTRING_LEN(shared));
1679 RUBY_ASSERT(OBJ_FROZEN(shared));
1680
1681 if ((ofs > 0) || (rest > 0) ||
1682 (klass != RBASIC(shared)->klass) ||
1683 ENCODING_GET(shared) != ENCODING_GET(orig)) {
1684 str = str_new_shared(klass, shared);
1685 RUBY_ASSERT(!STR_EMBED_P(str));
1686 RSTRING(str)->as.heap.ptr += ofs;
1687 STR_SET_LEN(str, RSTRING_LEN(str) - (ofs + rest));
1688 }
1689 else {
1690 if (RBASIC_CLASS(shared) == 0)
1691 FL_SET_RAW(shared, STR_BORROWED);
1692 return shared;
1693 }
1694 }
1695 else if (STR_EMBEDDABLE_P(RSTRING_LEN(orig), TERM_LEN(orig))) {
1696 str = str_alloc_embed(klass, RSTRING_LEN(orig) + TERM_LEN(orig));
1697 STR_SET_EMBED(str);
1698 memcpy(RSTRING_PTR(str), RSTRING_PTR(orig), RSTRING_LEN(orig));
1699 STR_SET_LEN(str, RSTRING_LEN(orig));
1700 ENC_CODERANGE_SET(str, ENC_CODERANGE(orig));
1701 TERM_FILL(RSTRING_END(str), TERM_LEN(orig));
1702 }
1703 else {
1704 if (RB_OBJ_SHAREABLE_P(orig)) {
1705 str = str_enc_new(klass, RSTRING_PTR(orig), RSTRING_LEN(orig), enc);
1706 }
1707 else {
1708 str = heap_str_make_shared(klass, orig);
1709 }
1710 }
1711 }
1712
1713 if (copy_encoding) rb_enc_cr_str_exact_copy(str, orig);
1714 OBJ_FREEZE(str);
1715 return str;
1716}
1717
1718VALUE
1719rb_str_new_with_class(VALUE obj, const char *ptr, long len)
1720{
1721 return str_enc_new(rb_obj_class(obj), ptr, len, STR_ENC_GET(obj));
1722}
1723
1724static VALUE
1725str_new_empty_String(VALUE str)
1726{
1727 VALUE v = rb_str_new(0, 0);
1728 rb_enc_copy(v, str);
1729 return v;
1730}
1731
1732#define STR_BUF_MIN_SIZE 63
1733
1734VALUE
1736{
1737 if (STR_EMBEDDABLE_P(capa, 1)) {
1738 return str_alloc_embed(rb_cString, capa + 1);
1739 }
1740
1741 VALUE str = str_alloc_heap(rb_cString);
1742
1743 RSTRING(str)->as.heap.aux.capa = capa;
1744 RSTRING(str)->as.heap.ptr = ALLOC_N(char, (size_t)capa + 1);
1745 RSTRING(str)->as.heap.ptr[0] = '\0';
1746
1747 return str;
1748}
1749
1750VALUE
1752{
1753 VALUE str;
1754 long len = strlen(ptr);
1755
1756 str = rb_str_buf_new(len);
1757 rb_str_buf_cat(str, ptr, len);
1758
1759 return str;
1760}
1761
1762VALUE
1764{
1765 return str_new(0, 0, len);
1766}
1767
1768void
1770{
1771 if (STR_EMBED_P(str)) {
1772 RB_DEBUG_COUNTER_INC(obj_str_embed);
1773 }
1774 else if (FL_TEST(str, STR_SHARED | STR_NOFREE)) {
1775 (void)RB_DEBUG_COUNTER_INC_IF(obj_str_shared, FL_TEST(str, STR_SHARED));
1776 (void)RB_DEBUG_COUNTER_INC_IF(obj_str_shared, FL_TEST(str, STR_NOFREE));
1777 }
1778 else {
1779 RB_DEBUG_COUNTER_INC(obj_str_ptr);
1780 ruby_sized_xfree(STR_HEAP_PTR(str), STR_HEAP_SIZE(str));
1781 }
1782}
1783
1784size_t
1785rb_str_memsize(VALUE str)
1786{
1787 if (FL_TEST(str, STR_NOEMBED|STR_SHARED|STR_NOFREE) == STR_NOEMBED) {
1788 return STR_HEAP_SIZE(str);
1789 }
1790 else {
1791 return 0;
1792 }
1793}
1794
1795VALUE
1797{
1798 return rb_convert_type_with_id(str, T_STRING, "String", idTo_str);
1799}
1800
1801static inline void str_discard(VALUE str);
1802static void str_shared_replace(VALUE str, VALUE str2);
1803
1804void
1806{
1807 if (str != str2) str_shared_replace(str, str2);
1808}
1809
1810static void
1811str_shared_replace(VALUE str, VALUE str2)
1812{
1813 rb_encoding *enc;
1814 int cr;
1815 int termlen;
1816
1817 RUBY_ASSERT(str2 != str);
1818 enc = STR_ENC_GET(str2);
1819 cr = ENC_CODERANGE(str2);
1820 str_discard(str);
1821 termlen = rb_enc_mbminlen(enc);
1822
1823 STR_SET_LEN(str, RSTRING_LEN(str2));
1824
1825 if (str_embed_capa(str) >= RSTRING_LEN(str2) + termlen) {
1826 STR_SET_EMBED(str);
1827 memcpy(RSTRING_PTR(str), RSTRING_PTR(str2), (size_t)RSTRING_LEN(str2) + termlen);
1828 }
1829 else {
1830 if (STR_EMBED_P(str2)) {
1831 RUBY_ASSERT(!FL_TEST(str2, STR_SHARED));
1832 long len = RSTRING_LEN(str2);
1833 RUBY_ASSERT(len + termlen <= str_embed_capa(str2));
1834
1835 char *new_ptr = ALLOC_N(char, len + termlen);
1836 memcpy(new_ptr, RSTRING(str2)->as.embed.ary, len + termlen);
1837 RSTRING(str2)->as.heap.ptr = new_ptr;
1838 STR_SET_LEN(str2, len);
1839 RSTRING(str2)->as.heap.aux.capa = len;
1840 STR_SET_NOEMBED(str2);
1841 }
1842
1843 STR_SET_NOEMBED(str);
1844 FL_UNSET(str, STR_SHARED);
1845 RSTRING(str)->as.heap.ptr = RSTRING_PTR(str2);
1846
1847 if (FL_TEST(str2, STR_SHARED)) {
1848 VALUE shared = RSTRING(str2)->as.heap.aux.shared;
1849 STR_SET_SHARED(str, shared);
1850 }
1851 else {
1852 RSTRING(str)->as.heap.aux.capa = RSTRING(str2)->as.heap.aux.capa;
1853 }
1854
1855 /* abandon str2 */
1856 STR_SET_EMBED(str2);
1857 RSTRING_PTR(str2)[0] = 0;
1858 STR_SET_LEN(str2, 0);
1859 }
1860
1861 // We used str2's termlen above so we set enc raw
1862 // to avoid adjusting it based on str1's old enc.
1863 rb_enc_raw_set(str, enc);
1864 ENC_CODERANGE_SET(str, cr);
1865}
1866
1867VALUE
1869{
1870 VALUE str;
1871
1872 if (RB_TYPE_P(obj, T_STRING)) {
1873 return obj;
1874 }
1875 str = rb_funcall(obj, idTo_s, 0);
1876 return rb_obj_as_string_result(str, obj);
1877}
1878
1879VALUE
1880rb_obj_as_string_result(VALUE str, VALUE obj)
1881{
1882 if (!RB_TYPE_P(str, T_STRING))
1883 return rb_any_to_s(obj);
1884 return str;
1885}
1886
1887static VALUE
1888str_replace(VALUE str, VALUE str2)
1889{
1890 long len;
1891
1892 len = RSTRING_LEN(str2);
1893 if (STR_SHARED_P(str2)) {
1894 VALUE shared = RSTRING(str2)->as.heap.aux.shared;
1895 RUBY_ASSERT(OBJ_FROZEN(shared));
1896 STR_SET_NOEMBED(str);
1897 STR_SET_LEN(str, len);
1898 RSTRING(str)->as.heap.ptr = RSTRING_PTR(str2);
1899 STR_SET_SHARED(str, shared);
1900 rb_enc_cr_str_exact_copy(str, str2);
1901 }
1902 else {
1903 str_replace_shared(str, str2);
1904 }
1905
1906 return str;
1907}
1908
1909static inline VALUE
1910ec_str_alloc_embed(struct rb_execution_context_struct *ec, VALUE klass, size_t capa)
1911{
1912 size_t size = rb_str_embed_size(capa, 0);
1913 RUBY_ASSERT(size > 0);
1914 RUBY_ASSERT(rb_gc_size_allocatable_p(size));
1915
1916 NEWOBJ_OF(str, struct RString, klass,
1918
1919 str->len = 0;
1920
1921 return (VALUE)str;
1922}
1923
1924static inline VALUE
1925ec_str_alloc_heap(struct rb_execution_context_struct *ec, VALUE klass)
1926{
1927 NEWOBJ_OF(str, struct RString, klass,
1928 T_STRING | STR_NOEMBED | (RGENGC_WB_PROTECTED_STRING ? FL_WB_PROTECTED : 0), sizeof(struct RString), ec);
1929
1930 str->as.heap.aux.capa = 0;
1931 str->as.heap.ptr = NULL;
1932
1933 return (VALUE)str;
1934}
1935
1936static inline VALUE
1937str_duplicate_setup_encoding(VALUE str, VALUE dup, VALUE flags)
1938{
1939 int encidx = 0;
1940 if ((flags & ENCODING_MASK) == (ENCODING_INLINE_MAX<<ENCODING_SHIFT)) {
1941 encidx = rb_enc_get_index(str);
1942 flags &= ~ENCODING_MASK;
1943 }
1944 FL_SET_RAW(dup, flags & ~FL_FREEZE);
1945 if (encidx) rb_enc_associate_index(dup, encidx);
1946 return dup;
1947}
1948
1949static const VALUE flag_mask = ENC_CODERANGE_MASK | ENCODING_MASK | FL_FREEZE;
1950
1951static inline VALUE
1952str_duplicate_setup_embed(VALUE klass, VALUE str, VALUE dup)
1953{
1954 VALUE flags = FL_TEST_RAW(str, flag_mask);
1955 long len = RSTRING_LEN(str);
1956
1957 RUBY_ASSERT(STR_EMBED_P(dup));
1958 RUBY_ASSERT(str_embed_capa(dup) >= len + TERM_LEN(str));
1959 MEMCPY(RSTRING(dup)->as.embed.ary, RSTRING(str)->as.embed.ary, char, len + TERM_LEN(str));
1960 STR_SET_LEN(dup, RSTRING_LEN(str));
1961 return str_duplicate_setup_encoding(str, dup, flags);
1962}
1963
1964static inline VALUE
1965str_duplicate_setup_heap(VALUE klass, VALUE str, VALUE dup)
1966{
1967 VALUE flags = FL_TEST_RAW(str, flag_mask);
1968 VALUE root = str;
1969 if (FL_TEST_RAW(str, STR_SHARED)) {
1970 root = RSTRING(str)->as.heap.aux.shared;
1971 }
1972 else if (UNLIKELY(!OBJ_FROZEN_RAW(str))) {
1973 root = str = str_new_frozen(klass, str);
1974 flags = FL_TEST_RAW(str, flag_mask);
1975 }
1976 RUBY_ASSERT(!STR_SHARED_P(root));
1977 RUBY_ASSERT(RB_OBJ_FROZEN_RAW(root));
1978
1979 RSTRING(dup)->as.heap.ptr = RSTRING_PTR(str);
1980 FL_SET(root, STR_SHARED_ROOT);
1981 RB_OBJ_WRITE(dup, &RSTRING(dup)->as.heap.aux.shared, root);
1982 flags |= RSTRING_NOEMBED | STR_SHARED;
1983
1984 STR_SET_LEN(dup, RSTRING_LEN(str));
1985 return str_duplicate_setup_encoding(str, dup, flags);
1986}
1987
1988static inline VALUE
1989str_duplicate_setup(VALUE klass, VALUE str, VALUE dup)
1990{
1991 if (STR_EMBED_P(str)) {
1992 return str_duplicate_setup_embed(klass, str, dup);
1993 }
1994 else {
1995 return str_duplicate_setup_heap(klass, str, dup);
1996 }
1997}
1998
1999static inline VALUE
2000str_duplicate(VALUE klass, VALUE str)
2001{
2002 VALUE dup;
2003 if (STR_EMBED_P(str)) {
2004 dup = str_alloc_embed(klass, RSTRING_LEN(str) + TERM_LEN(str));
2005 }
2006 else {
2007 dup = str_alloc_heap(klass);
2008 }
2009
2010 return str_duplicate_setup(klass, str, dup);
2011}
2012
2013VALUE
2015{
2016 return str_duplicate(rb_obj_class(str), str);
2017}
2018
2019/* :nodoc: */
2020VALUE
2021rb_str_dup_m(VALUE str)
2022{
2023 if (LIKELY(BARE_STRING_P(str))) {
2024 return str_duplicate(rb_cString, str);
2025 }
2026 else {
2027 return rb_obj_dup(str);
2028 }
2029}
2030
2031VALUE
2033{
2034 RUBY_DTRACE_CREATE_HOOK(STRING, RSTRING_LEN(str));
2035 return str_duplicate(rb_cString, str);
2036}
2037
2038VALUE
2039rb_ec_str_resurrect(struct rb_execution_context_struct *ec, VALUE str, bool chilled)
2040{
2041 RUBY_DTRACE_CREATE_HOOK(STRING, RSTRING_LEN(str));
2042 VALUE new_str, klass = rb_cString;
2043
2044 if (!(chilled && RTEST(rb_ivar_defined(str, id_debug_created_info))) && STR_EMBED_P(str)) {
2045 new_str = ec_str_alloc_embed(ec, klass, RSTRING_LEN(str) + TERM_LEN(str));
2046 str_duplicate_setup_embed(klass, str, new_str);
2047 }
2048 else {
2049 new_str = ec_str_alloc_heap(ec, klass);
2050 str_duplicate_setup_heap(klass, str, new_str);
2051 }
2052 if (chilled) {
2053 FL_SET_RAW(new_str, STR_CHILLED_LITERAL);
2054 }
2055 return new_str;
2056}
2057
2058VALUE
2059rb_str_with_debug_created_info(VALUE str, VALUE path, int line)
2060{
2061 VALUE debug_info = rb_ary_new_from_args(2, path, INT2FIX(line));
2062 if (OBJ_FROZEN_RAW(str)) str = rb_str_dup(str);
2063 rb_ivar_set(str, id_debug_created_info, rb_ary_freeze(debug_info));
2064 FL_SET_RAW(str, STR_CHILLED_LITERAL);
2065 return rb_str_freeze(str);
2066}
2067
2068/*
2069 * The documentation block below uses an include (instead of inline text)
2070 * because the included text has non-ASCII characters (which are not allowed in a C file).
2071 */
2072
2073/*
2074 *
2075 * call-seq:
2076 * String.new(string = ''.encode(Encoding::ASCII_8BIT) , **options) -> new_string
2077 *
2078 * :include: doc/string/new.rdoc
2079 *
2080 */
2081
2082static VALUE
2083rb_str_init(int argc, VALUE *argv, VALUE str)
2084{
2085 static ID keyword_ids[2];
2086 VALUE orig, opt, venc, vcapa;
2087 VALUE kwargs[2];
2088 rb_encoding *enc = 0;
2089 int n;
2090
2091 if (!keyword_ids[0]) {
2092 keyword_ids[0] = rb_id_encoding();
2093 CONST_ID(keyword_ids[1], "capacity");
2094 }
2095
2096 n = rb_scan_args(argc, argv, "01:", &orig, &opt);
2097 if (!NIL_P(opt)) {
2098 rb_get_kwargs(opt, keyword_ids, 0, 2, kwargs);
2099 venc = kwargs[0];
2100 vcapa = kwargs[1];
2101 if (!UNDEF_P(venc) && !NIL_P(venc)) {
2102 enc = rb_to_encoding(venc);
2103 }
2104 if (!UNDEF_P(vcapa) && !NIL_P(vcapa)) {
2105 long capa = NUM2LONG(vcapa);
2106 long len = 0;
2107 int termlen;
2108
2109 if (capa < STR_BUF_MIN_SIZE) {
2110 capa = STR_BUF_MIN_SIZE;
2111 }
2112 if (n == 1) {
2113 StringValue(orig);
2114 len = RSTRING_LEN(orig);
2115 if (capa < len) {
2116 capa = len;
2117 }
2118 if (orig == str) n = 0;
2119 }
2120 termlen = enc ? rb_enc_mbminlen(enc) : TERM_LEN(n == 1 ? orig : str);
2121 str_modifiable(str);
2122 if (STR_EMBED_P(str) || FL_TEST(str, STR_SHARED|STR_NOFREE)) {
2123 /* make noembed always */
2124 const size_t size = (size_t)capa + termlen;
2125 const char *const old_ptr = RSTRING_PTR(str);
2126 const size_t osize = RSTRING_LEN(str) + TERM_LEN(str);
2127 char *new_ptr = ALLOC_N(char, size);
2128 if (STR_EMBED_P(str)) RUBY_ASSERT((long)osize <= str_embed_capa(str));
2129 memcpy(new_ptr, old_ptr, osize < size ? osize : size);
2130 FL_UNSET_RAW(str, STR_SHARED|STR_NOFREE);
2131 RSTRING(str)->as.heap.ptr = new_ptr;
2132 }
2133 else if (STR_HEAP_SIZE(str) != (size_t)capa + termlen) {
2134 SIZED_REALLOC_N(RSTRING(str)->as.heap.ptr, char,
2135 (size_t)capa + termlen, STR_HEAP_SIZE(str));
2136 }
2137 STR_SET_LEN(str, len);
2138 TERM_FILL(&RSTRING(str)->as.heap.ptr[len], termlen);
2139 if (n == 1) {
2140 memcpy(RSTRING(str)->as.heap.ptr, RSTRING_PTR(orig), len);
2141 if (!enc || rb_enc_get(orig) == enc) {
2142 rb_enc_cr_str_exact_copy(str, orig);
2143 enc = 0;
2144 }
2145 }
2146 FL_SET(str, STR_NOEMBED);
2147 RSTRING(str)->as.heap.aux.capa = capa;
2148 }
2149 else if (n == 1) {
2150 rb_str_replace(str, orig);
2151 }
2152 if (enc) {
2153 str_modifiable(str);
2154 if (UNDEF_P(vcapa) || NIL_P(vcapa)) {
2155 rb_enc_associate(str, enc);
2156 }
2157 else {
2158 /* the buffer is already sized for enc's terminator */
2159 rb_enc_raw_set(str, enc);
2160 }
2162 }
2163 }
2164 else if (n == 1) {
2165 rb_str_replace(str, orig);
2166 }
2167 return str;
2168}
2169
2170/* :nodoc: */
2171static VALUE
2172rb_str_s_new(int argc, VALUE *argv, VALUE klass)
2173{
2174 if (klass != rb_cString) {
2175 return rb_class_new_instance_pass_kw(argc, argv, klass);
2176 }
2177
2178 static ID keyword_ids[2];
2179 VALUE orig, opt, encoding = Qnil, capacity = Qnil;
2180 VALUE kwargs[2];
2181 rb_encoding *enc = NULL;
2182
2183 int n = rb_scan_args(argc, argv, "01:", &orig, &opt);
2184 if (NIL_P(opt)) {
2185 return rb_class_new_instance_pass_kw(argc, argv, klass);
2186 }
2187
2188 keyword_ids[0] = rb_id_encoding();
2189 CONST_ID(keyword_ids[1], "capacity");
2190 rb_get_kwargs(opt, keyword_ids, 0, 2, kwargs);
2191 encoding = kwargs[0];
2192 capacity = kwargs[1];
2193
2194 if (n == 1) {
2195 orig = StringValue(orig);
2196 }
2197 else {
2198 orig = Qnil;
2199 }
2200
2201 if (UNDEF_P(encoding)) {
2202 if (!NIL_P(orig)) {
2203 encoding = rb_obj_encoding(orig);
2204 }
2205 }
2206
2207 if (!UNDEF_P(encoding)) {
2208 enc = rb_to_encoding(encoding);
2209 }
2210
2211 // If capacity is nil, we're basically just duping `orig`.
2212 if (UNDEF_P(capacity)) {
2213 if (NIL_P(orig)) {
2214 VALUE empty_str = str_new(klass, "", 0);
2215 if (enc) {
2216 rb_enc_associate(empty_str, enc);
2217 }
2218 return empty_str;
2219 }
2220 VALUE copy = str_duplicate(klass, orig);
2221 rb_enc_associate(copy, enc);
2222 ENC_CODERANGE_CLEAR(copy);
2223 return copy;
2224 }
2225
2226 long capa = 0;
2227 capa = NUM2LONG(capacity);
2228 if (capa < 0) {
2229 capa = 0;
2230 }
2231
2232 if (!NIL_P(orig)) {
2233 long orig_capa = rb_str_capacity(orig);
2234 if (orig_capa > capa) {
2235 capa = orig_capa;
2236 }
2237 }
2238
2239 VALUE str = str_enc_new(klass, NULL, capa, enc);
2240 STR_SET_LEN(str, 0);
2241 TERM_FILL(RSTRING_PTR(str), enc ? rb_enc_mbmaxlen(enc) : 1);
2242
2243 if (!NIL_P(orig)) {
2244 rb_str_buf_append(str, orig);
2245 }
2246
2247 return str;
2248}
2249
2250#ifdef NONASCII_MASK
2251#define is_utf8_lead_byte(c) (((c)&0xC0) != 0x80)
2252
2253/*
2254 * UTF-8 leading bytes have either 0xxxxxxx or 11xxxxxx
2255 * bit representation. (see https://en.wikipedia.org/wiki/UTF-8)
2256 * Therefore, the following pseudocode can detect UTF-8 leading bytes.
2257 *
2258 * if (!(byte & 0x80))
2259 * byte |= 0x40; // turn on bit6
2260 * return ((byte>>6) & 1); // bit6 represent whether this byte is leading or not.
2261 *
2262 * This function calculates whether a byte is leading or not for all bytes
2263 * in the argument word by concurrently using the above logic, and then
2264 * adds up the number of leading bytes in the word.
2265 */
2266static inline uintptr_t
2267count_utf8_lead_bytes_with_word(const uintptr_t *s)
2268{
2269 uintptr_t d = *s;
2270
2271 /* Transform so that bit0 indicates whether we have a UTF-8 leading byte or not. */
2272 d = (d>>6) | (~d>>7);
2273 d &= NONASCII_MASK >> 7;
2274
2275 /* Gather all bytes. */
2276#if defined(HAVE_BUILTIN___BUILTIN_POPCOUNT) && defined(__POPCNT__)
2277 /* use only if it can use POPCNT */
2278 return rb_popcount_intptr(d);
2279#else
2280 d += (d>>8);
2281 d += (d>>16);
2282# if SIZEOF_VOIDP == 8
2283 d += (d>>32);
2284# endif
2285 return (d&0xF);
2286#endif
2287}
2288#endif
2289
2290static inline long
2291enc_strlen(const char *p, const char *e, rb_encoding *enc, int cr)
2292{
2293 long c;
2294 const char *q;
2295
2296 if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) {
2297 long diff = (long)(e - p);
2298 return diff / rb_enc_mbminlen(enc) + !!(diff % rb_enc_mbminlen(enc));
2299 }
2300#ifdef NONASCII_MASK
2301 else if (cr == ENC_CODERANGE_VALID && enc == rb_utf8_encoding()) {
2302 uintptr_t len = 0;
2303 if ((int)sizeof(uintptr_t) * 2 < e - p) {
2304 const uintptr_t *s, *t;
2305 const uintptr_t lowbits = sizeof(uintptr_t) - 1;
2306 s = (const uintptr_t*)(~lowbits & ((uintptr_t)p + lowbits));
2307 t = (const uintptr_t*)(~lowbits & (uintptr_t)e);
2308 while (p < (const char *)s) {
2309 if (is_utf8_lead_byte(*p)) len++;
2310 p++;
2311 }
2312 while (s < t) {
2313 len += count_utf8_lead_bytes_with_word(s);
2314 s++;
2315 }
2316 p = (const char *)s;
2317 }
2318 while (p < e) {
2319 if (is_utf8_lead_byte(*p)) len++;
2320 p++;
2321 }
2322 return (long)len;
2323 }
2324#endif
2325 else if (rb_enc_asciicompat(enc)) {
2326 c = 0;
2327 if (ENC_CODERANGE_CLEAN_P(cr)) {
2328 while (p < e) {
2329 if (ISASCII(*p)) {
2330 q = search_nonascii(p, e);
2331 if (!q)
2332 return c + (e - p);
2333 c += q - p;
2334 p = q;
2335 }
2336 p += rb_enc_fast_mbclen(p, e, enc);
2337 c++;
2338 }
2339 }
2340 else {
2341 while (p < e) {
2342 if (ISASCII(*p)) {
2343 q = search_nonascii(p, e);
2344 if (!q)
2345 return c + (e - p);
2346 c += q - p;
2347 p = q;
2348 }
2349 p += rb_enc_mbclen(p, e, enc);
2350 c++;
2351 }
2352 }
2353 return c;
2354 }
2355
2356 for (c=0; p<e; c++) {
2357 p += rb_enc_mbclen(p, e, enc);
2358 }
2359 return c;
2360}
2361
2362long
2363rb_enc_strlen(const char *p, const char *e, rb_encoding *enc)
2364{
2365 return enc_strlen(p, e, enc, ENC_CODERANGE_UNKNOWN);
2366}
2367
2368/* To get strlen with cr
2369 * Note that given cr is not used.
2370 */
2371long
2372rb_enc_strlen_cr(const char *p, const char *e, rb_encoding *enc, int *cr)
2373{
2374 long c;
2375 const char *q;
2376 int ret;
2377
2378 *cr = 0;
2379 if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) {
2380 long diff = (long)(e - p);
2381 return diff / rb_enc_mbminlen(enc) + !!(diff % rb_enc_mbminlen(enc));
2382 }
2383 else if (rb_enc_asciicompat(enc)) {
2384 c = 0;
2385 while (p < e) {
2386 if (ISASCII(*p)) {
2387 q = search_nonascii(p, e);
2388 if (!q) {
2389 if (!*cr) *cr = ENC_CODERANGE_7BIT;
2390 return c + (e - p);
2391 }
2392 c += q - p;
2393 p = q;
2394 }
2395 ret = rb_enc_precise_mbclen(p, e, enc);
2396 if (MBCLEN_CHARFOUND_P(ret)) {
2397 *cr |= ENC_CODERANGE_VALID;
2398 p += MBCLEN_CHARFOUND_LEN(ret);
2399 }
2400 else {
2402 p++;
2403 }
2404 c++;
2405 }
2406 if (!*cr) *cr = ENC_CODERANGE_7BIT;
2407 return c;
2408 }
2409
2410 for (c=0; p<e; c++) {
2411 ret = rb_enc_precise_mbclen(p, e, enc);
2412 if (MBCLEN_CHARFOUND_P(ret)) {
2413 *cr |= ENC_CODERANGE_VALID;
2414 p += MBCLEN_CHARFOUND_LEN(ret);
2415 }
2416 else {
2418 if (p + rb_enc_mbminlen(enc) <= e)
2419 p += rb_enc_mbminlen(enc);
2420 else
2421 p = e;
2422 }
2423 }
2424 if (!*cr) *cr = ENC_CODERANGE_7BIT;
2425 return c;
2426}
2427
2428/* enc must be str's enc or rb_enc_check(str, str2) */
2429static long
2430str_strlen(VALUE str, rb_encoding *enc)
2431{
2432 const char *p, *e;
2433 int cr;
2434
2435 if (single_byte_optimizable(str)) return RSTRING_LEN(str);
2436 if (!enc) enc = STR_ENC_GET(str);
2437 p = RSTRING_PTR(str);
2438 e = RSTRING_END(str);
2439 cr = ENC_CODERANGE(str);
2440
2441 if (cr == ENC_CODERANGE_UNKNOWN) {
2442 long n = rb_enc_strlen_cr(p, e, enc, &cr);
2443 if (cr) ENC_CODERANGE_SET(str, cr);
2444 return n;
2445 }
2446 else {
2447 return enc_strlen(p, e, enc, cr);
2448 }
2449}
2450
2451long
2453{
2454 return str_strlen(str, NULL);
2455}
2456
2457/*
2458 * call-seq:
2459 * length -> integer
2460 *
2461 * :include: doc/string/length.rdoc
2462 *
2463 */
2464
2465VALUE
2467{
2468 return LONG2NUM(str_strlen(str, NULL));
2469}
2470
2471/*
2472 * call-seq:
2473 * bytesize -> integer
2474 *
2475 * :include: doc/string/bytesize.rdoc
2476 *
2477 */
2478
2479VALUE
2480rb_str_bytesize(VALUE str)
2481{
2482 return LONG2NUM(RSTRING_LEN(str));
2483}
2484
2485/*
2486 * call-seq:
2487 * empty? -> true or false
2488 *
2489 * Returns whether the length of +self+ is zero:
2490 *
2491 * 'hello'.empty? # => false
2492 * ' '.empty? # => false
2493 * ''.empty? # => true
2494 *
2495 * Related: see {Querying}[rdoc-ref:String@Querying].
2496 */
2497
2498static VALUE
2499rb_str_empty(VALUE str)
2500{
2501 return RBOOL(RSTRING_LEN(str) == 0);
2502}
2503
2504/*
2505 * call-seq:
2506 * self + other_string -> new_string
2507 *
2508 * Returns a new string containing +other_string+ concatenated to +self+:
2509 *
2510 * 'Hello from ' + self.to_s # => "Hello from main"
2511 *
2512 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
2513 */
2514
2515VALUE
2517{
2518 VALUE str3;
2519 rb_encoding *enc;
2520 char *ptr1, *ptr2, *ptr3;
2521 long len1, len2;
2522 int termlen;
2523
2524 StringValue(str2);
2525 enc = rb_enc_check_str(str1, str2);
2526 RSTRING_GETMEM(str1, ptr1, len1);
2527 RSTRING_GETMEM(str2, ptr2, len2);
2528 termlen = rb_enc_mbminlen(enc);
2529 if (len1 > LONG_MAX - len2) {
2530 rb_raise(rb_eArgError, "string size too big");
2531 }
2532 str3 = str_enc_new(rb_cString, 0, len1+len2, enc);
2533 ptr3 = RSTRING_PTR(str3);
2534 memcpy(ptr3, ptr1, len1);
2535 memcpy(ptr3+len1, ptr2, len2);
2536 TERM_FILL(&ptr3[len1+len2], termlen);
2537
2538 ENCODING_CODERANGE_SET(str3, rb_enc_to_index(enc),
2540 RB_GC_GUARD(str1);
2541 RB_GC_GUARD(str2);
2542 return str3;
2543}
2544
2545/* A variant of rb_str_plus that does not raise but return Qundef instead. */
2546VALUE
2547rb_str_opt_plus(VALUE str1, VALUE str2)
2548{
2549 RUBY_ASSERT(RBASIC_CLASS(str1) == rb_cString);
2550 RUBY_ASSERT(RBASIC_CLASS(str2) == rb_cString);
2551 long len1, len2;
2552 MAYBE_UNUSED(char) *ptr1, *ptr2;
2553 RSTRING_GETMEM(str1, ptr1, len1);
2554 RSTRING_GETMEM(str2, ptr2, len2);
2555 int enc1 = rb_enc_get_index(str1);
2556 int enc2 = rb_enc_get_index(str2);
2557
2558 if (enc1 < 0) {
2559 return Qundef;
2560 }
2561 else if (enc2 < 0) {
2562 return Qundef;
2563 }
2564 else if (enc1 != enc2) {
2565 return Qundef;
2566 }
2567 else if (len1 > LONG_MAX - len2) {
2568 return Qundef;
2569 }
2570 else {
2571 return rb_str_plus(str1, str2);
2572 }
2573
2574}
2575
2576/*
2577 * call-seq:
2578 * self * n -> new_string
2579 *
2580 * Returns a new string containing +n+ copies of +self+:
2581 *
2582 * 'Ho!' * 3 # => "Ho!Ho!Ho!"
2583 * 'No!' * 0 # => ""
2584 *
2585 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
2586 */
2587
2588VALUE
2590{
2591 VALUE str2;
2592 long n, len;
2593 char *ptr2;
2594 int termlen;
2595
2596 if (times == INT2FIX(1)) {
2597 return str_duplicate(rb_cString, str);
2598 }
2599 if (times == INT2FIX(0)) {
2600 str2 = str_alloc_embed(rb_cString, 0);
2601 rb_enc_copy(str2, str);
2602 return str2;
2603 }
2604 len = NUM2LONG(times);
2605 if (len < 0) {
2606 rb_raise(rb_eArgError, "negative argument");
2607 }
2608 if (RSTRING_LEN(str) == 1 && RSTRING_PTR(str)[0] == 0) {
2609 if (STR_EMBEDDABLE_P(len, 1)) {
2610 str2 = str_alloc_embed(rb_cString, len + 1);
2611 memset(RSTRING_PTR(str2), 0, len + 1);
2612 }
2613 else {
2614 str2 = str_alloc_heap(rb_cString);
2615 RSTRING(str2)->as.heap.aux.capa = len;
2616 RSTRING(str2)->as.heap.ptr = ZALLOC_N(char, (size_t)len + 1);
2617 }
2618 STR_SET_LEN(str2, len);
2619 rb_enc_copy(str2, str);
2620 return str2;
2621 }
2622 if (len && LONG_MAX/len < RSTRING_LEN(str)) {
2623 rb_raise(rb_eArgError, "argument too big");
2624 }
2625
2626 len *= RSTRING_LEN(str);
2627 termlen = TERM_LEN(str);
2628 str2 = str_enc_new(rb_cString, 0, len, STR_ENC_GET(str));
2629 ptr2 = RSTRING_PTR(str2);
2630 if (len) {
2631 n = RSTRING_LEN(str);
2632 memcpy(ptr2, RSTRING_PTR(str), n);
2633 while (n <= len/2) {
2634 memcpy(ptr2 + n, ptr2, n);
2635 n *= 2;
2636 }
2637 memcpy(ptr2 + n, ptr2, len-n);
2638 }
2639 STR_SET_LEN(str2, len);
2640 TERM_FILL(&ptr2[len], termlen);
2641 rb_enc_cr_str_copy_for_substr(str2, str);
2642
2643 return str2;
2644}
2645
2646/*
2647 * call-seq:
2648 * self % object -> new_string
2649 *
2650 * Returns the result of formatting +object+ into the format specifications
2651 * contained in +self+
2652 * (see {Format Specifications}[rdoc-ref:language/format_specifications.rdoc]):
2653 *
2654 * '%05d' % 123 # => "00123"
2655 *
2656 * If +self+ contains multiple format specifications,
2657 * +object+ must be an array or hash containing the objects to be formatted:
2658 *
2659 * '%-5s: %016x' % [ 'ID', self.object_id ] # => "ID : 00002b054ec93168"
2660 * 'foo = %{foo}' % {foo: 'bar'} # => "foo = bar"
2661 * 'foo = %{foo}, baz = %{baz}' % {foo: 'bar', baz: 'bat'} # => "foo = bar, baz = bat"
2662 *
2663 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
2664 */
2665
2666static VALUE
2667rb_str_format_m(VALUE str, VALUE arg)
2668{
2669 VALUE tmp = rb_check_array_type(arg);
2670
2671 if (!NIL_P(tmp)) {
2672 VALUE result = rb_str_format_ary(RARRAY_LENINT(tmp), RARRAY_CONST_PTR(tmp), str, tmp);
2673 RB_GC_GUARD(tmp);
2674 return result;
2675 }
2676 return rb_str_format(1, &arg, str);
2677}
2678
2679static inline void
2680rb_check_lockedtmp(VALUE str)
2681{
2682 if (FL_TEST(str, STR_TMPLOCK)) {
2683 rb_raise(rb_eRuntimeError, "can't modify string; temporarily locked");
2684 }
2685}
2686
2687// If none of these flags are set, we know we have an modifiable string.
2688// If any is set, we need to do more detailed checks.
2689#define STR_UNMODIFIABLE_MASK (FL_FREEZE | STR_TMPLOCK | STR_CHILLED)
2690static inline void
2691str_modifiable(VALUE str)
2692{
2693 RUBY_ASSERT(ruby_thread_has_gvl_p());
2694
2695 if (RB_UNLIKELY(FL_ANY_RAW(str, STR_UNMODIFIABLE_MASK))) {
2696 if (CHILLED_STRING_P(str)) {
2697 CHILLED_STRING_MUTATED(str);
2698 }
2699 rb_check_lockedtmp(str);
2700 rb_check_frozen(str);
2701 }
2702}
2703
2704static inline int
2705str_dependent_p(VALUE str)
2706{
2707 if (STR_EMBED_P(str) || !FL_TEST(str, STR_SHARED|STR_NOFREE)) {
2708 return FALSE;
2709 }
2710 else {
2711 return TRUE;
2712 }
2713}
2714
2715// If none of these flags are set, we know we have an independent string.
2716// If any is set, we need to do more detailed checks.
2717#define STR_DEPENDANT_MASK (STR_UNMODIFIABLE_MASK | STR_SHARED | STR_NOFREE)
2718static inline int
2719str_independent(VALUE str)
2720{
2721 RUBY_ASSERT(ruby_thread_has_gvl_p());
2722
2723 if (RB_UNLIKELY(FL_ANY_RAW(str, STR_DEPENDANT_MASK))) {
2724 str_modifiable(str);
2725 return !str_dependent_p(str);
2726 }
2727 return TRUE;
2728}
2729
2730static void
2731str_make_independent_expand(VALUE str, long len, long expand, const int termlen)
2732{
2733 RUBY_ASSERT(ruby_thread_has_gvl_p());
2734
2735 char *ptr;
2736 char *oldptr;
2737 long capa = len + expand;
2738
2739 if (len > capa) len = capa;
2740
2741 if (!STR_EMBED_P(str) && str_embed_capa(str) >= capa + termlen) {
2742 ptr = RSTRING(str)->as.heap.ptr;
2743 STR_SET_EMBED(str);
2744 memcpy(RSTRING(str)->as.embed.ary, ptr, len);
2745 TERM_FILL(RSTRING(str)->as.embed.ary + len, termlen);
2746 STR_SET_LEN(str, len);
2747 return;
2748 }
2749
2750 ptr = ALLOC_N(char, (size_t)capa + termlen);
2751 oldptr = RSTRING_PTR(str);
2752 if (oldptr) {
2753 memcpy(ptr, oldptr, len);
2754 }
2755 if (FL_TEST_RAW(str, STR_NOEMBED|STR_NOFREE|STR_SHARED) == STR_NOEMBED) {
2756 xfree(oldptr);
2757 }
2758 STR_SET_NOEMBED(str);
2759 FL_UNSET(str, STR_SHARED|STR_NOFREE);
2760 TERM_FILL(ptr + len, termlen);
2761 RSTRING(str)->as.heap.ptr = ptr;
2762 STR_SET_LEN(str, len);
2763 RSTRING(str)->as.heap.aux.capa = capa;
2764}
2765
2766void
2767rb_str_modify(VALUE str)
2768{
2769 if (!str_independent(str))
2770 str_make_independent(str);
2772}
2773
2774void
2776{
2777 RUBY_ASSERT(ruby_thread_has_gvl_p());
2778
2779 int termlen = TERM_LEN(str);
2780 long len = RSTRING_LEN(str);
2781
2782 if (expand < 0) {
2783 rb_raise(rb_eArgError, "negative expanding string size");
2784 }
2785 if (expand > LONG_MAX - termlen - len) {
2786 rb_raise(rb_eArgError, "string size too big");
2787 }
2788
2789 if (!str_independent(str)) {
2790 str_make_independent_expand(str, len, expand, termlen);
2791 }
2792 else if (expand > 0) {
2793 RESIZE_CAPA_TERM(str, len + expand, termlen);
2794 }
2796}
2797
2798/* As rb_str_modify(), but don't clear coderange */
2799static void
2800str_modify_keep_cr(VALUE str)
2801{
2802 if (!str_independent(str))
2803 str_make_independent(str);
2805 /* Force re-scan later */
2807}
2808
2809static inline void
2810str_discard(VALUE str)
2811{
2812 str_modifiable(str);
2813 if (!STR_EMBED_P(str) && !FL_TEST(str, STR_SHARED|STR_NOFREE)) {
2814 ruby_sized_xfree(STR_HEAP_PTR(str), STR_HEAP_SIZE(str));
2815 RSTRING(str)->as.heap.ptr = 0;
2816 STR_SET_LEN(str, 0);
2817 }
2818}
2819
2820void
2822{
2823 int encindex = rb_enc_get_index(str);
2824
2825 if (RB_UNLIKELY(encindex == -1)) {
2826 rb_raise(rb_eTypeError, "not encoding capable object");
2827 }
2828
2829 if (RB_LIKELY(str_encindex_fastpath(encindex))) {
2830 return;
2831 }
2832
2833 rb_encoding *enc = rb_enc_from_index(encindex);
2834 if (!rb_enc_asciicompat(enc)) {
2835 rb_raise(rb_eEncCompatError, "ASCII incompatible encoding: %s", rb_enc_name(enc));
2836 }
2837}
2838
2839VALUE
2841{
2842 RUBY_ASSERT(ruby_thread_has_gvl_p());
2843
2844 VALUE s = *ptr;
2845 if (!RB_TYPE_P(s, T_STRING)) {
2846 s = rb_str_to_str(s);
2847 *ptr = s;
2848 }
2849 return s;
2850}
2851
2852char *
2854{
2855 VALUE str = rb_string_value(ptr);
2856 return RSTRING_PTR(str);
2857}
2858
2859static int
2860zero_filled(const char *s, int n)
2861{
2862 for (; n > 0; --n) {
2863 if (*s++) return 0;
2864 }
2865 return 1;
2866}
2867
2868static const char *
2869str_null_char(const char *s, long len, const int minlen, rb_encoding *enc)
2870{
2871 const char *e = s + len;
2872
2873 for (; s + minlen <= e; s += rb_enc_mbclen(s, e, enc)) {
2874 if (zero_filled(s, minlen)) return s;
2875 }
2876 return 0;
2877}
2878
2879static char *
2880str_fill_term(VALUE str, char *s, long len, int termlen)
2881{
2882 /* This function assumes that (capa + termlen) bytes of memory
2883 * is allocated, like many other functions in this file.
2884 */
2885 if (str_dependent_p(str)) {
2886 if (!zero_filled(s + len, termlen))
2887 str_make_independent_expand(str, len, 0L, termlen);
2888 }
2889 else {
2890 TERM_FILL(s + len, termlen);
2891 return s;
2892 }
2893 return RSTRING_PTR(str);
2894}
2895
2896void
2897rb_str_change_terminator_length(VALUE str, const int oldtermlen, const int termlen)
2898{
2899 long capa = str_capacity(str, oldtermlen) + oldtermlen;
2900 long len = RSTRING_LEN(str);
2901
2902 RUBY_ASSERT(capa >= len);
2903 if (capa - len < termlen) {
2904 rb_check_lockedtmp(str);
2905 str_make_independent_expand(str, len, 0L, termlen);
2906 }
2907 else if (str_dependent_p(str)) {
2908 if (termlen > oldtermlen)
2909 str_make_independent_expand(str, len, 0L, termlen);
2910 }
2911 else {
2912 if (!STR_EMBED_P(str)) {
2913 /* modify capa instead of realloc */
2914 RUBY_ASSERT(!FL_TEST((str), STR_SHARED));
2915 RSTRING(str)->as.heap.aux.capa = capa - termlen;
2916 }
2917 if (termlen > oldtermlen) {
2918 TERM_FILL(RSTRING_PTR(str) + len, termlen);
2919 }
2920 }
2921
2922 return;
2923}
2924
2925static char *
2926str_null_check(VALUE str, int *w)
2927{
2928 char *s = RSTRING_PTR(str);
2929 long len = RSTRING_LEN(str);
2930 rb_encoding *enc = rb_enc_get(str);
2931 const int minlen = rb_enc_mbminlen(enc);
2932
2933 if (minlen > 1) {
2934 *w = 1;
2935 if (str_null_char(s, len, minlen, enc)) {
2936 return NULL;
2937 }
2938 return str_fill_term(str, s, len, minlen);
2939 }
2940 *w = 0;
2941 if (!s || memchr(s, 0, len)) {
2942 return NULL;
2943 }
2944 if (s[len]) {
2945 s = str_fill_term(str, s, len, minlen);
2946 }
2947 return s;
2948}
2949
2950char *
2951rb_str_to_cstr(VALUE str)
2952{
2953 int w;
2954 return str_null_check(str, &w);
2955}
2956
2957char *
2959{
2960 VALUE str = rb_string_value(ptr);
2961 int w;
2962 char *s = str_null_check(str, &w);
2963 if (!s) {
2964 if (w) {
2965 rb_raise(rb_eArgError, "string contains null char");
2966 }
2967 rb_raise(rb_eArgError, "string contains null byte");
2968 }
2969 return s;
2970}
2971
2972char *
2973rb_str_fill_terminator(VALUE str, const int newminlen)
2974{
2975 char *s = RSTRING_PTR(str);
2976 long len = RSTRING_LEN(str);
2977 return str_fill_term(str, s, len, newminlen);
2978}
2979
2980VALUE
2982{
2983 str = rb_check_convert_type_with_id(str, T_STRING, "String", idTo_str);
2984 return str;
2985}
2986
2987/*
2988 * call-seq:
2989 * String.try_convert(object) -> object, new_string, or nil
2990 *
2991 * Attempts to convert the given +object+ to a string.
2992 *
2993 * If +object+ is already a string, returns +object+, unmodified.
2994 *
2995 * Otherwise if +object+ responds to <tt>:to_str</tt>,
2996 * calls <tt>object.to_str</tt> and returns the result.
2997 *
2998 * Returns +nil+ if +object+ does not respond to <tt>:to_str</tt>.
2999 *
3000 * Raises an exception unless <tt>object.to_str</tt> returns a string.
3001 */
3002static VALUE
3003rb_str_s_try_convert(VALUE dummy, VALUE str)
3004{
3005 return rb_check_string_type(str);
3006}
3007
3008static char*
3009str_nth_len(const char *p, const char *e, long *nthp, rb_encoding *enc)
3010{
3011 long nth = *nthp;
3012 if (rb_enc_mbmaxlen(enc) == 1) {
3013 p += nth;
3014 }
3015 else if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) {
3016 p += nth * rb_enc_mbmaxlen(enc);
3017 }
3018 else if (rb_enc_asciicompat(enc)) {
3019 const char *p2, *e2;
3020 int n;
3021
3022 while (p < e && 0 < nth) {
3023 e2 = p + nth;
3024 if (e < e2) {
3025 *nthp = nth;
3026 return (char *)e;
3027 }
3028 if (ISASCII(*p)) {
3029 p2 = search_nonascii(p, e2);
3030 if (!p2) {
3031 nth -= e2 - p;
3032 *nthp = nth;
3033 return (char *)e2;
3034 }
3035 nth -= p2 - p;
3036 p = p2;
3037 }
3038 n = rb_enc_mbclen(p, e, enc);
3039 p += n;
3040 nth--;
3041 }
3042 *nthp = nth;
3043 if (nth != 0) {
3044 return (char *)e;
3045 }
3046 return (char *)p;
3047 }
3048 else {
3049 while (p < e && nth--) {
3050 p += rb_enc_mbclen(p, e, enc);
3051 }
3052 }
3053 if (p > e) p = e;
3054 *nthp = nth;
3055 return (char*)p;
3056}
3057
3058char*
3059rb_enc_nth(const char *p, const char *e, long nth, rb_encoding *enc)
3060{
3061 return str_nth_len(p, e, &nth, enc);
3062}
3063
3064static char*
3065str_nth(const char *p, const char *e, long nth, rb_encoding *enc, int singlebyte)
3066{
3067 if (singlebyte)
3068 p += nth;
3069 else {
3070 p = str_nth_len(p, e, &nth, enc);
3071 }
3072 if (!p) return 0;
3073 if (p > e) p = e;
3074 return (char *)p;
3075}
3076
3077/* char offset to byte offset */
3078static long
3079str_offset(const char *p, const char *e, long nth, rb_encoding *enc, int singlebyte)
3080{
3081 const char *pp = str_nth(p, e, nth, enc, singlebyte);
3082 if (!pp) return e - p;
3083 return pp - p;
3084}
3085
3086long
3087rb_str_offset(VALUE str, long pos)
3088{
3089 return str_offset(RSTRING_PTR(str), RSTRING_END(str), pos,
3090 STR_ENC_GET(str), single_byte_optimizable(str));
3091}
3092
3093#ifdef NONASCII_MASK
3094static char *
3095str_utf8_nth(const char *p, const char *e, long *nthp)
3096{
3097 long nth = *nthp;
3098 if ((int)SIZEOF_VOIDP * 2 < e - p && (int)SIZEOF_VOIDP * 2 < nth) {
3099 const uintptr_t *s, *t;
3100 const uintptr_t lowbits = SIZEOF_VOIDP - 1;
3101 s = (const uintptr_t*)(~lowbits & ((uintptr_t)p + lowbits));
3102 t = (const uintptr_t*)(~lowbits & (uintptr_t)e);
3103 while (p < (const char *)s) {
3104 if (is_utf8_lead_byte(*p)) nth--;
3105 p++;
3106 }
3107 do {
3108 nth -= count_utf8_lead_bytes_with_word(s);
3109 s++;
3110 } while (s < t && (int)SIZEOF_VOIDP <= nth);
3111 p = (char *)s;
3112 }
3113 while (p < e) {
3114 if (is_utf8_lead_byte(*p)) {
3115 if (nth == 0) break;
3116 nth--;
3117 }
3118 p++;
3119 }
3120 *nthp = nth;
3121 return (char *)p;
3122}
3123
3124static long
3125str_utf8_offset(const char *p, const char *e, long nth)
3126{
3127 const char *pp = str_utf8_nth(p, e, &nth);
3128 return pp - p;
3129}
3130#endif
3131
3132/* byte offset to char offset */
3133long
3134rb_str_sublen(VALUE str, long pos)
3135{
3136 if (single_byte_optimizable(str) || pos < 0)
3137 return pos;
3138 else {
3139 char *p = RSTRING_PTR(str);
3140 return enc_strlen(p, p + pos, STR_ENC_GET(str), ENC_CODERANGE(str));
3141 }
3142}
3143
3144static VALUE
3145str_subseq(VALUE str, long beg, long len)
3146{
3147 VALUE str2;
3148
3149 RUBY_ASSERT(beg >= 0);
3150 RUBY_ASSERT(len >= 0);
3151 RUBY_ASSERT(beg+len <= RSTRING_LEN(str));
3152
3153 const int termlen = TERM_LEN(str);
3154 if (!SHARABLE_SUBSTRING_P(beg, len, RSTRING_LEN(str))) {
3155 str2 = rb_enc_str_new(RSTRING_PTR(str) + beg, len, STR_ENC_GET(str));
3156 RB_GC_GUARD(str);
3157 return str2;
3158 }
3159
3160 str2 = str_alloc_heap(rb_cString);
3161 if (str_embed_capa(str2) >= len + termlen) {
3162 char *ptr2 = RSTRING(str2)->as.embed.ary;
3163 STR_SET_EMBED(str2);
3164 memcpy(ptr2, RSTRING_PTR(str) + beg, len);
3165 TERM_FILL(ptr2+len, termlen);
3166
3167 STR_SET_LEN(str2, len);
3168 RB_GC_GUARD(str);
3169 }
3170 else {
3171 str_replace_shared(str2, str);
3172 RUBY_ASSERT(!STR_EMBED_P(str2));
3173 ENC_CODERANGE_CLEAR(str2);
3174 RSTRING(str2)->as.heap.ptr += beg;
3175 if (RSTRING_LEN(str2) > len) {
3176 STR_SET_LEN(str2, len);
3177 }
3178 }
3179
3180 return str2;
3181}
3182
3183VALUE
3184rb_str_subseq(VALUE str, long beg, long len)
3185{
3186 VALUE str2 = str_subseq(str, beg, len);
3187 rb_enc_cr_str_copy_for_substr(str2, str);
3188 return str2;
3189}
3190
3191char *
3192rb_str_subpos(VALUE str, long beg, long *lenp)
3193{
3194 long len = *lenp;
3195 long slen = -1L;
3196 const long blen = RSTRING_LEN(str);
3197 rb_encoding *enc = STR_ENC_GET(str);
3198 char *p, *s = RSTRING_PTR(str), *e = s + blen;
3199
3200 if (len < 0) return 0;
3201 if (beg == LONG_MIN) return 0;
3202 if (!blen) {
3203 len = 0;
3204 }
3205 if (single_byte_optimizable(str)) {
3206 if (beg > blen) return 0;
3207 if (beg < 0) {
3208 beg += blen;
3209 if (beg < 0) return 0;
3210 }
3211 if (len > blen - beg)
3212 len = blen - beg;
3213 if (len < 0) return 0;
3214 p = s + beg;
3215 goto end;
3216 }
3217 if (beg < 0) {
3218 if (len > -beg) len = -beg;
3219 if ((ENC_CODERANGE(str) == ENC_CODERANGE_VALID) &&
3220 (-beg * rb_enc_mbmaxlen(enc) < blen / 8)) {
3221 beg = -beg;
3222 while (beg-- > len && (e = rb_enc_prev_char(s, e, e, enc)) != 0);
3223 p = e;
3224 if (!p) return 0;
3225 while (len-- > 0 && (p = rb_enc_prev_char(s, p, e, enc)) != 0);
3226 if (!p) return 0;
3227 len = e - p;
3228 goto end;
3229 }
3230 else {
3231 slen = str_strlen(str, enc);
3232 beg += slen;
3233 if (beg < 0) return 0;
3234 p = s + beg;
3235 if (len == 0) goto end;
3236 }
3237 }
3238 else if (beg > 0 && beg > blen) {
3239 return 0;
3240 }
3241 if (len == 0) {
3242 if (beg > str_strlen(str, enc)) return 0; /* str's enc */
3243 p = s + beg;
3244 }
3245#ifdef NONASCII_MASK
3246 else if (ENC_CODERANGE(str) == ENC_CODERANGE_VALID &&
3247 enc == rb_utf8_encoding()) {
3248 p = str_utf8_nth(s, e, &beg);
3249 if (beg > 0) return 0;
3250 len = str_utf8_offset(p, e, len);
3251 }
3252#endif
3253 else if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) {
3254 int char_sz = rb_enc_mbmaxlen(enc);
3255
3256 p = s + beg * char_sz;
3257 if (p > e) {
3258 return 0;
3259 }
3260 else if (len * char_sz > e - p)
3261 len = e - p;
3262 else
3263 len *= char_sz;
3264 }
3265 else if ((p = str_nth_len(s, e, &beg, enc)) == e) {
3266 if (beg > 0) return 0;
3267 len = 0;
3268 }
3269 else {
3270 len = str_offset(p, e, len, enc, 0);
3271 }
3272 end:
3273 *lenp = len;
3274 RB_GC_GUARD(str);
3275 return p;
3276}
3277
3278static VALUE str_substr(VALUE str, long beg, long len, int empty);
3279
3280VALUE
3281rb_str_substr(VALUE str, long beg, long len)
3282{
3283 return str_substr(str, beg, len, TRUE);
3284}
3285
3286VALUE
3287rb_str_substr_two_fixnums(VALUE str, VALUE beg, VALUE len, int empty)
3288{
3289 return str_substr(str, NUM2LONG(beg), NUM2LONG(len), empty);
3290}
3291
3292static VALUE
3293str_substr(VALUE str, long beg, long len, int empty)
3294{
3295 char *p = rb_str_subpos(str, beg, &len);
3296
3297 if (!p) return Qnil;
3298 if (!len && !empty) return Qnil;
3299
3300 beg = p - RSTRING_PTR(str);
3301
3302 VALUE str2 = str_subseq(str, beg, len);
3303 rb_enc_cr_str_copy_for_substr(str2, str);
3304 return str2;
3305}
3306
3307/* :nodoc: */
3308VALUE
3310{
3311 if (CHILLED_STRING_P(str)) {
3312 FL_UNSET_RAW(str, STR_CHILLED);
3313 }
3314
3315 if (OBJ_FROZEN(str)) return str;
3316 rb_str_resize(str, RSTRING_LEN(str));
3317 return rb_obj_freeze(str);
3318}
3319
3320/*
3321 * call-seq:
3322 * +string -> new_string or self
3323 *
3324 * Returns +self+ if +self+ is not frozen and can be mutated
3325 * without warning issuance.
3326 *
3327 * Otherwise returns <tt>self.dup</tt>, which is not frozen.
3328 *
3329 * Related: see {Freezing/Unfreezing}[rdoc-ref:String@Freezing-2FUnfreezing].
3330 */
3331static VALUE
3332str_uplus(VALUE str)
3333{
3334 if (OBJ_FROZEN(str) || CHILLED_STRING_P(str)) {
3335 return rb_str_dup(str);
3336 }
3337 else {
3338 return str;
3339 }
3340}
3341
3342/*
3343 * call-seq:
3344 * -self -> frozen_string
3345 *
3346 * Returns a frozen string equal to +self+.
3347 *
3348 * The returned string is +self+ if and only if all of the following are true:
3349 *
3350 * - +self+ is already frozen.
3351 * - +self+ is an instance of \String (rather than of a subclass of \String)
3352 * - +self+ has no instance variables set on it.
3353 *
3354 * Otherwise, the returned string is a frozen copy of +self+.
3355 *
3356 * Returning +self+, when possible, saves duplicating +self+;
3357 * see {Data deduplication}[https://en.wikipedia.org/wiki/Data_deduplication].
3358 *
3359 * It may also save duplicating other, already-existing, strings:
3360 *
3361 * s0 = 'foo'
3362 * s1 = 'foo'
3363 * s0.object_id == s1.object_id # => false
3364 * (-s0).object_id == (-s1).object_id # => true
3365 *
3366 * Note that method #-@ is convenient for defining a constant:
3367 *
3368 * FileName = -'config/database.yml'
3369 *
3370 * While its alias #dedup is better suited for chaining:
3371 *
3372 * 'foo'.dedup.gsub!('o')
3373 *
3374 * Related: see {Freezing/Unfreezing}[rdoc-ref:String@Freezing-2FUnfreezing].
3375 */
3376static VALUE
3377str_uminus(VALUE str)
3378{
3379 if (!BARE_STRING_P(str) && !rb_obj_frozen_p(str)) {
3380 str = rb_str_dup(str);
3381 }
3382 return rb_fstring(str);
3383}
3384
3385RUBY_ALIAS_FUNCTION(rb_str_dup_frozen(VALUE str), rb_str_new_frozen, (str))
3386#define rb_str_dup_frozen rb_str_new_frozen
3387
3388VALUE
3390{
3391 rb_check_frozen(str);
3392 if (FL_TEST(str, STR_TMPLOCK)) {
3393 rb_raise(rb_eRuntimeError, "temporal locking already locked string");
3394 }
3395 FL_SET(str, STR_TMPLOCK);
3396 return str;
3397}
3398
3399VALUE
3401{
3402 rb_check_frozen(str);
3403 if (!FL_TEST(str, STR_TMPLOCK)) {
3404 rb_raise(rb_eRuntimeError, "temporal unlocking already unlocked string");
3405 }
3406 FL_UNSET(str, STR_TMPLOCK);
3407 return str;
3408}
3409
3410VALUE
3411rb_str_locktmp_ensure(VALUE str, VALUE (*func)(VALUE), VALUE arg)
3412{
3413 rb_str_locktmp(str);
3414 return rb_ensure(func, arg, rb_str_unlocktmp, str);
3415}
3416
3417void
3419{
3420 RUBY_ASSERT(ruby_thread_has_gvl_p());
3421
3422 long capa;
3423 const int termlen = TERM_LEN(str);
3424
3425 str_modifiable(str);
3426 if (STR_SHARED_P(str)) {
3427 rb_raise(rb_eRuntimeError, "can't set length of shared string");
3428 }
3429 if (len > (capa = (long)str_capacity(str, termlen)) || len < 0) {
3430 rb_bug("probable buffer overflow: %ld for %ld", len, capa);
3431 }
3432
3433 int cr = ENC_CODERANGE(str);
3434 if (len == 0) {
3435 /* Empty string does not contain non-ASCII */
3437 }
3438 else if (cr == ENC_CODERANGE_UNKNOWN) {
3439 /* Leave unknown. */
3440 }
3441 else if (len > RSTRING_LEN(str)) {
3442 if (ENC_CODERANGE_CLEAN_P(cr)) {
3443 /* Update the coderange regarding the extended part. */
3444 const char *const prev_end = RSTRING_END(str);
3445 const char *const new_end = RSTRING_PTR(str) + len;
3446 rb_encoding *enc = rb_enc_get(str);
3447 rb_str_coderange_scan_restartable(prev_end, new_end, enc, &cr);
3448 ENC_CODERANGE_SET(str, cr);
3449 }
3450 else if (cr == ENC_CODERANGE_BROKEN) {
3451 /* May be valid now, by appended part. */
3453 }
3454 }
3455 else if (len < RSTRING_LEN(str)) {
3456 if (cr != ENC_CODERANGE_7BIT) {
3457 /* ASCII-only string is keeping after truncated. Valid
3458 * and broken may be invalid or valid, leave unknown. */
3460 }
3461 }
3462
3463 STR_SET_LEN(str, len);
3464 TERM_FILL(&RSTRING_PTR(str)[len], termlen);
3465}
3466
3467VALUE
3468rb_str_resize(VALUE str, long len)
3469{
3470 if (len < 0) {
3471 rb_raise(rb_eArgError, "negative string size (or size too big)");
3472 }
3473
3474 int independent = str_independent(str);
3475 long slen = RSTRING_LEN(str);
3476 const int termlen = TERM_LEN(str);
3477 if (len > LONG_MAX - termlen) {
3478 rb_raise(rb_eArgError, "string size too big");
3479 }
3480
3481 if (slen > len || (termlen != 1 && slen < len)) {
3483 }
3484
3485 {
3486 long capa;
3487 if (STR_EMBED_P(str)) {
3488 if (len == slen) return str;
3489 if (str_embed_capa(str) >= len + termlen) {
3490 STR_SET_LEN(str, len);
3491 TERM_FILL(RSTRING(str)->as.embed.ary + len, termlen);
3492 return str;
3493 }
3494 str_make_independent_expand(str, slen, len - slen, termlen);
3495 }
3496 else if (str_embed_capa(str) >= len + termlen) {
3497 char *ptr = STR_HEAP_PTR(str);
3498 STR_SET_EMBED(str);
3499 if (slen > len) slen = len;
3500 if (slen > 0) MEMCPY(RSTRING(str)->as.embed.ary, ptr, char, slen);
3501 TERM_FILL(RSTRING(str)->as.embed.ary + len, termlen);
3502 STR_SET_LEN(str, len);
3503 if (independent) ruby_xfree(ptr);
3504 return str;
3505 }
3506 else if (!independent) {
3507 if (len == slen) return str;
3508 str_make_independent_expand(str, slen, len - slen, termlen);
3509 }
3510 else if ((capa = RSTRING(str)->as.heap.aux.capa) < len ||
3511 (capa - len) > (len < 1024 ? len : 1024)) {
3512 SIZED_REALLOC_N(RSTRING(str)->as.heap.ptr, char,
3513 (size_t)len + termlen, STR_HEAP_SIZE(str));
3514 RSTRING(str)->as.heap.aux.capa = len;
3515 }
3516 else if (len == slen) return str;
3517 STR_SET_LEN(str, len);
3518 TERM_FILL(RSTRING(str)->as.heap.ptr + len, termlen); /* sentinel */
3519 }
3520 return str;
3521}
3522
3523static void
3524str_ensure_available_capa(VALUE str, long len)
3525{
3526 str_modify_keep_cr(str);
3527
3528 const int termlen = TERM_LEN(str);
3529 long olen = RSTRING_LEN(str);
3530
3531 if (RB_UNLIKELY(olen > LONG_MAX - len)) {
3532 rb_raise(rb_eArgError, "string sizes too big");
3533 }
3534
3535 long total = olen + len;
3536 long capa = str_capacity(str, termlen);
3537
3538 if (capa < total) {
3539 if (total >= LONG_MAX / 2) {
3540 capa = total;
3541 }
3542 while (total > capa) {
3543 capa = 2 * capa + termlen; /* == 2*(capa+termlen)-termlen */
3544 }
3545 RESIZE_CAPA_TERM(str, capa, termlen);
3546 }
3547}
3548
3549static VALUE
3550str_buf_cat4(VALUE str, const char *ptr, long len, bool keep_cr)
3551{
3552 if (keep_cr) {
3553 str_modify_keep_cr(str);
3554 }
3555 else {
3556 rb_str_modify(str);
3557 }
3558 if (len == 0) return 0;
3559
3560 long total, olen, off = -1;
3561 char *sptr;
3562 const int termlen = TERM_LEN(str);
3563
3564 RSTRING_GETMEM(str, sptr, olen);
3565 if (ptr >= sptr && ptr <= sptr + olen) {
3566 off = ptr - sptr;
3567 }
3568
3569 long capa = str_capacity(str, termlen);
3570
3571 if (olen > LONG_MAX - len) {
3572 rb_raise(rb_eArgError, "string sizes too big");
3573 }
3574 total = olen + len;
3575 if (capa < total) {
3576 if (total >= LONG_MAX / 2) {
3577 capa = total;
3578 }
3579 while (total > capa) {
3580 capa = 2 * capa + termlen; /* == 2*(capa+termlen)-termlen */
3581 }
3582 RESIZE_CAPA_TERM(str, capa, termlen);
3583 sptr = RSTRING_PTR(str);
3584 }
3585 if (off != -1) {
3586 ptr = sptr + off;
3587 }
3588 memcpy(sptr + olen, ptr, len);
3589 STR_SET_LEN(str, total);
3590 TERM_FILL(sptr + total, termlen); /* sentinel */
3591
3592 return str;
3593}
3594
3595#define str_buf_cat(str, ptr, len) str_buf_cat4((str), (ptr), len, false)
3596#define str_buf_cat2(str, ptr) str_buf_cat4((str), (ptr), rb_strlen_lit(ptr), false)
3597
3598VALUE
3599rb_str_cat(VALUE str, const char *ptr, long len)
3600{
3601 if (len == 0) return str;
3602 if (len < 0) {
3603 rb_raise(rb_eArgError, "negative string size (or size too big)");
3604 }
3605 return str_buf_cat(str, ptr, len);
3606}
3607
3608VALUE
3609rb_str_cat_cstr(VALUE str, const char *ptr)
3610{
3611 must_not_null(ptr);
3612 return rb_str_buf_cat(str, ptr, strlen(ptr));
3613}
3614
3615static void
3616rb_str_buf_cat_byte(VALUE str, unsigned char byte)
3617{
3618 RUBY_ASSERT(RB_ENCODING_GET_INLINED(str) == ENCINDEX_ASCII_8BIT || RB_ENCODING_GET_INLINED(str) == ENCINDEX_US_ASCII);
3619
3620 // We can't write directly to shared strings without impacting others, so we must make the string independent.
3621 if (UNLIKELY(!str_independent(str))) {
3622 str_make_independent(str);
3623 }
3624
3625 long string_length = -1;
3626 const int null_terminator_length = 1;
3627 char *sptr;
3628 RSTRING_GETMEM(str, sptr, string_length);
3629
3630 // Ensure the resulting string wouldn't be too long.
3631 if (UNLIKELY(string_length > LONG_MAX - 1)) {
3632 rb_raise(rb_eArgError, "string sizes too big");
3633 }
3634
3635 long string_capacity = str_capacity(str, null_terminator_length);
3636
3637 // Get the code range before any modifications since those might clear the code range.
3638 int cr = ENC_CODERANGE(str);
3639
3640 // Check if the string has spare string_capacity to write the new byte.
3641 if (LIKELY(string_capacity >= string_length + 1)) {
3642 // In fast path we can write the new byte and note the string's new length.
3643 sptr[string_length] = byte;
3644 STR_SET_LEN(str, string_length + 1);
3645 TERM_FILL(sptr + string_length + 1, null_terminator_length);
3646 }
3647 else {
3648 // If there's not enough string_capacity, make a call into the general string concatenation function.
3649 str_buf_cat(str, (char *)&byte, 1);
3650 }
3651
3652 // If the code range is already known, we can derive the resulting code range cheaply by looking at the byte we
3653 // just appended. If the code range is unknown, but the string was empty, then we can also derive the code range
3654 // by looking at the byte we just appended. Otherwise, we'd have to scan the bytes to determine the code range so
3655 // we leave it as unknown. It cannot be broken for binary strings so we don't need to handle that option.
3656 if (cr == ENC_CODERANGE_7BIT || string_length == 0) {
3657 if (ISASCII(byte)) {
3659 }
3660 else {
3662
3663 // Promote a US-ASCII string to ASCII-8BIT when a non-ASCII byte is appended.
3664 if (UNLIKELY(RB_ENCODING_GET_INLINED(str) == ENCINDEX_US_ASCII)) {
3665 rb_enc_associate_index(str, ENCINDEX_ASCII_8BIT);
3666 }
3667 }
3668 }
3669}
3670
3671RUBY_ALIAS_FUNCTION(rb_str_buf_cat(VALUE str, const char *ptr, long len), rb_str_cat, (str, ptr, len))
3672RUBY_ALIAS_FUNCTION(rb_str_buf_cat2(VALUE str, const char *ptr), rb_str_cat_cstr, (str, ptr))
3673RUBY_ALIAS_FUNCTION(rb_str_cat2(VALUE str, const char *ptr), rb_str_cat_cstr, (str, ptr))
3674
3675static VALUE
3676rb_enc_cr_str_buf_cat(VALUE str, const char *ptr, long len,
3677 int ptr_encindex, int ptr_cr, int *ptr_cr_ret)
3678{
3679 int str_encindex = ENCODING_GET(str);
3680 int res_encindex;
3681 int str_cr, res_cr;
3682 rb_encoding *str_enc, *ptr_enc;
3683
3684 str_cr = RSTRING_LEN(str) ? ENC_CODERANGE(str) : ENC_CODERANGE_7BIT;
3685
3686 if (str_encindex == ptr_encindex) {
3687 if (str_cr != ENC_CODERANGE_UNKNOWN && ptr_cr == ENC_CODERANGE_UNKNOWN) {
3688 ptr_cr = coderange_scan(ptr, len, rb_enc_from_index(ptr_encindex));
3689 }
3690 }
3691 else {
3692 str_enc = rb_enc_from_index(str_encindex);
3693 ptr_enc = rb_enc_from_index(ptr_encindex);
3694 if (!rb_enc_asciicompat(str_enc) || !rb_enc_asciicompat(ptr_enc)) {
3695 if (len == 0)
3696 return str;
3697 if (RSTRING_LEN(str) == 0) {
3698 rb_str_buf_cat(str, ptr, len);
3699 ENCODING_CODERANGE_SET(str, ptr_encindex, ptr_cr);
3700 rb_str_change_terminator_length(str, rb_enc_mbminlen(str_enc), rb_enc_mbminlen(ptr_enc));
3701 return str;
3702 }
3703 goto incompatible;
3704 }
3705 if (ptr_cr == ENC_CODERANGE_UNKNOWN) {
3706 ptr_cr = coderange_scan(ptr, len, ptr_enc);
3707 }
3708 if (str_cr == ENC_CODERANGE_UNKNOWN) {
3709 if (ENCODING_IS_ASCII8BIT(str) || ptr_cr != ENC_CODERANGE_7BIT) {
3710 str_cr = rb_enc_str_coderange(str);
3711 }
3712 }
3713 }
3714 if (ptr_cr_ret)
3715 *ptr_cr_ret = ptr_cr;
3716
3717 if (str_encindex != ptr_encindex &&
3718 str_cr != ENC_CODERANGE_7BIT &&
3719 ptr_cr != ENC_CODERANGE_7BIT) {
3720 str_enc = rb_enc_from_index(str_encindex);
3721 ptr_enc = rb_enc_from_index(ptr_encindex);
3722 goto incompatible;
3723 }
3724
3725 if (str_cr == ENC_CODERANGE_UNKNOWN) {
3726 res_encindex = str_encindex;
3727 res_cr = ENC_CODERANGE_UNKNOWN;
3728 }
3729 else if (str_cr == ENC_CODERANGE_7BIT) {
3730 if (ptr_cr == ENC_CODERANGE_7BIT) {
3731 res_encindex = str_encindex;
3732 res_cr = ENC_CODERANGE_7BIT;
3733 }
3734 else {
3735 res_encindex = ptr_encindex;
3736 res_cr = ptr_cr;
3737 }
3738 }
3739 else if (str_cr == ENC_CODERANGE_VALID) {
3740 res_encindex = str_encindex;
3741 if (ENC_CODERANGE_CLEAN_P(ptr_cr))
3742 res_cr = str_cr;
3743 else
3744 res_cr = ptr_cr;
3745 }
3746 else { /* str_cr == ENC_CODERANGE_BROKEN */
3747 res_encindex = str_encindex;
3748 res_cr = str_cr;
3749 if (0 < len) res_cr = ENC_CODERANGE_UNKNOWN;
3750 }
3751
3752 if (len < 0) {
3753 rb_raise(rb_eArgError, "negative string size (or size too big)");
3754 }
3755 str_buf_cat(str, ptr, len);
3756 ENCODING_CODERANGE_SET(str, res_encindex, res_cr);
3757 return str;
3758
3759 incompatible:
3760 rb_raise(rb_eEncCompatError, "incompatible character encodings: %s and %s",
3761 rb_enc_inspect_name(str_enc), rb_enc_inspect_name(ptr_enc));
3763}
3764
3765VALUE
3766rb_enc_str_buf_cat(VALUE str, const char *ptr, long len, rb_encoding *ptr_enc)
3767{
3768 return rb_enc_cr_str_buf_cat(str, ptr, len,
3769 rb_enc_to_index(ptr_enc), ENC_CODERANGE_UNKNOWN, NULL);
3770}
3771
3772VALUE
3774{
3775 /* ptr must reference NUL terminated ASCII string. */
3776 int encindex = ENCODING_GET(str);
3777 rb_encoding *enc = rb_enc_from_index(encindex);
3778 if (rb_enc_asciicompat(enc)) {
3779 return rb_enc_cr_str_buf_cat(str, ptr, strlen(ptr),
3780 encindex, ENC_CODERANGE_7BIT, 0);
3781 }
3782 else {
3783 char *buf = ALLOCA_N(char, rb_enc_mbmaxlen(enc));
3784 while (*ptr) {
3785 unsigned int c = (unsigned char)*ptr;
3786 int len = rb_enc_codelen(c, enc);
3787 rb_enc_mbcput(c, buf, enc);
3788 rb_enc_cr_str_buf_cat(str, buf, len,
3789 encindex, ENC_CODERANGE_VALID, 0);
3790 ptr++;
3791 }
3792 return str;
3793 }
3794}
3795
3796VALUE
3798{
3799 str_modify_keep_cr(str);
3800
3801 int str2_cr = rb_enc_str_coderange(str2);
3802
3803 if (str_enc_fastpath(str)) {
3804 switch (str2_cr) {
3805 case ENC_CODERANGE_7BIT:
3806 // If RHS is 7bit we can do simple concatenation
3807 str_buf_cat4(str, RSTRING_PTR(str2), RSTRING_LEN(str2), true);
3808 RB_GC_GUARD(str2);
3809 return str;
3811 // If RHS is valid, we can do simple concatenation if encodings are the same
3812 if (ENCODING_GET_INLINED(str) == ENCODING_GET_INLINED(str2)) {
3813 str_buf_cat4(str, RSTRING_PTR(str2), RSTRING_LEN(str2), true);
3814 int str_cr = ENC_CODERANGE(str);
3815 if (UNLIKELY(str_cr != ENC_CODERANGE_VALID)) {
3816 ENC_CODERANGE_SET(str, RB_ENC_CODERANGE_AND(str_cr, str2_cr));
3817 }
3818 RB_GC_GUARD(str2);
3819 return str;
3820 }
3821 }
3822 }
3823
3824 // NULL: str2's coderange is already cached
3825 rb_enc_cr_str_buf_cat(str, RSTRING_PTR(str2), RSTRING_LEN(str2),
3826 ENCODING_GET(str2), str2_cr, NULL);
3827 RB_GC_GUARD(str2);
3828
3829 return str;
3830}
3831
3832VALUE
3834{
3835 StringValue(str2);
3836 return rb_str_buf_append(str, str2);
3837}
3838
3839VALUE
3840rb_str_concat_literals(size_t num, const VALUE *strary)
3841{
3842 VALUE str;
3843 size_t i, s = 0;
3844 unsigned long len = 1;
3845
3846 if (UNLIKELY(!num)) return rb_str_new(0, 0);
3847 if (UNLIKELY(num == 1)) return rb_str_resurrect(strary[0]);
3848
3849 for (i = 0; i < num; ++i) { len += RSTRING_LEN(strary[i]); }
3850 str = rb_str_buf_new(len);
3851 rb_enc_associate_index(str, ENCODING_GET(strary[0]));
3852
3853 for (i = s; i < num; ++i) {
3854 const VALUE v = strary[i];
3855 int encidx = ENCODING_GET(v);
3856
3857 rb_str_buf_append(str, v);
3858 if (encidx != ENCINDEX_US_ASCII) {
3859 if (ENCODING_GET_INLINED(str) == ENCINDEX_US_ASCII)
3860 rb_enc_associate_index(str, encidx);
3861 }
3862 }
3863 return str;
3864}
3865
3866/*
3867 * call-seq:
3868 * concat(*objects) -> string
3869 *
3870 * :include: doc/string/concat.rdoc
3871 */
3872static VALUE
3873rb_str_concat_multi(int argc, VALUE *argv, VALUE str)
3874{
3875 str_modifiable(str);
3876
3877 if (argc == 1) {
3878 return rb_str_concat(str, argv[0]);
3879 }
3880 else if (argc > 1) {
3881 int i;
3882 VALUE arg_str = rb_str_tmp_new(0);
3883 rb_enc_copy(arg_str, str);
3884 for (i = 0; i < argc; i++) {
3885 rb_str_concat(arg_str, argv[i]);
3886 }
3887 rb_str_buf_append(str, arg_str);
3888 }
3889
3890 return str;
3891}
3892
3893/*
3894 * call-seq:
3895 * append_as_bytes(*objects) -> self
3896 *
3897 * Concatenates each object in +objects+ into +self+; returns +self+;
3898 * performs no encoding validation or conversion:
3899 *
3900 * s = 'foo'
3901 * s.append_as_bytes(" \xE2\x82") # => "foo \xE2\x82"
3902 * s.valid_encoding? # => false
3903 * s.append_as_bytes("\xAC 12")
3904 * s.valid_encoding? # => true
3905 *
3906 * When a given object is an integer,
3907 * the value is considered an 8-bit byte;
3908 * if the integer occupies more than one byte (i.e,. is greater than 255),
3909 * appends only the low-order byte (similar to String#setbyte):
3910 *
3911 * s = ""
3912 * s.append_as_bytes(0, 257) # => "\u0000\u0001"
3913 * s.bytesize # => 2
3914 *
3915 * Related: see {Modifying}[rdoc-ref:String@Modifying].
3916 */
3917
3918VALUE
3919rb_str_append_as_bytes(int argc, VALUE *argv, VALUE str)
3920{
3921 long needed_capacity = 0;
3922 volatile VALUE t0;
3923
3924 str_modify_keep_cr(str);
3925
3926 enum ruby_value_type *types = ALLOCV_N(enum ruby_value_type, t0, argc);
3927
3928 for (int index = 0; index < argc; index++) {
3929 VALUE obj = argv[index];
3930 enum ruby_value_type type = types[index] = rb_type(obj);
3931 switch (type) {
3932 case T_FIXNUM:
3933 case T_BIGNUM:
3934 needed_capacity++;
3935 break;
3936 case T_STRING:
3937 needed_capacity += RSTRING_LEN(obj);
3938 break;
3939 default:
3940 rb_raise(
3942 "wrong argument type %"PRIsVALUE" (expected String or Integer)",
3943 rb_obj_class(obj)
3944 );
3945 break;
3946 }
3947 }
3948
3949 str_ensure_available_capa(str, needed_capacity);
3950 char *sptr = RSTRING_END(str);
3951
3952 for (int index = 0; index < argc; index++) {
3953 VALUE obj = argv[index];
3954 enum ruby_value_type type = types[index];
3955 switch (type) {
3956 case T_FIXNUM:
3957 case T_BIGNUM: {
3958 argv[index] = obj = rb_int_and(obj, INT2FIX(0xff));
3959 char byte = (char)(NUM2INT(obj) & 0xFF);
3960 *sptr = byte;
3961 sptr++;
3962 break;
3963 }
3964 case T_STRING: {
3965 const char *ptr;
3966 long len;
3967 RSTRING_GETMEM(obj, ptr, len);
3968 memcpy(sptr, ptr, len);
3969 sptr += len;
3970 break;
3971 }
3972 default:
3973 rb_bug("append_as_bytes arguments should have been validated");
3974 }
3975 }
3976
3977 STR_SET_LEN(str, RSTRING_LEN(str) + needed_capacity);
3978 TERM_FILL(sptr, TERM_LEN(str)); /* sentinel */
3979
3980 int cr = ENC_CODERANGE(str);
3981 switch (cr) {
3982 case ENC_CODERANGE_7BIT: {
3983 for (int index = 0; index < argc; index++) {
3984 VALUE obj = argv[index];
3985 enum ruby_value_type type = types[index];
3986 switch (type) {
3987 case T_FIXNUM:
3988 case T_BIGNUM: {
3989 if (!ISASCII(NUM2INT(obj))) {
3990 goto clear_cr;
3991 }
3992 break;
3993 }
3994 case T_STRING: {
3995 if (ENC_CODERANGE(obj) != ENC_CODERANGE_7BIT) {
3996 goto clear_cr;
3997 }
3998 break;
3999 }
4000 default:
4001 rb_bug("append_as_bytes arguments should have been validated");
4002 }
4003 }
4004 break;
4005 }
4007 if (ENCODING_GET_INLINED(str) == ENCINDEX_ASCII_8BIT) {
4008 goto keep_cr;
4009 }
4010 else {
4011 goto clear_cr;
4012 }
4013 break;
4014 default:
4015 goto clear_cr;
4016 break;
4017 }
4018
4019 RB_GC_GUARD(t0);
4020
4021 clear_cr:
4022 // If no fast path was hit, we clear the coderange.
4023 // append_as_bytes is predominantly meant to be used in
4024 // buffering situation, hence it's likely the coderange
4025 // will never be scanned, so it's not worth spending time
4026 // precomputing the coderange except for simple and common
4027 // situations.
4029 keep_cr:
4030 return str;
4031}
4032
4033/*
4034 * call-seq:
4035 * self << object -> self
4036 *
4037 * Appends a string representation of +object+ to +self+;
4038 * returns +self+.
4039 *
4040 * If +object+ is a string, appends it to +self+:
4041 *
4042 * s = 'foo'
4043 * s << 'bar' # => "foobar"
4044 * s # => "foobar"
4045 *
4046 * If +object+ is an integer,
4047 * its value is considered a codepoint;
4048 * converts the value to a character before concatenating:
4049 *
4050 * s = 'foo'
4051 * s << 33 # => "foo!"
4052 *
4053 * Additionally, if the codepoint is in range <tt>0..0xff</tt>
4054 * and the encoding of +self+ is Encoding::US_ASCII,
4055 * changes the encoding to Encoding::ASCII_8BIT:
4056 *
4057 * s = 'foo'.encode(Encoding::US_ASCII)
4058 * s.encoding # => #<Encoding:US-ASCII>
4059 * s << 0xff # => "foo\xFF"
4060 * s.encoding # => #<Encoding:BINARY (ASCII-8BIT)>
4061 *
4062 * Raises RangeError if that codepoint is not representable in the encoding of +self+:
4063 *
4064 * s = 'foo'
4065 * s.encoding # => <Encoding:UTF-8>
4066 * s << 0x00110000 # 1114112 out of char range (RangeError)
4067 * s = 'foo'.encode(Encoding::EUC_JP)
4068 * s << 0x00800080 # invalid codepoint 0x800080 in EUC-JP (RangeError)
4069 *
4070 * Related: see {Modifying}[rdoc-ref:String@Modifying].
4071 */
4072VALUE
4074{
4075 unsigned int code;
4076 rb_encoding *enc;
4077 int encidx;
4078
4079 /* For a chilled string this emits the deprecation warning, which may run
4080 * Ruby code (Warning.warn) that modifies str1. Do it before reading the
4081 * encoding, length and coderange used by the codepoint path below. */
4082 str_modify_keep_cr(str1);
4083 enc = STR_ENC_GET(str1);
4084
4085 if (RB_INTEGER_TYPE_P(str2)) {
4086 if (rb_num_to_uint(str2, &code) == 0) {
4087 }
4088 else if (FIXNUM_P(str2)) {
4089 rb_raise(rb_eRangeError, "%ld out of char range", FIX2LONG(str2));
4090 }
4091 else {
4092 rb_raise(rb_eRangeError, "bignum out of char range");
4093 }
4094 }
4095 else {
4096 return rb_str_append(str1, str2);
4097 }
4098
4099 encidx = rb_ascii8bit_appendable_encoding_index(enc, code);
4100
4101 if (encidx >= 0) {
4102 rb_str_buf_cat_byte(str1, (unsigned char)code);
4103 }
4104 else {
4105 long pos = RSTRING_LEN(str1);
4106 int cr = ENC_CODERANGE(str1);
4107 int len;
4108 char *buf;
4109
4110 switch (len = rb_enc_codelen(code, enc)) {
4111 case ONIGERR_INVALID_CODE_POINT_VALUE:
4112 rb_raise(rb_eRangeError, "invalid codepoint 0x%X in %s", code, rb_enc_name(enc));
4113 break;
4114 case ONIGERR_TOO_BIG_WIDE_CHAR_VALUE:
4115 case 0:
4116 rb_raise(rb_eRangeError, "%u out of char range", code);
4117 break;
4118 }
4119 buf = ALLOCA_N(char, len + 1);
4120 rb_enc_mbcput(code, buf, enc);
4121 if (rb_enc_precise_mbclen(buf, buf + len + 1, enc) != len) {
4122 rb_raise(rb_eRangeError, "invalid codepoint 0x%X in %s", code, rb_enc_name(enc));
4123 }
4124 rb_str_resize(str1, pos+len);
4125 memcpy(RSTRING_PTR(str1) + pos, buf, len);
4126 if (cr == ENC_CODERANGE_7BIT && code > 127) {
4128 }
4129 else if (cr == ENC_CODERANGE_BROKEN) {
4131 }
4132 ENC_CODERANGE_SET(str1, cr);
4133 }
4134 return str1;
4135}
4136
4137int
4138rb_ascii8bit_appendable_encoding_index(rb_encoding *enc, unsigned int code)
4139{
4140 int encidx = rb_enc_to_index(enc);
4141
4142 if (encidx == ENCINDEX_ASCII_8BIT || encidx == ENCINDEX_US_ASCII) {
4143 /* US-ASCII automatically extended to ASCII-8BIT */
4144 if (code > 0xFF) {
4145 rb_raise(rb_eRangeError, "%u out of char range", code);
4146 }
4147 if (encidx == ENCINDEX_US_ASCII && code > 127) {
4148 return ENCINDEX_ASCII_8BIT;
4149 }
4150 return encidx;
4151 }
4152 else {
4153 return -1;
4154 }
4155}
4156
4157/*
4158 * call-seq:
4159 * prepend(*other_strings) -> new_string
4160 *
4161 * Prefixes to +self+ the concatenation of the given +other_strings+; returns +self+:
4162 *
4163 * 'baz'.prepend('foo', 'bar') # => "foobarbaz"
4164 *
4165 * Related: see {Modifying}[rdoc-ref:String@Modifying].
4166 *
4167 */
4168
4169static VALUE
4170rb_str_prepend_multi(int argc, VALUE *argv, VALUE str)
4171{
4172 str_modifiable(str);
4173
4174 if (argc == 1) {
4175 rb_str_update(str, 0L, 0L, argv[0]);
4176 }
4177 else if (argc > 1) {
4178 int i;
4179 VALUE arg_str = rb_str_tmp_new(0);
4180 rb_enc_copy(arg_str, str);
4181 for (i = 0; i < argc; i++) {
4182 rb_str_append(arg_str, argv[i]);
4183 }
4184 rb_str_update(str, 0L, 0L, arg_str);
4185 }
4186
4187 return str;
4188}
4189
4190st_index_t
4192{
4193 if (FL_TEST_RAW(str, STR_PRECOMPUTED_HASH)) {
4194 st_index_t precomputed_hash;
4195 memcpy(&precomputed_hash, RSTRING_END(str) + TERM_LEN(str), sizeof(precomputed_hash));
4196
4197 RUBY_ASSERT(precomputed_hash == str_do_hash(str));
4198 return precomputed_hash;
4199 }
4200
4201 return str_do_hash(str);
4202}
4203
4204int
4206{
4207 long len1, len2;
4208 const char *ptr1, *ptr2;
4209 RSTRING_GETMEM(str1, ptr1, len1);
4210 RSTRING_GETMEM(str2, ptr2, len2);
4211 return (len1 != len2 ||
4212 !rb_str_comparable(str1, str2) ||
4213 memcmp(ptr1, ptr2, len1) != 0);
4214}
4215
4216/*
4217 * call-seq:
4218 * hash -> integer
4219 *
4220 * :include: doc/string/hash.rdoc
4221 *
4222 */
4223
4224static VALUE
4225rb_str_hash_m(VALUE str)
4226{
4227 st_index_t hval = rb_str_hash(str);
4228 return ST2FIX(hval);
4229}
4230
4231#define lesser(a,b) (((a)>(b))?(b):(a))
4232
4233int
4235{
4236 int idx1, idx2;
4237 int rc1, rc2;
4238
4239 if (RSTRING_LEN(str1) == 0) return TRUE;
4240 if (RSTRING_LEN(str2) == 0) return TRUE;
4241 idx1 = ENCODING_GET(str1);
4242 idx2 = ENCODING_GET(str2);
4243 if (idx1 == idx2) return TRUE;
4244 rc1 = rb_enc_str_coderange(str1);
4245 rc2 = rb_enc_str_coderange(str2);
4246 if (rc1 == ENC_CODERANGE_7BIT) {
4247 if (rc2 == ENC_CODERANGE_7BIT) return TRUE;
4248 if (rb_enc_asciicompat(rb_enc_from_index(idx2)))
4249 return TRUE;
4250 }
4251 if (rc2 == ENC_CODERANGE_7BIT) {
4252 if (rb_enc_asciicompat(rb_enc_from_index(idx1)))
4253 return TRUE;
4254 }
4255 return FALSE;
4256}
4257
4258int
4260{
4261 long len1, len2;
4262 const char *ptr1, *ptr2;
4263 int retval;
4264
4265 if (str1 == str2) return 0;
4266 RSTRING_GETMEM(str1, ptr1, len1);
4267 RSTRING_GETMEM(str2, ptr2, len2);
4268 if (ptr1 == ptr2 || (retval = memcmp(ptr1, ptr2, lesser(len1, len2))) == 0) {
4269 if (len1 == len2) {
4270 if (!rb_str_comparable(str1, str2)) {
4271 if (ENCODING_GET(str1) > ENCODING_GET(str2))
4272 return 1;
4273 return -1;
4274 }
4275 return 0;
4276 }
4277 if (len1 > len2) return 1;
4278 return -1;
4279 }
4280 if (retval > 0) return 1;
4281 return -1;
4282}
4283
4284/*
4285 * call-seq:
4286 * self == object -> true or false
4287 *
4288 * Returns whether +object+ is equal to +self+.
4289 *
4290 * When +object+ is a string, returns whether +object+ has the same length and content as +self+:
4291 *
4292 * s = 'foo'
4293 * s == 'foo' # => true
4294 * s == 'food' # => false
4295 * s == 'FOO' # => false
4296 *
4297 * Returns +false+ if the two strings' encodings are not compatible:
4298 *
4299 * "\u{e4 f6 fc}".encode(Encoding::ISO_8859_1) == ("\u{c4 d6 dc}") # => false
4300 *
4301 * When +object+ is not a string:
4302 *
4303 * - If +object+ responds to method <tt>to_str</tt>,
4304 * <tt>object == self</tt> is called and its return value is returned.
4305 * - If +object+ does not respond to <tt>to_str</tt>,
4306 * +false+ is returned.
4307 *
4308 * Related: {Comparing}[rdoc-ref:String@Comparing].
4309 */
4310
4311VALUE
4313{
4314 if (str1 == str2) return Qtrue;
4315 if (!RB_TYPE_P(str2, T_STRING)) {
4316 if (!rb_respond_to(str2, idTo_str)) {
4317 return Qfalse;
4318 }
4319 return rb_equal(str2, str1);
4320 }
4321 return rb_str_eql_internal(str1, str2);
4322}
4323
4324/*
4325 * call-seq:
4326 * eql?(object) -> true or false
4327 *
4328 * :include: doc/string/eql_p.rdoc
4329 *
4330 */
4331
4332VALUE
4333rb_str_eql(VALUE str1, VALUE str2)
4334{
4335 if (str1 == str2) return Qtrue;
4336 if (!RB_TYPE_P(str2, T_STRING)) return Qfalse;
4337 return rb_str_eql_internal(str1, str2);
4338}
4339
4340/*
4341 * call-seq:
4342 * self <=> other -> -1, 0, 1, or nil
4343 *
4344 * Compares +self+ and +other+,
4345 * evaluating their _contents_, not their _lengths_.
4346 *
4347 * Returns:
4348 *
4349 * - +-1+, if +self+ is smaller.
4350 * - +0+, if the two are equal.
4351 * - +1+, if +self+ is larger.
4352 * - +nil+, if the two are incomparable.
4353 *
4354 * Examples:
4355 *
4356 * 'a' <=> 'b' # => -1
4357 * 'a' <=> 'ab' # => -1
4358 * 'a' <=> 'a' # => 0
4359 * 'b' <=> 'a' # => 1
4360 * 'ab' <=> 'a' # => 1
4361 * 'a' <=> :a # => nil
4362 *
4363 * \Class \String includes module Comparable,
4364 * each of whose methods uses String#<=> for comparison.
4365 *
4366 * Related: see {Comparing}[rdoc-ref:String@Comparing].
4367 */
4368
4369static VALUE
4370rb_str_cmp_m(VALUE str1, VALUE str2)
4371{
4372 int result;
4373 VALUE s = rb_check_string_type(str2);
4374 if (NIL_P(s)) {
4375 return rb_invcmp(str1, str2);
4376 }
4377 result = rb_str_cmp(str1, s);
4378 return INT2FIX(result);
4379}
4380
4381static VALUE str_casecmp(VALUE str1, VALUE str2);
4382static VALUE str_casecmp_p(VALUE str1, VALUE str2);
4383
4384/*
4385 * call-seq:
4386 * casecmp(other_string) -> -1, 0, 1, or nil
4387 *
4388 * Ignoring case, compares +self+ and +other_string+; returns:
4389 *
4390 * - -1 if <tt>self.downcase</tt> is smaller than <tt>other_string.downcase</tt>.
4391 * - 0 if the two are equal.
4392 * - 1 if <tt>self.downcase</tt> is larger than <tt>other_string.downcase</tt>.
4393 * - +nil+ if the two are incomparable.
4394 *
4395 * See {Case Mapping}[rdoc-ref:case_mapping.rdoc].
4396 *
4397 * Examples:
4398 *
4399 * 'foo'.casecmp('goo') # => -1
4400 * 'goo'.casecmp('foo') # => 1
4401 * 'foo'.casecmp('food') # => -1
4402 * 'food'.casecmp('foo') # => 1
4403 * 'FOO'.casecmp('foo') # => 0
4404 * 'foo'.casecmp('FOO') # => 0
4405 * 'foo'.casecmp(1) # => nil
4406 *
4407 * Related: see {Comparing}[rdoc-ref:String@Comparing].
4408 */
4409
4410static VALUE
4411rb_str_casecmp(VALUE str1, VALUE str2)
4412{
4413 VALUE s = rb_check_string_type(str2);
4414 if (NIL_P(s)) {
4415 return Qnil;
4416 }
4417 return str_casecmp(str1, s);
4418}
4419
4420static VALUE
4421str_casecmp(VALUE str1, VALUE str2)
4422{
4423 long len;
4424 rb_encoding *enc;
4425 const char *p1, *p1end, *p2, *p2end;
4426
4427 enc = rb_enc_compatible(str1, str2);
4428 if (!enc) {
4429 return Qnil;
4430 }
4431
4432 p1 = RSTRING_PTR(str1); p1end = RSTRING_END(str1);
4433 p2 = RSTRING_PTR(str2); p2end = RSTRING_END(str2);
4434 if (single_byte_optimizable(str1) && single_byte_optimizable(str2)) {
4435 while (p1 < p1end && p2 < p2end) {
4436 if (*p1 != *p2) {
4437 unsigned int c1 = TOLOWER(*p1 & 0xff);
4438 unsigned int c2 = TOLOWER(*p2 & 0xff);
4439 if (c1 != c2)
4440 return INT2FIX(c1 < c2 ? -1 : 1);
4441 }
4442 p1++;
4443 p2++;
4444 }
4445 }
4446 else {
4447 while (p1 < p1end && p2 < p2end) {
4448 int l1, c1 = rb_enc_ascget(p1, p1end, &l1, enc);
4449 int l2, c2 = rb_enc_ascget(p2, p2end, &l2, enc);
4450
4451 if (0 <= c1 && 0 <= c2) {
4452 c1 = TOLOWER(c1);
4453 c2 = TOLOWER(c2);
4454 if (c1 != c2)
4455 return INT2FIX(c1 < c2 ? -1 : 1);
4456 }
4457 else {
4458 int r;
4459 l1 = rb_enc_mbclen(p1, p1end, enc);
4460 l2 = rb_enc_mbclen(p2, p2end, enc);
4461 len = l1 < l2 ? l1 : l2;
4462 r = memcmp(p1, p2, len);
4463 if (r != 0)
4464 return INT2FIX(r < 0 ? -1 : 1);
4465 if (l1 != l2)
4466 return INT2FIX(l1 < l2 ? -1 : 1);
4467 }
4468 p1 += l1;
4469 p2 += l2;
4470 }
4471 }
4472 if (p1 == p1end && p2 == p2end) return INT2FIX(0);
4473 if (p1 == p1end) return INT2FIX(-1);
4474 return INT2FIX(1);
4475}
4476
4477/*
4478 * call-seq:
4479 * casecmp?(other_string) -> true, false, or nil
4480 *
4481 * Returns +true+ if +self+ and +other_string+ are equal after
4482 * Unicode case folding, +false+ if unequal, +nil+ if incomparable.
4483 *
4484 * See {Case Mapping}[rdoc-ref:case_mapping.rdoc].
4485 *
4486 * Examples:
4487 *
4488 * 'foo'.casecmp?('goo') # => false
4489 * 'goo'.casecmp?('foo') # => false
4490 * 'foo'.casecmp?('food') # => false
4491 * 'food'.casecmp?('foo') # => false
4492 * 'FOO'.casecmp?('foo') # => true
4493 * 'foo'.casecmp?('FOO') # => true
4494 * 'foo'.casecmp?(1) # => nil
4495 *
4496 * Related: see {Comparing}[rdoc-ref:String@Comparing].
4497 */
4498
4499static VALUE
4500rb_str_casecmp_p(VALUE str1, VALUE str2)
4501{
4502 VALUE s = rb_check_string_type(str2);
4503 if (NIL_P(s)) {
4504 return Qnil;
4505 }
4506 return str_casecmp_p(str1, s);
4507}
4508
4509static VALUE
4510str_casecmp_p(VALUE str1, VALUE str2)
4511{
4512 rb_encoding *enc;
4513 VALUE folded_str1, folded_str2;
4514 VALUE fold_opt = sym_fold;
4515
4516 enc = rb_enc_compatible(str1, str2);
4517 if (!enc) {
4518 return Qnil;
4519 }
4520
4521 folded_str1 = rb_str_downcase(1, &fold_opt, str1);
4522 folded_str2 = rb_str_downcase(1, &fold_opt, str2);
4523
4524 return rb_str_eql(folded_str1, folded_str2);
4525}
4526
4527static long
4528strseq_core(const char *str_ptr, const char *str_ptr_end, long str_len,
4529 const char *sub_ptr, long sub_len, long offset, rb_encoding *enc)
4530{
4531 const char *search_start = str_ptr;
4532 long pos, search_len = str_len - offset;
4533
4534 for (;;) {
4535 const char *t;
4536 pos = rb_memsearch(sub_ptr, sub_len, search_start, search_len, enc);
4537 if (pos < 0) return pos;
4538 t = rb_enc_right_char_head(search_start, search_start+pos, str_ptr_end, enc);
4539 if (t == search_start + pos) break;
4540 search_len -= t - search_start;
4541 if (search_len <= 0) return -1;
4542 offset += t - search_start;
4543 search_start = t;
4544 }
4545 return pos + offset;
4546}
4547
4548/* found index in byte */
4549#define rb_str_index(str, sub, offset) rb_strseq_index(str, sub, offset, 0)
4550#define rb_str_byteindex(str, sub, offset) rb_strseq_index(str, sub, offset, 1)
4551
4552static long
4553rb_strseq_index(VALUE str, VALUE sub, long offset, int in_byte)
4554{
4555 const char *str_ptr, *str_ptr_end, *sub_ptr;
4556 long str_len, sub_len;
4557 rb_encoding *enc;
4558
4559 enc = rb_enc_check(str, sub);
4560 if (is_broken_string(sub)) return -1;
4561
4562 str_ptr = RSTRING_PTR(str);
4563 str_ptr_end = RSTRING_END(str);
4564 str_len = RSTRING_LEN(str);
4565 sub_ptr = RSTRING_PTR(sub);
4566 sub_len = RSTRING_LEN(sub);
4567
4568 if (str_len < sub_len) return -1;
4569
4570 if (offset != 0) {
4571 long str_len_char, sub_len_char;
4572 int single_byte = single_byte_optimizable(str);
4573 str_len_char = (in_byte || single_byte) ? str_len : str_strlen(str, enc);
4574 sub_len_char = in_byte ? sub_len : str_strlen(sub, enc);
4575 if (offset < 0) {
4576 offset += str_len_char;
4577 if (offset < 0) return -1;
4578 }
4579 if (str_len_char - offset < sub_len_char) return -1;
4580 if (!in_byte) offset = str_offset(str_ptr, str_ptr_end, offset, enc, single_byte);
4581 str_ptr += offset;
4582 }
4583 if (sub_len == 0) return offset;
4584
4585 /* need proceed one character at a time */
4586 return strseq_core(str_ptr, str_ptr_end, str_len, sub_ptr, sub_len, offset, enc);
4587}
4588
4589
4590/*
4591 * call-seq:
4592 * index(pattern, offset = 0) -> integer or nil
4593 *
4594 * :include: doc/string/index.rdoc
4595 *
4596 */
4597
4598static VALUE
4599rb_str_index_m(int argc, VALUE *argv, VALUE str)
4600{
4601 VALUE sub;
4602 VALUE initpos;
4603 rb_encoding *enc = STR_ENC_GET(str);
4604 long pos;
4605
4606 if (rb_scan_args(argc, argv, "11", &sub, &initpos) == 2) {
4607 long slen = str_strlen(str, enc); /* str's enc */
4608 pos = NUM2LONG(initpos);
4609 if (pos < 0 ? (pos += slen) < 0 : pos > slen) {
4610 if (RB_TYPE_P(sub, T_REGEXP)) {
4612 }
4613 return Qnil;
4614 }
4615 }
4616 else {
4617 pos = 0;
4618 }
4619
4620 if (RB_TYPE_P(sub, T_REGEXP)) {
4621 pos = str_offset(RSTRING_PTR(str), RSTRING_END(str), pos,
4622 enc, single_byte_optimizable(str));
4623
4624 if (rb_reg_search(sub, str, pos, 0) >= 0) {
4625 VALUE match = rb_backref_get();
4626 struct re_registers *regs = RMATCH_REGS(match);
4627 pos = rb_str_sublen(str, BEG(0));
4628 return LONG2NUM(pos);
4629 }
4630 }
4631 else {
4632 StringValue(sub);
4633 pos = rb_str_index(str, sub, pos);
4634 if (pos >= 0) {
4635 pos = rb_str_sublen(str, pos);
4636 return LONG2NUM(pos);
4637 }
4638 }
4639 return Qnil;
4640}
4641
4642/* Ensure that the given pos is a valid character boundary.
4643 * Note that in this function, "character" means a code point
4644 * (Unicode scalar value), not a grapheme cluster.
4645 */
4646static void
4647str_ensure_byte_pos(VALUE str, long pos)
4648{
4649 if (!single_byte_optimizable(str)) {
4650 const char *s = RSTRING_PTR(str);
4651 const char *e = RSTRING_END(str);
4652 const char *p = s + pos;
4653 if (!at_char_boundary(s, p, e, rb_enc_get(str))) {
4654 rb_raise(rb_eIndexError,
4655 "offset %ld does not land on character boundary", pos);
4656 }
4657 }
4658}
4659
4660/*
4661 * call-seq:
4662 * byteindex(object, offset = 0) -> integer or nil
4663 *
4664 * Returns the 0-based integer index of a substring of +self+
4665 * specified by +object+ (a string or Regexp) and +offset+,
4666 * or +nil+ if there is no such substring;
4667 * the returned index is the count of _bytes_ (not characters).
4668 *
4669 * When +object+ is a string,
4670 * returns the index of the first found substring equal to +object+:
4671 *
4672 * s = 'foo' # => "foo"
4673 * s.size # => 3 # Three 1-byte characters.
4674 * s.bytesize # => 3 # Three bytes.
4675 * s.byteindex('f') # => 0
4676 * s.byteindex('o') # => 1
4677 * s.byteindex('oo') # => 1
4678 * s.byteindex('ooo') # => nil
4679 *
4680 * When +object+ is a Regexp,
4681 * returns the index of the first found substring matching +object+;
4682 * updates {Regexp-related global variables}[rdoc-ref:Regexp@Global+Variables]:
4683 *
4684 * s = 'foo'
4685 * s.byteindex(/f/) # => 0
4686 * $~ # => #<MatchData "f">
4687 * s.byteindex(/o/) # => 1
4688 * s.byteindex(/oo/) # => 1
4689 * s.byteindex(/ooo/) # => nil
4690 * $~ # => nil
4691 *
4692 * \Integer argument +offset+, if given, specifies the 0-based index
4693 * of the byte where searching is to begin.
4694 *
4695 * When +offset+ is non-negative,
4696 * searching begins at byte position +offset+:
4697 *
4698 * s = 'foo'
4699 * s.byteindex('o', 1) # => 1
4700 * s.byteindex('o', 2) # => 2
4701 * s.byteindex('o', 3) # => nil
4702 *
4703 * When +offset+ is negative, counts backward from the end of +self+:
4704 *
4705 * s = 'foo'
4706 * s.byteindex('o', -1) # => 2
4707 * s.byteindex('o', -2) # => 1
4708 * s.byteindex('o', -3) # => 1
4709 * s.byteindex('o', -4) # => nil
4710 *
4711 * Raises IndexError if the byte at +offset+ is not the first byte of a character:
4712 *
4713 * s = "\uFFFF\uFFFF" # => "\uFFFF\uFFFF"
4714 * s.size # => 2 # Two 3-byte characters.
4715 * s.bytesize # => 6 # Six bytes.
4716 * s.byteindex("\uFFFF") # => 0
4717 * s.byteindex("\uFFFF", 1) # Raises IndexError
4718 * s.byteindex("\uFFFF", 2) # Raises IndexError
4719 * s.byteindex("\uFFFF", 3) # => 3
4720 * s.byteindex("\uFFFF", 4) # Raises IndexError
4721 * s.byteindex("\uFFFF", 5) # Raises IndexError
4722 * s.byteindex("\uFFFF", 6) # => nil
4723 *
4724 * Related: see {Querying}[rdoc-ref:String@Querying].
4725 */
4726
4727static VALUE
4728rb_str_byteindex_m(int argc, VALUE *argv, VALUE str)
4729{
4730 VALUE sub;
4731 VALUE initpos;
4732 long pos;
4733
4734 if (rb_scan_args(argc, argv, "11", &sub, &initpos) == 2) {
4735 pos = NUM2LONG(initpos);
4736 long slen = RSTRING_LEN(str);
4737 if (pos < 0 ? (pos += slen) < 0 : pos > slen) {
4738 if (RB_TYPE_P(sub, T_REGEXP)) {
4740 }
4741 return Qnil;
4742 }
4743 }
4744 else {
4745 pos = 0;
4746 }
4747
4748 str_ensure_byte_pos(str, pos);
4749
4750 if (RB_TYPE_P(sub, T_REGEXP)) {
4751 if (rb_reg_search(sub, str, pos, 0) >= 0) {
4752 VALUE match = rb_backref_get();
4753 struct re_registers *regs = RMATCH_REGS(match);
4754 pos = BEG(0);
4755 return LONG2NUM(pos);
4756 }
4757 }
4758 else {
4759 StringValue(sub);
4760 pos = rb_str_byteindex(str, sub, pos);
4761 if (pos >= 0) return LONG2NUM(pos);
4762 }
4763 return Qnil;
4764}
4765
4766#ifndef HAVE_MEMRCHR
4767static void*
4768memrchr(const char *search_str, int chr, long search_len)
4769{
4770 const char *ptr = search_str + search_len;
4771 while (ptr > search_str) {
4772 if ((unsigned char)*(--ptr) == chr) return (void *)ptr;
4773 }
4774
4775 return ((void *)0);
4776}
4777#endif
4778
4779static long
4780str_rindex(VALUE str, VALUE sub, const char *s, rb_encoding *enc)
4781{
4782 char *hit, *adjusted;
4783 int c;
4784 long slen, searchlen;
4785 char *sbeg, *e, *t;
4786
4787 sbeg = RSTRING_PTR(str);
4788 slen = RSTRING_LEN(sub);
4789 if (slen == 0) return s - sbeg;
4790 e = RSTRING_END(str);
4791 t = RSTRING_PTR(sub);
4792 c = *t & 0xff;
4793 searchlen = s - sbeg + 1;
4794
4795 if (s + slen <= e && memcmp(s, t, slen) == 0) {
4796 return s - sbeg;
4797 }
4798
4799 do {
4800 hit = memrchr(sbeg, c, searchlen);
4801 if (!hit) break;
4802 adjusted = rb_enc_left_char_head(sbeg, hit, e, enc);
4803 if (hit != adjusted) {
4804 searchlen = adjusted - sbeg;
4805 continue;
4806 }
4807 if (hit + slen <= e && memcmp(hit, t, slen) == 0)
4808 return hit - sbeg;
4809 searchlen = adjusted - sbeg;
4810 } while (searchlen > 0);
4811
4812 return -1;
4813}
4814
4815/* found index in byte */
4816static long
4817rb_str_rindex(VALUE str, VALUE sub, long pos)
4818{
4819 long len, slen;
4820 char *sbeg, *s;
4821 rb_encoding *enc;
4822 int singlebyte;
4823
4824 enc = rb_enc_check(str, sub);
4825 if (is_broken_string(sub)) return -1;
4826 singlebyte = single_byte_optimizable(str);
4827 len = singlebyte ? RSTRING_LEN(str) : str_strlen(str, enc); /* rb_enc_check */
4828 slen = str_strlen(sub, enc); /* rb_enc_check */
4829
4830 /* substring longer than string */
4831 if (len < slen) return -1;
4832 /* character counts, so the byte tail can still be shorter than sub */
4833 if (len - pos < slen) pos = len - slen;
4834 if (len == 0) return pos;
4835
4836 sbeg = RSTRING_PTR(str);
4837
4838 if (pos == 0) {
4839 if (RSTRING_LEN(sub) <= RSTRING_LEN(str) &&
4840 memcmp(sbeg, RSTRING_PTR(sub), RSTRING_LEN(sub)) == 0) {
4841 return 0;
4842 }
4843 else {
4844 return -1;
4845 }
4846 }
4847
4848 s = str_nth(sbeg, RSTRING_END(str), pos, enc, singlebyte);
4849 return str_rindex(str, sub, s, enc);
4850}
4851
4852/*
4853 * call-seq:
4854 * rindex(pattern, offset = self.length) -> integer or nil
4855 *
4856 * :include:doc/string/rindex.rdoc
4857 *
4858 */
4859
4860static VALUE
4861rb_str_rindex_m(int argc, VALUE *argv, VALUE str)
4862{
4863 VALUE sub;
4864 VALUE initpos;
4865 rb_encoding *enc = STR_ENC_GET(str);
4866 long pos, len = str_strlen(str, enc); /* str's enc */
4867
4868 if (rb_scan_args(argc, argv, "11", &sub, &initpos) == 2) {
4869 pos = NUM2LONG(initpos);
4870 if (pos < 0 && (pos += len) < 0) {
4871 if (RB_TYPE_P(sub, T_REGEXP)) {
4873 }
4874 return Qnil;
4875 }
4876 if (pos > len) pos = len;
4877 }
4878 else {
4879 pos = len;
4880 }
4881
4882 if (RB_TYPE_P(sub, T_REGEXP)) {
4883 /* enc = rb_enc_check(str, sub); */
4884 pos = str_offset(RSTRING_PTR(str), RSTRING_END(str), pos,
4885 enc, single_byte_optimizable(str));
4886
4887 if (rb_reg_search(sub, str, pos, 1) >= 0) {
4888 VALUE match = rb_backref_get();
4889 struct re_registers *regs = RMATCH_REGS(match);
4890 pos = rb_str_sublen(str, BEG(0));
4891 return LONG2NUM(pos);
4892 }
4893 }
4894 else {
4895 StringValue(sub);
4896 pos = rb_str_rindex(str, sub, pos);
4897 if (pos >= 0) {
4898 pos = rb_str_sublen(str, pos);
4899 return LONG2NUM(pos);
4900 }
4901 }
4902 return Qnil;
4903}
4904
4905static long
4906rb_str_byterindex(VALUE str, VALUE sub, long pos)
4907{
4908 long len, slen;
4909 char *sbeg, *s;
4910 rb_encoding *enc;
4911
4912 enc = rb_enc_check(str, sub);
4913 if (is_broken_string(sub)) return -1;
4914 len = RSTRING_LEN(str);
4915 slen = RSTRING_LEN(sub);
4916
4917 /* substring longer than string */
4918 if (len < slen) return -1;
4919 if (len - pos < slen) pos = len - slen;
4920 if (len == 0) return pos;
4921
4922 sbeg = RSTRING_PTR(str);
4923
4924 if (pos == 0) {
4925 if (memcmp(sbeg, RSTRING_PTR(sub), RSTRING_LEN(sub)) == 0)
4926 return 0;
4927 else
4928 return -1;
4929 }
4930
4931 s = sbeg + pos;
4932 return str_rindex(str, sub, s, enc);
4933}
4934
4935/*
4936 * call-seq:
4937 * byterindex(object, offset = self.bytesize) -> integer or nil
4938 *
4939 * Returns the 0-based integer index of a substring of +self+
4940 * that is the _last_ match for the given +object+ (a string or Regexp) and +offset+,
4941 * or +nil+ if there is no such substring;
4942 * the returned index is the count of _bytes_ (not characters).
4943 *
4944 * When +object+ is a string,
4945 * returns the index of the _last_ found substring equal to +object+:
4946 *
4947 * s = 'foo' # => "foo"
4948 * s.size # => 3 # Three 1-byte characters.
4949 * s.bytesize # => 3 # Three bytes.
4950 * s.byterindex('f') # => 0
4951 s.byterindex('o') # => 2
4952 s.byterindex('oo') # => 1
4953 s.byterindex('ooo') # => nil
4954 *
4955 * When +object+ is a Regexp,
4956 * returns the index of the last found substring matching +object+;
4957 * updates {Regexp-related global variables}[rdoc-ref:Regexp@Global+Variables]:
4958 *
4959 * s = 'foo'
4960 * s.byterindex(/f/) # => 0
4961 * $~ # => #<MatchData "f">
4962 * s.byterindex(/o/) # => 2
4963 * s.byterindex(/oo/) # => 1
4964 * s.byterindex(/ooo/) # => nil
4965 * $~ # => nil
4966 *
4967 * The last match means starting at the possible last position,
4968 * not the last of the longest matches:
4969 *
4970 * s = 'foo'
4971 * s.byterindex(/o+/) # => 2
4972 * $~ #=> #<MatchData "o">
4973 *
4974 * To get the last longest match, use a negative lookbehind:
4975 *
4976 * s = 'foo'
4977 * s.byterindex(/(?<!o)o+/) # => 1
4978 * $~ # => #<MatchData "oo">
4979 *
4980 * Or use method #byteindex with negative lookahead:
4981 *
4982 * s = 'foo'
4983 * s.byteindex(/o+(?!.*o)/) # => 1
4984 * $~ #=> #<MatchData "oo">
4985 *
4986 * \Integer argument +offset+, if given, specifies the 0-based index
4987 * of the byte where searching is to end.
4988 *
4989 * When +offset+ is non-negative,
4990 * searching ends at byte position +offset+:
4991 *
4992 * s = 'foo'
4993 * s.byterindex('o', 0) # => nil
4994 * s.byterindex('o', 1) # => 1
4995 * s.byterindex('o', 2) # => 2
4996 * s.byterindex('o', 3) # => 2
4997 *
4998 * When +offset+ is negative, counts backward from the end of +self+:
4999 *
5000 * s = 'foo'
5001 * s.byterindex('o', -1) # => 2
5002 * s.byterindex('o', -2) # => 1
5003 * s.byterindex('o', -3) # => nil
5004 *
5005 * Raises IndexError if the byte at +offset+ is not the first byte of a character:
5006 *
5007 * s = "\uFFFF\uFFFF" # => "\uFFFF\uFFFF"
5008 * s.size # => 2 # Two 3-byte characters.
5009 * s.bytesize # => 6 # Six bytes.
5010 * s.byterindex("\uFFFF") # => 3
5011 * s.byterindex("\uFFFF", 1) # Raises IndexError
5012 * s.byterindex("\uFFFF", 2) # Raises IndexError
5013 * s.byterindex("\uFFFF", 3) # => 3
5014 * s.byterindex("\uFFFF", 4) # Raises IndexError
5015 * s.byterindex("\uFFFF", 5) # Raises IndexError
5016 * s.byterindex("\uFFFF", 6) # => nil
5017 *
5018 * Related: see {Querying}[rdoc-ref:String@Querying].
5019 */
5020
5021static VALUE
5022rb_str_byterindex_m(int argc, VALUE *argv, VALUE str)
5023{
5024 VALUE sub;
5025 VALUE initpos;
5026 long pos;
5027
5028 if (rb_scan_args(argc, argv, "11", &sub, &initpos) == 2) {
5029 pos = NUM2LONG(initpos);
5030 long len = RSTRING_LEN(str);
5031 if (pos < 0 && (pos += len) < 0) {
5032 if (RB_TYPE_P(sub, T_REGEXP)) {
5034 }
5035 return Qnil;
5036 }
5037 if (pos > len) pos = len;
5038 }
5039 else {
5040 pos = RSTRING_LEN(str);
5041 }
5042
5043 str_ensure_byte_pos(str, pos);
5044
5045 if (RB_TYPE_P(sub, T_REGEXP)) {
5046 if (rb_reg_search(sub, str, pos, 1) >= 0) {
5047 VALUE match = rb_backref_get();
5048 struct re_registers *regs = RMATCH_REGS(match);
5049 pos = BEG(0);
5050 return LONG2NUM(pos);
5051 }
5052 }
5053 else {
5054 StringValue(sub);
5055 pos = rb_str_byterindex(str, sub, pos);
5056 if (pos >= 0) return LONG2NUM(pos);
5057 }
5058 return Qnil;
5059}
5060
5061/*
5062 * call-seq:
5063 * self =~ object -> integer or nil
5064 *
5065 * When +object+ is a Regexp, returns the index of the first substring in +self+
5066 * matched by +object+,
5067 * or +nil+ if no match is found;
5068 * updates {Regexp-related global variables}[rdoc-ref:Regexp@Global+Variables]:
5069 *
5070 * 'foo' =~ /f/ # => 0
5071 * $~ # => #<MatchData "f">
5072 * 'foo' =~ /o/ # => 1
5073 * $~ # => #<MatchData "o">
5074 * 'foo' =~ /x/ # => nil
5075 * $~ # => nil
5076 *
5077 * Note that <tt>string =~ regexp</tt> is different from <tt>regexp =~ string</tt>
5078 * (see Regexp#=~):
5079 *
5080 * number = nil
5081 * 'no. 9' =~ /(?<number>\d+)/ # => 4
5082 * number # => nil # Not assigned.
5083 * /(?<number>\d+)/ =~ 'no. 9' # => 4
5084 * number # => "9" # Assigned.
5085 *
5086 * If +object+ is not a Regexp, returns the value
5087 * returned by <tt>object =~ self</tt>.
5088 *
5089 * Related: see {Querying}[rdoc-ref:String@Querying].
5090 */
5091
5092static VALUE
5093rb_str_match(VALUE x, VALUE y)
5094{
5095 switch (OBJ_BUILTIN_TYPE(y)) {
5096 case T_STRING:
5097 rb_raise(rb_eTypeError, "type mismatch: String given");
5098
5099 case T_REGEXP:
5100 return rb_reg_match(y, x);
5101
5102 default:
5103 return rb_funcall(y, idEqTilde, 1, x);
5104 }
5105}
5106
5107
5108static VALUE get_pat(VALUE);
5109
5110
5111/*
5112 * call-seq:
5113 * match(pattern, offset = 0) -> matchdata or nil
5114 * match(pattern, offset = 0) {|matchdata| ... } -> object
5115 *
5116 * Creates a MatchData object based on +self+ and the given arguments;
5117 * updates {Regexp Global Variables}[rdoc-ref:Regexp@Global+Variables].
5118 *
5119 * - Computes +regexp+ by converting +pattern+ (if not already a Regexp).
5120 *
5121 * regexp = Regexp.new(pattern)
5122 *
5123 * - Computes +matchdata+, which will be either a MatchData object or +nil+
5124 * (see Regexp#match):
5125 *
5126 * matchdata = regexp.match(self[offset..])
5127 *
5128 * With no block given, returns the computed +matchdata+ or +nil+:
5129 *
5130 * 'foo'.match('f') # => #<MatchData "f">
5131 * 'foo'.match('o') # => #<MatchData "o">
5132 * 'foo'.match('x') # => nil
5133 * 'foo'.match('f', 1) # => nil
5134 * 'foo'.match('o', 1) # => #<MatchData "o">
5135 *
5136 * With a block given and computed +matchdata+ non-nil, calls the block with +matchdata+;
5137 * returns the block's return value:
5138 *
5139 * 'foo'.match(/o/) {|matchdata| matchdata } # => #<MatchData "o">
5140 *
5141 * With a block given and +nil+ +matchdata+, does not call the block:
5142 *
5143 * 'foo'.match(/x/) {|matchdata| fail 'Cannot happen' } # => nil
5144 *
5145 * Related: see {Querying}[rdoc-ref:String@Querying].
5146 */
5147
5148static VALUE
5149rb_str_match_m(int argc, VALUE *argv, VALUE str)
5150{
5151 VALUE re, result;
5152 if (argc < 1)
5153 rb_check_arity(argc, 1, 2);
5154 re = argv[0];
5155 argv[0] = str;
5156 result = rb_funcallv(get_pat(re), rb_intern("match"), argc, argv);
5157 if (!NIL_P(result) && rb_block_given_p()) {
5158 return rb_yield(result);
5159 }
5160 return result;
5161}
5162
5163/*
5164 * call-seq:
5165 * match?(pattern, offset = 0) -> true or false
5166 *
5167 * Returns whether a match is found for +self+ and the given arguments;
5168 * does not update {Regexp Global Variables}[rdoc-ref:Regexp@Global+Variables].
5169 *
5170 * Computes +regexp+ by converting +pattern+ (if not already a Regexp):
5171 *
5172 * regexp = Regexp.new(pattern)
5173 *
5174 * Returns +true+ if <tt>self[offset..].match(regexp)</tt> returns a MatchData object,
5175 * +false+ otherwise:
5176 *
5177 * 'foo'.match?(/o/) # => true
5178 * 'foo'.match?('o') # => true
5179 * 'foo'.match?(/x/) # => false
5180 * 'foo'.match?('f', 1) # => false
5181 * 'foo'.match?('o', 1) # => true
5182 *
5183 * Related: see {Querying}[rdoc-ref:String@Querying].
5184 */
5185
5186static VALUE
5187rb_str_match_m_p(int argc, VALUE *argv, VALUE str)
5188{
5189 VALUE re;
5190 rb_check_arity(argc, 1, 2);
5191 re = get_pat(argv[0]);
5192 return rb_reg_match_p(re, str, argc > 1 ? NUM2LONG(argv[1]) : 0);
5193}
5194
5195enum neighbor_char {
5196 NEIGHBOR_NOT_CHAR,
5197 NEIGHBOR_FOUND,
5198 NEIGHBOR_WRAPPED
5199};
5200
5201static enum neighbor_char
5202enc_succ_char(char *p, long len, rb_encoding *enc)
5203{
5204 long i;
5205 int l;
5206
5207 if (rb_enc_mbminlen(enc) > 1) {
5208 /* wchar, trivial case */
5209 int r = rb_enc_precise_mbclen(p, p + len, enc), c;
5210 if (!MBCLEN_CHARFOUND_P(r)) {
5211 return NEIGHBOR_NOT_CHAR;
5212 }
5213 c = rb_enc_mbc_to_codepoint(p, p + len, enc) + 1;
5214 l = rb_enc_code_to_mbclen(c, enc);
5215 if (!l) return NEIGHBOR_NOT_CHAR;
5216 if (l != len) return NEIGHBOR_WRAPPED;
5217 rb_enc_mbcput(c, p, enc);
5218 r = rb_enc_precise_mbclen(p, p + len, enc);
5219 if (!MBCLEN_CHARFOUND_P(r)) {
5220 return NEIGHBOR_NOT_CHAR;
5221 }
5222 return NEIGHBOR_FOUND;
5223 }
5224 while (1) {
5225 for (i = len-1; 0 <= i && (unsigned char)p[i] == 0xff; i--)
5226 p[i] = '\0';
5227 if (i < 0)
5228 return NEIGHBOR_WRAPPED;
5229 ++((unsigned char*)p)[i];
5230 l = rb_enc_precise_mbclen(p, p+len, enc);
5231 if (MBCLEN_CHARFOUND_P(l)) {
5232 l = MBCLEN_CHARFOUND_LEN(l);
5233 if (l == len) {
5234 return NEIGHBOR_FOUND;
5235 }
5236 else {
5237 memset(p+l, 0xff, len-l);
5238 }
5239 }
5240 if (MBCLEN_INVALID_P(l) && i < len-1) {
5241 long len2;
5242 int l2;
5243 for (len2 = len-1; 0 < len2; len2--) {
5244 l2 = rb_enc_precise_mbclen(p, p+len2, enc);
5245 if (!MBCLEN_INVALID_P(l2))
5246 break;
5247 }
5248 memset(p+len2+1, 0xff, len-(len2+1));
5249 }
5250 }
5251}
5252
5253static enum neighbor_char
5254enc_pred_char(char *p, long len, rb_encoding *enc)
5255{
5256 long i;
5257 int l;
5258 if (rb_enc_mbminlen(enc) > 1) {
5259 /* wchar, trivial case */
5260 int r = rb_enc_precise_mbclen(p, p + len, enc), c;
5261 if (!MBCLEN_CHARFOUND_P(r)) {
5262 return NEIGHBOR_NOT_CHAR;
5263 }
5264 c = rb_enc_mbc_to_codepoint(p, p + len, enc);
5265 if (!c) return NEIGHBOR_NOT_CHAR;
5266 --c;
5267 l = rb_enc_code_to_mbclen(c, enc);
5268 if (!l) return NEIGHBOR_NOT_CHAR;
5269 if (l != len) return NEIGHBOR_WRAPPED;
5270 rb_enc_mbcput(c, p, enc);
5271 r = rb_enc_precise_mbclen(p, p + len, enc);
5272 if (!MBCLEN_CHARFOUND_P(r)) {
5273 return NEIGHBOR_NOT_CHAR;
5274 }
5275 return NEIGHBOR_FOUND;
5276 }
5277 while (1) {
5278 for (i = len-1; 0 <= i && (unsigned char)p[i] == 0; i--)
5279 p[i] = '\xff';
5280 if (i < 0)
5281 return NEIGHBOR_WRAPPED;
5282 --((unsigned char*)p)[i];
5283 l = rb_enc_precise_mbclen(p, p+len, enc);
5284 if (MBCLEN_CHARFOUND_P(l)) {
5285 l = MBCLEN_CHARFOUND_LEN(l);
5286 if (l == len) {
5287 return NEIGHBOR_FOUND;
5288 }
5289 else {
5290 memset(p+l, 0, len-l);
5291 }
5292 }
5293 if (MBCLEN_INVALID_P(l) && i < len-1) {
5294 long len2;
5295 int l2;
5296 for (len2 = len-1; 0 < len2; len2--) {
5297 l2 = rb_enc_precise_mbclen(p, p+len2, enc);
5298 if (!MBCLEN_INVALID_P(l2))
5299 break;
5300 }
5301 memset(p+len2+1, 0, len-(len2+1));
5302 }
5303 }
5304}
5305
5306/*
5307 overwrite +p+ by succeeding letter in +enc+ and returns
5308 NEIGHBOR_FOUND or NEIGHBOR_WRAPPED.
5309 When NEIGHBOR_WRAPPED, carried-out letter is stored into carry.
5310 assuming each ranges are successive, and mbclen
5311 never change in each ranges.
5312 NEIGHBOR_NOT_CHAR is returned if invalid character or the range has only one
5313 character.
5314 */
5315static enum neighbor_char
5316enc_succ_alnum_char(char *p, long len, rb_encoding *enc, char *carry)
5317{
5318 enum neighbor_char ret;
5319 unsigned int c;
5320 int ctype;
5321 int range;
5322 char save[ONIGENC_CODE_TO_MBC_MAXLEN];
5323
5324 /* skip 03A2, invalid char between GREEK CAPITAL LETTERS */
5325 int try;
5326 const int max_gaps = 1;
5327
5328 c = rb_enc_mbc_to_codepoint(p, p+len, enc);
5329 if (rb_enc_isctype(c, ONIGENC_CTYPE_DIGIT, enc))
5330 ctype = ONIGENC_CTYPE_DIGIT;
5331 else if (rb_enc_isctype(c, ONIGENC_CTYPE_ALPHA, enc))
5332 ctype = ONIGENC_CTYPE_ALPHA;
5333 else
5334 return NEIGHBOR_NOT_CHAR;
5335
5336 MEMCPY(save, p, char, len);
5337 for (try = 0; try <= max_gaps; ++try) {
5338 ret = enc_succ_char(p, len, enc);
5339 if (ret == NEIGHBOR_FOUND) {
5340 c = rb_enc_mbc_to_codepoint(p, p+len, enc);
5341 if (rb_enc_isctype(c, ctype, enc))
5342 return NEIGHBOR_FOUND;
5343 }
5344 }
5345 MEMCPY(p, save, char, len);
5346 range = 1;
5347 while (1) {
5348 MEMCPY(save, p, char, len);
5349 ret = enc_pred_char(p, len, enc);
5350 if (ret == NEIGHBOR_FOUND) {
5351 c = rb_enc_mbc_to_codepoint(p, p+len, enc);
5352 if (!rb_enc_isctype(c, ctype, enc)) {
5353 MEMCPY(p, save, char, len);
5354 break;
5355 }
5356 }
5357 else {
5358 MEMCPY(p, save, char, len);
5359 break;
5360 }
5361 range++;
5362 }
5363 if (range == 1) {
5364 return NEIGHBOR_NOT_CHAR;
5365 }
5366
5367 if (ctype != ONIGENC_CTYPE_DIGIT) {
5368 MEMCPY(carry, p, char, len);
5369 return NEIGHBOR_WRAPPED;
5370 }
5371
5372 MEMCPY(carry, p, char, len);
5373 enc_succ_char(carry, len, enc);
5374 return NEIGHBOR_WRAPPED;
5375}
5376
5377
5378static VALUE str_succ(VALUE str);
5379
5380/*
5381 * call-seq:
5382 * succ -> new_str
5383 *
5384 * :include: doc/string/succ.rdoc
5385 *
5386 */
5387
5388VALUE
5390{
5391 VALUE str;
5392 str = str_enc_new(rb_cString, RSTRING_PTR(orig), RSTRING_LEN(orig), STR_ENC_GET(orig));
5393 rb_enc_cr_str_copy_for_substr(str, orig);
5394 return str_succ(str);
5395}
5396
5397static VALUE
5398str_succ(VALUE str)
5399{
5400 rb_encoding *enc;
5401 char *sbeg, *s, *e, *last_alnum = 0;
5402 int found_alnum = 0;
5403 long l, slen;
5404 char carry[ONIGENC_CODE_TO_MBC_MAXLEN] = "\1";
5405 long carry_pos = 0, carry_len = 1;
5406 enum neighbor_char neighbor = NEIGHBOR_FOUND;
5407
5408 slen = RSTRING_LEN(str);
5409 if (slen == 0) return str;
5410
5411 enc = STR_ENC_GET(str);
5412 sbeg = RSTRING_PTR(str);
5413 s = e = sbeg + slen;
5414
5415 while ((s = rb_enc_prev_char(sbeg, s, e, enc)) != 0) {
5416 if (neighbor == NEIGHBOR_NOT_CHAR && last_alnum) {
5417 if (ISALPHA(*last_alnum) ? ISDIGIT(*s) :
5418 ISDIGIT(*last_alnum) ? ISALPHA(*s) : 0) {
5419 break;
5420 }
5421 }
5422 l = rb_enc_precise_mbclen(s, e, enc);
5423 if (!ONIGENC_MBCLEN_CHARFOUND_P(l)) continue;
5424 l = ONIGENC_MBCLEN_CHARFOUND_LEN(l);
5425 neighbor = enc_succ_alnum_char(s, l, enc, carry);
5426 switch (neighbor) {
5427 case NEIGHBOR_NOT_CHAR:
5428 continue;
5429 case NEIGHBOR_FOUND:
5430 return str;
5431 case NEIGHBOR_WRAPPED:
5432 last_alnum = s;
5433 break;
5434 }
5435 found_alnum = 1;
5436 carry_pos = s - sbeg;
5437 carry_len = l;
5438 }
5439 if (!found_alnum) { /* str contains no alnum */
5440 s = e;
5441 while ((s = rb_enc_prev_char(sbeg, s, e, enc)) != 0) {
5442 enum neighbor_char neighbor;
5443 char tmp[ONIGENC_CODE_TO_MBC_MAXLEN];
5444 l = rb_enc_precise_mbclen(s, e, enc);
5445 if (!ONIGENC_MBCLEN_CHARFOUND_P(l)) continue;
5446 l = ONIGENC_MBCLEN_CHARFOUND_LEN(l);
5447 MEMCPY(tmp, s, char, l);
5448 neighbor = enc_succ_char(tmp, l, enc);
5449 switch (neighbor) {
5450 case NEIGHBOR_FOUND:
5451 MEMCPY(s, tmp, char, l);
5452 return str;
5453 break;
5454 case NEIGHBOR_WRAPPED:
5455 MEMCPY(s, tmp, char, l);
5456 break;
5457 case NEIGHBOR_NOT_CHAR:
5458 break;
5459 }
5460 if (rb_enc_precise_mbclen(s, s+l, enc) != l) {
5461 /* wrapped to \0...\0. search next valid char. */
5462 enc_succ_char(s, l, enc);
5463 }
5464 if (!rb_enc_asciicompat(enc)) {
5465 MEMCPY(carry, s, char, l);
5466 carry_len = l;
5467 }
5468 carry_pos = s - sbeg;
5469 }
5471 }
5472 RESIZE_CAPA(str, slen + carry_len);
5473 sbeg = RSTRING_PTR(str);
5474 s = sbeg + carry_pos;
5475 memmove(s + carry_len, s, slen - carry_pos);
5476 memmove(s, carry, carry_len);
5477 slen += carry_len;
5478 STR_SET_LEN(str, slen);
5479 TERM_FILL(&sbeg[slen], rb_enc_mbminlen(enc));
5481 return str;
5482}
5483
5484
5485/*
5486 * call-seq:
5487 * succ! -> self
5488 *
5489 * Like String#succ, but modifies +self+ in place; returns +self+.
5490 *
5491 * Related: see {Modifying}[rdoc-ref:String@Modifying].
5492 */
5493
5494static VALUE
5495rb_str_succ_bang(VALUE str)
5496{
5497 rb_str_modify(str);
5498 str_succ(str);
5499 return str;
5500}
5501
5502static int
5503all_digits_p(const char *s, long len)
5504{
5505 while (len-- > 0) {
5506 if (!ISDIGIT(*s)) return 0;
5507 s++;
5508 }
5509 return 1;
5510}
5511
5512static int
5513str_upto_i(VALUE str, VALUE arg)
5514{
5515 rb_yield(str);
5516 return 0;
5517}
5518
5519/*
5520 * call-seq:
5521 * upto(other_string, exclusive = false) {|string| ... } -> self
5522 * upto(other_string, exclusive = false) -> new_enumerator
5523 *
5524 * :include: doc/string/upto.rdoc
5525 *
5526 */
5527
5528static VALUE
5529rb_str_upto(int argc, VALUE *argv, VALUE beg)
5530{
5531 VALUE end, exclusive;
5532
5533 rb_scan_args(argc, argv, "11", &end, &exclusive);
5534 RETURN_ENUMERATOR(beg, argc, argv);
5535 return rb_str_upto_each(beg, end, RTEST(exclusive), str_upto_i, Qnil);
5536}
5537
5538VALUE
5539rb_str_upto_each(VALUE beg, VALUE end, int excl, int (*each)(VALUE, VALUE), VALUE arg)
5540{
5541 VALUE current, after_end;
5542 ID succ;
5543 int n, ascii;
5544 rb_encoding *enc;
5545
5546 CONST_ID(succ, "succ");
5547 StringValue(end);
5548 enc = rb_enc_check(beg, end);
5549 ascii = (is_ascii_string(beg) && is_ascii_string(end));
5550 /* single character */
5551 if (RSTRING_LEN(beg) == 1 && RSTRING_LEN(end) == 1 && ascii) {
5552 char c = RSTRING_PTR(beg)[0];
5553 char e = RSTRING_PTR(end)[0];
5554
5555 if (c > e || (excl && c == e)) return beg;
5556 for (;;) {
5557 VALUE str = rb_enc_str_new(&c, 1, enc);
5559 if ((*each)(str, arg)) break;
5560 if (!excl && c == e) break;
5561 c++;
5562 if (excl && c == e) break;
5563 }
5564 return beg;
5565 }
5566 /* both edges are all digits */
5567 if (ascii && ISDIGIT(RSTRING_PTR(beg)[0]) && ISDIGIT(RSTRING_PTR(end)[0]) &&
5568 all_digits_p(RSTRING_PTR(beg), RSTRING_LEN(beg)) &&
5569 all_digits_p(RSTRING_PTR(end), RSTRING_LEN(end))) {
5570 VALUE b, e;
5571 int width;
5572
5573 width = RSTRING_LENINT(beg);
5574 b = rb_str_to_inum(beg, 10, FALSE);
5575 e = rb_str_to_inum(end, 10, FALSE);
5576 if (FIXNUM_P(b) && FIXNUM_P(e)) {
5577 long bi = FIX2LONG(b);
5578 long ei = FIX2LONG(e);
5579 rb_encoding *usascii = rb_usascii_encoding();
5580
5581 while (bi <= ei) {
5582 if (excl && bi == ei) break;
5583 if ((*each)(rb_enc_sprintf(usascii, "%.*ld", width, bi), arg)) break;
5584 bi++;
5585 }
5586 }
5587 else {
5588 ID op = excl ? '<' : idLE;
5589 VALUE args[2], fmt = rb_fstring_lit("%.*d");
5590
5591 args[0] = INT2FIX(width);
5592 while (rb_funcall(b, op, 1, e)) {
5593 args[1] = b;
5594 if ((*each)(rb_str_format(numberof(args), args, fmt), arg)) break;
5595 b = rb_funcallv(b, succ, 0, 0);
5596 }
5597 }
5598 return beg;
5599 }
5600 /* normal case */
5601 n = rb_str_cmp(beg, end);
5602 if (n > 0 || (excl && n == 0)) return beg;
5603
5604 after_end = rb_funcallv(end, succ, 0, 0);
5605 current = str_duplicate(rb_cString, beg);
5606 while (!rb_str_equal(current, after_end)) {
5607 VALUE next = Qnil;
5608 if (excl || !rb_str_equal(current, end))
5609 next = rb_funcallv(current, succ, 0, 0);
5610 if ((*each)(current, arg)) break;
5611 if (NIL_P(next)) break;
5612 current = next;
5613 StringValue(current);
5614 if (excl && rb_str_equal(current, end)) break;
5615 if (RSTRING_LEN(current) > RSTRING_LEN(end) || RSTRING_LEN(current) == 0)
5616 break;
5617 }
5618
5619 return beg;
5620}
5621
5622VALUE
5623rb_str_upto_endless_each(VALUE beg, int (*each)(VALUE, VALUE), VALUE arg)
5624{
5625 VALUE current;
5626 ID succ;
5627
5628 CONST_ID(succ, "succ");
5629 /* both edges are all digits */
5630 if (is_ascii_string(beg) && ISDIGIT(RSTRING_PTR(beg)[0]) &&
5631 all_digits_p(RSTRING_PTR(beg), RSTRING_LEN(beg))) {
5632 VALUE b, args[2], fmt = rb_fstring_lit("%.*d");
5633 int width = RSTRING_LENINT(beg);
5634 b = rb_str_to_inum(beg, 10, FALSE);
5635 if (FIXNUM_P(b)) {
5636 long bi = FIX2LONG(b);
5637 rb_encoding *usascii = rb_usascii_encoding();
5638
5639 while (FIXABLE(bi)) {
5640 if ((*each)(rb_enc_sprintf(usascii, "%.*ld", width, bi), arg)) break;
5641 bi++;
5642 }
5643 b = LONG2NUM(bi);
5644 }
5645 args[0] = INT2FIX(width);
5646 while (1) {
5647 args[1] = b;
5648 if ((*each)(rb_str_format(numberof(args), args, fmt), arg)) break;
5649 b = rb_funcallv(b, succ, 0, 0);
5650 }
5651 }
5652 /* normal case */
5653 current = str_duplicate(rb_cString, beg);
5654 while (1) {
5655 VALUE next = rb_funcallv(current, succ, 0, 0);
5656 if ((*each)(current, arg)) break;
5657 current = next;
5658 StringValue(current);
5659 if (RSTRING_LEN(current) == 0)
5660 break;
5661 }
5662
5663 return beg;
5664}
5665
5666static int
5667include_range_i(VALUE str, VALUE arg)
5668{
5669 VALUE *argp = (VALUE *)arg;
5670 if (!rb_equal(str, *argp)) return 0;
5671 *argp = Qnil;
5672 return 1;
5673}
5674
5675VALUE
5676rb_str_include_range_p(VALUE beg, VALUE end, VALUE val, VALUE exclusive)
5677{
5678 beg = rb_str_new_frozen(beg);
5679 StringValue(end);
5680 end = rb_str_new_frozen(end);
5681 if (NIL_P(val)) return Qfalse;
5682 val = rb_check_string_type(val);
5683 if (NIL_P(val)) return Qfalse;
5684 if (rb_enc_asciicompat(STR_ENC_GET(beg)) &&
5685 rb_enc_asciicompat(STR_ENC_GET(end)) &&
5686 rb_enc_asciicompat(STR_ENC_GET(val))) {
5687 const char *bp = RSTRING_PTR(beg);
5688 const char *ep = RSTRING_PTR(end);
5689 const char *vp = RSTRING_PTR(val);
5690 if (RSTRING_LEN(beg) == 1 && RSTRING_LEN(end) == 1) {
5691 if (RSTRING_LEN(val) == 0 || RSTRING_LEN(val) > 1)
5692 return Qfalse;
5693 else {
5694 char b = *bp;
5695 char e = *ep;
5696 char v = *vp;
5697
5698 if (ISASCII(b) && ISASCII(e) && ISASCII(v)) {
5699 if (b <= v && v < e) return Qtrue;
5700 return RBOOL(!RTEST(exclusive) && b <= v && v == e);
5701 }
5702 }
5703 }
5704#if 0
5705 /* both edges are all digits */
5706 if (ISDIGIT(*bp) && ISDIGIT(*ep) &&
5707 all_digits_p(bp, RSTRING_LEN(beg)) &&
5708 all_digits_p(ep, RSTRING_LEN(end))) {
5709 /* TODO */
5710 }
5711#endif
5712 }
5713 rb_str_upto_each(beg, end, RTEST(exclusive), include_range_i, (VALUE)&val);
5714
5715 return RBOOL(NIL_P(val));
5716}
5717
5718static VALUE
5719rb_str_subpat(VALUE str, VALUE re, VALUE backref)
5720{
5721 if (rb_reg_search(re, str, 0, 0) >= 0) {
5722 VALUE match = rb_backref_get();
5723 int nth = rb_reg_backref_number(match, backref);
5724 return rb_reg_nth_match(nth, match);
5725 }
5726 return Qnil;
5727}
5728
5729static VALUE
5730rb_str_aref(VALUE str, VALUE indx)
5731{
5732 long idx;
5733
5734 if (FIXNUM_P(indx)) {
5735 idx = FIX2LONG(indx);
5736 }
5737 else if (RB_TYPE_P(indx, T_REGEXP)) {
5738 return rb_str_subpat(str, indx, INT2FIX(0));
5739 }
5740 else if (RB_TYPE_P(indx, T_STRING)) {
5741 if (rb_str_index(str, indx, 0) != -1)
5742 return str_duplicate(rb_cString, indx);
5743 return Qnil;
5744 }
5745 else {
5746 /* check if indx is Range */
5747 long beg, len = str_strlen(str, NULL);
5748 switch (rb_range_beg_len(indx, &beg, &len, len, 0)) {
5749 case Qfalse:
5750 break;
5751 case Qnil:
5752 return Qnil;
5753 default:
5754 return rb_str_substr(str, beg, len);
5755 }
5756 idx = NUM2LONG(indx);
5757 }
5758
5759 return str_substr(str, idx, 1, FALSE);
5760}
5761
5762
5763/*
5764 * call-seq:
5765 * self[index] -> new_string or nil
5766 * self[start, length] -> new_string or nil
5767 * self[range] -> new_string or nil
5768 * self[regexp, capture = 0] -> new_string or nil
5769 * self[substring] -> new_string or nil
5770 *
5771 * :include: doc/string/aref.rdoc
5772 *
5773 */
5774
5775static VALUE
5776rb_str_aref_m(int argc, VALUE *argv, VALUE str)
5777{
5778 if (argc == 2) {
5779 if (RB_TYPE_P(argv[0], T_REGEXP)) {
5780 return rb_str_subpat(str, argv[0], argv[1]);
5781 }
5782 else {
5783 return rb_str_substr_two_fixnums(str, argv[0], argv[1], TRUE);
5784 }
5785 }
5786 rb_check_arity(argc, 1, 2);
5787 return rb_str_aref(str, argv[0]);
5788}
5789
5790VALUE
5792{
5793 str_modifiable(str);
5794
5795 char *ptr;
5796 long olen;
5797 RSTRING_GETMEM(str, ptr, olen);
5798 if (len > olen) len = olen;
5799 long nlen = olen - len;
5800 if (str_embed_capa(str) >= nlen + TERM_LEN(str)) {
5801 char *oldptr = ptr;
5802 int fl = (int)(RBASIC(str)->flags & (STR_NOEMBED|STR_SHARED|STR_NOFREE));
5803 STR_SET_EMBED(str);
5804 ptr = RSTRING(str)->as.embed.ary;
5805 memmove(ptr, oldptr + len, nlen);
5806 if (fl == STR_NOEMBED) xfree(oldptr);
5807 }
5808 else {
5809 if (!STR_SHARED_P(str)) {
5810 VALUE shared = heap_str_make_shared(rb_obj_class(str), str);
5811 rb_enc_cr_str_exact_copy(shared, str);
5812 OBJ_FREEZE(shared);
5813 }
5814 ptr = RSTRING(str)->as.heap.ptr += len;
5815 }
5816 STR_SET_LEN(str, nlen);
5817
5818 if (!SHARABLE_MIDDLE_SUBSTRING) {
5819 TERM_FILL(ptr + nlen, TERM_LEN(str));
5820 }
5822 return str;
5823}
5824
5825static void
5826rb_str_update_1(VALUE str, long beg, long len, VALUE val, long vbeg, long vlen)
5827{
5828 char *sptr;
5829 long slen;
5830 int cr;
5831
5832 if (beg == 0 && vlen == 0) {
5833 rb_str_drop_bytes(str, len);
5834 return;
5835 }
5836
5837 str_modify_keep_cr(str);
5838 RSTRING_GETMEM(str, sptr, slen);
5839 if (len < vlen) {
5840 /* expand string */
5841 RESIZE_CAPA(str, slen + vlen - len);
5842 sptr = RSTRING_PTR(str);
5843 }
5844
5846 cr = rb_enc_str_coderange(val);
5847 else
5849
5850 if (vlen != len) {
5851 memmove(sptr + beg + vlen,
5852 sptr + beg + len,
5853 slen - (beg + len));
5854 }
5855 if (vlen < beg && len < 0) {
5856 MEMZERO(sptr + slen, char, -len);
5857 }
5858 if (vlen > 0) {
5859 memmove(sptr + beg, RSTRING_PTR(val) + vbeg, vlen);
5860 }
5861 slen += vlen - len;
5862 STR_SET_LEN(str, slen);
5863 TERM_FILL(&sptr[slen], TERM_LEN(str));
5864 ENC_CODERANGE_SET(str, cr);
5865}
5866
5867static inline void
5868rb_str_update_0(VALUE str, long beg, long len, VALUE val)
5869{
5870 rb_str_update_1(str, beg, len, val, 0, RSTRING_LEN(val));
5871}
5872
5873void
5874rb_str_update(VALUE str, long beg, long len, VALUE val)
5875{
5876 long slen;
5877 char *p, *e;
5878 rb_encoding *enc;
5879 int singlebyte = single_byte_optimizable(str);
5880 int cr;
5881
5882 if (len < 0) rb_raise(rb_eIndexError, "negative length %ld", len);
5883
5884 StringValue(val);
5885 enc = rb_enc_check(str, val);
5886 slen = str_strlen(str, enc); /* rb_enc_check */
5887
5888 if ((slen < beg) || ((beg < 0) && (beg + slen < 0))) {
5889 rb_raise(rb_eIndexError, "index %ld out of string", beg);
5890 }
5891 if (beg < 0) {
5892 beg += slen;
5893 }
5894 RUBY_ASSERT(beg >= 0);
5895 RUBY_ASSERT(beg <= slen);
5896
5897 if (len > slen - beg) {
5898 len = slen - beg;
5899 }
5900 p = str_nth(RSTRING_PTR(str), RSTRING_END(str), beg, enc, singlebyte);
5901 if (!p) p = RSTRING_END(str);
5902 e = str_nth(p, RSTRING_END(str), len, enc, singlebyte);
5903 if (!e) e = RSTRING_END(str);
5904 /* error check */
5905 beg = p - RSTRING_PTR(str); /* physical position */
5906 len = e - p; /* physical length */
5907 rb_str_update_0(str, beg, len, val);
5908 rb_enc_associate(str, enc);
5910 if (cr != ENC_CODERANGE_BROKEN)
5911 ENC_CODERANGE_SET(str, cr);
5912}
5913
5914static void
5915rb_str_subpat_set(VALUE str, VALUE re, VALUE backref, VALUE val)
5916{
5917 int nth;
5918 VALUE match;
5919 long start, end, len;
5920 rb_encoding *enc;
5921 struct re_registers *regs;
5922
5923 if (rb_reg_search(re, str, 0, 0) < 0) {
5924 rb_raise(rb_eIndexError, "regexp not matched");
5925 }
5926 match = rb_backref_get();
5927 nth = rb_reg_backref_number(match, backref);
5928 regs = RMATCH_REGS(match);
5929 /* 1 <= regs->num_regs <= ONIG_MAX_CAPTURE_GROUP_NUM (= 32767) */
5930 if (nth >= regs->num_regs || nth + regs->num_regs <= 0) {
5931 rb_raise(rb_eIndexError, "index %d out of regexp", nth);
5932 }
5933 if (nth < 0) {
5934 nth += regs->num_regs;
5935 }
5936
5937 start = BEG(nth);
5938 if (start == -1) {
5939 rb_raise(rb_eIndexError, "regexp group %d not matched", nth);
5940 }
5941 end = END(nth);
5942 len = end - start;
5943
5944 StringValue(val);
5945 if (start + len > RSTRING_LEN(str)) {
5946 rb_raise(rb_eRuntimeError, "string modified");
5947 }
5948
5949 enc = rb_enc_check_str(str, val);
5950 rb_str_update_0(str, start, len, val);
5951 rb_enc_associate(str, enc);
5952}
5953
5954static VALUE
5955rb_str_aset(VALUE str, VALUE indx, VALUE val)
5956{
5957 long idx, beg;
5958
5959 switch (TYPE(indx)) {
5960 case T_REGEXP:
5961 rb_str_subpat_set(str, indx, INT2FIX(0), val);
5962 return val;
5963
5964 case T_STRING:
5965 beg = rb_str_index(str, indx, 0);
5966 if (beg < 0) {
5967 rb_raise(rb_eIndexError, "string not matched");
5968 }
5969 beg = rb_str_sublen(str, beg);
5970 rb_str_update(str, beg, str_strlen(indx, NULL), val);
5971 return val;
5972
5973 default:
5974 /* check if indx is Range */
5975 {
5976 long beg, len;
5977 if (rb_range_beg_len(indx, &beg, &len, str_strlen(str, NULL), 2)) {
5978 rb_str_update(str, beg, len, val);
5979 return val;
5980 }
5981 }
5982 /* FALLTHROUGH */
5983
5984 case T_FIXNUM:
5985 idx = NUM2LONG(indx);
5986 rb_str_update(str, idx, 1, val);
5987 return val;
5988 }
5989}
5990
5991/*
5992 * call-seq:
5993 * self[index] = other_string -> new_string
5994 * self[start, length] = other_string -> new_string
5995 * self[range] = other_string -> new_string
5996 * self[regexp, capture = 0] = other_string -> new_string
5997 * self[substring] = other_string -> new_string
5998 *
5999 * :include: doc/string/aset.rdoc
6000 *
6001 */
6002
6003static VALUE
6004rb_str_aset_m(int argc, VALUE *argv, VALUE str)
6005{
6006 if (argc == 3) {
6007 if (RB_TYPE_P(argv[0], T_REGEXP)) {
6008 rb_str_subpat_set(str, argv[0], argv[1], argv[2]);
6009 }
6010 else {
6011 rb_str_update(str, NUM2LONG(argv[0]), NUM2LONG(argv[1]), argv[2]);
6012 }
6013 return argv[2];
6014 }
6015 rb_check_arity(argc, 2, 3);
6016 return rb_str_aset(str, argv[0], argv[1]);
6017}
6018
6019/*
6020 * call-seq:
6021 * insert(offset, other_string) -> self
6022 *
6023 * :include: doc/string/insert.rdoc
6024 *
6025 */
6026
6027static VALUE
6028rb_str_insert(VALUE str, VALUE idx, VALUE str2)
6029{
6030 long pos = NUM2LONG(idx);
6031
6032 if (pos == -1) {
6033 return rb_str_append(str, str2);
6034 }
6035 else if (pos < 0) {
6036 pos++;
6037 }
6038 rb_str_update(str, pos, 0, str2);
6039 return str;
6040}
6041
6042
6043/*
6044 * call-seq:
6045 * slice!(index) -> new_string or nil
6046 * slice!(start, length) -> new_string or nil
6047 * slice!(range) -> new_string or nil
6048 * slice!(regexp, capture = 0) -> new_string or nil
6049 * slice!(substring) -> new_string or nil
6050 *
6051 * Like String#[] (and its alias String#slice), except that:
6052 *
6053 * - Performs substitutions in +self+ (not in a copy of +self+).
6054 * - Returns the removed substring if any modifications were made, +nil+ otherwise.
6055 *
6056 * A few examples:
6057 *
6058 * s = 'hello'
6059 * s.slice!('e') # => "e"
6060 * s # => "hllo"
6061 * s.slice!('e') # => nil
6062 * s # => "hllo"
6063 *
6064 * Related: see {Modifying}[rdoc-ref:String@Modifying].
6065 */
6066
6067static VALUE
6068rb_str_slice_bang(int argc, VALUE *argv, VALUE str)
6069{
6070 VALUE result = Qnil;
6071 VALUE indx;
6072 long beg, len = 1;
6073 char *p;
6074
6075 rb_check_arity(argc, 1, 2);
6076 str_modify_keep_cr(str);
6077 indx = argv[0];
6078 if (RB_TYPE_P(indx, T_REGEXP)) {
6079 if (rb_reg_search(indx, str, 0, 0) < 0) return Qnil;
6080 VALUE match = rb_backref_get();
6081 struct re_registers *regs = RMATCH_REGS(match);
6082 int nth = 0;
6083 if (argc > 1 && (nth = rb_reg_backref_number(match, argv[1])) < 0) {
6084 if ((nth += regs->num_regs) <= 0) return Qnil;
6085 }
6086 else if (nth >= regs->num_regs) return Qnil;
6087 beg = BEG(nth);
6088 len = END(nth) - beg;
6089 /* Converting the backref may have modified the string. */
6090 if (beg > RSTRING_LEN(str)) return Qnil;
6091 if (len > RSTRING_LEN(str) - beg) len = RSTRING_LEN(str) - beg;
6092 goto subseq;
6093 }
6094 else if (argc == 2) {
6095 beg = NUM2LONG(indx);
6096 len = NUM2LONG(argv[1]);
6097 goto num_index;
6098 }
6099 else if (FIXNUM_P(indx)) {
6100 beg = FIX2LONG(indx);
6101 if (!(p = rb_str_subpos(str, beg, &len))) return Qnil;
6102 if (!len) return Qnil;
6103 beg = p - RSTRING_PTR(str);
6104 goto subseq;
6105 }
6106 else if (RB_TYPE_P(indx, T_STRING)) {
6107 beg = rb_str_index(str, indx, 0);
6108 if (beg == -1) return Qnil;
6109 len = RSTRING_LEN(indx);
6110 result = str_duplicate(rb_cString, indx);
6111 goto squash;
6112 }
6113 else {
6114 switch (rb_range_beg_len(indx, &beg, &len, str_strlen(str, NULL), 0)) {
6115 case Qnil:
6116 return Qnil;
6117 case Qfalse:
6118 beg = NUM2LONG(indx);
6119 if (!(p = rb_str_subpos(str, beg, &len))) return Qnil;
6120 if (!len) return Qnil;
6121 beg = p - RSTRING_PTR(str);
6122 goto subseq;
6123 default:
6124 goto num_index;
6125 }
6126 }
6127
6128 num_index:
6129 if (!(p = rb_str_subpos(str, beg, &len))) return Qnil;
6130 beg = p - RSTRING_PTR(str);
6131
6132 subseq:
6133 result = str_enc_new(rb_cString, RSTRING_PTR(str)+beg, len, STR_ENC_GET(str));
6134 rb_enc_cr_str_copy_for_substr(result, str);
6135
6136 squash:
6137 if (len > 0) {
6138 if (beg == 0) {
6139 rb_str_drop_bytes(str, len);
6140 }
6141 else {
6142 char *sptr = RSTRING_PTR(str);
6143 long slen = RSTRING_LEN(str);
6144 if (beg + len > slen) /* pathological check */
6145 len = slen - beg;
6146 memmove(sptr + beg,
6147 sptr + beg + len,
6148 slen - (beg + len));
6149 slen -= len;
6150 STR_SET_LEN(str, slen);
6151 TERM_FILL(&sptr[slen], TERM_LEN(str));
6152 }
6153 }
6154 return result;
6155}
6156
6157static VALUE
6158get_pat(VALUE pat)
6159{
6160 VALUE val;
6161
6162 switch (OBJ_BUILTIN_TYPE(pat)) {
6163 case T_REGEXP:
6164 return pat;
6165
6166 case T_STRING:
6167 break;
6168
6169 default:
6170 val = rb_check_string_type(pat);
6171 if (NIL_P(val)) {
6172 Check_Type(pat, T_REGEXP);
6173 }
6174 pat = val;
6175 }
6176
6177 return rb_reg_regcomp(pat);
6178}
6179
6180static VALUE
6181get_pat_quoted(VALUE pat, int check)
6182{
6183 VALUE val;
6184
6185 switch (OBJ_BUILTIN_TYPE(pat)) {
6186 case T_REGEXP:
6187 return pat;
6188
6189 case T_STRING:
6190 break;
6191
6192 default:
6193 val = rb_check_string_type(pat);
6194 if (NIL_P(val)) {
6195 Check_Type(pat, T_REGEXP);
6196 }
6197 pat = val;
6198 }
6199 if (check && is_broken_string(pat)) {
6200 rb_exc_raise(rb_reg_check_preprocess(pat));
6201 }
6202 return pat;
6203}
6204
6205static long
6206rb_pat_search0(VALUE pat, VALUE str, long pos, int set_backref_str, VALUE *match)
6207{
6208 if (BUILTIN_TYPE(pat) == T_STRING) {
6209 pos = rb_str_byteindex(str, pat, pos);
6210 if (set_backref_str) {
6211 if (pos >= 0) {
6212 str = rb_str_new_frozen_String(str);
6213 VALUE match_data = rb_backref_set_string(str, pos, RSTRING_LEN(pat));
6214 if (match) {
6215 *match = match_data;
6216 }
6217 }
6218 else {
6220 }
6221 }
6222 return pos;
6223 }
6224 else {
6225 return rb_reg_search0(pat, str, pos, 0, set_backref_str, match);
6226 }
6227}
6228
6229static long
6230rb_pat_search(VALUE pat, VALUE str, long pos, int set_backref_str)
6231{
6232 return rb_pat_search0(pat, str, pos, set_backref_str, NULL);
6233}
6234
6235
6236/*
6237 * call-seq:
6238 * sub!(pattern, replacement) -> self or nil
6239 * sub!(pattern) {|match| ... } -> self or nil
6240 *
6241 * Like String#sub, except that:
6242 *
6243 * - Changes are made to +self+, not to copy of +self+.
6244 * - Returns +self+ if any changes are made, +nil+ otherwise.
6245 *
6246 * Related: see {Modifying}[rdoc-ref:String@Modifying].
6247 */
6248
6249static VALUE
6250rb_str_sub_bang(int argc, VALUE *argv, VALUE str)
6251{
6252 VALUE pat, repl, hash = Qnil;
6253 int iter = 0;
6254 long plen;
6255 int min_arity = rb_block_given_p() ? 1 : 2;
6256 long beg;
6257
6258 rb_check_arity(argc, min_arity, 2);
6259 if (argc == 1) {
6260 iter = 1;
6261 }
6262 else {
6263 repl = argv[1];
6264 hash = rb_check_hash_type(argv[1]);
6265 if (NIL_P(hash)) {
6266 StringValue(repl);
6267 }
6268 }
6269
6270 pat = get_pat_quoted(argv[0], 1);
6271
6272 str_modifiable(str);
6273 beg = rb_pat_search(pat, str, 0, 1);
6274 if (beg >= 0) {
6275 rb_encoding *enc;
6276 int cr = ENC_CODERANGE(str);
6277 long beg0, end0;
6278 VALUE match, match0 = Qnil;
6279 struct re_registers *regs;
6280 char *p, *rp;
6281 long len, rlen;
6282
6283 match = rb_backref_get();
6284 regs = RMATCH_REGS(match);
6285 if (RB_TYPE_P(pat, T_STRING)) {
6286 beg0 = beg;
6287 end0 = beg0 + RSTRING_LEN(pat);
6288 match0 = pat;
6289 }
6290 else {
6291 beg0 = BEG(0);
6292 end0 = END(0);
6293 if (iter) match0 = rb_reg_nth_match(0, match);
6294 }
6295
6296 if (iter || !NIL_P(hash)) {
6297 p = RSTRING_PTR(str); len = RSTRING_LEN(str);
6298
6299 if (iter) {
6300 repl = rb_obj_as_string(rb_yield(match0));
6301 }
6302 else {
6303 repl = rb_hash_aref(hash, rb_str_subseq(str, beg0, end0 - beg0));
6304 repl = rb_obj_as_string(repl);
6305 }
6306 str_mod_check(str, p, len);
6307 rb_check_frozen(str);
6308 }
6309 else {
6310 repl = rb_reg_regsub(repl, str, regs, RB_TYPE_P(pat, T_STRING) ? Qnil : pat);
6311 }
6312
6313 enc = rb_enc_compatible(str, repl);
6314 if (!enc) {
6315 rb_encoding *str_enc = STR_ENC_GET(str);
6316 p = RSTRING_PTR(str); len = RSTRING_LEN(str);
6317 if (coderange_scan(p, beg0, str_enc) != ENC_CODERANGE_7BIT ||
6318 coderange_scan(p+end0, len-end0, str_enc) != ENC_CODERANGE_7BIT) {
6319 rb_raise(rb_eEncCompatError, "incompatible character encodings: %s and %s",
6320 rb_enc_inspect_name(str_enc),
6321 rb_enc_inspect_name(STR_ENC_GET(repl)));
6322 }
6323 enc = STR_ENC_GET(repl);
6324 }
6325 rb_str_modify(str);
6326 rb_enc_associate(str, enc);
6328 int cr2 = ENC_CODERANGE(repl);
6329 if (cr2 == ENC_CODERANGE_BROKEN ||
6330 (cr == ENC_CODERANGE_VALID && cr2 == ENC_CODERANGE_7BIT))
6332 else
6333 cr = cr2;
6334 }
6335 plen = end0 - beg0;
6336 rlen = RSTRING_LEN(repl);
6337 len = RSTRING_LEN(str);
6338 if (rlen > plen) {
6339 RESIZE_CAPA(str, len + rlen - plen);
6340 }
6341 p = RSTRING_PTR(str);
6342 if (rlen != plen) {
6343 memmove(p + beg0 + rlen, p + beg0 + plen, len - beg0 - plen);
6344 }
6345 rp = RSTRING_PTR(repl);
6346 memmove(p + beg0, rp, rlen);
6347 len += rlen - plen;
6348 STR_SET_LEN(str, len);
6349 TERM_FILL(&RSTRING_PTR(str)[len], TERM_LEN(str));
6350 ENC_CODERANGE_SET(str, cr);
6351
6352 RB_GC_GUARD(match);
6353
6354 return str;
6355 }
6356 return Qnil;
6357}
6358
6359
6360/*
6361 * call-seq:
6362 * sub(pattern, replacement) -> new_string
6363 * sub(pattern) {|match| ... } -> new_string
6364 *
6365 * :include: doc/string/sub.rdoc
6366 */
6367
6368static VALUE
6369rb_str_sub(int argc, VALUE *argv, VALUE str)
6370{
6371 str = str_duplicate(rb_cString, str);
6372 rb_str_sub_bang(argc, argv, str);
6373 return str;
6374}
6375
6376static VALUE
6377str_gsub(int argc, VALUE *argv, VALUE str, int bang)
6378{
6379 VALUE pat, val = Qnil, repl, match0 = Qnil, dest, hash = Qnil, match = Qnil;
6380 long beg, beg0, end0;
6381 long offset, blen, slen, len, last;
6382 enum {STR, ITER, FAST_MAP, MAP} mode = STR;
6383 char *sp, *cp;
6384 int need_backref_str = -1;
6385 rb_encoding *str_enc;
6386
6387 switch (argc) {
6388 case 1:
6389 RETURN_ENUMERATOR(str, argc, argv);
6390 mode = ITER;
6391 break;
6392 case 2:
6393 repl = argv[1];
6394 hash = rb_check_hash_type(argv[1]);
6395 if (NIL_P(hash)) {
6396 StringValue(repl);
6397 }
6398 else if (rb_hash_default_unredefined(hash) && !FL_TEST_RAW(hash, RHASH_PROC_DEFAULT)) {
6399 mode = FAST_MAP;
6400 }
6401 else {
6402 mode = MAP;
6403 }
6404 break;
6405 default:
6406 rb_error_arity(argc, 1, 2);
6407 }
6408
6409 pat = get_pat_quoted(argv[0], 1);
6410 beg = rb_pat_search0(pat, str, 0, need_backref_str, &match);
6411
6412 if (beg < 0) {
6413 if (bang) return Qnil; /* no match, no substitution */
6414 return str_duplicate(rb_cString, str);
6415 }
6416
6417 offset = 0;
6418 blen = RSTRING_LEN(str) + 30; /* len + margin */
6419 dest = rb_str_buf_new(blen);
6420 sp = RSTRING_PTR(str);
6421 slen = RSTRING_LEN(str);
6422 cp = sp;
6423 str_enc = STR_ENC_GET(str);
6424 rb_enc_associate(dest, str_enc);
6425 ENC_CODERANGE_SET(dest, rb_enc_asciicompat(str_enc) ? ENC_CODERANGE_7BIT : ENC_CODERANGE_VALID);
6426
6427 do {
6428 struct re_registers *regs = RMATCH_REGS(match);
6429 if (RB_TYPE_P(pat, T_STRING)) {
6430 beg0 = beg;
6431 end0 = beg0 + RSTRING_LEN(pat);
6432 match0 = pat;
6433 }
6434 else {
6435 beg0 = BEG(0);
6436 end0 = END(0);
6437 if (mode == ITER) match0 = rb_reg_nth_match(0, match);
6438 }
6439
6440 if (mode != STR) {
6441 if (mode == ITER) {
6442 val = rb_obj_as_string(rb_yield(match0));
6443 }
6444 else {
6445 struct RString fake_str = {RBASIC_INIT};
6446 VALUE key;
6447 if (mode == FAST_MAP) {
6448 // It is safe to use a fake_str here because we established that it won't escape,
6449 // as it's only used for `rb_hash_aref` and we checked the hash doesn't have a
6450 // default proc.
6451 key = setup_fake_str(&fake_str, sp + beg0, end0 - beg0, ENCODING_GET_INLINED(str));
6452 }
6453 else {
6454 key = rb_str_subseq(str, beg0, end0 - beg0);
6455 }
6456 val = rb_hash_aref(hash, key);
6457 val = rb_obj_as_string(val);
6458 }
6459 str_mod_check(str, sp, slen);
6460 if (val == dest) { /* paranoid check [ruby-dev:24827] */
6461 rb_raise(rb_eRuntimeError, "block should not cheat");
6462 }
6463 }
6464 else if (need_backref_str) {
6465 val = rb_reg_regsub(repl, str, regs, RB_TYPE_P(pat, T_STRING) ? Qnil : pat);
6466 if (need_backref_str < 0) {
6467 need_backref_str = val != repl;
6468 }
6469 }
6470 else {
6471 val = repl;
6472 }
6473
6474 len = beg0 - offset; /* copy pre-match substr */
6475 if (len) {
6476 rb_enc_str_buf_cat(dest, cp, len, str_enc);
6477 }
6478
6479 rb_str_buf_append(dest, val);
6480
6481 last = offset;
6482 offset = end0;
6483 if (beg0 == end0) {
6484 /*
6485 * Always consume at least one character of the input string
6486 * in order to prevent infinite loops.
6487 */
6488 if (RSTRING_LEN(str) <= end0) break;
6489 len = rb_enc_fast_mbclen(RSTRING_PTR(str)+end0, RSTRING_END(str), str_enc);
6490 rb_enc_str_buf_cat(dest, RSTRING_PTR(str)+end0, len, str_enc);
6491 offset = end0 + len;
6492 }
6493 cp = RSTRING_PTR(str) + offset;
6494 if (offset > RSTRING_LEN(str)) break;
6495
6496 // In FAST_MAP and STR mode the backref can't escape so we can re-use the MatchData safely.
6497 if (mode != FAST_MAP && mode != STR) {
6498 match = Qnil;
6499 }
6500 beg = rb_pat_search0(pat, str, offset, need_backref_str, &match);
6501
6502 RB_GC_GUARD(match);
6503 } while (beg >= 0);
6504
6505 if (RSTRING_LEN(str) > offset) {
6506 rb_enc_str_buf_cat(dest, cp, RSTRING_LEN(str) - offset, str_enc);
6507 }
6508 rb_pat_search0(pat, str, last, 1, &match);
6509 if (bang) {
6510 str_shared_replace(str, dest);
6511 }
6512 else {
6513 str = dest;
6514 }
6515
6516 return str;
6517}
6518
6519
6520/*
6521 * call-seq:
6522 * gsub!(pattern, replacement) -> self or nil
6523 * gsub!(pattern) {|match| ... } -> self or nil
6524 * gsub!(pattern) -> an_enumerator
6525 *
6526 * Like String#gsub, except that:
6527 *
6528 * - Performs substitutions in +self+ (not in a copy of +self+).
6529 * - Returns +self+ if any characters are removed, +nil+ otherwise.
6530 *
6531 * Related: see {Modifying}[rdoc-ref:String@Modifying].
6532 */
6533
6534static VALUE
6535rb_str_gsub_bang(int argc, VALUE *argv, VALUE str)
6536{
6537 str_modify_keep_cr(str);
6538 return str_gsub(argc, argv, str, 1);
6539}
6540
6541
6542/*
6543 * call-seq:
6544 * gsub(pattern, replacement) -> new_string
6545 * gsub(pattern) {|match| ... } -> new_string
6546 * gsub(pattern) -> enumerator
6547 *
6548 * Returns a copy of +self+ with zero or more substrings replaced.
6549 *
6550 * Argument +pattern+ may be a string or a Regexp;
6551 * argument +replacement+ may be a string or a Hash.
6552 * Varying types for the argument values makes this method very versatile.
6553 *
6554 * Below are some simple examples;
6555 * for many more examples, see {Substitution Methods}[rdoc-ref:String@Substitution+Methods].
6556 *
6557 * With arguments +pattern+ and string +replacement+ given,
6558 * replaces each matching substring with the given +replacement+ string:
6559 *
6560 * s = 'abracadabra'
6561 * s.gsub('ab', 'AB') # => "ABracadABra"
6562 * s.gsub(/[a-c]/, 'X') # => "XXrXXXdXXrX"
6563 *
6564 * With arguments +pattern+ and hash +replacement+ given,
6565 * replaces each matching substring with a value from the given +replacement+ hash,
6566 * or removes it:
6567 *
6568 * h = {'a' => 'A', 'b' => 'B', 'c' => 'C'}
6569 * s.gsub(/[a-c]/, h) # => "ABrACAdABrA" # 'a', 'b', 'c' replaced.
6570 * s.gsub(/[a-d]/, h) # => "ABrACAABrA" # 'd' removed.
6571 *
6572 * With argument +pattern+ and a block given,
6573 * calls the block with each matching substring;
6574 * replaces that substring with the block's return value:
6575 *
6576 * s.gsub(/[a-d]/) {|substring| substring.upcase }
6577 * # => "ABrACADABrA"
6578 *
6579 * With argument +pattern+ and no block given,
6580 * returns a new Enumerator.
6581 *
6582 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
6583 */
6584
6585static VALUE
6586rb_str_gsub(int argc, VALUE *argv, VALUE str)
6587{
6588 return str_gsub(argc, argv, str, 0);
6589}
6590
6591
6592/*
6593 * call-seq:
6594 * replace(other_string) -> self
6595 *
6596 * Replaces the contents of +self+ with the contents of +other_string+;
6597 * returns +self+:
6598 *
6599 * s = 'foo' # => "foo"
6600 * s.replace('bar') # => "bar"
6601 *
6602 * Related: see {Modifying}[rdoc-ref:String@Modifying].
6603 */
6604
6605VALUE
6607{
6608 str_modifiable(str);
6609 if (str == str2) return str;
6610
6611 StringValue(str2);
6612 str_discard(str);
6613 return str_replace(str, str2);
6614}
6615
6616/*
6617 * call-seq:
6618 * clear -> self
6619 *
6620 * Removes the contents of +self+:
6621 *
6622 * s = 'foo'
6623 * s.clear # => ""
6624 * s # => ""
6625 *
6626 * Related: see {Modifying}[rdoc-ref:String@Modifying].
6627 */
6628
6629static VALUE
6630rb_str_clear(VALUE str)
6631{
6632 str_discard(str);
6633 STR_SET_EMBED(str);
6634 STR_SET_LEN(str, 0);
6635 RSTRING_PTR(str)[0] = 0;
6636 if (rb_enc_asciicompat(STR_ENC_GET(str)))
6638 else
6640 return str;
6641}
6642
6643/*
6644 * call-seq:
6645 * chr -> string
6646 *
6647 * :include: doc/string/chr.rdoc
6648 *
6649 */
6650
6651static VALUE
6652rb_str_chr(VALUE str)
6653{
6654 return rb_str_substr(str, 0, 1);
6655}
6656
6657/*
6658 * call-seq:
6659 * getbyte(index) -> integer or nil
6660 *
6661 * :include: doc/string/getbyte.rdoc
6662 *
6663 */
6664VALUE
6665rb_str_getbyte(VALUE str, VALUE index)
6666{
6667 long pos = NUM2LONG(index);
6668
6669 if (pos < 0)
6670 pos += RSTRING_LEN(str);
6671 if (pos < 0 || RSTRING_LEN(str) <= pos)
6672 return Qnil;
6673
6674 return INT2FIX((unsigned char)RSTRING_PTR(str)[pos]);
6675}
6676
6677/*
6678 * call-seq:
6679 * setbyte(index, integer) -> integer
6680 *
6681 * Sets the byte at zero-based offset +index+ to the value of the given +integer+;
6682 * returns +integer+:
6683 *
6684 * s = 'xyzzy'
6685 * s.setbyte(2, 129) # => 129
6686 * s # => "xy\x81zy"
6687 *
6688 * Related: see {Modifying}[rdoc-ref:String@Modifying].
6689 */
6690VALUE
6691rb_str_setbyte(VALUE str, VALUE index, VALUE value)
6692{
6693 long pos = NUM2LONG(index);
6694 char *ptr, *head, *left = 0;
6695 rb_encoding *enc;
6696 int cr = ENC_CODERANGE_UNKNOWN, width, nlen;
6697
6698 VALUE v = rb_to_int(value);
6699 VALUE w = rb_int_and(v, INT2FIX(0xff));
6700 char byte = (char)(NUM2INT(w) & 0xFF);
6701
6702 if (!str_independent(str))
6703 str_make_independent(str);
6704
6705 long len = RSTRING_LEN(str);
6706 if (pos < -len || len <= pos)
6707 rb_raise(rb_eIndexError, "index %ld out of string", pos);
6708 if (pos < 0)
6709 pos += len;
6710
6711 enc = STR_ENC_GET(str);
6712 head = RSTRING_PTR(str);
6713 ptr = &head[pos];
6714 if (!STR_EMBED_P(str)) {
6715 cr = ENC_CODERANGE(str);
6716 switch (cr) {
6717 case ENC_CODERANGE_7BIT:
6718 left = ptr;
6719 *ptr = byte;
6720 if (ISASCII(byte)) goto end;
6721 nlen = rb_enc_precise_mbclen(left, head+len, enc);
6722 if (!MBCLEN_CHARFOUND_P(nlen))
6724 else
6726 goto end;
6728 left = rb_enc_left_char_head(head, ptr, head+len, enc);
6729 width = rb_enc_precise_mbclen(left, head+len, enc);
6730 *ptr = byte;
6731 nlen = rb_enc_precise_mbclen(left, head+len, enc);
6732 if (!MBCLEN_CHARFOUND_P(nlen))
6734 else if (MBCLEN_CHARFOUND_LEN(nlen) != width || ISASCII(byte))
6736 goto end;
6737 }
6738 }
6740 *ptr = byte;
6741
6742 end:
6743 return value;
6744}
6745
6746static VALUE
6747str_byte_substr(VALUE str, long beg, long len, int empty)
6748{
6749 long n = RSTRING_LEN(str);
6750
6751 if (beg > n || len < 0) return Qnil;
6752 if (beg < 0) {
6753 beg += n;
6754 if (beg < 0) return Qnil;
6755 }
6756 if (len > n - beg)
6757 len = n - beg;
6758 if (len <= 0) {
6759 if (!empty) return Qnil;
6760 len = 0;
6761 }
6762
6763 VALUE str2 = str_subseq(str, beg, len);
6764
6765 str_enc_copy_direct(str2, str);
6766
6767 if (RSTRING_LEN(str2) == 0) {
6768 if (!rb_enc_asciicompat(STR_ENC_GET(str)))
6770 else
6772 }
6773 else {
6774 switch (ENC_CODERANGE(str)) {
6775 case ENC_CODERANGE_7BIT:
6777 break;
6778 default:
6780 break;
6781 }
6782 }
6783
6784 return str2;
6785}
6786
6787VALUE
6788rb_str_byte_substr(VALUE str, VALUE beg, VALUE len)
6789{
6790 return str_byte_substr(str, NUM2LONG(beg), NUM2LONG(len), TRUE);
6791}
6792
6793static VALUE
6794str_byte_aref(VALUE str, VALUE indx)
6795{
6796 long idx;
6797 if (FIXNUM_P(indx)) {
6798 idx = FIX2LONG(indx);
6799 }
6800 else {
6801 /* check if indx is Range */
6802 long beg, len = RSTRING_LEN(str);
6803
6804 switch (rb_range_beg_len(indx, &beg, &len, len, 0)) {
6805 case Qfalse:
6806 break;
6807 case Qnil:
6808 return Qnil;
6809 default:
6810 return str_byte_substr(str, beg, len, TRUE);
6811 }
6812
6813 idx = NUM2LONG(indx);
6814 }
6815 return str_byte_substr(str, idx, 1, FALSE);
6816}
6817
6818/*
6819 * call-seq:
6820 * byteslice(offset, length = 1) -> string or nil
6821 * byteslice(range) -> string or nil
6822 *
6823 * :include: doc/string/byteslice.rdoc
6824 */
6825
6826static VALUE
6827rb_str_byteslice(int argc, VALUE *argv, VALUE str)
6828{
6829 if (argc == 2) {
6830 long beg = NUM2LONG(argv[0]);
6831 long len = NUM2LONG(argv[1]);
6832 return str_byte_substr(str, beg, len, TRUE);
6833 }
6834 rb_check_arity(argc, 1, 2);
6835 return str_byte_aref(str, argv[0]);
6836}
6837
6838static void
6839str_check_beg_len(VALUE str, long *beg, long *len)
6840{
6841 long end, slen = RSTRING_LEN(str);
6842
6843 if (*len < 0) rb_raise(rb_eIndexError, "negative length %ld", *len);
6844 if ((slen < *beg) || ((*beg < 0) && (*beg + slen < 0))) {
6845 rb_raise(rb_eIndexError, "index %ld out of string", *beg);
6846 }
6847 if (*beg < 0) {
6848 *beg += slen;
6849 }
6850 RUBY_ASSERT(*beg >= 0);
6851 RUBY_ASSERT(*beg <= slen);
6852
6853 if (*len > slen - *beg) {
6854 *len = slen - *beg;
6855 }
6856 end = *beg + *len;
6857 str_ensure_byte_pos(str, *beg);
6858 str_ensure_byte_pos(str, end);
6859}
6860
6861/*
6862 * call-seq:
6863 * bytesplice(offset, length, str) -> self
6864 * bytesplice(offset, length, str, str_offset, str_length) -> self
6865 * bytesplice(range, str) -> self
6866 * bytesplice(range, str, str_range) -> self
6867 *
6868 * :include: doc/string/bytesplice.rdoc
6869 */
6870
6871static VALUE
6872rb_str_bytesplice(int argc, VALUE *argv, VALUE str)
6873{
6874 long beg, len, vbeg, vlen;
6875 VALUE val;
6876 int cr;
6877
6878 rb_check_arity(argc, 2, 5);
6879 if (!(argc == 2 || argc == 3 || argc == 5)) {
6880 rb_raise(rb_eArgError, "wrong number of arguments (given %d, expected 2, 3, or 5)", argc);
6881 }
6882 if (argc == 2 || (argc == 3 && !RB_INTEGER_TYPE_P(argv[0]))) {
6883 if (!rb_range_beg_len(argv[0], &beg, &len, RSTRING_LEN(str), 2)) {
6884 rb_raise(rb_eTypeError, "wrong argument type %s (expected Range)",
6885 rb_builtin_class_name(argv[0]));
6886 }
6887 val = argv[1];
6888 StringValue(val);
6889 if (argc == 2) {
6890 /* bytesplice(range, str) */
6891 vbeg = 0;
6892 vlen = RSTRING_LEN(val);
6893 }
6894 else {
6895 /* bytesplice(range, str, str_range) */
6896 if (!rb_range_beg_len(argv[2], &vbeg, &vlen, RSTRING_LEN(val), 2)) {
6897 rb_raise(rb_eTypeError, "wrong argument type %s (expected Range)",
6898 rb_builtin_class_name(argv[2]));
6899 }
6900 }
6901 }
6902 else {
6903 beg = NUM2LONG(argv[0]);
6904 len = NUM2LONG(argv[1]);
6905 val = argv[2];
6906 StringValue(val);
6907 if (argc == 3) {
6908 /* bytesplice(index, length, str) */
6909 vbeg = 0;
6910 vlen = RSTRING_LEN(val);
6911 }
6912 else {
6913 /* bytesplice(index, length, str, str_index, str_length) */
6914 vbeg = NUM2LONG(argv[3]);
6915 vlen = NUM2LONG(argv[4]);
6916 }
6917 }
6918 str_check_beg_len(str, &beg, &len);
6919 str_check_beg_len(val, &vbeg, &vlen);
6920 str_modify_keep_cr(str);
6921
6922 if (RB_UNLIKELY(ENCODING_GET_INLINED(str) != ENCODING_GET_INLINED(val))) {
6923 rb_enc_associate(str, rb_enc_check(str, val));
6924 }
6925
6926 rb_str_update_1(str, beg, len, val, vbeg, vlen);
6928 if (cr != ENC_CODERANGE_BROKEN)
6929 ENC_CODERANGE_SET(str, cr);
6930 return str;
6931}
6932
6933/*
6934 * call-seq:
6935 * reverse -> new_string
6936 *
6937 * Returns a new string with the characters from +self+ in reverse order.
6938 *
6939 * 'drawer'.reverse # => "reward"
6940 * 'reviled'.reverse # => "deliver"
6941 * 'stressed'.reverse # => "desserts"
6942 * 'semordnilaps'.reverse # => "spalindromes"
6943 *
6944 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
6945 */
6946
6947static VALUE
6948rb_str_reverse(VALUE str)
6949{
6950 rb_encoding *enc;
6951 VALUE rev;
6952 char *s, *e, *p;
6953 int cr;
6954
6955 if (RSTRING_LEN(str) <= 1) return str_duplicate(rb_cString, str);
6956 enc = STR_ENC_GET(str);
6957 rev = str_enc_new(rb_cString, 0, RSTRING_LEN(str), enc);
6958 s = RSTRING_PTR(str); e = RSTRING_END(str);
6959 p = RSTRING_END(rev);
6960 cr = ENC_CODERANGE(str);
6961
6962 if (RSTRING_LEN(str) > 1) {
6963 if (single_byte_optimizable(str)) {
6964 while (s < e) {
6965 *--p = *s++;
6966 }
6967 }
6968 else if (cr == ENC_CODERANGE_VALID) {
6969 while (s < e) {
6970 int clen = rb_enc_fast_mbclen(s, e, enc);
6971
6972 p -= clen;
6973 memcpy(p, s, clen);
6974 s += clen;
6975 }
6976 }
6977 else {
6978 cr = rb_enc_asciicompat(enc) ?
6980 while (s < e) {
6981 int clen = rb_enc_mbclen(s, e, enc);
6982
6983 if (clen > 1 || (*s & 0x80)) cr = ENC_CODERANGE_UNKNOWN;
6984 p -= clen;
6985 memcpy(p, s, clen);
6986 s += clen;
6987 }
6988 }
6989 }
6990 STR_SET_LEN(rev, RSTRING_LEN(str));
6991 ENC_CODERANGE_SET(rev, cr);
6992
6993 return rev;
6994}
6995
6996
6997/*
6998 * call-seq:
6999 * reverse! -> self
7000 *
7001 * Returns +self+ with its characters reversed:
7002 *
7003 * 'drawer'.reverse! # => "reward"
7004 * 'reviled'.reverse! # => "deliver"
7005 * 'stressed'.reverse! # => "desserts"
7006 * 'semordnilaps'.reverse! # => "spalindromes"
7007 *
7008 * Related: see {Modifying}[rdoc-ref:String@Modifying].
7009 */
7010
7011static VALUE
7012rb_str_reverse_bang(VALUE str)
7013{
7014 if (RSTRING_LEN(str) > 1) {
7015 if (single_byte_optimizable(str)) {
7016 char *s, *e, c;
7017
7018 str_modify_keep_cr(str);
7019 s = RSTRING_PTR(str);
7020 e = RSTRING_END(str) - 1;
7021 while (s < e) {
7022 c = *s;
7023 *s++ = *e;
7024 *e-- = c;
7025 }
7026 }
7027 else {
7028 str_shared_replace(str, rb_str_reverse(str));
7029 }
7030 }
7031 else {
7032 str_modify_keep_cr(str);
7033 }
7034 return str;
7035}
7036
7037
7038/*
7039 * call-seq:
7040 * include?(other_string) -> true or false
7041 *
7042 * Returns whether +self+ contains +other_string+:
7043 *
7044 * s = 'bar'
7045 * s.include?('ba') # => true
7046 * s.include?('ar') # => true
7047 * s.include?('bar') # => true
7048 * s.include?('a') # => true
7049 * s.include?('') # => true
7050 * s.include?('foo') # => false
7051 *
7052 * Related: see {Querying}[rdoc-ref:String@Querying].
7053 */
7054
7055VALUE
7056rb_str_include(VALUE str, VALUE arg)
7057{
7058 long i;
7059
7060 StringValue(arg);
7061 i = rb_str_index(str, arg, 0);
7062
7063 return RBOOL(i != -1);
7064}
7065
7066
7067/*
7068 * call-seq:
7069 * to_i(base = 10) -> integer
7070 *
7071 * Returns the result of interpreting leading characters in +self+
7072 * as an integer in the given +base+;
7073 * +base+ must be either +0+ or in range <tt>(2..36)</tt>:
7074 *
7075 * '123456'.to_i # => 123456
7076 * '123def'.to_i(16) # => 1195503
7077 *
7078 * With +base+ zero given, string +object+ may contain leading characters
7079 * to specify the actual base:
7080 *
7081 * '123def'.to_i(0) # => 123
7082 * '0123def'.to_i(0) # => 83
7083 * '0b123def'.to_i(0) # => 1
7084 * '0o123def'.to_i(0) # => 83
7085 * '0d123def'.to_i(0) # => 123
7086 * '0x123def'.to_i(0) # => 1195503
7087 *
7088 * Characters past a leading valid number (in the given +base+) are ignored:
7089 *
7090 * '12.345'.to_i # => 12
7091 * '12345'.to_i(2) # => 1
7092 *
7093 * Returns zero if there is no leading valid number:
7094 *
7095 * 'abcdef'.to_i # => 0
7096 * '2'.to_i(2) # => 0
7097 *
7098 * Related: see {Converting to Non-String}[rdoc-ref:String@Converting+to+Non--5CString].
7099 */
7100
7101static VALUE
7102rb_str_to_i(int argc, VALUE *argv, VALUE str)
7103{
7104 int base = 10;
7105
7106 if (rb_check_arity(argc, 0, 1) && (base = NUM2INT(argv[0])) < 0) {
7107 rb_raise(rb_eArgError, "invalid radix %d", base);
7108 }
7109 return rb_str_to_inum(str, base, FALSE);
7110}
7111
7112
7113/*
7114 * call-seq:
7115 * to_f -> float
7116 *
7117 * Returns the result of interpreting leading characters in +self+ as a Float:
7118 *
7119 * '3.14159'.to_f # => 3.14159
7120 * '1.234e-2'.to_f # => 0.01234
7121 *
7122 * Characters past a leading valid number are ignored:
7123 *
7124 * '3.14 (pi to two places)'.to_f # => 3.14
7125 *
7126 * Returns zero if there is no leading valid number:
7127 *
7128 * 'abcdef'.to_f # => 0.0
7129 *
7130 * See {Converting to Non-String}[rdoc-ref:String@Converting+to+Non--5CString].
7131 */
7132
7133static VALUE
7134rb_str_to_f(VALUE str)
7135{
7136 return DBL2NUM(rb_str_to_dbl(str, FALSE));
7137}
7138
7139
7140/*
7141 * call-seq:
7142 * to_s -> self or new_string
7143 *
7144 * Returns +self+ if +self+ is a +String+,
7145 * or +self+ converted to a +String+ if +self+ is a subclass of +String+.
7146 *
7147 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
7148 */
7149
7150static VALUE
7151rb_str_to_s(VALUE str)
7152{
7153 if (rb_obj_class(str) != rb_cString) {
7154 return str_duplicate(rb_cString, str);
7155 }
7156 return str;
7157}
7158
7159#if 0
7160static void
7161str_cat_char(VALUE str, unsigned int c, rb_encoding *enc)
7162{
7163 char s[RUBY_MAX_CHAR_LEN];
7164 int n = rb_enc_codelen(c, enc);
7165
7166 rb_enc_mbcput(c, s, enc);
7167 rb_enc_str_buf_cat(str, s, n, enc);
7168}
7169#endif
7170
7171#define CHAR_ESC_LEN 13 /* sizeof(\x{ hex of 32bit unsigned int } \0) */
7172
7173int
7174rb_str_buf_cat_escaped_char(VALUE result, unsigned int c, int unicode_p)
7175{
7176 char buf[CHAR_ESC_LEN + 1];
7177 int l;
7178
7179#if SIZEOF_INT > 4
7180 c &= 0xffffffff;
7181#endif
7182 if (unicode_p) {
7183 if (c < 0x7F && ISPRINT(c)) {
7184 snprintf(buf, CHAR_ESC_LEN, "%c", c);
7185 }
7186 else if (c < 0x10000) {
7187 snprintf(buf, CHAR_ESC_LEN, "\\u%04X", c);
7188 }
7189 else {
7190 snprintf(buf, CHAR_ESC_LEN, "\\u{%X}", c);
7191 }
7192 }
7193 else {
7194 if (c < 0x100) {
7195 snprintf(buf, CHAR_ESC_LEN, "\\x%02X", c);
7196 }
7197 else {
7198 snprintf(buf, CHAR_ESC_LEN, "\\x{%X}", c);
7199 }
7200 }
7201 l = (int)strlen(buf); /* CHAR_ESC_LEN cannot exceed INT_MAX */
7202 rb_str_buf_cat(result, buf, l);
7203 return l;
7204}
7205
7206const char *
7207ruby_escaped_char(int c)
7208{
7209 switch (c) {
7210 case '\0': return "\\0";
7211 case '\n': return "\\n";
7212 case '\r': return "\\r";
7213 case '\t': return "\\t";
7214 case '\f': return "\\f";
7215 case '\013': return "\\v";
7216 case '\010': return "\\b";
7217 case '\007': return "\\a";
7218 case '\033': return "\\e";
7219 case '\x7f': return "\\c?";
7220 }
7221 return NULL;
7222}
7223
7224VALUE
7225rb_str_escape(VALUE str)
7226{
7227 int encidx = ENCODING_GET(str);
7228 rb_encoding *enc = rb_enc_from_index(encidx);
7229 const char *p = RSTRING_PTR(str);
7230 const char *pend = RSTRING_END(str);
7231 const char *prev = p;
7232 char buf[CHAR_ESC_LEN + 1];
7233 VALUE result = rb_str_buf_new(0);
7234 int unicode_p = rb_enc_unicode_p(enc);
7235 int asciicompat = rb_enc_asciicompat(enc);
7236
7237 while (p < pend) {
7238 unsigned int c;
7239 const char *cc;
7240 int n = rb_enc_precise_mbclen(p, pend, enc);
7241 if (!MBCLEN_CHARFOUND_P(n)) {
7242 if (p > prev) str_buf_cat(result, prev, p - prev);
7243 n = rb_enc_mbminlen(enc);
7244 if (pend < p + n)
7245 n = (int)(pend - p);
7246 while (n--) {
7247 snprintf(buf, CHAR_ESC_LEN, "\\x%02X", *p & 0377);
7248 str_buf_cat(result, buf, strlen(buf));
7249 prev = ++p;
7250 }
7251 continue;
7252 }
7253 n = MBCLEN_CHARFOUND_LEN(n);
7254 c = rb_enc_mbc_to_codepoint(p, pend, enc);
7255 p += n;
7256 cc = ruby_escaped_char(c);
7257 if (cc) {
7258 if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
7259 str_buf_cat(result, cc, strlen(cc));
7260 prev = p;
7261 }
7262 else if (asciicompat && rb_enc_isascii(c, enc) && ISPRINT(c)) {
7263 }
7264 else {
7265 if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
7266 rb_str_buf_cat_escaped_char(result, c, unicode_p);
7267 prev = p;
7268 }
7269 }
7270 if (p > prev) str_buf_cat(result, prev, p - prev);
7271 ENCODING_CODERANGE_SET(result, rb_usascii_encindex(), ENC_CODERANGE_7BIT);
7272
7273 return result;
7274}
7275
7276/*
7277 * call-seq:
7278 * inspect -> string
7279 *
7280 * :include: doc/string/inspect.rdoc
7281 *
7282 */
7283
7284VALUE
7286{
7287 int encidx = ENCODING_GET(str);
7288 rb_encoding *enc = rb_enc_from_index(encidx);
7289 const char *p, *pend, *prev;
7290 char buf[CHAR_ESC_LEN + 1];
7291 VALUE result = rb_str_buf_new(0);
7292 rb_encoding *resenc = rb_default_internal_encoding();
7293 int unicode_p = rb_enc_unicode_p(enc);
7294 int asciicompat = rb_enc_asciicompat(enc);
7295
7296 if (resenc == NULL) resenc = rb_default_external_encoding();
7297 if (!rb_enc_asciicompat(resenc)) resenc = rb_usascii_encoding();
7298 rb_enc_associate(result, resenc);
7299 str_buf_cat2(result, "\"");
7300
7301 p = RSTRING_PTR(str); pend = RSTRING_END(str);
7302 prev = p;
7303 while (p < pend) {
7304 unsigned int c, cc;
7305 int n;
7306
7307 n = rb_enc_precise_mbclen(p, pend, enc);
7308 if (!MBCLEN_CHARFOUND_P(n)) {
7309 if (p > prev) str_buf_cat(result, prev, p - prev);
7310 n = rb_enc_mbminlen(enc);
7311 if (pend < p + n)
7312 n = (int)(pend - p);
7313 while (n--) {
7314 snprintf(buf, CHAR_ESC_LEN, "\\x%02X", *p & 0377);
7315 str_buf_cat(result, buf, strlen(buf));
7316 prev = ++p;
7317 }
7318 continue;
7319 }
7320 n = MBCLEN_CHARFOUND_LEN(n);
7321 c = rb_enc_mbc_to_codepoint(p, pend, enc);
7322 p += n;
7323 if ((asciicompat || unicode_p) &&
7324 (c == '"'|| c == '\\' ||
7325 (c == '#' &&
7326 p < pend &&
7327 MBCLEN_CHARFOUND_P(rb_enc_precise_mbclen(p,pend,enc)) &&
7328 (cc = rb_enc_codepoint(p,pend,enc),
7329 (cc == '$' || cc == '@' || cc == '{'))))) {
7330 if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
7331 str_buf_cat2(result, "\\");
7332 if (asciicompat || enc == resenc) {
7333 prev = p - n;
7334 continue;
7335 }
7336 }
7337 switch (c) {
7338 case '\n': cc = 'n'; break;
7339 case '\r': cc = 'r'; break;
7340 case '\t': cc = 't'; break;
7341 case '\f': cc = 'f'; break;
7342 case '\013': cc = 'v'; break;
7343 case '\010': cc = 'b'; break;
7344 case '\007': cc = 'a'; break;
7345 case 033: cc = 'e'; break;
7346 default: cc = 0; break;
7347 }
7348 if (cc) {
7349 if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
7350 buf[0] = '\\';
7351 buf[1] = (char)cc;
7352 str_buf_cat(result, buf, 2);
7353 prev = p;
7354 continue;
7355 }
7356 /* The special casing of 0x85 (NEXT_LINE) here is because
7357 * Oniguruma historically treats it as printable, but it
7358 * doesn't match the print POSIX bracket class or character
7359 * property in regexps.
7360 *
7361 * See Ruby Bug #16842 for details:
7362 * https://bugs.ruby-lang.org/issues/16842
7363 */
7364 if ((enc == resenc && rb_enc_isprint(c, enc) && c != 0x85) ||
7365 (asciicompat && rb_enc_isascii(c, enc) && ISPRINT(c))) {
7366 continue;
7367 }
7368 else {
7369 if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
7370 rb_str_buf_cat_escaped_char(result, c, unicode_p);
7371 prev = p;
7372 continue;
7373 }
7374 }
7375 if (p > prev) str_buf_cat(result, prev, p - prev);
7376 str_buf_cat2(result, "\"");
7377
7378 return result;
7379}
7380
7381#define IS_EVSTR(p,e) ((p) < (e) && (*(p) == '$' || *(p) == '@' || *(p) == '{'))
7382
7383/*
7384 * call-seq:
7385 * dump -> new_string
7386 *
7387 * :include: doc/string/dump.rdoc
7388 *
7389 */
7390
7391VALUE
7393{
7394 int encidx = rb_enc_get_index(str);
7395 rb_encoding *enc = rb_enc_from_index(encidx);
7396 long len;
7397 const char *p, *pend;
7398 char *q, *qend;
7399 VALUE result;
7400 int u8 = (encidx == rb_utf8_encindex());
7401 static const char nonascii_suffix[] = ".dup.force_encoding(\"%s\")";
7402
7403 len = 2; /* "" */
7404 if (!rb_enc_asciicompat(enc)) {
7405 len += strlen(nonascii_suffix) - rb_strlen_lit("%s");
7406 len += strlen(enc->name);
7407 }
7408
7409 p = RSTRING_PTR(str); pend = p + RSTRING_LEN(str);
7410 while (p < pend) {
7411 int clen;
7412 unsigned char c = *p++;
7413
7414 switch (c) {
7415 case '"': case '\\':
7416 case '\n': case '\r':
7417 case '\t': case '\f':
7418 case '\013': case '\010': case '\007': case '\033':
7419 clen = 2;
7420 break;
7421
7422 case '#':
7423 clen = IS_EVSTR(p, pend) ? 2 : 1;
7424 break;
7425
7426 default:
7427 if (ISPRINT(c)) {
7428 clen = 1;
7429 }
7430 else {
7431 if (u8 && c > 0x7F) { /* \u notation */
7432 int n = rb_enc_precise_mbclen(p-1, pend, enc);
7433 if (MBCLEN_CHARFOUND_P(n)) {
7434 unsigned int cc = rb_enc_mbc_to_codepoint(p-1, pend, enc);
7435 if (cc <= 0xFFFF)
7436 clen = 6; /* \uXXXX */
7437 else if (cc <= 0xFFFFF)
7438 clen = 9; /* \u{XXXXX} */
7439 else
7440 clen = 10; /* \u{XXXXXX} */
7441 p += MBCLEN_CHARFOUND_LEN(n)-1;
7442 break;
7443 }
7444 }
7445 clen = 4; /* \xNN */
7446 }
7447 break;
7448 }
7449
7450 if (clen > LONG_MAX - len) {
7451 rb_raise(rb_eRuntimeError, "string size too big");
7452 }
7453 len += clen;
7454 }
7455
7456 result = rb_str_new(0, len);
7457 p = RSTRING_PTR(str); pend = p + RSTRING_LEN(str);
7458 q = RSTRING_PTR(result); qend = q + len + 1;
7459
7460 *q++ = '"';
7461 while (p < pend) {
7462 unsigned char c = *p++;
7463
7464 if (c == '"' || c == '\\') {
7465 *q++ = '\\';
7466 *q++ = c;
7467 }
7468 else if (c == '#') {
7469 if (IS_EVSTR(p, pend)) *q++ = '\\';
7470 *q++ = '#';
7471 }
7472 else if (c == '\n') {
7473 *q++ = '\\';
7474 *q++ = 'n';
7475 }
7476 else if (c == '\r') {
7477 *q++ = '\\';
7478 *q++ = 'r';
7479 }
7480 else if (c == '\t') {
7481 *q++ = '\\';
7482 *q++ = 't';
7483 }
7484 else if (c == '\f') {
7485 *q++ = '\\';
7486 *q++ = 'f';
7487 }
7488 else if (c == '\013') {
7489 *q++ = '\\';
7490 *q++ = 'v';
7491 }
7492 else if (c == '\010') {
7493 *q++ = '\\';
7494 *q++ = 'b';
7495 }
7496 else if (c == '\007') {
7497 *q++ = '\\';
7498 *q++ = 'a';
7499 }
7500 else if (c == '\033') {
7501 *q++ = '\\';
7502 *q++ = 'e';
7503 }
7504 else if (ISPRINT(c)) {
7505 *q++ = c;
7506 }
7507 else {
7508 *q++ = '\\';
7509 if (u8) {
7510 int n = rb_enc_precise_mbclen(p-1, pend, enc) - 1;
7511 if (MBCLEN_CHARFOUND_P(n)) {
7512 int cc = rb_enc_mbc_to_codepoint(p-1, pend, enc);
7513 p += n;
7514 if (cc <= 0xFFFF)
7515 snprintf(q, qend-q, "u%04X", cc); /* \uXXXX */
7516 else
7517 snprintf(q, qend-q, "u{%X}", cc); /* \u{XXXXX} or \u{XXXXXX} */
7518 q += strlen(q);
7519 continue;
7520 }
7521 }
7522 snprintf(q, qend-q, "x%02X", c);
7523 q += 3;
7524 }
7525 }
7526 *q++ = '"';
7527 *q = '\0';
7528 if (!rb_enc_asciicompat(enc)) {
7529 snprintf(q, qend-q, nonascii_suffix, enc->name);
7530 encidx = rb_ascii8bit_encindex();
7531 }
7532 /* result from dump is ASCII */
7533 rb_enc_associate_index(result, encidx);
7535 return result;
7536}
7537
7538static int
7539unescape_ascii(unsigned int c)
7540{
7541 switch (c) {
7542 case 'n':
7543 return '\n';
7544 case 'r':
7545 return '\r';
7546 case 't':
7547 return '\t';
7548 case 'f':
7549 return '\f';
7550 case 'v':
7551 return '\13';
7552 case 'b':
7553 return '\010';
7554 case 'a':
7555 return '\007';
7556 case 'e':
7557 return 033;
7558 }
7560}
7561
7562static void
7563undump_after_backslash(VALUE undumped, const char **ss, const char *s_end, rb_encoding **penc, bool *utf8, bool *binary)
7564{
7565 const char *s = *ss;
7566 unsigned int c;
7567 int codelen;
7568 size_t hexlen;
7569 unsigned char buf[6];
7570 static rb_encoding *enc_utf8 = NULL;
7571
7572 switch (*s) {
7573 case '\\':
7574 case '"':
7575 case '#':
7576 rb_str_cat(undumped, s, 1); /* cat itself */
7577 s++;
7578 break;
7579 case 'n':
7580 case 'r':
7581 case 't':
7582 case 'f':
7583 case 'v':
7584 case 'b':
7585 case 'a':
7586 case 'e':
7587 *buf = unescape_ascii(*s);
7588 rb_str_cat(undumped, (char *)buf, 1);
7589 s++;
7590 break;
7591 case 'u':
7592 if (*binary) {
7593 rb_raise(rb_eRuntimeError, "hex escape and Unicode escape are mixed");
7594 }
7595 *utf8 = true;
7596 if (++s >= s_end) {
7597 rb_raise(rb_eRuntimeError, "invalid Unicode escape");
7598 }
7599 if (enc_utf8 == NULL) enc_utf8 = rb_utf8_encoding();
7600 if (*penc != enc_utf8) {
7601 *penc = enc_utf8;
7602 rb_enc_associate(undumped, enc_utf8);
7603 }
7604 if (*s == '{') { /* handle \u{...} form */
7605 s++;
7606 for (;;) {
7607 if (s >= s_end) {
7608 rb_raise(rb_eRuntimeError, "unterminated Unicode escape");
7609 }
7610 if (*s == '}') {
7611 s++;
7612 break;
7613 }
7614 if (ISSPACE(*s)) {
7615 s++;
7616 continue;
7617 }
7618 c = scan_hex(s, s_end-s, &hexlen);
7619 if (hexlen == 0 || hexlen > 6) {
7620 rb_raise(rb_eRuntimeError, "invalid Unicode escape");
7621 }
7622 if (c > 0x10ffff) {
7623 rb_raise(rb_eRuntimeError, "invalid Unicode codepoint (too large)");
7624 }
7625 if (0xd800 <= c && c <= 0xdfff) {
7626 rb_raise(rb_eRuntimeError, "invalid Unicode codepoint");
7627 }
7628 codelen = rb_enc_mbcput(c, (char *)buf, *penc);
7629 rb_str_cat(undumped, (char *)buf, codelen);
7630 s += hexlen;
7631 }
7632 }
7633 else { /* handle \uXXXX form */
7634 c = scan_hex(s, 4, &hexlen);
7635 if (hexlen != 4) {
7636 rb_raise(rb_eRuntimeError, "invalid Unicode escape");
7637 }
7638 if (0xd800 <= c && c <= 0xdfff) {
7639 rb_raise(rb_eRuntimeError, "invalid Unicode codepoint");
7640 }
7641 codelen = rb_enc_mbcput(c, (char *)buf, *penc);
7642 rb_str_cat(undumped, (char *)buf, codelen);
7643 s += hexlen;
7644 }
7645 break;
7646 case 'x':
7647 if (++s >= s_end) {
7648 rb_raise(rb_eRuntimeError, "invalid hex escape");
7649 }
7650 *buf = scan_hex(s, 2, &hexlen);
7651 if (hexlen != 2) {
7652 rb_raise(rb_eRuntimeError, "invalid hex escape");
7653 }
7654 if (!ISASCII(*buf)) {
7655 if (*utf8) {
7656 rb_raise(rb_eRuntimeError, "hex escape and Unicode escape are mixed");
7657 }
7658 *binary = true;
7659 }
7660 rb_str_cat(undumped, (char *)buf, 1);
7661 s += hexlen;
7662 break;
7663 default:
7664 rb_str_cat(undumped, s-1, 2);
7665 s++;
7666 }
7667
7668 *ss = s;
7669}
7670
7671static VALUE rb_str_is_ascii_only_p(VALUE str);
7672
7673/*
7674 * call-seq:
7675 * undump -> new_string
7676 *
7677 * Inverse of String#dump; returns a copy of +self+ with changes of the kinds made by String#dump "undone."
7678 *
7679 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
7680 */
7681
7682static VALUE
7683str_undump(VALUE str)
7684{
7685 const char *s = RSTRING_PTR(str);
7686 const char *s_end = RSTRING_END(str);
7687 rb_encoding *enc = rb_enc_get(str);
7688 VALUE undumped = rb_enc_str_new(s, 0L, enc);
7689 bool utf8 = false;
7690 bool binary = false;
7691 int w;
7692
7694 if (rb_str_is_ascii_only_p(str) == Qfalse) {
7695 rb_raise(rb_eRuntimeError, "non-ASCII character detected");
7696 }
7697 if (!str_null_check(str, &w)) {
7698 rb_raise(rb_eRuntimeError, "string contains null byte");
7699 }
7700 if (RSTRING_LEN(str) < 2) goto invalid_format;
7701 if (*s != '"') goto invalid_format;
7702
7703 /* strip '"' at the start */
7704 s++;
7705
7706 for (;;) {
7707 if (s >= s_end) {
7708 rb_raise(rb_eRuntimeError, "unterminated dumped string");
7709 }
7710
7711 if (*s == '"') {
7712 /* epilogue */
7713 s++;
7714 if (s == s_end) {
7715 /* ascii compatible dumped string */
7716 break;
7717 }
7718 else {
7719 static const char force_encoding_suffix[] = ".force_encoding(\""; /* "\")" */
7720 static const char dup_suffix[] = ".dup";
7721 const char *encname;
7722 int encidx;
7723 ptrdiff_t size;
7724
7725 /* check separately for strings dumped by older versions */
7726 size = sizeof(dup_suffix) - 1;
7727 if (s_end - s > size && memcmp(s, dup_suffix, size) == 0) s += size;
7728
7729 size = sizeof(force_encoding_suffix) - 1;
7730 if (s_end - s <= size) goto invalid_format;
7731 if (memcmp(s, force_encoding_suffix, size) != 0) goto invalid_format;
7732 s += size;
7733
7734 if (utf8) {
7735 rb_raise(rb_eRuntimeError, "dumped string contained Unicode escape but used force_encoding");
7736 }
7737
7738 encname = s;
7739 s = memchr(s, '"', s_end-s);
7740 size = s - encname;
7741 if (!s) goto invalid_format;
7742 if (s_end - s != 2) goto invalid_format;
7743 if (s[0] != '"' || s[1] != ')') goto invalid_format;
7744
7745 encidx = rb_enc_find_index2(encname, (long)size);
7746 if (encidx < 0) {
7747 rb_raise(rb_eRuntimeError, "dumped string has unknown encoding name");
7748 }
7749 rb_enc_associate_index(undumped, encidx);
7750 }
7751 break;
7752 }
7753
7754 if (*s == '\\') {
7755 s++;
7756 if (s >= s_end) {
7757 rb_raise(rb_eRuntimeError, "invalid escape");
7758 }
7759 undump_after_backslash(undumped, &s, s_end, &enc, &utf8, &binary);
7760 }
7761 else {
7762 rb_str_cat(undumped, s++, 1);
7763 }
7764 }
7765
7766 RB_GC_GUARD(str);
7767
7768 return undumped;
7769invalid_format:
7770 rb_raise(rb_eRuntimeError, "invalid dumped string; not wrapped with '\"' nor '\"...\".force_encoding(\"...\")' form");
7771}
7772
7773static void
7774rb_str_check_dummy_enc(rb_encoding *enc)
7775{
7776 if (rb_enc_dummy_p(enc)) {
7777 rb_raise(rb_eEncCompatError, "incompatible encoding with this operation: %s",
7778 rb_enc_name(enc));
7779 }
7780}
7781
7782static rb_encoding *
7783str_true_enc(VALUE str)
7784{
7785 rb_encoding *enc = STR_ENC_GET(str);
7786 rb_str_check_dummy_enc(enc);
7787 return enc;
7788}
7789
7790static OnigCaseFoldType
7791check_case_options(int argc, VALUE *argv, OnigCaseFoldType flags)
7792{
7793 if (argc==0)
7794 return flags;
7795 if (argc>2)
7796 rb_raise(rb_eArgError, "too many options");
7797 if (argv[0]==sym_turkic) {
7798 flags |= ONIGENC_CASE_FOLD_TURKISH_AZERI;
7799 if (argc==2) {
7800 if (argv[1]==sym_lithuanian)
7801 flags |= ONIGENC_CASE_FOLD_LITHUANIAN;
7802 else
7803 rb_raise(rb_eArgError, "invalid second option");
7804 }
7805 }
7806 else if (argv[0]==sym_lithuanian) {
7807 flags |= ONIGENC_CASE_FOLD_LITHUANIAN;
7808 if (argc==2) {
7809 if (argv[1]==sym_turkic)
7810 flags |= ONIGENC_CASE_FOLD_TURKISH_AZERI;
7811 else
7812 rb_raise(rb_eArgError, "invalid second option");
7813 }
7814 }
7815 else if (argc>1)
7816 rb_raise(rb_eArgError, "too many options");
7817 else if (argv[0]==sym_ascii)
7818 flags |= ONIGENC_CASE_ASCII_ONLY;
7819 else if (argv[0]==sym_fold) {
7820 if ((flags & (ONIGENC_CASE_UPCASE|ONIGENC_CASE_DOWNCASE)) == ONIGENC_CASE_DOWNCASE)
7821 flags ^= ONIGENC_CASE_FOLD|ONIGENC_CASE_DOWNCASE;
7822 else
7823 rb_raise(rb_eArgError, "option :fold only allowed for downcasing");
7824 }
7825 else
7826 rb_raise(rb_eArgError, "invalid option");
7827 return flags;
7828}
7829
7830static inline bool
7831case_option_single_p(OnigCaseFoldType flags, rb_encoding *enc, VALUE str)
7832{
7833 if ((flags & ONIGENC_CASE_ASCII_ONLY) && (enc==rb_utf8_encoding() || rb_enc_mbmaxlen(enc) == 1))
7834 return true;
7835 return !(flags & ONIGENC_CASE_FOLD_TURKISH_AZERI) && ENC_CODERANGE(str) == ENC_CODERANGE_7BIT;
7836}
7837
7838/* 16 should be long enough to absorb any kind of single character length increase */
7839#define CASE_MAPPING_ADDITIONAL_LENGTH 20
7840#ifndef CASEMAP_DEBUG
7841# define CASEMAP_DEBUG 0
7842#endif
7843
7844struct mapping_buffer;
7845typedef struct mapping_buffer {
7846 size_t capa;
7847 size_t used;
7848 struct mapping_buffer *next;
7849 OnigUChar space[FLEX_ARY_LEN];
7851
7852static void
7853mapping_buffer_free(void *p)
7854{
7855 mapping_buffer *previous_buffer;
7856 mapping_buffer *current_buffer = p;
7857 while (current_buffer) {
7858 previous_buffer = current_buffer;
7859 current_buffer = current_buffer->next;
7860 ruby_sized_xfree(previous_buffer, previous_buffer->capa);
7861 }
7862}
7863
7864static const rb_data_type_t mapping_buffer_type = {
7865 "mapping_buffer",
7866 {0, mapping_buffer_free,},
7867 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
7868};
7869
7870static VALUE
7871rb_str_casemap(VALUE source, OnigCaseFoldType *flags, rb_encoding *enc)
7872{
7873 VALUE target;
7874
7875 const OnigUChar *source_current, *source_end;
7876 long target_length = 0;
7877 VALUE buffer_anchor;
7878 mapping_buffer *current_buffer = 0;
7879 mapping_buffer **pre_buffer;
7880 size_t buffer_count = 0;
7881 int buffer_length_or_invalid;
7882
7883 if (RSTRING_LEN(source) == 0) return str_duplicate(rb_cString, source);
7884 if (RSTRING_LEN(source) > INT_MAX - CASE_MAPPING_ADDITIONAL_LENGTH)
7885 rb_raise(rb_eArgError, "string size too big");
7886
7887 source_current = (OnigUChar*)RSTRING_PTR(source);
7888 source_end = (OnigUChar*)RSTRING_END(source);
7889
7890 buffer_anchor = TypedData_Wrap_Struct(0, &mapping_buffer_type, 0);
7891 pre_buffer = (mapping_buffer **)&DATA_PTR(buffer_anchor);
7892 while (source_current < source_end) {
7893 /* increase multiplier using buffer count to converge quickly */
7894 size_t capa = (size_t)(source_end-source_current);
7895 if (capa > (INT_MAX - CASE_MAPPING_ADDITIONAL_LENGTH) / ++buffer_count)
7896 capa = INT_MAX;
7897 else
7898 capa = capa * buffer_count + CASE_MAPPING_ADDITIONAL_LENGTH;
7899 if (CASEMAP_DEBUG) {
7900 fprintf(stderr, "Buffer allocation, capa is %"PRIuSIZE"\n", capa); /* for tuning */
7901 }
7902 current_buffer = xmalloc(offsetof(mapping_buffer, space) + capa);
7903 *pre_buffer = current_buffer;
7904 pre_buffer = &current_buffer->next;
7905 current_buffer->next = NULL;
7906 current_buffer->capa = capa;
7907 buffer_length_or_invalid = enc->case_map(flags,
7908 &source_current, source_end,
7909 current_buffer->space,
7910 current_buffer->space+current_buffer->capa,
7911 enc);
7912 if (buffer_length_or_invalid < 0 || buffer_length_or_invalid > LONG_MAX - target_length) {
7913 current_buffer = DATA_PTR(buffer_anchor);
7914 DATA_PTR(buffer_anchor) = 0;
7915 mapping_buffer_free(current_buffer);
7916 if (buffer_length_or_invalid < 0)
7917 rb_raise(rb_eArgError, "input string invalid");
7918 rb_raise(rb_eArgError, "string size too big");
7919 }
7920 target_length += current_buffer->used = buffer_length_or_invalid;
7921 }
7922 if (CASEMAP_DEBUG) {
7923 fprintf(stderr, "Buffer count is %"PRIuSIZE"\n", buffer_count); /* for tuning */
7924 }
7925
7926 if (buffer_count==1) {
7927 target = str_enc_new(rb_cString, (const char*)current_buffer->space, target_length, enc);
7928 }
7929 else {
7930 char *target_current;
7931
7932 target = str_enc_new(rb_cString, 0, target_length, enc);
7933 target_current = RSTRING_PTR(target);
7934 current_buffer = DATA_PTR(buffer_anchor);
7935 while (current_buffer) {
7936 memcpy(target_current, current_buffer->space, current_buffer->used);
7937 target_current += current_buffer->used;
7938 current_buffer = current_buffer->next;
7939 }
7940 }
7941 current_buffer = DATA_PTR(buffer_anchor);
7942 DATA_PTR(buffer_anchor) = 0;
7943 mapping_buffer_free(current_buffer);
7944
7945 RB_GC_GUARD(buffer_anchor);
7946
7947 /*ENC_CODERANGE_SET(mapped, cr);*/
7948
7949 return target;
7950}
7951
7952static VALUE
7953rb_str_ascii_casemap(VALUE source, VALUE target, OnigCaseFoldType *flags, rb_encoding *enc)
7954{
7955 const OnigUChar *source_current, *source_end;
7956 OnigUChar *target_current, *target_end;
7957 long old_length = RSTRING_LEN(source);
7958 int length_or_invalid;
7959
7960 if (old_length == 0) return Qnil;
7961
7962 source_current = (OnigUChar*)RSTRING_PTR(source);
7963 source_end = (OnigUChar*)RSTRING_END(source);
7964 if (source == target) {
7965 target_current = (OnigUChar*)source_current;
7966 target_end = (OnigUChar*)source_end;
7967 }
7968 else {
7969 target_current = (OnigUChar*)RSTRING_PTR(target);
7970 target_end = (OnigUChar*)RSTRING_END(target);
7971 }
7972
7973 length_or_invalid = onigenc_ascii_only_case_map(flags,
7974 &source_current, source_end,
7975 target_current, target_end, enc);
7976 if (length_or_invalid < 0)
7977 rb_raise(rb_eArgError, "input string invalid");
7978 if (CASEMAP_DEBUG && length_or_invalid != old_length) {
7979 fprintf(stderr, "problem with rb_str_ascii_casemap"
7980 "; old_length=%ld, new_length=%d\n", old_length, length_or_invalid);
7981 rb_raise(rb_eArgError, "internal problem with rb_str_ascii_casemap"
7982 "; old_length=%ld, new_length=%d\n", old_length, length_or_invalid);
7983 }
7984
7985 rb_enc_associate_index(target, ENCODING_GET(source));
7986
7987 return target;
7988}
7989
7990static bool
7991upcase_single(VALUE str)
7992{
7993 char *s = RSTRING_PTR(str), *send = RSTRING_END(str);
7994 bool modified = false;
7995
7996 while (s < send) {
7997 unsigned int c = *(unsigned char*)s;
7998
7999 if ('a' <= c && c <= 'z') {
8000 *s = 'A' + (c - 'a');
8001 modified = true;
8002 }
8003 s++;
8004 }
8005 return modified;
8006}
8007
8008/*
8009 * call-seq:
8010 * upcase!(mapping) -> self or nil
8011 *
8012 * Like String#upcase, except that:
8013 *
8014 * - Changes character casings in +self+ (not in a copy of +self+).
8015 * - Returns +self+ if any changes are made, +nil+ otherwise.
8016 *
8017 * Related: See {Modifying}[rdoc-ref:String@Modifying].
8018 */
8019
8020static VALUE
8021rb_str_upcase_bang(int argc, VALUE *argv, VALUE str)
8022{
8023 rb_encoding *enc;
8024 OnigCaseFoldType flags = ONIGENC_CASE_UPCASE;
8025
8026 flags = check_case_options(argc, argv, flags);
8027 str_modify_keep_cr(str);
8028 enc = str_true_enc(str);
8029 if (case_option_single_p(flags, enc, str)) {
8030 if (upcase_single(str))
8031 flags |= ONIGENC_CASE_MODIFIED;
8032 }
8033 else if (flags&ONIGENC_CASE_ASCII_ONLY)
8034 rb_str_ascii_casemap(str, str, &flags, enc);
8035 else
8036 str_shared_replace(str, rb_str_casemap(str, &flags, enc));
8037
8038 if (ONIGENC_CASE_MODIFIED&flags) return str;
8039 return Qnil;
8040}
8041
8042
8043/*
8044 * call-seq:
8045 * upcase(mapping = :ascii) -> new_string
8046 *
8047 * :include: doc/string/upcase.rdoc
8048 */
8049
8050static VALUE
8051rb_str_upcase(int argc, VALUE *argv, VALUE str)
8052{
8053 rb_encoding *enc;
8054 OnigCaseFoldType flags = ONIGENC_CASE_UPCASE;
8055 VALUE ret;
8056
8057 flags = check_case_options(argc, argv, flags);
8058 enc = str_true_enc(str);
8059 if (case_option_single_p(flags, enc, str)) {
8060 ret = rb_str_new(RSTRING_PTR(str), RSTRING_LEN(str));
8061 str_enc_copy_direct(ret, str);
8062 upcase_single(ret);
8063 }
8064 else if (flags&ONIGENC_CASE_ASCII_ONLY) {
8065 ret = rb_str_new(0, RSTRING_LEN(str));
8066 rb_str_ascii_casemap(str, ret, &flags, enc);
8067 }
8068 else {
8069 ret = rb_str_casemap(str, &flags, enc);
8070 }
8071
8072 return ret;
8073}
8074
8075static bool
8076downcase_single(VALUE str)
8077{
8078 char *s = RSTRING_PTR(str), *send = RSTRING_END(str);
8079 bool modified = false;
8080
8081 while (s < send) {
8082 unsigned int c = *(unsigned char*)s;
8083
8084 if ('A' <= c && c <= 'Z') {
8085 *s = 'a' + (c - 'A');
8086 modified = true;
8087 }
8088 s++;
8089 }
8090
8091 return modified;
8092}
8093
8094/*
8095 * call-seq:
8096 * downcase!(mapping) -> self or nil
8097 *
8098 * Like String#downcase, except that:
8099 *
8100 * - Changes character casings in +self+ (not in a copy of +self+).
8101 * - Returns +self+ if any changes are made, +nil+ otherwise.
8102 *
8103 * Related: See {Modifying}[rdoc-ref:String@Modifying].
8104 */
8105
8106static VALUE
8107rb_str_downcase_bang(int argc, VALUE *argv, VALUE str)
8108{
8109 rb_encoding *enc;
8110 OnigCaseFoldType flags = ONIGENC_CASE_DOWNCASE;
8111
8112 flags = check_case_options(argc, argv, flags);
8113 str_modify_keep_cr(str);
8114 enc = str_true_enc(str);
8115 if (case_option_single_p(flags, enc, str)) {
8116 if (downcase_single(str))
8117 flags |= ONIGENC_CASE_MODIFIED;
8118 }
8119 else if (flags&ONIGENC_CASE_ASCII_ONLY)
8120 rb_str_ascii_casemap(str, str, &flags, enc);
8121 else
8122 str_shared_replace(str, rb_str_casemap(str, &flags, enc));
8123
8124 if (ONIGENC_CASE_MODIFIED&flags) return str;
8125 return Qnil;
8126}
8127
8128
8129/*
8130 * call-seq:
8131 * downcase(mapping = :ascii) -> new_string
8132 *
8133 * :include: doc/string/downcase.rdoc
8134 *
8135 */
8136
8137static VALUE
8138rb_str_downcase(int argc, VALUE *argv, VALUE str)
8139{
8140 rb_encoding *enc;
8141 OnigCaseFoldType flags = ONIGENC_CASE_DOWNCASE;
8142 VALUE ret;
8143
8144 flags = check_case_options(argc, argv, flags);
8145 enc = str_true_enc(str);
8146 if (case_option_single_p(flags, enc, str)) {
8147 ret = rb_str_new(RSTRING_PTR(str), RSTRING_LEN(str));
8148 str_enc_copy_direct(ret, str);
8149 downcase_single(ret);
8150 }
8151 else if (flags&ONIGENC_CASE_ASCII_ONLY) {
8152 ret = rb_str_new(0, RSTRING_LEN(str));
8153 rb_str_ascii_casemap(str, ret, &flags, enc);
8154 }
8155 else {
8156 ret = rb_str_casemap(str, &flags, enc);
8157 }
8158
8159 return ret;
8160}
8161
8162
8163/*
8164 * call-seq:
8165 * capitalize!(mapping = :ascii) -> self or nil
8166 *
8167 * Like String#capitalize, except that:
8168 *
8169 * - Changes character casings in +self+ (not in a copy of +self+).
8170 * - Returns +self+ if any changes are made, +nil+ otherwise.
8171 *
8172 * Related: See {Modifying}[rdoc-ref:String@Modifying].
8173 */
8174
8175static VALUE
8176rb_str_capitalize_bang(int argc, VALUE *argv, VALUE str)
8177{
8178 rb_encoding *enc;
8179 OnigCaseFoldType flags = ONIGENC_CASE_UPCASE | ONIGENC_CASE_TITLECASE;
8180
8181 flags = check_case_options(argc, argv, flags);
8182 str_modify_keep_cr(str);
8183 enc = str_true_enc(str);
8184 if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return Qnil;
8185 if (flags&ONIGENC_CASE_ASCII_ONLY)
8186 rb_str_ascii_casemap(str, str, &flags, enc);
8187 else
8188 str_shared_replace(str, rb_str_casemap(str, &flags, enc));
8189
8190 if (ONIGENC_CASE_MODIFIED&flags) return str;
8191 return Qnil;
8192}
8193
8194
8195/*
8196 * call-seq:
8197 * capitalize(mapping = :ascii) -> new_string
8198 *
8199 * :include: doc/string/capitalize.rdoc
8200 *
8201 */
8202
8203static VALUE
8204rb_str_capitalize(int argc, VALUE *argv, VALUE str)
8205{
8206 rb_encoding *enc;
8207 OnigCaseFoldType flags = ONIGENC_CASE_UPCASE | ONIGENC_CASE_TITLECASE;
8208 VALUE ret;
8209
8210 flags = check_case_options(argc, argv, flags);
8211 enc = str_true_enc(str);
8212 if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return str;
8213 if (flags&ONIGENC_CASE_ASCII_ONLY) {
8214 ret = rb_str_new(0, RSTRING_LEN(str));
8215 rb_str_ascii_casemap(str, ret, &flags, enc);
8216 }
8217 else {
8218 ret = rb_str_casemap(str, &flags, enc);
8219 }
8220 return ret;
8221}
8222
8223
8224/*
8225 * call-seq:
8226 * swapcase!(mapping) -> self or nil
8227 *
8228 * Like String#swapcase, except that:
8229 *
8230 * - Changes are made to +self+, not to copy of +self+.
8231 * - Returns +self+ if any changes are made, +nil+ otherwise.
8232 *
8233 * Related: see {Modifying}[rdoc-ref:String@Modifying].
8234 */
8235
8236static VALUE
8237rb_str_swapcase_bang(int argc, VALUE *argv, VALUE str)
8238{
8239 rb_encoding *enc;
8240 OnigCaseFoldType flags = ONIGENC_CASE_UPCASE | ONIGENC_CASE_DOWNCASE;
8241
8242 flags = check_case_options(argc, argv, flags);
8243 str_modify_keep_cr(str);
8244 enc = str_true_enc(str);
8245 if (flags&ONIGENC_CASE_ASCII_ONLY)
8246 rb_str_ascii_casemap(str, str, &flags, enc);
8247 else
8248 str_shared_replace(str, rb_str_casemap(str, &flags, enc));
8249
8250 if (ONIGENC_CASE_MODIFIED&flags) return str;
8251 return Qnil;
8252}
8253
8254
8255/*
8256 * call-seq:
8257 * swapcase(mapping = :ascii) -> new_string
8258 *
8259 * :include: doc/string/swapcase.rdoc
8260 *
8261 */
8262
8263static VALUE
8264rb_str_swapcase(int argc, VALUE *argv, VALUE str)
8265{
8266 rb_encoding *enc;
8267 OnigCaseFoldType flags = ONIGENC_CASE_UPCASE | ONIGENC_CASE_DOWNCASE;
8268 VALUE ret;
8269
8270 flags = check_case_options(argc, argv, flags);
8271 enc = str_true_enc(str);
8272 if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return str_duplicate(rb_cString, str);
8273 if (flags&ONIGENC_CASE_ASCII_ONLY) {
8274 ret = rb_str_new(0, RSTRING_LEN(str));
8275 rb_str_ascii_casemap(str, ret, &flags, enc);
8276 }
8277 else {
8278 ret = rb_str_casemap(str, &flags, enc);
8279 }
8280 return ret;
8281}
8282
8283typedef unsigned char *USTR;
8284
8285struct tr {
8286 int gen;
8287 unsigned int now, max;
8288 char *p, *pend;
8289};
8290
8291static unsigned int
8292trnext(struct tr *t, rb_encoding *enc)
8293{
8294 int n;
8295
8296 for (;;) {
8297 nextpart:
8298 if (!t->gen) {
8299 if (t->p == t->pend) return -1;
8300 if (rb_enc_ascget(t->p, t->pend, &n, enc) == '\\' && t->p + n < t->pend) {
8301 t->p += n;
8302 }
8303 t->now = rb_enc_codepoint_len(t->p, t->pend, &n, enc);
8304 t->p += n;
8305 if (rb_enc_ascget(t->p, t->pend, &n, enc) == '-' && t->p + n < t->pend) {
8306 t->p += n;
8307 if (t->p < t->pend) {
8308 unsigned int c = rb_enc_codepoint_len(t->p, t->pend, &n, enc);
8309 t->p += n;
8310 if (t->now > c) {
8311 if (t->now < 0x80 && c < 0x80) {
8312 rb_raise(rb_eArgError,
8313 "invalid range \"%c-%c\" in string transliteration",
8314 t->now, c);
8315 }
8316 else {
8317 rb_raise(rb_eArgError, "invalid range in string transliteration");
8318 }
8319 continue; /* not reached */
8320 }
8321 else if (t->now < c) {
8322 t->gen = 1;
8323 t->max = c;
8324 }
8325 }
8326 }
8327 return t->now;
8328 }
8329 else {
8330 while (ONIGENC_CODE_TO_MBCLEN(enc, ++t->now) <= 0) {
8331 if (t->now == t->max) {
8332 t->gen = 0;
8333 goto nextpart;
8334 }
8335 }
8336 if (t->now < t->max) {
8337 return t->now;
8338 }
8339 else {
8340 t->gen = 0;
8341 return t->max;
8342 }
8343 }
8344 }
8345}
8346
8347static VALUE rb_str_delete_bang(int,VALUE*,VALUE);
8348
8349static VALUE
8350tr_trans(VALUE str, VALUE src, VALUE repl, int sflag)
8351{
8352 const unsigned int errc = -1;
8353 unsigned int trans[256];
8354 rb_encoding *enc, *e1, *e2;
8355 struct tr trsrc, trrepl;
8356 int cflag = 0;
8357 unsigned int c, c0, last = 0;
8358 int modify = 0, i, l;
8359 unsigned char *s, *send;
8360 VALUE hash = 0;
8361 int singlebyte = single_byte_optimizable(str);
8362 int termlen;
8363 int cr;
8364
8365#define CHECK_IF_ASCII(c) \
8366 (void)((cr == ENC_CODERANGE_7BIT && !rb_isascii(c)) ? \
8367 (cr = ENC_CODERANGE_VALID) : 0)
8368
8369 StringValue(src);
8370 StringValue(repl);
8371 if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return Qnil;
8372 if (RSTRING_LEN(repl) == 0) {
8373 return rb_str_delete_bang(1, &src, str);
8374 }
8375
8376 cr = ENC_CODERANGE(str);
8377 e1 = rb_enc_check(str, src);
8378 e2 = rb_enc_check(str, repl);
8379 if (e1 == e2) {
8380 enc = e1;
8381 }
8382 else {
8383 enc = rb_enc_check(src, repl);
8384 }
8385 trsrc.p = RSTRING_PTR(src); trsrc.pend = trsrc.p + RSTRING_LEN(src);
8386 if (RSTRING_LEN(src) > 1 &&
8387 rb_enc_ascget(trsrc.p, trsrc.pend, &l, enc) == '^' &&
8388 trsrc.p + l < trsrc.pend) {
8389 cflag = 1;
8390 trsrc.p += l;
8391 }
8392 trrepl.p = RSTRING_PTR(repl);
8393 trrepl.pend = trrepl.p + RSTRING_LEN(repl);
8394 trsrc.gen = trrepl.gen = 0;
8395 trsrc.now = trrepl.now = 0;
8396 trsrc.max = trrepl.max = 0;
8397
8398 if (cflag) {
8399 for (i=0; i<256; i++) {
8400 trans[i] = 1;
8401 }
8402 while ((c = trnext(&trsrc, enc)) != errc) {
8403 if (c < 256) {
8404 trans[c] = errc;
8405 }
8406 else {
8407 if (!hash) hash = rb_hash_new();
8408 rb_hash_aset(hash, UINT2NUM(c), Qtrue);
8409 }
8410 }
8411 while ((c = trnext(&trrepl, enc)) != errc)
8412 /* retrieve last replacer */;
8413 last = trrepl.now;
8414 for (i=0; i<256; i++) {
8415 if (trans[i] != errc) {
8416 trans[i] = last;
8417 }
8418 }
8419 }
8420 else {
8421 unsigned int r;
8422
8423 for (i=0; i<256; i++) {
8424 trans[i] = errc;
8425 }
8426 while ((c = trnext(&trsrc, enc)) != errc) {
8427 r = trnext(&trrepl, enc);
8428 if (r == errc) r = trrepl.now;
8429 if (c < 256) {
8430 trans[c] = r;
8431 if (rb_enc_codelen(r, enc) != 1) singlebyte = 0;
8432 }
8433 else {
8434 if (!hash) hash = rb_hash_new();
8435 rb_hash_aset(hash, UINT2NUM(c), UINT2NUM(r));
8436 }
8437 }
8438 }
8439
8440 if (cr == ENC_CODERANGE_VALID && rb_enc_asciicompat(e1))
8441 cr = ENC_CODERANGE_7BIT;
8442 str_modify_keep_cr(str);
8443 s = (unsigned char *)RSTRING_PTR(str); send = (unsigned char *)RSTRING_END(str);
8444 termlen = rb_enc_mbminlen(enc);
8445 if (sflag) {
8446 int clen, tlen;
8447 long offset, max = RSTRING_LEN(str);
8448 unsigned int save = -1;
8449 unsigned char *buf = ALLOC_N(unsigned char, max + termlen), *t = buf;
8450
8451 while (s < send) {
8452 int may_modify = 0;
8453
8454 int r = rb_enc_precise_mbclen((char *)s, (char *)send, e1);
8455 if (!MBCLEN_CHARFOUND_P(r)) {
8456 xfree(buf);
8457 rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(e1));
8458 }
8459 clen = MBCLEN_CHARFOUND_LEN(r);
8460 c0 = c = rb_enc_mbc_to_codepoint((char *)s, (char *)send, e1);
8461
8462 tlen = enc == e1 ? clen : rb_enc_codelen(c, enc);
8463
8464 s += clen;
8465 if (c < 256) {
8466 c = trans[c];
8467 }
8468 else if (hash) {
8469 VALUE tmp = rb_hash_lookup(hash, UINT2NUM(c));
8470 if (NIL_P(tmp)) {
8471 if (cflag) c = last;
8472 else c = errc;
8473 }
8474 else if (cflag) c = errc;
8475 else c = NUM2INT(tmp);
8476 }
8477 else {
8478 c = errc;
8479 }
8480 if (c != (unsigned int)-1) {
8481 if (save == c) {
8482 CHECK_IF_ASCII(c);
8483 continue;
8484 }
8485 save = c;
8486 tlen = rb_enc_codelen(c, enc);
8487 modify = 1;
8488 }
8489 else {
8490 save = -1;
8491 c = c0;
8492 if (enc != e1) may_modify = 1;
8493 }
8494 if ((offset = t - buf) + tlen > max) {
8495 size_t MAYBE_UNUSED(old) = max + termlen;
8496 max = offset + tlen + (send - s);
8497 SIZED_REALLOC_N(buf, unsigned char, max + termlen, old);
8498 t = buf + offset;
8499 }
8500 rb_enc_mbcput(c, t, enc);
8501 if (may_modify && memcmp(s, t, tlen) != 0) {
8502 modify = 1;
8503 }
8504 CHECK_IF_ASCII(c);
8505 t += tlen;
8506 }
8507 if (!STR_EMBED_P(str)) {
8508 ruby_sized_xfree(STR_HEAP_PTR(str), STR_HEAP_SIZE(str));
8509 }
8510 TERM_FILL((char *)t, termlen);
8511 RSTRING(str)->as.heap.ptr = (char *)buf;
8512 STR_SET_LEN(str, t - buf);
8513 STR_SET_NOEMBED(str);
8514 RSTRING(str)->as.heap.aux.capa = max;
8515 }
8516 else if (rb_enc_mbmaxlen(enc) == 1 || (singlebyte && !hash)) {
8517 while (s < send) {
8518 c = (unsigned char)*s;
8519 if (trans[c] != errc) {
8520 if (!cflag) {
8521 c = trans[c];
8522 *s = c;
8523 modify = 1;
8524 }
8525 else {
8526 *s = last;
8527 modify = 1;
8528 }
8529 }
8530 CHECK_IF_ASCII(c);
8531 s++;
8532 }
8533 }
8534 else {
8535 int clen, tlen;
8536 long offset, max = (long)((send - s) * 1.2);
8537 unsigned char *buf = ALLOC_N(unsigned char, max + termlen), *t = buf;
8538
8539 while (s < send) {
8540 int may_modify = 0;
8541
8542 int r = rb_enc_precise_mbclen((char *)s, (char *)send, e1);
8543 if (!MBCLEN_CHARFOUND_P(r)) {
8544 xfree(buf);
8545 rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(e1));
8546 }
8547 clen = MBCLEN_CHARFOUND_LEN(r);
8548 c0 = c = rb_enc_mbc_to_codepoint((char *)s, (char *)send, e1);
8549
8550 tlen = enc == e1 ? clen : rb_enc_codelen(c, enc);
8551
8552 if (c < 256) {
8553 c = trans[c];
8554 }
8555 else if (hash) {
8556 VALUE tmp = rb_hash_lookup(hash, UINT2NUM(c));
8557 if (NIL_P(tmp)) {
8558 if (cflag) c = last;
8559 else c = errc;
8560 }
8561 else if (cflag) c = errc;
8562 else c = NUM2INT(tmp);
8563 }
8564 else {
8565 c = cflag ? last : errc;
8566 }
8567 if (c != errc) {
8568 tlen = rb_enc_codelen(c, enc);
8569 modify = 1;
8570 }
8571 else {
8572 c = c0;
8573 if (enc != e1) may_modify = 1;
8574 }
8575 if ((offset = t - buf) + tlen > max) {
8576 size_t MAYBE_UNUSED(old) = max + termlen;
8577 max = offset + tlen + (long)((send - s) * 1.2);
8578 SIZED_REALLOC_N(buf, unsigned char, max + termlen, old);
8579 t = buf + offset;
8580 }
8581 if (s != t) {
8582 rb_enc_mbcput(c, t, enc);
8583 if (may_modify && memcmp(s, t, tlen) != 0) {
8584 modify = 1;
8585 }
8586 }
8587 CHECK_IF_ASCII(c);
8588 s += clen;
8589 t += tlen;
8590 }
8591 if (!STR_EMBED_P(str)) {
8592 ruby_sized_xfree(STR_HEAP_PTR(str), STR_HEAP_SIZE(str));
8593 }
8594 TERM_FILL((char *)t, termlen);
8595 RSTRING(str)->as.heap.ptr = (char *)buf;
8596 STR_SET_LEN(str, t - buf);
8597 STR_SET_NOEMBED(str);
8598 RSTRING(str)->as.heap.aux.capa = max;
8599 }
8600
8601 if (modify) {
8602 if (cr != ENC_CODERANGE_BROKEN)
8603 ENC_CODERANGE_SET(str, cr);
8604 rb_enc_associate(str, enc);
8605 return str;
8606 }
8607 return Qnil;
8608}
8609
8610
8611/*
8612 * call-seq:
8613 * tr!(selector, replacements) -> self or nil
8614 *
8615 * Like String#tr, except:
8616 *
8617 * - Performs substitutions in +self+ (not in a copy of +self+).
8618 * - Returns +self+ if any modifications were made, +nil+ otherwise.
8619 *
8620 * Related: {Modifying}[rdoc-ref:String@Modifying].
8621 */
8622
8623static VALUE
8624rb_str_tr_bang(VALUE str, VALUE src, VALUE repl)
8625{
8626 return tr_trans(str, src, repl, 0);
8627}
8628
8629
8630/*
8631 * call-seq:
8632 * tr(selector, replacements) -> new_string
8633 *
8634 * Returns a copy of +self+ with each character specified by string +selector+
8635 * translated to the corresponding character in string +replacements+.
8636 * The correspondence is _positional_:
8637 *
8638 * - Each occurrence of the first character specified by +selector+
8639 * is translated to the first character in +replacements+.
8640 * - Each occurrence of the second character specified by +selector+
8641 * is translated to the second character in +replacements+.
8642 * - And so on.
8643 *
8644 * Example:
8645 *
8646 * 'hello'.tr('el', 'ip') #=> "hippo"
8647 *
8648 * If +replacements+ is shorter than +selector+,
8649 * it is implicitly padded with its own last character:
8650 *
8651 * 'hello'.tr('aeiou', '-') # => "h-ll-"
8652 * 'hello'.tr('aeiou', 'AA-') # => "hAll-"
8653 *
8654 * Arguments +selector+ and +replacements+ must be valid character selectors
8655 * (see {Character Selectors}[rdoc-ref:character_selectors.rdoc]),
8656 * and may use any of its valid forms, including negation, ranges, and escapes:
8657 *
8658 * 'hello'.tr('^aeiou', '-') # => "-e--o" # Negation.
8659 * 'ibm'.tr('b-z', 'a-z') # => "hal" # Range.
8660 * 'hel^lo'.tr('\^aeiou', '-') # => "h-l-l-" # Escaped leading caret.
8661 * 'i-b-m'.tr('b\-z', 'a-z') # => "ibabm" # Escaped embedded hyphen.
8662 * 'foo\\bar'.tr('ab\\', 'XYZ') # => "fooZYXr" # Escaped backslash.
8663 *
8664 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
8665 */
8666
8667static VALUE
8668rb_str_tr(VALUE str, VALUE src, VALUE repl)
8669{
8670 str = str_duplicate(rb_cString, str);
8671 tr_trans(str, src, repl, 0);
8672 return str;
8673}
8674
8675#define TR_TABLE_MAX (UCHAR_MAX+1)
8676#define TR_TABLE_SIZE (TR_TABLE_MAX+1)
8677static void
8678tr_setup_table(VALUE str, char stable[TR_TABLE_SIZE], int first,
8679 VALUE *tablep, VALUE *ctablep, rb_encoding *enc)
8680{
8681 const unsigned int errc = -1;
8682 char buf[TR_TABLE_MAX];
8683 struct tr tr;
8684 unsigned int c;
8685 VALUE table = 0, ptable = 0;
8686 int i, l, cflag = 0;
8687
8688 tr.p = RSTRING_PTR(str); tr.pend = tr.p + RSTRING_LEN(str);
8689 tr.gen = tr.now = tr.max = 0;
8690
8691 if (RSTRING_LEN(str) > 1 && rb_enc_ascget(tr.p, tr.pend, &l, enc) == '^') {
8692 cflag = 1;
8693 tr.p += l;
8694 }
8695 if (first) {
8696 for (i=0; i<TR_TABLE_MAX; i++) {
8697 stable[i] = 1;
8698 }
8699 stable[TR_TABLE_MAX] = cflag;
8700 }
8701 else if (stable[TR_TABLE_MAX] && !cflag) {
8702 stable[TR_TABLE_MAX] = 0;
8703 }
8704 for (i=0; i<TR_TABLE_MAX; i++) {
8705 buf[i] = cflag;
8706 }
8707
8708 while ((c = trnext(&tr, enc)) != errc) {
8709 if (c < TR_TABLE_MAX) {
8710 buf[(unsigned char)c] = !cflag;
8711 }
8712 else {
8713 VALUE key = UINT2NUM(c);
8714
8715 if (!table && (first || *tablep || stable[TR_TABLE_MAX])) {
8716 if (cflag) {
8717 ptable = *ctablep;
8718 table = ptable ? ptable : rb_hash_new();
8719 *ctablep = table;
8720 }
8721 else {
8722 table = rb_hash_new();
8723 ptable = *tablep;
8724 *tablep = table;
8725 }
8726 }
8727 if (table && (!ptable || (cflag ^ !NIL_P(rb_hash_aref(ptable, key))))) {
8728 rb_hash_aset(table, key, Qtrue);
8729 }
8730 }
8731 }
8732 for (i=0; i<TR_TABLE_MAX; i++) {
8733 stable[i] = stable[i] && buf[i];
8734 }
8735 if (!table && !cflag) {
8736 *tablep = 0;
8737 }
8738}
8739
8740
8741static int
8742tr_find(unsigned int c, const char table[TR_TABLE_SIZE], VALUE del, VALUE nodel)
8743{
8744 if (c < TR_TABLE_MAX) {
8745 return table[c] != 0;
8746 }
8747 else {
8748 VALUE v = UINT2NUM(c);
8749
8750 if (del) {
8751 if (!NIL_P(rb_hash_lookup(del, v)) &&
8752 (!nodel || NIL_P(rb_hash_lookup(nodel, v)))) {
8753 return TRUE;
8754 }
8755 }
8756 else if (nodel && !NIL_P(rb_hash_lookup(nodel, v))) {
8757 return FALSE;
8758 }
8759 return table[TR_TABLE_MAX] ? TRUE : FALSE;
8760 }
8761}
8762
8763/*
8764 * call-seq:
8765 * delete!(*selectors) -> self or nil
8766 *
8767 * Like String#delete, but modifies +self+ in place;
8768 * returns +self+ if any characters were deleted, +nil+ otherwise.
8769 *
8770 * Related: see {Modifying}[rdoc-ref:String@Modifying].
8771 */
8772
8773static VALUE
8774rb_str_delete_bang(int argc, VALUE *argv, VALUE str)
8775{
8776 char squeez[TR_TABLE_SIZE];
8777 rb_encoding *enc = 0;
8778 char *s, *send, *t;
8779 VALUE del = 0, nodel = 0;
8780 int modify = 0;
8781 int i, ascompat, cr;
8782
8783 if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return Qnil;
8784 rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
8785 for (i=0; i<argc; i++) {
8786 VALUE s = argv[i];
8787
8788 StringValue(s);
8789 enc = rb_enc_check(str, s);
8790 tr_setup_table(s, squeez, i==0, &del, &nodel, enc);
8791 }
8792
8793 str_modify_keep_cr(str);
8794 ascompat = rb_enc_asciicompat(enc);
8795 s = t = RSTRING_PTR(str);
8796 send = RSTRING_END(str);
8797 cr = ascompat ? ENC_CODERANGE_7BIT : ENC_CODERANGE_VALID;
8798 while (s < send) {
8799 unsigned int c;
8800 int clen;
8801
8802 if (ascompat && (c = *(unsigned char*)s) < 0x80) {
8803 if (squeez[c]) {
8804 modify = 1;
8805 }
8806 else {
8807 if (t != s) *t = c;
8808 t++;
8809 }
8810 s++;
8811 }
8812 else {
8813 c = rb_enc_codepoint_len(s, send, &clen, enc);
8814
8815 if (tr_find(c, squeez, del, nodel)) {
8816 modify = 1;
8817 }
8818 else {
8819 if (t != s) rb_enc_mbcput(c, t, enc);
8820 t += clen;
8822 }
8823 s += clen;
8824 }
8825 }
8826 TERM_FILL(t, TERM_LEN(str));
8827 STR_SET_LEN(str, t - RSTRING_PTR(str));
8828 ENC_CODERANGE_SET(str, cr);
8829
8830 if (modify) return str;
8831 return Qnil;
8832}
8833
8834
8835/*
8836 * call-seq:
8837 * delete(*selectors) -> new_string
8838 *
8839 * :include: doc/string/delete.rdoc
8840 *
8841 */
8842
8843static VALUE
8844rb_str_delete(int argc, VALUE *argv, VALUE str)
8845{
8846 str = str_duplicate(rb_cString, str);
8847 rb_str_delete_bang(argc, argv, str);
8848 return str;
8849}
8850
8851
8852/*
8853 * call-seq:
8854 * squeeze!(*selectors) -> self or nil
8855 *
8856 * Like String#squeeze, except that:
8857 *
8858 * - Characters are squeezed in +self+ (not in a copy of +self+).
8859 * - Returns +self+ if any changes are made, +nil+ otherwise.
8860 *
8861 * Related: See {Modifying}[rdoc-ref:String@Modifying].
8862 */
8863
8864static VALUE
8865rb_str_squeeze_bang(int argc, VALUE *argv, VALUE str)
8866{
8867 char squeez[TR_TABLE_SIZE];
8868 rb_encoding *enc = 0;
8869 VALUE del = 0, nodel = 0;
8870 unsigned char *s, *send, *t;
8871 int i, modify = 0;
8872 int ascompat, singlebyte = single_byte_optimizable(str);
8873 unsigned int save;
8874
8875 if (argc == 0) {
8876 enc = STR_ENC_GET(str);
8877 }
8878 else {
8879 for (i=0; i<argc; i++) {
8880 VALUE s = argv[i];
8881
8882 StringValue(s);
8883 enc = rb_enc_check(str, s);
8884 if (singlebyte && !single_byte_optimizable(s))
8885 singlebyte = 0;
8886 tr_setup_table(s, squeez, i==0, &del, &nodel, enc);
8887 }
8888 }
8889
8890 str_modify_keep_cr(str);
8891 s = t = (unsigned char *)RSTRING_PTR(str);
8892 if (!s || RSTRING_LEN(str) == 0) return Qnil;
8893 send = (unsigned char *)RSTRING_END(str);
8894 save = -1;
8895 ascompat = rb_enc_asciicompat(enc);
8896
8897 if (singlebyte) {
8898 while (s < send) {
8899 unsigned int c = *s++;
8900 if (c != save || (argc > 0 && !squeez[c])) {
8901 *t++ = save = c;
8902 }
8903 }
8904 }
8905 else {
8906 while (s < send) {
8907 unsigned int c;
8908 int clen;
8909
8910 if (ascompat && (c = *s) < 0x80) {
8911 if (c != save || (argc > 0 && !squeez[c])) {
8912 *t++ = save = c;
8913 }
8914 s++;
8915 }
8916 else {
8917 c = rb_enc_codepoint_len((char *)s, (char *)send, &clen, enc);
8918
8919 if (c != save || (argc > 0 && !tr_find(c, squeez, del, nodel))) {
8920 if (t != s) rb_enc_mbcput(c, t, enc);
8921 save = c;
8922 t += clen;
8923 }
8924 s += clen;
8925 }
8926 }
8927 }
8928
8929 TERM_FILL((char *)t, TERM_LEN(str));
8930 if ((char *)t - RSTRING_PTR(str) != RSTRING_LEN(str)) {
8931 STR_SET_LEN(str, (char *)t - RSTRING_PTR(str));
8932 modify = 1;
8933 }
8934
8935 if (modify) return str;
8936 return Qnil;
8937}
8938
8939
8940/*
8941 * call-seq:
8942 * squeeze(*selectors) -> new_string
8943 *
8944 * :include: doc/string/squeeze.rdoc
8945 *
8946 */
8947
8948static VALUE
8949rb_str_squeeze(int argc, VALUE *argv, VALUE str)
8950{
8951 str = str_duplicate(rb_cString, str);
8952 rb_str_squeeze_bang(argc, argv, str);
8953 return str;
8954}
8955
8956
8957/*
8958 * call-seq:
8959 * tr_s!(selector, replacements) -> self or nil
8960 *
8961 * Like String#tr_s, except:
8962 *
8963 * - Modifies +self+ in place (not a copy of +self+).
8964 * - Returns +self+ if any changes were made, +nil+ otherwise.
8965 *
8966 * Related: {Modifying}[rdoc-ref:String@Modifying].
8967 */
8968
8969static VALUE
8970rb_str_tr_s_bang(VALUE str, VALUE src, VALUE repl)
8971{
8972 return tr_trans(str, src, repl, 1);
8973}
8974
8975
8976/*
8977 * call-seq:
8978 * tr_s(selector, replacements) -> new_string
8979 *
8980 * Like String#tr, except:
8981 *
8982 * - Also squeezes the modified portions of the translated string;
8983 * see String#squeeze.
8984 * - Returns the translated and squeezed string.
8985 *
8986 * Examples:
8987 *
8988 * 'hello'.tr_s('l', 'r') #=> "hero"
8989 * 'hello'.tr_s('el', '-') #=> "h-o"
8990 * 'hello'.tr_s('el', 'hx') #=> "hhxo"
8991 *
8992 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
8993 *
8994 */
8995
8996static VALUE
8997rb_str_tr_s(VALUE str, VALUE src, VALUE repl)
8998{
8999 str = str_duplicate(rb_cString, str);
9000 tr_trans(str, src, repl, 1);
9001 return str;
9002}
9003
9004
9005/*
9006 * call-seq:
9007 * count(*selectors) -> integer
9008 *
9009 * :include: doc/string/count.rdoc
9010 */
9011
9012static VALUE
9013rb_str_count(int argc, VALUE *argv, VALUE str)
9014{
9015 char table[TR_TABLE_SIZE];
9016 rb_encoding *enc = 0;
9017 VALUE del = 0, nodel = 0, tstr;
9018 char *s, *send;
9019 int i;
9020 int ascompat;
9021 size_t n = 0;
9022
9023 rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
9024
9025 tstr = argv[0];
9026 StringValue(tstr);
9027 enc = rb_enc_check(str, tstr);
9028 if (argc == 1) {
9029 const char *ptstr;
9030 if (RSTRING_LEN(tstr) == 1 && rb_enc_asciicompat(enc) &&
9031 (ptstr = RSTRING_PTR(tstr),
9032 ONIGENC_IS_ALLOWED_REVERSE_MATCH(enc, (const unsigned char *)ptstr, (const unsigned char *)ptstr+1)) &&
9033 !is_broken_string(str)) {
9034 int clen;
9035 unsigned char c = rb_enc_codepoint_len(ptstr, ptstr+1, &clen, enc);
9036
9037 s = RSTRING_PTR(str);
9038 if (!s || RSTRING_LEN(str) == 0) return INT2FIX(0);
9039 send = RSTRING_END(str);
9040 while (s < send) {
9041 if (*(unsigned char*)s++ == c) n++;
9042 }
9043 return SIZET2NUM(n);
9044 }
9045 }
9046
9047 tr_setup_table(tstr, table, TRUE, &del, &nodel, enc);
9048 for (i=1; i<argc; i++) {
9049 tstr = argv[i];
9050 StringValue(tstr);
9051 enc = rb_enc_check(str, tstr);
9052 tr_setup_table(tstr, table, FALSE, &del, &nodel, enc);
9053 }
9054
9055 s = RSTRING_PTR(str);
9056 if (!s || RSTRING_LEN(str) == 0) return INT2FIX(0);
9057 send = RSTRING_END(str);
9058 ascompat = rb_enc_asciicompat(enc);
9059 while (s < send) {
9060 unsigned int c;
9061
9062 if (ascompat && (c = *(unsigned char*)s) < 0x80) {
9063 if (table[c]) {
9064 n++;
9065 }
9066 s++;
9067 }
9068 else {
9069 int clen;
9070 c = rb_enc_codepoint_len(s, send, &clen, enc);
9071 if (tr_find(c, table, del, nodel)) {
9072 n++;
9073 }
9074 s += clen;
9075 }
9076 }
9077
9078 return SIZET2NUM(n);
9079}
9080
9081static VALUE
9082rb_fs_check(VALUE val)
9083{
9084 if (!NIL_P(val) && !RB_TYPE_P(val, T_STRING) && !RB_TYPE_P(val, T_REGEXP)) {
9085 val = rb_check_string_type(val);
9086 if (NIL_P(val)) return 0;
9087 }
9088 return val;
9089}
9090
9091static const char isspacetable[256] = {
9092 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0,
9093 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9094 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9095 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9096 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9097 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9098 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9099 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9100 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9101 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9102 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9103 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9104 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9105 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9106 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9107 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
9108};
9109
9110#define ascii_isspace(c) isspacetable[(unsigned char)(c)]
9111
9112static long
9113split_string(VALUE result, VALUE str, long beg, long len, long empty_count)
9114{
9115 if (empty_count >= 0 && len == 0) {
9116 return empty_count + 1;
9117 }
9118 if (empty_count > 0) {
9119 /* make different substrings */
9120 if (result) {
9121 do {
9122 rb_ary_push(result, str_new_empty_String(str));
9123 } while (--empty_count > 0);
9124 }
9125 else {
9126 do {
9127 rb_yield(str_new_empty_String(str));
9128 } while (--empty_count > 0);
9129 }
9130 }
9131 str = rb_str_subseq(str, beg, len);
9132 if (result) {
9133 rb_ary_push(result, str);
9134 }
9135 else {
9136 rb_yield(str);
9137 }
9138 return empty_count;
9139}
9140
9141typedef enum {
9142 SPLIT_TYPE_AWK, SPLIT_TYPE_STRING, SPLIT_TYPE_REGEXP, SPLIT_TYPE_CHARS
9143} split_type_t;
9144
9145static split_type_t
9146literal_split_pattern(VALUE spat, split_type_t default_type)
9147{
9148 rb_encoding *enc = STR_ENC_GET(spat);
9149 const char *ptr;
9150 long len;
9151 RSTRING_GETMEM(spat, ptr, len);
9152 if (len == 0) {
9153 /* Special case - split into chars */
9154 return SPLIT_TYPE_CHARS;
9155 }
9156 else if (rb_enc_asciicompat(enc)) {
9157 if (len == 1 && ptr[0] == ' ') {
9158 return SPLIT_TYPE_AWK;
9159 }
9160 }
9161 else {
9162 int l;
9163 if (rb_enc_ascget(ptr, ptr + len, &l, enc) == ' ' && len == l) {
9164 return SPLIT_TYPE_AWK;
9165 }
9166 }
9167 return default_type;
9168}
9169
9170/*
9171 * call-seq:
9172 * split(field_sep = $;, limit = 0) -> array_of_substrings
9173 * split(field_sep = $;, limit = 0) {|substring| ... } -> self
9174 *
9175 * :include: doc/string/split.rdoc
9176 *
9177 */
9178
9179static VALUE
9180rb_str_split_m(int argc, VALUE *argv, VALUE str)
9181{
9182 rb_encoding *enc;
9183 VALUE spat;
9184 VALUE limit;
9185 split_type_t split_type;
9186 long beg, end, i = 0, empty_count = -1;
9187 int lim = 0;
9188 VALUE result, tmp;
9189
9190 result = rb_block_given_p() ? Qfalse : Qnil;
9191 if (rb_scan_args(argc, argv, "02", &spat, &limit) == 2) {
9192 lim = NUM2INT(limit);
9193 if (lim <= 0) limit = Qnil;
9194 else if (lim == 1) {
9195 if (RSTRING_LEN(str) == 0)
9196 return result ? rb_ary_new2(0) : str;
9197 tmp = str_duplicate(rb_cString, str);
9198 if (!result) {
9199 rb_yield(tmp);
9200 return str;
9201 }
9202 return rb_ary_new3(1, tmp);
9203 }
9204 i = 1;
9205 }
9206 if (NIL_P(limit) && !lim) empty_count = 0;
9207
9208 enc = STR_ENC_GET(str);
9209 split_type = SPLIT_TYPE_REGEXP;
9210 if (!NIL_P(spat)) {
9211 spat = get_pat_quoted(spat, 0);
9212 }
9213 else if (NIL_P(spat = rb_fs)) {
9214 split_type = SPLIT_TYPE_AWK;
9215 }
9216 else if (!(spat = rb_fs_check(spat))) {
9217 rb_raise(rb_eTypeError, "value of $; must be String or Regexp");
9218 }
9219 else {
9220 rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "$; is set to non-nil value");
9221 }
9222 if (split_type != SPLIT_TYPE_AWK) {
9223 switch (BUILTIN_TYPE(spat)) {
9224 case T_REGEXP:
9225 rb_reg_options(spat); /* check if uninitialized */
9226 tmp = RREGEXP_SRC(spat);
9227 split_type = literal_split_pattern(tmp, SPLIT_TYPE_REGEXP);
9228 if (split_type == SPLIT_TYPE_AWK) {
9229 spat = tmp;
9230 split_type = SPLIT_TYPE_STRING;
9231 }
9232 break;
9233
9234 case T_STRING:
9235 mustnot_broken(spat);
9236 split_type = literal_split_pattern(spat, SPLIT_TYPE_STRING);
9237 break;
9238
9239 default:
9241 }
9242 }
9243
9244#define SPLIT_STR(beg, len) ( \
9245 empty_count = split_string(result, str, beg, len, empty_count), \
9246 str_mod_check(str, str_start, str_len))
9247
9248 beg = 0;
9249 char *ptr = RSTRING_PTR(str);
9250 char *const str_start = ptr;
9251 const long str_len = RSTRING_LEN(str);
9252 char *const eptr = str_start + str_len;
9253 if (split_type == SPLIT_TYPE_AWK) {
9254 char *bptr = ptr;
9255 int skip = 1;
9256 unsigned int c;
9257
9258 if (result) result = rb_ary_new();
9259 end = beg;
9260 if (is_ascii_string(str)) {
9261 while (ptr < eptr) {
9262 c = (unsigned char)*ptr++;
9263 if (skip) {
9264 if (ascii_isspace(c)) {
9265 beg = ptr - bptr;
9266 }
9267 else {
9268 end = ptr - bptr;
9269 skip = 0;
9270 if (!NIL_P(limit) && lim <= i) break;
9271 }
9272 }
9273 else if (ascii_isspace(c)) {
9274 SPLIT_STR(beg, end-beg);
9275 skip = 1;
9276 beg = ptr - bptr;
9277 if (!NIL_P(limit)) ++i;
9278 }
9279 else {
9280 end = ptr - bptr;
9281 }
9282 }
9283 }
9284 else {
9285 while (ptr < eptr) {
9286 int n;
9287
9288 c = rb_enc_codepoint_len(ptr, eptr, &n, enc);
9289 ptr += n;
9290 if (skip) {
9291 if (rb_isspace(c)) {
9292 beg = ptr - bptr;
9293 }
9294 else {
9295 end = ptr - bptr;
9296 skip = 0;
9297 if (!NIL_P(limit) && lim <= i) break;
9298 }
9299 }
9300 else if (rb_isspace(c)) {
9301 SPLIT_STR(beg, end-beg);
9302 skip = 1;
9303 beg = ptr - bptr;
9304 if (!NIL_P(limit)) ++i;
9305 }
9306 else {
9307 end = ptr - bptr;
9308 }
9309 }
9310 }
9311 }
9312 else if (split_type == SPLIT_TYPE_STRING) {
9313 char *substr_start = ptr;
9314 char *sptr = RSTRING_PTR(spat);
9315 long slen = RSTRING_LEN(spat);
9316
9317 if (result) result = rb_ary_new();
9318 mustnot_broken(str);
9319 enc = rb_enc_check(str, spat);
9320 while (ptr < eptr &&
9321 (end = rb_memsearch(sptr, slen, ptr, eptr - ptr, enc)) >= 0) {
9322 /* Check we are at the start of a char */
9323 char *t = rb_enc_right_char_head(ptr, ptr + end, eptr, enc);
9324 if (t != ptr + end) {
9325 ptr = t;
9326 continue;
9327 }
9328 SPLIT_STR(substr_start - str_start, (ptr+end) - substr_start);
9329 str_mod_check(spat, sptr, slen);
9330 ptr += end + slen;
9331 substr_start = ptr;
9332 if (!NIL_P(limit) && lim <= ++i) break;
9333 }
9334 beg = ptr - str_start;
9335 }
9336 else if (split_type == SPLIT_TYPE_CHARS) {
9337 int n;
9338
9339 if (result) result = rb_ary_new_capa(RSTRING_LEN(str));
9340 mustnot_broken(str);
9341 enc = rb_enc_get(str);
9342 while (ptr < eptr &&
9343 (n = rb_enc_precise_mbclen(ptr, eptr, enc)) > 0) {
9344 SPLIT_STR(ptr - str_start, n);
9345 ptr += n;
9346 if (!NIL_P(limit) && lim <= ++i) break;
9347 }
9348 beg = ptr - str_start;
9349 }
9350 else {
9351 if (result) result = rb_ary_new();
9352 long len = RSTRING_LEN(str);
9353 long start = beg;
9354 long idx;
9355 int last_null = 0;
9356 struct re_registers *regs;
9357 VALUE match = 0;
9358
9359 for (; rb_reg_search(spat, str, start, 0) >= 0;
9360 (match ? (rb_match_unbusy(match), rb_backref_set(match)) : (void)0)) {
9361 match = rb_backref_get();
9362 if (!result) rb_match_busy(match);
9363 regs = RMATCH_REGS(match);
9364 end = BEG(0);
9365 if (start == end && BEG(0) == END(0)) {
9366 if (!ptr) {
9367 SPLIT_STR(0, 0);
9368 break;
9369 }
9370 else if (last_null == 1) {
9371 SPLIT_STR(beg, rb_enc_fast_mbclen(ptr+beg, eptr, enc));
9372 beg = start;
9373 }
9374 else {
9375 if (start == len)
9376 start++;
9377 else
9378 start += rb_enc_fast_mbclen(ptr+start,eptr,enc);
9379 last_null = 1;
9380 continue;
9381 }
9382 }
9383 else {
9384 SPLIT_STR(beg, end-beg);
9385 beg = start = END(0);
9386 }
9387 last_null = 0;
9388
9389 for (idx=1; idx < regs->num_regs; idx++) {
9390 if (BEG(idx) == -1) continue;
9391 SPLIT_STR(BEG(idx), END(idx)-BEG(idx));
9392 }
9393 if (!NIL_P(limit) && lim <= ++i) break;
9394 }
9395 if (match) rb_match_unbusy(match);
9396 }
9397 if (RSTRING_LEN(str) > 0 && (!NIL_P(limit) || RSTRING_LEN(str) > beg || lim < 0)) {
9398 SPLIT_STR(beg, RSTRING_LEN(str)-beg);
9399 }
9400
9401 return result ? result : str;
9402}
9403
9404VALUE
9405rb_str_split(VALUE str, const char *sep0)
9406{
9407 VALUE sep;
9408
9409 StringValue(str);
9410 sep = rb_str_new_cstr(sep0);
9411 return rb_str_split_m(1, &sep, str);
9412}
9413
9414#define WANTARRAY(m, size) (!rb_block_given_p() ? rb_ary_new_capa(size) : 0)
9415
9416static inline int
9417enumerator_element(VALUE ary, VALUE e)
9418{
9419 if (ary) {
9420 rb_ary_push(ary, e);
9421 return 0;
9422 }
9423 else {
9424 rb_yield(e);
9425 return 1;
9426 }
9427}
9428
9429#define ENUM_ELEM(ary, e) enumerator_element(ary, e)
9430
9431static const char *
9432chomp_newline(const char *p, const char *e, rb_encoding *enc)
9433{
9434 const char *prev = rb_enc_prev_char(p, e, e, enc);
9435 if (rb_enc_is_newline(prev, e, enc)) {
9436 e = prev;
9437 prev = rb_enc_prev_char(p, e, e, enc);
9438 if (prev && rb_enc_ascget(prev, e, NULL, enc) == '\r')
9439 e = prev;
9440 }
9441 return e;
9442}
9443
9444static VALUE
9445get_rs(void)
9446{
9447 VALUE rs = rb_rs;
9448 if (!NIL_P(rs) &&
9449 (!RB_TYPE_P(rs, T_STRING) ||
9450 RSTRING_LEN(rs) != 1 ||
9451 RSTRING_PTR(rs)[0] != '\n')) {
9452 rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "$/ is set to non-default value");
9453 }
9454 return rs;
9455}
9456
9457#define rb_rs get_rs()
9458
9459static VALUE
9460rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, VALUE ary)
9461{
9462 rb_encoding *enc;
9463 VALUE line, rs, orig = str, opts = Qnil, chomp = Qfalse;
9464 const char *ptr, *pend, *subptr, *subend, *rsptr, *hit, *adjusted;
9465 long pos, len, rslen;
9466 int rsnewline = 0;
9467
9468 if (rb_scan_args(argc, argv, "01:", &rs, &opts) == 0)
9469 rs = rb_rs;
9470 if (!NIL_P(opts)) {
9471 static ID keywords[1];
9472 if (!keywords[0]) {
9473 keywords[0] = rb_intern_const("chomp");
9474 }
9475 rb_get_kwargs(opts, keywords, 0, 1, &chomp);
9476 chomp = (!UNDEF_P(chomp) && RTEST(chomp));
9477 }
9478
9479 if (NIL_P(rs)) {
9480 if (!ENUM_ELEM(ary, str)) {
9481 return ary;
9482 }
9483 else {
9484 return orig;
9485 }
9486 }
9487
9488 if (!RSTRING_LEN(str)) goto end;
9489 str = rb_str_new_frozen(str);
9490 ptr = subptr = RSTRING_PTR(str);
9491 pend = RSTRING_END(str);
9492 len = RSTRING_LEN(str);
9493 StringValue(rs);
9494 rslen = RSTRING_LEN(rs);
9495
9496 if (rs == rb_default_rs)
9497 enc = rb_enc_get(str);
9498 else
9499 enc = rb_enc_check(str, rs);
9500
9501 if (rslen == 0) {
9502 /* paragraph mode */
9503 int n;
9504 const char *eol = NULL;
9505 subend = subptr;
9506 while (subend < pend) {
9507 long chomp_rslen = 0;
9508 do {
9509 if (rb_enc_ascget(subend, pend, &n, enc) != '\r')
9510 n = 0;
9511 rslen = n + rb_enc_mbclen(subend + n, pend, enc);
9512 if (rb_enc_is_newline(subend + n, pend, enc)) {
9513 if (eol == subend) break;
9514 subend += rslen;
9515 if (subptr) {
9516 eol = subend;
9517 chomp_rslen = -rslen;
9518 }
9519 }
9520 else {
9521 if (!subptr) subptr = subend;
9522 subend += rslen;
9523 }
9524 rslen = 0;
9525 } while (subend < pend);
9526 if (!subptr) break;
9527 if (rslen == 0) chomp_rslen = 0;
9528 line = rb_str_subseq(str, subptr - ptr,
9529 subend - subptr + (chomp ? chomp_rslen : rslen));
9530 if (ENUM_ELEM(ary, line)) {
9531 str_mod_check(str, ptr, len);
9532 }
9533 subptr = eol = NULL;
9534 }
9535 goto end;
9536 }
9537 else {
9538 rsptr = RSTRING_PTR(rs);
9539 if (RSTRING_LEN(rs) == rb_enc_mbminlen(enc) &&
9540 rb_enc_is_newline(rsptr, rsptr + RSTRING_LEN(rs), enc)) {
9541 rsnewline = 1;
9542 }
9543 }
9544
9545 if ((rs == rb_default_rs) && !rb_enc_asciicompat(enc)) {
9546 rs = rb_str_new(rsptr, rslen);
9547 rs = rb_str_encode(rs, rb_enc_from_encoding(enc), 0, Qnil);
9548 rsptr = RSTRING_PTR(rs);
9549 rslen = RSTRING_LEN(rs);
9550 }
9551
9552 while (subptr < pend) {
9553 pos = rb_memsearch(rsptr, rslen, subptr, pend - subptr, enc);
9554 if (pos < 0) break;
9555 hit = subptr + pos;
9556 adjusted = rb_enc_right_char_head(subptr, hit, pend, enc);
9557 if (hit != adjusted) {
9558 subptr = adjusted;
9559 continue;
9560 }
9561 subend = hit += rslen;
9562 if (chomp) {
9563 if (rsnewline) {
9564 subend = chomp_newline(subptr, subend, enc);
9565 }
9566 else {
9567 subend -= rslen;
9568 }
9569 }
9570 line = rb_str_subseq(str, subptr - ptr, subend - subptr);
9571 if (ENUM_ELEM(ary, line)) {
9572 str_mod_check(str, ptr, len);
9573 str_mod_check(rs, rsptr, rslen);
9574 }
9575 subptr = hit;
9576 }
9577
9578 if (subptr < pend) {
9579 if (chomp) {
9580 if (rsnewline) {
9581 pend = chomp_newline(subptr, pend, enc);
9582 }
9583 else if (pend - subptr >= rslen &&
9584 memcmp(pend - rslen, rsptr, rslen) == 0) {
9585 pend -= rslen;
9586 }
9587 }
9588 line = rb_str_subseq(str, subptr - ptr, pend - subptr);
9589 ENUM_ELEM(ary, line);
9590 RB_GC_GUARD(str);
9591 }
9592
9593 end:
9594 if (ary)
9595 return ary;
9596 else
9597 return orig;
9598}
9599
9600/*
9601 * call-seq:
9602 * each_line(record_separator = $/, chomp: false) {|substring| ... } -> self
9603 * each_line(record_separator = $/, chomp: false) -> enumerator
9604 *
9605 * :include: doc/string/each_line.rdoc
9606 *
9607 */
9608
9609static VALUE
9610rb_str_each_line(int argc, VALUE *argv, VALUE str)
9611{
9612 RETURN_SIZED_ENUMERATOR(str, argc, argv, 0);
9613 return rb_str_enumerate_lines(argc, argv, str, 0);
9614}
9615
9616/*
9617 * call-seq:
9618 * lines(record_separator = $/, chomp: false) -> array_of_strings
9619 *
9620 * Returns substrings ("lines") of +self+
9621 * according to the given arguments:
9622 *
9623 * s = <<~EOT
9624 * This is the first line.
9625 * This is line two.
9626 *
9627 * This is line four.
9628 * This is line five.
9629 * EOT
9630 *
9631 * With the default argument values:
9632 *
9633 * $/ # => "\n"
9634 * s.lines
9635 * # =>
9636 * ["This is the first line.\n",
9637 * "This is line two.\n",
9638 * "\n",
9639 * "This is line four.\n",
9640 * "This is line five.\n"]
9641 *
9642 * With a different +record_separator+:
9643 *
9644 * record_separator = ' is '
9645 * s.lines(record_separator)
9646 * # =>
9647 * ["This is ",
9648 * "the first line.\nThis is ",
9649 * "line two.\n\nThis is ",
9650 * "line four.\nThis is ",
9651 * "line five.\n"]
9652 *
9653 * With keyword argument +chomp+ as +true+,
9654 * removes the trailing newline from each line:
9655 *
9656 * s.lines(chomp: true)
9657 * # =>
9658 * ["This is the first line.",
9659 * "This is line two.",
9660 * "",
9661 * "This is line four.",
9662 * "This is line five."]
9663 *
9664 * Related: see {Converting to Non-String}[rdoc-ref:String@Converting+to+Non--5CString].
9665 */
9666
9667static VALUE
9668rb_str_lines(int argc, VALUE *argv, VALUE str)
9669{
9670 VALUE ary = WANTARRAY("lines", 0);
9671 return rb_str_enumerate_lines(argc, argv, str, ary);
9672}
9673
9674static VALUE
9675rb_str_each_byte_size(VALUE str, VALUE args, VALUE eobj)
9676{
9677 return LONG2FIX(RSTRING_LEN(str));
9678}
9679
9680static VALUE
9681rb_str_enumerate_bytes(VALUE str, VALUE ary)
9682{
9683 long i;
9684
9685 for (i=0; i<RSTRING_LEN(str); i++) {
9686 ENUM_ELEM(ary, INT2FIX((unsigned char)RSTRING_PTR(str)[i]));
9687 }
9688 if (ary)
9689 return ary;
9690 else
9691 return str;
9692}
9693
9694/*
9695 * call-seq:
9696 * each_byte {|byte| ... } -> self
9697 * each_byte -> enumerator
9698 *
9699 * :include: doc/string/each_byte.rdoc
9700 *
9701 */
9702
9703static VALUE
9704rb_str_each_byte(VALUE str)
9705{
9706 RETURN_SIZED_ENUMERATOR(str, 0, 0, rb_str_each_byte_size);
9707 return rb_str_enumerate_bytes(str, 0);
9708}
9709
9710/*
9711 * call-seq:
9712 * bytes -> array_of_bytes
9713 *
9714 * :include: doc/string/bytes.rdoc
9715 *
9716 */
9717
9718static VALUE
9719rb_str_bytes(VALUE str)
9720{
9721 VALUE ary = WANTARRAY("bytes", RSTRING_LEN(str));
9722 return rb_str_enumerate_bytes(str, ary);
9723}
9724
9725static VALUE
9726rb_str_each_char_size(VALUE str, VALUE args, VALUE eobj)
9727{
9728 return rb_str_length(str);
9729}
9730
9731static VALUE
9732rb_str_enumerate_chars(VALUE str, VALUE ary)
9733{
9734 VALUE orig = str;
9735 long i, len, n;
9736 const char *ptr;
9737 rb_encoding *enc;
9738
9739 str = rb_str_new_frozen(str);
9740 ptr = RSTRING_PTR(str);
9741 len = RSTRING_LEN(str);
9742 enc = rb_enc_get(str);
9743
9745 for (i = 0; i < len; i += n) {
9746 n = rb_enc_fast_mbclen(ptr + i, ptr + len, enc);
9747 ENUM_ELEM(ary, rb_str_subseq(str, i, n));
9748 }
9749 }
9750 else {
9751 for (i = 0; i < len; i += n) {
9752 n = rb_enc_mbclen(ptr + i, ptr + len, enc);
9753 ENUM_ELEM(ary, rb_str_subseq(str, i, n));
9754 }
9755 }
9756 RB_GC_GUARD(str);
9757 if (ary)
9758 return ary;
9759 else
9760 return orig;
9761}
9762
9763/*
9764 * call-seq:
9765 * each_char {|char| ... } -> self
9766 * each_char -> enumerator
9767 *
9768 * :include: doc/string/each_char.rdoc
9769 *
9770 */
9771
9772static VALUE
9773rb_str_each_char(VALUE str)
9774{
9775 RETURN_SIZED_ENUMERATOR(str, 0, 0, rb_str_each_char_size);
9776 return rb_str_enumerate_chars(str, 0);
9777}
9778
9779/*
9780 * call-seq:
9781 * chars -> array_of_characters
9782 *
9783 * :include: doc/string/chars.rdoc
9784 *
9785 */
9786
9787static VALUE
9788rb_str_chars(VALUE str)
9789{
9790 VALUE ary = WANTARRAY("chars", rb_str_strlen(str));
9791 return rb_str_enumerate_chars(str, ary);
9792}
9793
9794static VALUE
9795rb_str_enumerate_codepoints(VALUE str, VALUE ary)
9796{
9797 VALUE orig = str;
9798 int n;
9799 unsigned int c;
9800 const char *ptr, *end;
9801 rb_encoding *enc;
9802
9803 if (single_byte_optimizable(str))
9804 return rb_str_enumerate_bytes(str, ary);
9805
9806 str = rb_str_new_frozen(str);
9807 ptr = RSTRING_PTR(str);
9808 end = RSTRING_END(str);
9809 enc = STR_ENC_GET(str);
9810
9811 while (ptr < end) {
9812 c = rb_enc_codepoint_len(ptr, end, &n, enc);
9813 ENUM_ELEM(ary, UINT2NUM(c));
9814 ptr += n;
9815 }
9816 RB_GC_GUARD(str);
9817 if (ary)
9818 return ary;
9819 else
9820 return orig;
9821}
9822
9823/*
9824 * call-seq:
9825 * each_codepoint {|codepoint| ... } -> self
9826 * each_codepoint -> enumerator
9827 *
9828 * :include: doc/string/each_codepoint.rdoc
9829 *
9830 */
9831
9832static VALUE
9833rb_str_each_codepoint(VALUE str)
9834{
9835 RETURN_SIZED_ENUMERATOR(str, 0, 0, rb_str_each_char_size);
9836 return rb_str_enumerate_codepoints(str, 0);
9837}
9838
9839/*
9840 * call-seq:
9841 * codepoints -> array_of_integers
9842 *
9843 * :include: doc/string/codepoints.rdoc
9844 *
9845 */
9846
9847static VALUE
9848rb_str_codepoints(VALUE str)
9849{
9850 VALUE ary = WANTARRAY("codepoints", rb_str_strlen(str));
9851 return rb_str_enumerate_codepoints(str, ary);
9852}
9853
9854static regex_t *
9855get_reg_grapheme_cluster(rb_encoding *enc)
9856{
9857 int encidx = rb_enc_to_index(enc);
9858
9859 const OnigUChar source_ascii[] = "\\X";
9860 const OnigUChar *source = source_ascii;
9861 size_t source_len = sizeof(source_ascii) - 1;
9862
9863 switch (encidx) {
9864#define CHARS_16BE(x) (OnigUChar)((x)>>8), (OnigUChar)(x)
9865#define CHARS_16LE(x) (OnigUChar)(x), (OnigUChar)((x)>>8)
9866#define CHARS_32BE(x) CHARS_16BE((x)>>16), CHARS_16BE(x)
9867#define CHARS_32LE(x) CHARS_16LE(x), CHARS_16LE((x)>>16)
9868#define CASE_UTF(e) \
9869 case ENCINDEX_UTF_##e: { \
9870 static const OnigUChar source_UTF_##e[] = {CHARS_##e('\\'), CHARS_##e('X')}; \
9871 source = source_UTF_##e; \
9872 source_len = sizeof(source_UTF_##e); \
9873 break; \
9874 }
9875 CASE_UTF(16BE); CASE_UTF(16LE); CASE_UTF(32BE); CASE_UTF(32LE);
9876#undef CASE_UTF
9877#undef CHARS_16BE
9878#undef CHARS_16LE
9879#undef CHARS_32BE
9880#undef CHARS_32LE
9881 }
9882
9883 regex_t *reg_grapheme_cluster;
9884 OnigErrorInfo einfo;
9885 int r = onig_new(&reg_grapheme_cluster, source, source + source_len,
9886 ONIG_OPTION_DEFAULT, enc, OnigDefaultSyntax, &einfo);
9887 if (r) {
9888 UChar message[ONIG_MAX_ERROR_MESSAGE_LEN];
9889 onig_error_code_to_str(message, r, &einfo);
9890 rb_fatal("cannot compile grapheme cluster regexp: %s", (char *)message);
9891 }
9892
9893 return reg_grapheme_cluster;
9894}
9895
9896static regex_t *
9897get_cached_reg_grapheme_cluster(rb_encoding *enc)
9898{
9899 int encidx = rb_enc_to_index(enc);
9900 static regex_t *reg_grapheme_cluster_utf8 = NULL;
9901
9902 if (encidx == rb_utf8_encindex()) {
9903 if (!reg_grapheme_cluster_utf8) {
9904 reg_grapheme_cluster_utf8 = get_reg_grapheme_cluster(enc);
9905 }
9906
9907 return reg_grapheme_cluster_utf8;
9908 }
9909
9910 return NULL;
9911}
9912
9913static VALUE
9914rb_str_each_grapheme_cluster_size(VALUE str, VALUE args, VALUE eobj)
9915{
9916 size_t grapheme_cluster_count = 0;
9917 rb_encoding *enc = get_encoding(str);
9918 const char *ptr, *end;
9919
9920 if (!rb_enc_unicode_p(enc)) {
9921 return rb_str_length(str);
9922 }
9923
9924 bool cached_reg_grapheme_cluster = true;
9925 regex_t *reg_grapheme_cluster = get_cached_reg_grapheme_cluster(enc);
9926 if (!reg_grapheme_cluster) {
9927 reg_grapheme_cluster = get_reg_grapheme_cluster(enc);
9928 cached_reg_grapheme_cluster = false;
9929 }
9930
9931 ptr = RSTRING_PTR(str);
9932 end = RSTRING_END(str);
9933
9934 while (ptr < end) {
9935 OnigPosition len = onig_match(reg_grapheme_cluster,
9936 (const OnigUChar *)ptr, (const OnigUChar *)end,
9937 (const OnigUChar *)ptr, NULL, 0);
9938 if (len <= 0) break;
9939 grapheme_cluster_count++;
9940 ptr += len;
9941 }
9942
9943 if (!cached_reg_grapheme_cluster) {
9944 onig_free(reg_grapheme_cluster);
9945 }
9946
9947 return SIZET2NUM(grapheme_cluster_count);
9948}
9949
9950static VALUE
9951rb_str_enumerate_grapheme_clusters(VALUE str, VALUE ary)
9952{
9953 VALUE orig = str;
9954 rb_encoding *enc = get_encoding(str);
9955 const char *ptr0, *ptr, *end;
9956
9957 if (!rb_enc_unicode_p(enc)) {
9958 return rb_str_enumerate_chars(str, ary);
9959 }
9960
9961 if (!ary) str = rb_str_new_frozen(str);
9962
9963 bool cached_reg_grapheme_cluster = true;
9964 regex_t *reg_grapheme_cluster = get_cached_reg_grapheme_cluster(enc);
9965 if (!reg_grapheme_cluster) {
9966 reg_grapheme_cluster = get_reg_grapheme_cluster(enc);
9967 cached_reg_grapheme_cluster = false;
9968 }
9969
9970 ptr0 = ptr = RSTRING_PTR(str);
9971 end = RSTRING_END(str);
9972
9973 while (ptr < end) {
9974 OnigPosition len = onig_match(reg_grapheme_cluster,
9975 (const OnigUChar *)ptr, (const OnigUChar *)end,
9976 (const OnigUChar *)ptr, NULL, 0);
9977 if (len <= 0) break;
9978 ENUM_ELEM(ary, rb_str_subseq(str, ptr-ptr0, len));
9979 ptr += len;
9980 }
9981
9982 if (!cached_reg_grapheme_cluster) {
9983 onig_free(reg_grapheme_cluster);
9984 }
9985
9986 RB_GC_GUARD(str);
9987 if (ary)
9988 return ary;
9989 else
9990 return orig;
9991}
9992
9993/*
9994 * call-seq:
9995 * each_grapheme_cluster {|grapheme_cluster| ... } -> self
9996 * each_grapheme_cluster -> enumerator
9997 *
9998 * :include: doc/string/each_grapheme_cluster.rdoc
9999 *
10000 */
10001
10002static VALUE
10003rb_str_each_grapheme_cluster(VALUE str)
10004{
10005 RETURN_SIZED_ENUMERATOR(str, 0, 0, rb_str_each_grapheme_cluster_size);
10006 return rb_str_enumerate_grapheme_clusters(str, 0);
10007}
10008
10009/*
10010 * call-seq:
10011 * grapheme_clusters -> array_of_grapheme_clusters
10012 *
10013 * :include: doc/string/grapheme_clusters.rdoc
10014 *
10015 */
10016
10017static VALUE
10018rb_str_grapheme_clusters(VALUE str)
10019{
10020 VALUE ary = WANTARRAY("grapheme_clusters", rb_str_strlen(str));
10021 return rb_str_enumerate_grapheme_clusters(str, ary);
10022}
10023
10024static long
10025chopped_length(VALUE str)
10026{
10027 rb_encoding *enc = STR_ENC_GET(str);
10028 const char *p, *p2, *beg, *end;
10029
10030 beg = RSTRING_PTR(str);
10031 end = beg + RSTRING_LEN(str);
10032 if (beg >= end) return 0;
10033 p = rb_enc_prev_char(beg, end, end, enc);
10034 if (!p) return 0;
10035 if (p > beg && rb_enc_ascget(p, end, 0, enc) == '\n') {
10036 p2 = rb_enc_prev_char(beg, p, end, enc);
10037 if (p2 && rb_enc_ascget(p2, end, 0, enc) == '\r') p = p2;
10038 }
10039 return p - beg;
10040}
10041
10042/*
10043 * call-seq:
10044 * chop! -> self or nil
10045 *
10046 * Like String#chop, except that:
10047 *
10048 * - Removes trailing characters from +self+ (not from a copy of +self+).
10049 * - Returns +self+ if any characters are removed, +nil+ otherwise.
10050 *
10051 * Related: see {Modifying}[rdoc-ref:String@Modifying].
10052 */
10053
10054static VALUE
10055rb_str_chop_bang(VALUE str)
10056{
10057 str_modify_keep_cr(str);
10058 if (RSTRING_LEN(str) > 0) {
10059 long len;
10060 len = chopped_length(str);
10061 STR_SET_LEN(str, len);
10062 TERM_FILL(&RSTRING_PTR(str)[len], TERM_LEN(str));
10063 if (ENC_CODERANGE(str) != ENC_CODERANGE_7BIT) {
10065 }
10066 return str;
10067 }
10068 return Qnil;
10069}
10070
10071
10072/*
10073 * call-seq:
10074 * chop -> new_string
10075 *
10076 * :include: doc/string/chop.rdoc
10077 *
10078 */
10079
10080static VALUE
10081rb_str_chop(VALUE str)
10082{
10083 return rb_str_subseq(str, 0, chopped_length(str));
10084}
10085
10086static long
10087smart_chomp(VALUE str, const char *e, const char *p)
10088{
10089 rb_encoding *enc = rb_enc_get(str);
10090 if (rb_enc_mbminlen(enc) > 1) {
10091 /* a receiver shorter than one character has nothing to chomp */
10092 if (e - p < rb_enc_mbminlen(enc)) return e - p;
10093 const char *pp = rb_enc_left_char_head(p, e-rb_enc_mbminlen(enc), e, enc);
10094 if (rb_enc_is_newline(pp, e, enc)) {
10095 e = pp;
10096 }
10097 pp = e - rb_enc_mbminlen(enc);
10098 if (pp >= p) {
10099 pp = rb_enc_left_char_head(p, pp, e, enc);
10100 if (rb_enc_ascget(pp, e, 0, enc) == '\r') {
10101 e = pp;
10102 }
10103 }
10104 }
10105 else {
10106 switch (*(e-1)) { /* not e[-1] to get rid of VC bug */
10107 case '\n':
10108 if (--e > p && *(e-1) == '\r') {
10109 --e;
10110 }
10111 break;
10112 case '\r':
10113 --e;
10114 break;
10115 }
10116 }
10117 return e - p;
10118}
10119
10120static long
10121chompped_length(VALUE str, VALUE rs)
10122{
10123 rb_encoding *enc;
10124 int newline;
10125 char *pp, *e, *rsptr;
10126 long rslen;
10127 char *const p = RSTRING_PTR(str);
10128 long len = RSTRING_LEN(str);
10129
10130 if (len == 0) return 0;
10131 e = p + len;
10132 if (rs == rb_default_rs) {
10133 return smart_chomp(str, e, p);
10134 }
10135
10136 enc = rb_enc_get(str);
10137 RSTRING_GETMEM(rs, rsptr, rslen);
10138 if (rslen == 0) {
10139 if (rb_enc_mbminlen(enc) > 1) {
10140 while (e - p >= rb_enc_mbminlen(enc)) {
10141 pp = rb_enc_left_char_head(p, e-rb_enc_mbminlen(enc), e, enc);
10142 if (!rb_enc_is_newline(pp, e, enc)) break;
10143 e = pp;
10144 pp -= rb_enc_mbminlen(enc);
10145 if (pp >= p) {
10146 pp = rb_enc_left_char_head(p, pp, e, enc);
10147 if (rb_enc_ascget(pp, e, 0, enc) == '\r') {
10148 e = pp;
10149 }
10150 }
10151 }
10152 }
10153 else {
10154 while (e > p && *(e-1) == '\n') {
10155 --e;
10156 if (e > p && *(e-1) == '\r')
10157 --e;
10158 }
10159 }
10160 return e - p;
10161 }
10162 if (rslen > len) return len;
10163
10164 enc = rb_enc_get(rs);
10165 newline = rsptr[rslen-1];
10166 if (rslen == rb_enc_mbminlen(enc)) {
10167 if (rslen == 1) {
10168 if (newline == '\n')
10169 return smart_chomp(str, e, p);
10170 }
10171 else {
10172 if (rb_enc_is_newline(rsptr, rsptr+rslen, enc))
10173 return smart_chomp(str, e, p);
10174 }
10175 }
10176
10177 enc = rb_enc_check(str, rs);
10178 if (is_broken_string(rs)) {
10179 return len;
10180 }
10181 pp = e - rslen;
10182 if (p[len-1] == newline &&
10183 (rslen <= 1 ||
10184 memcmp(rsptr, pp, rslen) == 0)) {
10185 if (at_char_boundary(p, pp, e, enc))
10186 return len - rslen;
10187 RB_GC_GUARD(rs);
10188 }
10189 return len;
10190}
10191
10197static VALUE
10198chomp_rs(int argc, const VALUE *argv)
10199{
10200 rb_check_arity(argc, 0, 1);
10201 if (argc > 0) {
10202 VALUE rs = argv[0];
10203 if (!NIL_P(rs)) StringValue(rs);
10204 return rs;
10205 }
10206 else {
10207 return rb_rs;
10208 }
10209}
10210
10211VALUE
10212rb_str_chomp_string(VALUE str, VALUE rs)
10213{
10214 long olen = RSTRING_LEN(str);
10215 long len = chompped_length(str, rs);
10216 if (len >= olen) return Qnil;
10217 str_modify_keep_cr(str);
10218 STR_SET_LEN(str, len);
10219 TERM_FILL(&RSTRING_PTR(str)[len], TERM_LEN(str));
10220 if (ENC_CODERANGE(str) != ENC_CODERANGE_7BIT) {
10222 }
10223 return str;
10224}
10225
10226/*
10227 * call-seq:
10228 * chomp!(line_sep = $/) -> self or nil
10229 *
10230 * Like String#chomp, except that:
10231 *
10232 * - Removes trailing characters from +self+ (not from a copy of +self+).
10233 * - Returns +self+ if any characters are removed, +nil+ otherwise.
10234 *
10235 * Related: see {Modifying}[rdoc-ref:String@Modifying].
10236 */
10237
10238static VALUE
10239rb_str_chomp_bang(int argc, VALUE *argv, VALUE str)
10240{
10241 VALUE rs;
10242 str_modifiable(str);
10243 if (RSTRING_LEN(str) == 0 && argc < 2) return Qnil;
10244 rs = chomp_rs(argc, argv);
10245 if (NIL_P(rs)) return Qnil;
10246 return rb_str_chomp_string(str, rs);
10247}
10248
10249
10250/*
10251 * call-seq:
10252 * chomp(line_sep = $/) -> new_string
10253 *
10254 * :include: doc/string/chomp.rdoc
10255 *
10256 */
10257
10258static VALUE
10259rb_str_chomp(int argc, VALUE *argv, VALUE str)
10260{
10261 VALUE rs = chomp_rs(argc, argv);
10262 if (NIL_P(rs)) return str_duplicate(rb_cString, str);
10263 return rb_str_subseq(str, 0, chompped_length(str, rs));
10264}
10265
10266static void
10267tr_setup_table_multi(char table[TR_TABLE_SIZE], VALUE *tablep, VALUE *ctablep,
10268 VALUE str, int num_selectors, VALUE *selectors)
10269{
10270 int i;
10271
10272 for (i=0; i<num_selectors; i++) {
10273 VALUE selector = selectors[i];
10274 rb_encoding *enc;
10275
10276 StringValue(selector);
10277 enc = rb_enc_check(str, selector);
10278 tr_setup_table(selector, table, i==0, tablep, ctablep, enc);
10279 }
10280}
10281
10282static long
10283lstrip_offset(VALUE str, const char *s, const char *e, rb_encoding *enc)
10284{
10285 const char *const start = s;
10286
10287 if (!s || s >= e) return 0;
10288
10289 /* remove spaces at head */
10290 if (single_byte_optimizable(str)) {
10291 while (s < e && (*s == '\0' || ascii_isspace(*s))) s++;
10292 }
10293 else {
10294 while (s < e) {
10295 int n;
10296 unsigned int cc = rb_enc_codepoint_len(s, e, &n, enc);
10297
10298 if (cc && !rb_isspace(cc)) break;
10299 s += n;
10300 }
10301 }
10302 return s - start;
10303}
10304
10305static long
10306lstrip_offset_table(VALUE str, const char *s, const char *e, rb_encoding *enc,
10307 char table[TR_TABLE_SIZE], VALUE del, VALUE nodel)
10308{
10309 const char *const start = s;
10310
10311 if (!s || s >= e) return 0;
10312
10313 /* remove leading characters in the table */
10314 while (s < e) {
10315 int n;
10316 unsigned int cc = rb_enc_codepoint_len(s, e, &n, enc);
10317
10318 if (!tr_find(cc, table, del, nodel)) break;
10319 s += n;
10320 }
10321 return s - start;
10322}
10323
10324/*
10325 * call-seq:
10326 * lstrip!(*selectors) -> self or nil
10327 *
10328 * Like String#lstrip, except that:
10329 *
10330 * - Performs stripping in +self+ (not in a copy of +self+).
10331 * - Returns +self+ if any characters are stripped, +nil+ otherwise.
10332 *
10333 * Related: see {Modifying}[rdoc-ref:String@Modifying].
10334 */
10335
10336static VALUE
10337rb_str_lstrip_bang(int argc, VALUE *argv, VALUE str)
10338{
10339 rb_encoding *enc;
10340 char *start, *s;
10341 long olen, loffset;
10342
10343 str_modify_keep_cr(str);
10344 enc = STR_ENC_GET(str);
10345 RSTRING_GETMEM(str, start, olen);
10346 if (argc > 0) {
10347 char table[TR_TABLE_SIZE];
10348 VALUE del = 0, nodel = 0;
10349
10350 tr_setup_table_multi(table, &del, &nodel, str, argc, argv);
10351
10352 /* the selector conversion may have modified str */
10353 str_modify_keep_cr(str);
10354 enc = STR_ENC_GET(str);
10355 RSTRING_GETMEM(str, start, olen);
10356
10357 loffset = lstrip_offset_table(str, start, start+olen, enc, table, del, nodel);
10358 }
10359 else {
10360 loffset = lstrip_offset(str, start, start+olen, enc);
10361 }
10362
10363 if (loffset > 0) {
10364 long len = olen-loffset;
10365 s = start + loffset;
10366 memmove(start, s, len);
10367 STR_SET_LEN(str, len);
10368 TERM_FILL(start+len, rb_enc_mbminlen(enc));
10369 return str;
10370 }
10371 return Qnil;
10372}
10373
10374
10375/*
10376 * call-seq:
10377 * lstrip(*selectors) -> new_string
10378 *
10379 * Returns a copy of +self+ with leading whitespace removed;
10380 * see {Whitespace in Strings}[rdoc-ref:String@Whitespace+in+Strings]:
10381 *
10382 * whitespace = "\x00\t\n\v\f\r "
10383 * s = whitespace + 'abc' + whitespace
10384 * # => "\u0000\t\n\v\f\r abc\u0000\t\n\v\f\r "
10385 * s.lstrip
10386 * # => "abc\u0000\t\n\v\f\r "
10387 *
10388 * If +selectors+ are given, removes characters of +selectors+ from the beginning of +self+:
10389 *
10390 * s = "---abc+++"
10391 * s.lstrip("-") # => "abc+++"
10392 *
10393 * +selectors+ must be valid character selectors (see {Character Selectors}[rdoc-ref:character_selectors.rdoc]),
10394 * and may use any of its valid forms, including negation, ranges, and escapes:
10395 *
10396 * "01234abc56789".lstrip("0-9") # "abc56789"
10397 * "01234abc56789".lstrip("0-9", "^4-6") # "4abc56789"
10398 *
10399 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
10400 */
10401
10402static VALUE
10403rb_str_lstrip(int argc, VALUE *argv, VALUE str)
10404{
10405 char *start;
10406 long len, loffset;
10407
10408 RSTRING_GETMEM(str, start, len);
10409 if (argc > 0) {
10410 char table[TR_TABLE_SIZE];
10411 VALUE del = 0, nodel = 0;
10412
10413 tr_setup_table_multi(table, &del, &nodel, str, argc, argv);
10414
10415 /* the selector conversion may have modified str */
10416 RSTRING_GETMEM(str, start, len);
10417
10418 loffset = lstrip_offset_table(str, start, start+len, STR_ENC_GET(str), table, del, nodel);
10419 }
10420 else {
10421 loffset = lstrip_offset(str, start, start+len, STR_ENC_GET(str));
10422 }
10423 if (loffset <= 0) return str_duplicate(rb_cString, str);
10424 return rb_str_subseq(str, loffset, len - loffset);
10425}
10426
10427static long
10428rstrip_offset(VALUE str, const char *s, const char *e, rb_encoding *enc)
10429{
10430 const char *t;
10431
10432 rb_str_check_dummy_enc(enc);
10434 rb_raise(rb_eEncCompatError, "invalid byte sequence in %s", rb_enc_name(enc));
10435 }
10436 if (!s || s >= e) return 0;
10437 t = e;
10438
10439 /* remove trailing spaces or '\0's */
10440 if (single_byte_optimizable(str)) {
10441 unsigned char c;
10442 while (s < t && ((c = *(t-1)) == '\0' || ascii_isspace(c))) t--;
10443 }
10444 else {
10445 char *tp;
10446
10447 while ((tp = rb_enc_prev_char(s, t, e, enc)) != NULL) {
10448 unsigned int c = rb_enc_codepoint(tp, e, enc);
10449 if (c && !rb_isspace(c)) break;
10450 t = tp;
10451 }
10452 }
10453 return e - t;
10454}
10455
10456static long
10457rstrip_offset_table(VALUE str, const char *s, const char *e, rb_encoding *enc,
10458 char table[TR_TABLE_SIZE], VALUE del, VALUE nodel)
10459{
10460 const char *t;
10461 char *tp;
10462
10463 rb_str_check_dummy_enc(enc);
10465 rb_raise(rb_eEncCompatError, "invalid byte sequence in %s", rb_enc_name(enc));
10466 }
10467 if (!s || s >= e) return 0;
10468 t = e;
10469
10470 /* remove trailing characters in the table */
10471 while ((tp = rb_enc_prev_char(s, t, e, enc)) != NULL) {
10472 unsigned int c = rb_enc_codepoint(tp, e, enc);
10473 if (!tr_find(c, table, del, nodel)) break;
10474 t = tp;
10475 }
10476
10477 return e - t;
10478}
10479
10480/*
10481 * call-seq:
10482 * rstrip!(*selectors) -> self or nil
10483 *
10484 * Like String#rstrip, except that:
10485 *
10486 * - Performs stripping in +self+ (not in a copy of +self+).
10487 * - Returns +self+ if any characters are stripped, +nil+ otherwise.
10488 *
10489 * Related: see {Modifying}[rdoc-ref:String@Modifying].
10490 */
10491
10492static VALUE
10493rb_str_rstrip_bang(int argc, VALUE *argv, VALUE str)
10494{
10495 rb_encoding *enc;
10496 char *start;
10497 long olen, roffset;
10498
10499 str_modify_keep_cr(str);
10500 enc = STR_ENC_GET(str);
10501 RSTRING_GETMEM(str, start, olen);
10502 if (argc > 0) {
10503 char table[TR_TABLE_SIZE];
10504 VALUE del = 0, nodel = 0;
10505
10506 tr_setup_table_multi(table, &del, &nodel, str, argc, argv);
10507
10508 /* the selector conversion may have modified str */
10509 str_modify_keep_cr(str);
10510 enc = STR_ENC_GET(str);
10511 RSTRING_GETMEM(str, start, olen);
10512
10513 roffset = rstrip_offset_table(str, start, start+olen, enc, table, del, nodel);
10514 }
10515 else {
10516 roffset = rstrip_offset(str, start, start+olen, enc);
10517 }
10518 if (roffset > 0) {
10519 long len = olen - roffset;
10520
10521 STR_SET_LEN(str, len);
10522 TERM_FILL(start+len, rb_enc_mbminlen(enc));
10523 return str;
10524 }
10525 return Qnil;
10526}
10527
10528
10529/*
10530 * call-seq:
10531 * rstrip(*selectors) -> new_string
10532 *
10533 * Returns a copy of +self+ with trailing whitespace removed;
10534 * see {Whitespace in Strings}[rdoc-ref:String@Whitespace+in+Strings]:
10535 *
10536 * whitespace = "\x00\t\n\v\f\r "
10537 * s = whitespace + 'abc' + whitespace
10538 * s # => "\u0000\t\n\v\f\r abc\u0000\t\n\v\f\r "
10539 * s.rstrip # => "\u0000\t\n\v\f\r abc"
10540 *
10541 * If +selectors+ are given, removes characters of +selectors+ from the end of +self+:
10542 *
10543 * s = "---abc+++"
10544 * s.rstrip("+") # => "---abc"
10545 *
10546 * +selectors+ must be valid character selectors (see {Character Selectors}[rdoc-ref:character_selectors.rdoc]),
10547 * and may use any of its valid forms, including negation, ranges, and escapes:
10548 *
10549 * "01234abc56789".rstrip("0-9") # "01234abc"
10550 * "01234abc56789".rstrip("0-9", "^4-6") # "01234abc56"
10551 *
10552 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
10553 */
10554
10555static VALUE
10556rb_str_rstrip(int argc, VALUE *argv, VALUE str)
10557{
10558 rb_encoding *enc;
10559 char *start;
10560 long olen, roffset;
10561
10562 enc = STR_ENC_GET(str);
10563 RSTRING_GETMEM(str, start, olen);
10564 if (argc > 0) {
10565 char table[TR_TABLE_SIZE];
10566 VALUE del = 0, nodel = 0;
10567
10568 tr_setup_table_multi(table, &del, &nodel, str, argc, argv);
10569
10570 /* the selector conversion may have modified str */
10571 enc = STR_ENC_GET(str);
10572
10573 RSTRING_GETMEM(str, start, olen);
10574 roffset = rstrip_offset_table(str, start, start+olen, enc, table, del, nodel);
10575 }
10576 else {
10577 roffset = rstrip_offset(str, start, start+olen, enc);
10578 }
10579 if (roffset <= 0) return str_duplicate(rb_cString, str);
10580 return rb_str_subseq(str, 0, olen-roffset);
10581}
10582
10583
10584/*
10585 * call-seq:
10586 * strip!(*selectors) -> self or nil
10587 *
10588 * Like String#strip, except that:
10589 *
10590 * - Any modifications are made to +self+.
10591 * - Returns +self+ if any modification are made, +nil+ otherwise.
10592 *
10593 * Related: see {Modifying}[rdoc-ref:String@Modifying].
10594 */
10595
10596static VALUE
10597rb_str_strip_bang(int argc, VALUE *argv, VALUE str)
10598{
10599 char *start;
10600 long olen, loffset, roffset;
10601 rb_encoding *enc;
10602
10603 str_modify_keep_cr(str);
10604 enc = STR_ENC_GET(str);
10605 RSTRING_GETMEM(str, start, olen);
10606
10607 if (argc > 0) {
10608 char table[TR_TABLE_SIZE];
10609 VALUE del = 0, nodel = 0;
10610
10611 tr_setup_table_multi(table, &del, &nodel, str, argc, argv);
10612
10613 /* the selector conversion may have modified str */
10614 str_modify_keep_cr(str);
10615 enc = STR_ENC_GET(str);
10616 RSTRING_GETMEM(str, start, olen);
10617
10618 loffset = lstrip_offset_table(str, start, start+olen, enc, table, del, nodel);
10619 roffset = rstrip_offset_table(str, start+loffset, start+olen, enc, table, del, nodel);
10620 }
10621 else {
10622 loffset = lstrip_offset(str, start, start+olen, enc);
10623 roffset = rstrip_offset(str, start+loffset, start+olen, enc);
10624 }
10625
10626 if (loffset > 0 || roffset > 0) {
10627 long len = olen-roffset;
10628 if (loffset > 0) {
10629 len -= loffset;
10630 memmove(start, start + loffset, len);
10631 }
10632 STR_SET_LEN(str, len);
10633 TERM_FILL(start+len, rb_enc_mbminlen(enc));
10634 return str;
10635 }
10636 return Qnil;
10637}
10638
10639
10640/*
10641 * call-seq:
10642 * strip(*selectors) -> new_string
10643 *
10644 * Returns a copy of +self+ with leading and trailing whitespace removed;
10645 * see {Whitespace in Strings}[rdoc-ref:String@Whitespace+in+Strings]:
10646 *
10647 * whitespace = "\x00\t\n\v\f\r "
10648 * s = whitespace + 'abc' + whitespace
10649 * # => "\u0000\t\n\v\f\r abc\u0000\t\n\v\f\r "
10650 * s.strip # => "abc"
10651 *
10652 * If +selectors+ are given, removes characters of +selectors+ from both ends of +self+:
10653 *
10654 * s = "---abc+++"
10655 * s.strip("-+") # => "abc"
10656 * s.strip("+-") # => "abc"
10657 *
10658 * +selectors+ must be valid character selectors (see {Character Selectors}[rdoc-ref:character_selectors.rdoc]),
10659 * and may use any of its valid forms, including negation, ranges, and escapes:
10660 *
10661 * "01234abc56789".strip("0-9") # "abc"
10662 * "01234abc56789".strip("0-9", "^4-6") # "4abc56"
10663 *
10664 * Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
10665 */
10666
10667static VALUE
10668rb_str_strip(int argc, VALUE *argv, VALUE str)
10669{
10670 char *start;
10671 long olen, loffset, roffset;
10672 rb_encoding *enc = STR_ENC_GET(str);
10673
10674 RSTRING_GETMEM(str, start, olen);
10675
10676 if (argc > 0) {
10677 char table[TR_TABLE_SIZE];
10678 VALUE del = 0, nodel = 0;
10679
10680 tr_setup_table_multi(table, &del, &nodel, str, argc, argv);
10681
10682 /* the selector conversion may have modified str */
10683 enc = STR_ENC_GET(str);
10684 RSTRING_GETMEM(str, start, olen);
10685
10686 loffset = lstrip_offset_table(str, start, start+olen, enc, table, del, nodel);
10687 roffset = rstrip_offset_table(str, start+loffset, start+olen, enc, table, del, nodel);
10688 }
10689 else {
10690 loffset = lstrip_offset(str, start, start+olen, enc);
10691 roffset = rstrip_offset(str, start+loffset, start+olen, enc);
10692 }
10693
10694 if (loffset <= 0 && roffset <= 0) return str_duplicate(rb_cString, str);
10695 return rb_str_subseq(str, loffset, olen-loffset-roffset);
10696}
10697
10698static VALUE
10699scan_once(VALUE str, VALUE pat, long *start, int set_backref_str)
10700{
10701 VALUE result = Qnil;
10702 long end, pos = rb_pat_search(pat, str, *start, set_backref_str);
10703 if (pos >= 0) {
10704 VALUE match;
10705 struct re_registers *regs;
10706 if (BUILTIN_TYPE(pat) == T_STRING) {
10707 regs = NULL;
10708 end = pos + RSTRING_LEN(pat);
10709 }
10710 else {
10711 match = rb_backref_get();
10712 regs = RMATCH_REGS(match);
10713 pos = BEG(0);
10714 end = END(0);
10715 }
10716
10717 if (pos == end) {
10718 rb_encoding *enc = STR_ENC_GET(str);
10719 /*
10720 * Always consume at least one character of the input string
10721 */
10722 if (RSTRING_LEN(str) > end)
10723 *start = end + rb_enc_fast_mbclen(RSTRING_PTR(str) + end,
10724 RSTRING_END(str), enc);
10725 else
10726 *start = end + 1;
10727 }
10728 else {
10729 *start = end;
10730 }
10731
10732 if (!regs || regs->num_regs == 1) {
10733 result = rb_str_subseq(str, pos, end - pos);
10734 return result;
10735 }
10736 else {
10737 result = rb_ary_new2(regs->num_regs);
10738 for (int i = 1; i < regs->num_regs; i++) {
10739 VALUE s = Qnil;
10740 if (BEG(i) >= 0) {
10741 s = rb_str_subseq(str, BEG(i), END(i)-BEG(i));
10742 }
10743
10744 rb_ary_push(result, s);
10745 }
10746 }
10747
10748 RB_GC_GUARD(match);
10749 }
10750
10751 return result;
10752}
10753
10754
10755/*
10756 * call-seq:
10757 * scan(pattern) -> array_of_results
10758 * scan(pattern) {|result| ... } -> self
10759 *
10760 * :include: doc/string/scan.rdoc
10761 *
10762 */
10763
10764static VALUE
10765rb_str_scan(VALUE str, VALUE pat)
10766{
10767 VALUE result;
10768 long start = 0;
10769 long last = -1, prev = 0;
10770 char *p = RSTRING_PTR(str); long len = RSTRING_LEN(str);
10771
10772 pat = get_pat_quoted(pat, 1);
10773 mustnot_broken(str);
10774 if (!rb_block_given_p()) {
10775 VALUE ary = rb_ary_new();
10776
10777 while (!NIL_P(result = scan_once(str, pat, &start, 0))) {
10778 last = prev;
10779 prev = start;
10780 rb_ary_push(ary, result);
10781 }
10782 if (last >= 0) rb_pat_search(pat, str, last, 1);
10783 else rb_backref_set(Qnil);
10784 return ary;
10785 }
10786
10787 while (!NIL_P(result = scan_once(str, pat, &start, 1))) {
10788 last = prev;
10789 prev = start;
10790 rb_yield(result);
10791 str_mod_check(str, p, len);
10792 }
10793 if (last >= 0) rb_pat_search(pat, str, last, 1);
10794 return str;
10795}
10796
10797
10798/*
10799 * call-seq:
10800 * hex -> integer
10801 *
10802 * Interprets the leading substring of +self+ as hexadecimal, possibly signed;
10803 * returns its value as an integer.
10804 *
10805 * The leading substring is interpreted as hexadecimal when it begins with:
10806 *
10807 * - One or more character representing hexadecimal digits
10808 * (each in one of the ranges <tt>'0'..'9'</tt>, <tt>'a'..'f'</tt>, or <tt>'A'..'F'</tt>);
10809 * the string to be interpreted ends at the first character that does not represent a hexadecimal digit:
10810 *
10811 * 'f'.hex # => 15
10812 * '11'.hex # => 17
10813 * 'FFF'.hex # => 4095
10814 * 'fffg'.hex # => 4095
10815 * 'foo'.hex # => 15 # 'f' hexadecimal, 'oo' not.
10816 * 'bar'.hex # => 186 # 'ba' hexadecimal, 'r' not.
10817 * 'deadbeef'.hex # => 3735928559
10818 *
10819 * - <tt>'0x'</tt> or <tt>'0X'</tt>, followed by one or more hexadecimal digits:
10820 *
10821 * '0xfff'.hex # => 4095
10822 * '0xfffg'.hex # => 4095
10823 *
10824 * Any of the above may prefixed with <tt>'-'</tt>, which negates the interpreted value:
10825 *
10826 * '-fff'.hex # => -4095
10827 * '-0xFFF'.hex # => -4095
10828 *
10829 * For any substring not described above, returns zero:
10830 *
10831 * 'xxx'.hex # => 0
10832 * ''.hex # => 0
10833 *
10834 * Note that, unlike #oct, this method interprets only hexadecimal,
10835 * and not binary, octal, or decimal notations:
10836 *
10837 * '0b111'.hex # => 45329
10838 * '0o777'.hex # => 0
10839 * '0d999'.hex # => 55705
10840 *
10841 * Related: See {Converting to Non-String}[rdoc-ref:String@Converting+to+Non--5CString].
10842 */
10843
10844static VALUE
10845rb_str_hex(VALUE str)
10846{
10847 return rb_str_to_inum(str, 16, FALSE);
10848}
10849
10850
10851/*
10852 * call-seq:
10853 * oct -> integer
10854 *
10855 * Interprets the leading substring of +self+ as octal, binary, decimal, or hexadecimal, possibly signed;
10856 * returns their value as an integer.
10857 *
10858 * In brief:
10859 *
10860 * # Interpreted as octal.
10861 * '777'.oct # => 511
10862 * '777x'.oct # => 511
10863 * '0777'.oct # => 511
10864 * '0o777'.oct # => 511
10865 * '-777'.oct # => -511
10866 * # Not interpreted as octal.
10867 * '0b111'.oct # => 7 # Interpreted as binary.
10868 * '0d999'.oct # => 999 # Interpreted as decimal.
10869 * '0xfff'.oct # => 4095 # Interpreted as hexadecimal.
10870 *
10871 * The leading substring is interpreted as octal when it begins with:
10872 *
10873 * - One or more character representing octal digits
10874 * (each in the range <tt>'0'..'7'</tt>);
10875 * the string to be interpreted ends at the first character that does not represent an octal digit:
10876 *
10877 * '7'.oct @ => 7
10878 * '11'.oct # => 9
10879 * '777'.oct # => 511
10880 * '0777'.oct # => 511
10881 * '7778'.oct # => 511
10882 * '777x'.oct # => 511
10883 *
10884 * - <tt>'0o'</tt>, followed by one or more octal digits:
10885 *
10886 * '0o777'.oct # => 511
10887 * '0o7778'.oct # => 511
10888 *
10889 * The leading substring is _not_ interpreted as octal when it begins with:
10890 *
10891 * - <tt>'0b'</tt>, followed by one or more characters representing binary digits
10892 * (each in the range <tt>'0'..'1'</tt>);
10893 * the string to be interpreted ends at the first character that does not represent a binary digit.
10894 * the string is interpreted as binary digits (base 2):
10895 *
10896 * '0b111'.oct # => 7
10897 * '0b1112'.oct # => 7
10898 *
10899 * - <tt>'0d'</tt>, followed by one or more characters representing decimal digits
10900 * (each in the range <tt>'0'..'9'</tt>);
10901 * the string to be interpreted ends at the first character that does not represent a decimal digit.
10902 * the string is interpreted as decimal digits (base 10):
10903 *
10904 * '0d999'.oct # => 999
10905 * '0d999x'.oct # => 999
10906 *
10907 * - <tt>'0x'</tt>, followed by one or more characters representing hexadecimal digits
10908 * (each in one of the ranges <tt>'0'..'9'</tt>, <tt>'a'..'f'</tt>, or <tt>'A'..'F'</tt>);
10909 * the string to be interpreted ends at the first character that does not represent a hexadecimal digit.
10910 * the string is interpreted as hexadecimal digits (base 16):
10911 *
10912 * '0xfff'.oct # => 4095
10913 * '0xfffg'.oct # => 4095
10914 *
10915 * Any of the above may prefixed with <tt>'-'</tt>, which negates the interpreted value:
10916 *
10917 * '-777'.oct # => -511
10918 * '-0777'.oct # => -511
10919 * '-0b111'.oct # => -7
10920 * '-0xfff'.oct # => -4095
10921 *
10922 * For any substring not described above, returns zero:
10923 *
10924 * 'foo'.oct # => 0
10925 * ''.oct # => 0
10926 *
10927 * Related: see {Converting to Non-String}[rdoc-ref:String@Converting+to+Non--5CString].
10928 */
10929
10930static VALUE
10931rb_str_oct(VALUE str)
10932{
10933 return rb_str_to_inum(str, -8, FALSE);
10934}
10935
10936#ifndef HAVE_CRYPT_R
10937# include "ruby/thread_native.h"
10938# include "ruby/atomic.h"
10939
10940static struct {
10941 rb_nativethread_lock_t lock;
10942} crypt_mutex = {PTHREAD_MUTEX_INITIALIZER};
10943#endif
10944
10945/*
10946 * call-seq:
10947 * crypt(salt_str) -> new_string
10948 *
10949 * Returns the string generated by calling <code>crypt(3)</code>
10950 * standard library function with <code>str</code> and
10951 * <code>salt_str</code>, in this order, as its arguments. Please do
10952 * not use this method any longer. It is legacy; provided only for
10953 * backward compatibility with ruby scripts in earlier days. It is
10954 * bad to use in contemporary programs for several reasons:
10955 *
10956 * * Behaviour of C's <code>crypt(3)</code> depends on the OS it is
10957 * run. The generated string lacks data portability.
10958 *
10959 * * On some OSes such as Mac OS, <code>crypt(3)</code> never fails
10960 * (i.e. silently ends up in unexpected results).
10961 *
10962 * * On some OSes such as Mac OS, <code>crypt(3)</code> is not
10963 * thread safe.
10964 *
10965 * * So-called "traditional" usage of <code>crypt(3)</code> is very
10966 * very very weak. According to its manpage, Linux's traditional
10967 * <code>crypt(3)</code> output has only 2**56 variations; too
10968 * easy to brute force today. And this is the default behaviour.
10969 *
10970 * * In order to make things robust some OSes implement so-called
10971 * "modular" usage. To go through, you have to do a complex
10972 * build-up of the <code>salt_str</code> parameter, by hand.
10973 * Failure in generation of a proper salt string tends not to
10974 * yield any errors; typos in parameters are normally not
10975 * detectable.
10976 *
10977 * * For instance, in the following example, the second invocation
10978 * of String#crypt is wrong; it has a typo in "round=" (lacks
10979 * "s"). However the call does not fail and something unexpected
10980 * is generated.
10981 *
10982 * "foo".crypt("$5$rounds=1000$salt$") # OK, proper usage
10983 * "foo".crypt("$5$round=1000$salt$") # Typo not detected
10984 *
10985 * * Even in the "modular" mode, some hash functions are considered
10986 * archaic and no longer recommended at all; for instance module
10987 * <code>$1$</code> is officially abandoned by its author: see
10988 * http://phk.freebsd.dk/sagas/md5crypt_eol/ . For another
10989 * instance module <code>$3$</code> is considered completely
10990 * broken: see the manpage of FreeBSD.
10991 *
10992 * * On some OS such as Mac OS, there is no modular mode. Yet, as
10993 * written above, <code>crypt(3)</code> on Mac OS never fails.
10994 * This means even if you build up a proper salt string it
10995 * generates a traditional DES hash anyways, and there is no way
10996 * for you to be aware of.
10997 *
10998 * "foo".crypt("$5$rounds=1000$salt$") # => "$5fNPQMxC5j6."
10999 *
11000 * If for some reason you cannot migrate to other secure contemporary
11001 * password hashing algorithms, install the string-crypt gem and
11002 * <code>require 'string/crypt'</code> to continue using it.
11003 */
11004
11005static VALUE
11006rb_str_crypt(VALUE str, VALUE salt)
11007{
11008#ifdef HAVE_CRYPT_R
11009 VALUE databuf;
11010 struct crypt_data *data;
11011# define CRYPT_END() ALLOCV_END(databuf)
11012#else
11013 char *tmp_buf;
11014 extern char *crypt(const char *, const char *);
11015# define CRYPT_END() rb_nativethread_lock_unlock(&crypt_mutex.lock)
11016#endif
11017 VALUE result;
11018 const char *s, *saltp;
11019 char *res;
11020#ifdef BROKEN_CRYPT
11021 char salt_8bit_clean[3];
11022#endif
11023
11024 StringValue(salt);
11025 mustnot_wchar(str);
11026 mustnot_wchar(salt);
11027 s = StringValueCStr(str);
11028 saltp = RSTRING_PTR(salt);
11029 if (RSTRING_LEN(salt) < 2 || !saltp[0] || !saltp[1]) {
11030 rb_raise(rb_eArgError, "salt too short (need >=2 bytes)");
11031 }
11032
11033#ifdef BROKEN_CRYPT
11034 if (!ISASCII((unsigned char)saltp[0]) || !ISASCII((unsigned char)saltp[1])) {
11035 salt_8bit_clean[0] = saltp[0] & 0x7f;
11036 salt_8bit_clean[1] = saltp[1] & 0x7f;
11037 salt_8bit_clean[2] = '\0';
11038 saltp = salt_8bit_clean;
11039 }
11040#endif
11041#ifdef HAVE_CRYPT_R
11042 data = ALLOCV(databuf, sizeof(struct crypt_data));
11043# ifdef HAVE_STRUCT_CRYPT_DATA_INITIALIZED
11044 data->initialized = 0;
11045# endif
11046 res = crypt_r(s, saltp, data);
11047#else
11048 rb_nativethread_lock_lock(&crypt_mutex.lock);
11049 res = crypt(s, saltp);
11050#endif
11051 if (!res) {
11052 int err = errno;
11053 CRYPT_END();
11054 rb_syserr_fail(err, "crypt");
11055 }
11056#ifdef HAVE_CRYPT_R
11057 result = rb_str_new_cstr(res);
11058 CRYPT_END();
11059#else
11060 // We need to copy this buffer because it's static and we need to unlock the mutex
11061 // before allocating a new object (the string to be returned). If we allocate while
11062 // holding the lock, we could run GC which fires the VM barrier and causes a deadlock
11063 // if other ractors are waiting on this lock.
11064 size_t res_size = strlen(res)+1;
11065 tmp_buf = ALLOCA_N(char, res_size); // should be small enough to alloca
11066 memcpy(tmp_buf, res, res_size);
11067 res = tmp_buf;
11068 CRYPT_END();
11069 result = rb_str_new_cstr(res);
11070#endif
11071 return result;
11072}
11073
11074
11075/*
11076 * call-seq:
11077 * ord -> integer
11078 *
11079 * :include: doc/string/ord.rdoc
11080 *
11081 */
11082
11083static VALUE
11084rb_str_ord(VALUE s)
11085{
11086 unsigned int c;
11087
11088 c = rb_enc_codepoint(RSTRING_PTR(s), RSTRING_END(s), STR_ENC_GET(s));
11089 return UINT2NUM(c);
11090}
11091/*
11092 * call-seq:
11093 * sum(n = 16) -> integer
11094 *
11095 * :include: doc/string/sum.rdoc
11096 *
11097 */
11098
11099static VALUE
11100rb_str_sum(int argc, VALUE *argv, VALUE str)
11101{
11102 int bits = 16;
11103 char *ptr, *p, *pend;
11104 long len;
11105 VALUE sum = INT2FIX(0);
11106 unsigned long sum0 = 0;
11107
11108 if (rb_check_arity(argc, 0, 1) && (bits = NUM2INT(argv[0])) < 0) {
11109 bits = 0;
11110 }
11111 ptr = p = RSTRING_PTR(str);
11112 len = RSTRING_LEN(str);
11113 pend = p + len;
11114
11115 while (p < pend) {
11116 if (FIXNUM_MAX - UCHAR_MAX < sum0) {
11117 sum = rb_funcall(sum, '+', 1, LONG2FIX(sum0));
11118 str_mod_check(str, ptr, len);
11119 sum0 = 0;
11120 }
11121 sum0 += (unsigned char)*p;
11122 p++;
11123 }
11124
11125 if (bits == 0) {
11126 if (sum0) {
11127 sum = rb_funcall(sum, '+', 1, LONG2FIX(sum0));
11128 }
11129 }
11130 else {
11131 if (sum == INT2FIX(0)) {
11132 if (bits < (int)sizeof(long)*CHAR_BIT) {
11133 sum0 &= (((unsigned long)1)<<bits)-1;
11134 }
11135 sum = LONG2FIX(sum0);
11136 }
11137 else {
11138 VALUE mod;
11139
11140 if (sum0) {
11141 sum = rb_funcall(sum, '+', 1, LONG2FIX(sum0));
11142 }
11143
11144 mod = rb_funcall(INT2FIX(1), idLTLT, 1, INT2FIX(bits));
11145 mod = rb_funcall(mod, '-', 1, INT2FIX(1));
11146 sum = rb_funcall(sum, '&', 1, mod);
11147 }
11148 }
11149 return sum;
11150}
11151
11152static VALUE
11153rb_str_justify(int argc, VALUE *argv, VALUE str, char jflag)
11154{
11155 rb_encoding *enc;
11156 VALUE w;
11157 long width, len, flen = 1, fclen = 1;
11158 VALUE res;
11159 char *p;
11160 const char *f = " ";
11161 long n, size, llen, rlen, llen2 = 0, rlen2 = 0;
11162 VALUE pad;
11163 int singlebyte = 1, cr;
11164 int termlen;
11165
11166 rb_scan_args(argc, argv, "11", &w, &pad);
11167 enc = STR_ENC_GET(str);
11168 width = NUM2LONG(w);
11169 if (argc == 2) {
11170 StringValue(pad);
11171 enc = rb_enc_check(str, pad);
11172 f = RSTRING_PTR(pad);
11173 flen = RSTRING_LEN(pad);
11174 fclen = str_strlen(pad, enc); /* rb_enc_check */
11175 singlebyte = single_byte_optimizable(pad);
11176 if (flen == 0 || fclen == 0) {
11177 rb_raise(rb_eArgError, "zero width padding");
11178 }
11179 }
11180 termlen = rb_enc_mbminlen(enc);
11181 len = str_strlen(str, enc); /* rb_enc_check */
11182 if (width < 0 || len >= width) return str_duplicate(rb_cString, str);
11183 n = width - len;
11184 llen = (jflag == 'l') ? 0 : ((jflag == 'r') ? n : n/2);
11185 rlen = n - llen;
11186 cr = ENC_CODERANGE(str);
11187 if (flen > 1) {
11188 llen2 = str_offset(f, f + flen, llen % fclen, enc, singlebyte);
11189 rlen2 = str_offset(f, f + flen, rlen % fclen, enc, singlebyte);
11190 }
11191 size = RSTRING_LEN(str);
11192 if ((len = llen / fclen + rlen / fclen) >= LONG_MAX / flen ||
11193 (len *= flen) >= LONG_MAX - llen2 - rlen2 ||
11194 (len += llen2 + rlen2) >= LONG_MAX - size) {
11195 rb_raise(rb_eArgError, "argument too big");
11196 }
11197 len += size;
11198 res = str_enc_new(rb_cString, 0, len, enc);
11199 p = RSTRING_PTR(res);
11200 if (flen <= 1) {
11201 memset(p, *f, llen);
11202 p += llen;
11203 }
11204 else {
11205 while (llen >= fclen) {
11206 memcpy(p,f,flen);
11207 p += flen;
11208 llen -= fclen;
11209 }
11210 if (llen > 0) {
11211 memcpy(p, f, llen2);
11212 p += llen2;
11213 }
11214 }
11215 memcpy(p, RSTRING_PTR(str), size);
11216 p += size;
11217 if (flen <= 1) {
11218 memset(p, *f, rlen);
11219 p += rlen;
11220 }
11221 else {
11222 while (rlen >= fclen) {
11223 memcpy(p,f,flen);
11224 p += flen;
11225 rlen -= fclen;
11226 }
11227 if (rlen > 0) {
11228 memcpy(p, f, rlen2);
11229 p += rlen2;
11230 }
11231 }
11232 TERM_FILL(p, termlen);
11233 STR_SET_LEN(res, p-RSTRING_PTR(res));
11234
11235 if (argc == 2)
11236 cr = ENC_CODERANGE_AND(cr, ENC_CODERANGE(pad));
11237 if (cr != ENC_CODERANGE_BROKEN)
11238 ENC_CODERANGE_SET(res, cr);
11239
11240 RB_GC_GUARD(pad);
11241 return res;
11242}
11243
11244
11245/*
11246 * call-seq:
11247 * ljust(width, pad_string = ' ') -> new_string
11248 *
11249 * :include: doc/string/ljust.rdoc
11250 *
11251 */
11252
11253static VALUE
11254rb_str_ljust(int argc, VALUE *argv, VALUE str)
11255{
11256 return rb_str_justify(argc, argv, str, 'l');
11257}
11258
11259/*
11260 * call-seq:
11261 * rjust(width, pad_string = ' ') -> new_string
11262 *
11263 * :include: doc/string/rjust.rdoc
11264 *
11265 */
11266
11267static VALUE
11268rb_str_rjust(int argc, VALUE *argv, VALUE str)
11269{
11270 return rb_str_justify(argc, argv, str, 'r');
11271}
11272
11273
11274/*
11275 * call-seq:
11276 * center(size, pad_string = ' ') -> new_string
11277 *
11278 * :include: doc/string/center.rdoc
11279 *
11280 */
11281
11282static VALUE
11283rb_str_center(int argc, VALUE *argv, VALUE str)
11284{
11285 return rb_str_justify(argc, argv, str, 'c');
11286}
11287
11288/*
11289 * call-seq:
11290 * partition(pattern) -> [pre_match, first_match, post_match]
11291 *
11292 * :include: doc/string/partition.rdoc
11293 *
11294 */
11295
11296static VALUE
11297rb_str_partition(VALUE str, VALUE sep)
11298{
11299 long pos;
11300
11301 sep = get_pat_quoted(sep, 0);
11302 if (RB_TYPE_P(sep, T_REGEXP)) {
11303 if (rb_reg_search(sep, str, 0, 0) < 0) {
11304 goto failed;
11305 }
11306 VALUE match = rb_backref_get();
11307 struct re_registers *regs = RMATCH_REGS(match);
11308
11309 pos = BEG(0);
11310 sep = rb_str_subseq(str, pos, END(0) - pos);
11311 }
11312 else {
11313 pos = rb_str_index(str, sep, 0);
11314 if (pos < 0) goto failed;
11315 }
11316
11317 long rpos = pos + RSTRING_LEN(sep);
11318 if (rpos > RSTRING_LEN(str)) goto failed;
11319 return rb_ary_new3(3, rb_str_subseq(str, 0, pos),
11320 sep,
11321 rb_str_subseq(str, rpos, RSTRING_LEN(str)-rpos));
11322
11323 failed:
11324 return rb_ary_new3(3, str_duplicate(rb_cString, str), str_new_empty_String(str), str_new_empty_String(str));
11325}
11326
11327/*
11328 * call-seq:
11329 * rpartition(pattern) -> [pre_match, last_match, post_match]
11330 *
11331 * :include: doc/string/rpartition.rdoc
11332 *
11333 */
11334
11335static VALUE
11336rb_str_rpartition(VALUE str, VALUE sep)
11337{
11338 long pos;
11339
11340 sep = get_pat_quoted(sep, 0);
11341 if (RB_TYPE_P(sep, T_REGEXP)) {
11342 pos = RSTRING_LEN(str);
11343 if (rb_reg_search(sep, str, pos, 1) < 0) {
11344 goto failed;
11345 }
11346 VALUE match = rb_backref_get();
11347 struct re_registers *regs = RMATCH_REGS(match);
11348
11349 pos = BEG(0);
11350 sep = rb_str_subseq(str, pos, END(0) - pos);
11351 }
11352 else {
11353 /* str may have been modified by #to_str above */
11354 pos = rb_str_sublen(str, RSTRING_LEN(str));
11355 pos = rb_str_rindex(str, sep, pos);
11356 if (pos < 0) {
11357 goto failed;
11358 }
11359 }
11360
11361 long rpos = pos + RSTRING_LEN(sep);
11362 if (rpos > RSTRING_LEN(str)) goto failed;
11363 return rb_ary_new3(3, rb_str_subseq(str, 0, pos),
11364 sep,
11365 rb_str_subseq(str, rpos, RSTRING_LEN(str)-rpos));
11366 failed:
11367 return rb_ary_new3(3, str_new_empty_String(str), str_new_empty_String(str), str_duplicate(rb_cString, str));
11368}
11369
11370/*
11371 * call-seq:
11372 * start_with?(*patterns) -> true or false
11373 *
11374 * :include: doc/string/start_with_p.rdoc
11375 *
11376 */
11377
11378static VALUE
11379rb_str_start_with(int argc, VALUE *argv, VALUE str)
11380{
11381 int i;
11382
11383 for (i=0; i<argc; i++) {
11384 VALUE tmp = argv[i];
11385 if (RB_TYPE_P(tmp, T_REGEXP)) {
11386 if (rb_reg_start_with_p(tmp, str))
11387 return Qtrue;
11388 }
11389 else {
11390 const char *p, *s, *e;
11391 long slen, tlen;
11392 rb_encoding *enc;
11393
11394 StringValue(tmp);
11395 enc = rb_enc_check(str, tmp);
11396 if ((tlen = RSTRING_LEN(tmp)) == 0) return Qtrue;
11397 if ((slen = RSTRING_LEN(str)) < tlen) continue;
11398 p = RSTRING_PTR(str);
11399 e = p + slen;
11400 s = p + tlen;
11401 if (!at_char_right_boundary(p, s, e, enc))
11402 continue;
11403 if (memcmp(p, RSTRING_PTR(tmp), tlen) == 0)
11404 return Qtrue;
11405 }
11406 }
11407 return Qfalse;
11408}
11409
11410/*
11411 * call-seq:
11412 * end_with?(*strings) -> true or false
11413 *
11414 * :include: doc/string/end_with_p.rdoc
11415 *
11416 */
11417
11418static VALUE
11419rb_str_end_with(int argc, VALUE *argv, VALUE str)
11420{
11421 int i;
11422
11423 for (i=0; i<argc; i++) {
11424 VALUE tmp = argv[i];
11425 const char *p, *s, *e;
11426 long slen, tlen;
11427 rb_encoding *enc;
11428
11429 StringValue(tmp);
11430 enc = rb_enc_check(str, tmp);
11431 if ((tlen = RSTRING_LEN(tmp)) == 0) return Qtrue;
11432 if ((slen = RSTRING_LEN(str)) < tlen) continue;
11433 p = RSTRING_PTR(str);
11434 e = p + slen;
11435 s = e - tlen;
11436 if (!at_char_boundary(p, s, e, enc))
11437 continue;
11438 if (memcmp(s, RSTRING_PTR(tmp), tlen) == 0)
11439 return Qtrue;
11440 }
11441 return Qfalse;
11442}
11443
11453static long
11454deleted_prefix_length(VALUE str, VALUE prefix)
11455{
11456 const char *strptr, *prefixptr;
11457 long olen, prefixlen;
11458 rb_encoding *enc = rb_enc_get(str);
11459
11460 StringValue(prefix);
11461
11462 if (!is_broken_string(prefix) ||
11463 !rb_enc_asciicompat(enc) ||
11464 !rb_enc_asciicompat(rb_enc_get(prefix))) {
11465 enc = rb_enc_check(str, prefix);
11466 }
11467
11468 /* return 0 if not start with prefix */
11469 prefixlen = RSTRING_LEN(prefix);
11470 if (prefixlen <= 0) return 0;
11471 olen = RSTRING_LEN(str);
11472 if (olen < prefixlen) return 0;
11473 strptr = RSTRING_PTR(str);
11474 prefixptr = RSTRING_PTR(prefix);
11475 if (memcmp(strptr, prefixptr, prefixlen) != 0) return 0;
11476 if (is_broken_string(prefix)) {
11477 if (!is_broken_string(str)) {
11478 /* prefix in a valid string cannot be broken */
11479 return 0;
11480 }
11481 const char *strend = strptr + olen;
11482 const char *after_prefix = strptr + prefixlen;
11483 if (!at_char_right_boundary(strptr, after_prefix, strend, enc)) {
11484 /* prefix does not end at char-boundary */
11485 return 0;
11486 }
11487 }
11488 /* prefix part in `str` also should be valid. */
11489
11490 return prefixlen;
11491}
11492
11493/*
11494 * call-seq:
11495 * delete_prefix!(prefix) -> self or nil
11496 *
11497 * Like String#delete_prefix, except that +self+ is modified in place;
11498 * returns +self+ if the prefix is removed, +nil+ otherwise.
11499 *
11500 * Related: see {Modifying}[rdoc-ref:String@Modifying].
11501 */
11502
11503static VALUE
11504rb_str_delete_prefix_bang(VALUE str, VALUE prefix)
11505{
11506 long prefixlen;
11507 str_modify_keep_cr(str);
11508
11509 prefixlen = deleted_prefix_length(str, prefix);
11510 if (prefixlen <= 0) return Qnil;
11511
11512 return rb_str_drop_bytes(str, prefixlen);
11513}
11514
11515/*
11516 * call-seq:
11517 * delete_prefix(prefix) -> new_string
11518 *
11519 * :include: doc/string/delete_prefix.rdoc
11520 *
11521 */
11522
11523static VALUE
11524rb_str_delete_prefix(VALUE str, VALUE prefix)
11525{
11526 long prefixlen;
11527
11528 prefixlen = deleted_prefix_length(str, prefix);
11529 if (prefixlen <= 0) return str_duplicate(rb_cString, str);
11530
11531 return rb_str_subseq(str, prefixlen, RSTRING_LEN(str) - prefixlen);
11532}
11533
11543static long
11544deleted_suffix_length(VALUE str, VALUE suffix)
11545{
11546 const char *strptr, *suffixptr;
11547 long olen, suffixlen;
11548 rb_encoding *enc;
11549
11550 StringValue(suffix);
11551 if (is_broken_string(suffix)) return 0;
11552 enc = rb_enc_check(str, suffix);
11553
11554 /* return 0 if not start with suffix */
11555 suffixlen = RSTRING_LEN(suffix);
11556 if (suffixlen <= 0) return 0;
11557 olen = RSTRING_LEN(str);
11558 if (olen < suffixlen) return 0;
11559 strptr = RSTRING_PTR(str);
11560 suffixptr = RSTRING_PTR(suffix);
11561 const char *strend = strptr + olen;
11562 const char *before_suffix = strend - suffixlen;
11563 if (memcmp(before_suffix, suffixptr, suffixlen) != 0) return 0;
11564 if (!at_char_boundary(strptr, before_suffix, strend, enc)) return 0;
11565
11566 return suffixlen;
11567}
11568
11569/*
11570 * call-seq:
11571 * delete_suffix!(suffix) -> self or nil
11572 *
11573 * Like String#delete_suffix, except that +self+ is modified in place;
11574 * returns +self+ if the suffix is removed, +nil+ otherwise.
11575 *
11576 * Related: see {Modifying}[rdoc-ref:String@Modifying].
11577 */
11578
11579static VALUE
11580rb_str_delete_suffix_bang(VALUE str, VALUE suffix)
11581{
11582 long olen, suffixlen, len;
11583 str_modifiable(str);
11584
11585 suffixlen = deleted_suffix_length(str, suffix);
11586 if (suffixlen <= 0) return Qnil;
11587
11588 olen = RSTRING_LEN(str);
11589 str_modify_keep_cr(str);
11590 len = olen - suffixlen;
11591 STR_SET_LEN(str, len);
11592 TERM_FILL(&RSTRING_PTR(str)[len], TERM_LEN(str));
11593 if (ENC_CODERANGE(str) != ENC_CODERANGE_7BIT) {
11595 }
11596 return str;
11597}
11598
11599/*
11600 * call-seq:
11601 * delete_suffix(suffix) -> new_string
11602 *
11603 * :include: doc/string/delete_suffix.rdoc
11604 *
11605 */
11606
11607static VALUE
11608rb_str_delete_suffix(VALUE str, VALUE suffix)
11609{
11610 long suffixlen;
11611
11612 suffixlen = deleted_suffix_length(str, suffix);
11613 if (suffixlen <= 0) return str_duplicate(rb_cString, str);
11614
11615 return rb_str_subseq(str, 0, RSTRING_LEN(str) - suffixlen);
11616}
11617
11618void
11619rb_str_setter(VALUE val, ID id, VALUE *var)
11620{
11621 if (!NIL_P(val) && !RB_TYPE_P(val, T_STRING)) {
11622 rb_raise(rb_eTypeError, "value of %"PRIsVALUE" must be String", rb_id2str(id));
11623 }
11624 *var = val;
11625}
11626
11627static void
11628nil_setter_warning(ID id)
11629{
11630 rb_warn_deprecated("non-nil '%"PRIsVALUE"'", NULL, rb_id2str(id));
11631}
11632
11633void
11634rb_deprecated_str_setter(VALUE val, ID id, VALUE *var)
11635{
11636 rb_str_setter(val, id, var);
11637 if (!NIL_P(*var)) {
11638 nil_setter_warning(id);
11639 }
11640}
11641
11642static void
11643rb_fs_setter(VALUE val, ID id, VALUE *var)
11644{
11645 val = rb_fs_check(val);
11646 if (!val) {
11647 rb_raise(rb_eTypeError,
11648 "value of %"PRIsVALUE" must be String or Regexp",
11649 rb_id2str(id));
11650 }
11651 if (!NIL_P(val)) {
11652 nil_setter_warning(id);
11653 }
11654 *var = val;
11655}
11656
11657
11658/*
11659 * call-seq:
11660 * force_encoding(encoding) -> self
11661 *
11662 * :include: doc/string/force_encoding.rdoc
11663 *
11664 */
11665
11666static VALUE
11667rb_str_force_encoding(VALUE str, VALUE enc)
11668{
11669 str_modifiable(str);
11670
11671 rb_encoding *encoding = rb_to_encoding(enc);
11672 int idx = rb_enc_to_index(encoding);
11673
11674 // If the encoding is unchanged, we do nothing.
11675 if (ENCODING_GET(str) == idx) {
11676 return str;
11677 }
11678
11679 rb_enc_associate_index(str, idx);
11680
11681 // If the coderange was 7bit and the new encoding is ASCII-compatible
11682 // we can keep the coderange.
11683 if (ENC_CODERANGE(str) == ENC_CODERANGE_7BIT && encoding && rb_enc_asciicompat(encoding)) {
11684 return str;
11685 }
11686
11688 return str;
11689}
11690
11691/*
11692 * call-seq:
11693 * b -> new_string
11694 *
11695 * :include: doc/string/b.rdoc
11696 *
11697 */
11698
11699static VALUE
11700rb_str_b(VALUE str)
11701{
11702 VALUE str2;
11703 if (STR_EMBED_P(str)) {
11704 str2 = str_alloc_embed(rb_cString, RSTRING_LEN(str) + TERM_LEN(str));
11705 }
11706 else {
11707 str2 = str_alloc_heap(rb_cString);
11708 }
11709 str_replace_shared_without_enc(str2, str);
11710
11711 if (rb_enc_asciicompat(STR_ENC_GET(str))) {
11712 // BINARY strings can never be broken; they're either 7-bit ASCII or VALID.
11713 // If we know the receiver's code range then we know the result's code range.
11714 int cr = ENC_CODERANGE(str);
11715 switch (cr) {
11716 case ENC_CODERANGE_7BIT:
11718 break;
11722 break;
11723 default:
11724 ENC_CODERANGE_CLEAR(str2);
11725 break;
11726 }
11727 }
11728
11729 return str2;
11730}
11731
11732/*
11733 * call-seq:
11734 * valid_encoding? -> true or false
11735 *
11736 * :include: doc/string/valid_encoding_p.rdoc
11737 *
11738 */
11739
11740static VALUE
11741rb_str_valid_encoding_p(VALUE str)
11742{
11743 int cr = rb_enc_str_coderange(str);
11744
11745 return RBOOL(cr != ENC_CODERANGE_BROKEN);
11746}
11747
11748/*
11749 * call-seq:
11750 * ascii_only? -> true or false
11751 *
11752 * Returns whether +self+ contains only ASCII characters:
11753 *
11754 * 'abc'.ascii_only? # => true
11755 * "abc\u{6666}".ascii_only? # => false
11756 *
11757 * Related: see {Querying}[rdoc-ref:String@Querying].
11758 */
11759
11760static VALUE
11761rb_str_is_ascii_only_p(VALUE str)
11762{
11763 int cr = rb_enc_str_coderange(str);
11764
11765 return RBOOL(cr == ENC_CODERANGE_7BIT);
11766}
11767
11768VALUE
11770{
11771 static const char ellipsis[] = "...";
11772 const long ellipsislen = sizeof(ellipsis) - 1;
11773 rb_encoding *const enc = rb_enc_get(str);
11774 const long blen = RSTRING_LEN(str);
11775 const char *const p = RSTRING_PTR(str), *e = p + blen;
11776 VALUE estr, ret = 0;
11777
11778 if (len < 0) rb_raise(rb_eIndexError, "negative length %ld", len);
11779 if (len * rb_enc_mbminlen(enc) >= blen ||
11780 (e = rb_enc_nth(p, e, len, enc)) - p == blen) {
11781 ret = str;
11782 }
11783 else if (len <= ellipsislen ||
11784 !(e = rb_enc_step_back(p, e, e, len = ellipsislen, enc))) {
11785 if (rb_enc_asciicompat(enc)) {
11786 ret = rb_str_new(ellipsis, len);
11787 rb_enc_associate(ret, enc);
11788 }
11789 else {
11790 estr = rb_usascii_str_new(ellipsis, len);
11791 ret = rb_str_encode(estr, rb_enc_from_encoding(enc), 0, Qnil);
11792 }
11793 }
11794 else if (ret = rb_str_subseq(str, 0, e - p), rb_enc_asciicompat(enc)) {
11795 rb_str_cat(ret, ellipsis, ellipsislen);
11796 }
11797 else {
11798 estr = rb_str_encode(rb_usascii_str_new(ellipsis, ellipsislen),
11799 rb_enc_from_encoding(enc), 0, Qnil);
11800 rb_str_append(ret, estr);
11801 }
11802 return ret;
11803}
11804
11805static VALUE
11806str_compat_and_valid(VALUE str, rb_encoding *enc)
11807{
11808 int cr;
11809 str = StringValue(str);
11810 cr = rb_enc_str_coderange(str);
11811 if (cr == ENC_CODERANGE_BROKEN) {
11812 rb_raise(rb_eArgError, "replacement must be valid byte sequence '%+"PRIsVALUE"'", str);
11813 }
11814 else {
11815 rb_encoding *e = STR_ENC_GET(str);
11816 if (cr == ENC_CODERANGE_7BIT ? rb_enc_mbminlen(enc) != 1 : enc != e) {
11817 rb_raise(rb_eEncCompatError, "incompatible character encodings: %s and %s",
11818 rb_enc_inspect_name(enc), rb_enc_inspect_name(e));
11819 }
11820 }
11821 return str;
11822}
11823
11824static VALUE enc_str_scrub(rb_encoding *enc, VALUE str, VALUE repl, int cr);
11825
11826VALUE
11828{
11829 rb_encoding *enc = STR_ENC_GET(str);
11830 return enc_str_scrub(enc, str, repl, ENC_CODERANGE(str));
11831}
11832
11833VALUE
11834rb_enc_str_scrub(rb_encoding *enc, VALUE str, VALUE repl)
11835{
11836 int cr = ENC_CODERANGE_UNKNOWN;
11837 if (enc == STR_ENC_GET(str)) {
11838 /* cached coderange makes sense only when enc equals the
11839 * actual encoding of str */
11840 cr = ENC_CODERANGE(str);
11841 }
11842 return enc_str_scrub(enc, str, repl, cr);
11843}
11844
11845static VALUE
11846enc_str_scrub(rb_encoding *enc, VALUE str, VALUE repl, int cr)
11847{
11848 int encidx;
11849 VALUE buf = Qnil;
11850 const char *rep, *p, *e, *p1, *sp;
11851 long replen = -1;
11852 long slen;
11853
11854 if (rb_block_given_p()) {
11855 if (!NIL_P(repl))
11856 rb_raise(rb_eArgError, "both of block and replacement given");
11857 replen = 0;
11858 }
11859
11860 if (ENC_CODERANGE_CLEAN_P(cr))
11861 return Qnil;
11862
11863 if (!NIL_P(repl)) {
11864 repl = str_compat_and_valid(repl, enc);
11865 }
11866
11867 if (rb_enc_dummy_p(enc)) {
11868 return Qnil;
11869 }
11870 encidx = rb_enc_to_index(enc);
11871
11872#define DEFAULT_REPLACE_CHAR(str) do { \
11873 RBIMPL_ATTR_NONSTRING() static const char replace[sizeof(str)-1] = str; \
11874 rep = replace; replen = (int)sizeof(replace); \
11875 } while (0)
11876
11877 slen = RSTRING_LEN(str);
11878 p = RSTRING_PTR(str);
11879 e = RSTRING_END(str);
11880 p1 = p;
11881 sp = p;
11882
11883 if (rb_enc_asciicompat(enc)) {
11884 int rep7bit_p;
11885 if (!replen) {
11886 rep = NULL;
11887 rep7bit_p = FALSE;
11888 }
11889 else if (!NIL_P(repl)) {
11890 rep = RSTRING_PTR(repl);
11891 replen = RSTRING_LEN(repl);
11892 rep7bit_p = (ENC_CODERANGE(repl) == ENC_CODERANGE_7BIT);
11893 }
11894 else if (encidx == rb_utf8_encindex()) {
11895 DEFAULT_REPLACE_CHAR("\xEF\xBF\xBD");
11896 rep7bit_p = FALSE;
11897 }
11898 else {
11899 DEFAULT_REPLACE_CHAR("?");
11900 rep7bit_p = TRUE;
11901 }
11902 cr = ENC_CODERANGE_7BIT;
11903
11904 p = search_nonascii(p, e);
11905 if (!p) {
11906 p = e;
11907 }
11908 while (p < e) {
11909 int ret = rb_enc_precise_mbclen(p, e, enc);
11910 if (MBCLEN_NEEDMORE_P(ret)) {
11911 break;
11912 }
11913 else if (MBCLEN_CHARFOUND_P(ret)) {
11915 p += MBCLEN_CHARFOUND_LEN(ret);
11916 }
11917 else if (MBCLEN_INVALID_P(ret)) {
11918 /*
11919 * p1~p: valid ascii/multibyte chars
11920 * p ~e: invalid bytes + unknown bytes
11921 */
11922 long clen = rb_enc_mbmaxlen(enc);
11923 if (NIL_P(buf)) buf = rb_str_buf_new(RSTRING_LEN(str));
11924 if (p > p1) {
11925 rb_str_buf_cat(buf, p1, p - p1);
11926 }
11927
11928 if (e - p < clen) clen = e - p;
11929 if (clen <= 2) {
11930 clen = 1;
11931 }
11932 else {
11933 const char *q = p;
11934 clen--;
11935 for (; clen > 1; clen--) {
11936 ret = rb_enc_precise_mbclen(q, q + clen, enc);
11937 if (MBCLEN_NEEDMORE_P(ret)) break;
11938 if (MBCLEN_INVALID_P(ret)) continue;
11940 }
11941 }
11942 if (rep) {
11943 rb_str_buf_cat(buf, rep, replen);
11944 if (!rep7bit_p) cr = ENC_CODERANGE_VALID;
11945 }
11946 else {
11947 repl = rb_yield(rb_enc_str_new(p, clen, enc));
11948 str_mod_check(str, sp, slen);
11949 repl = str_compat_and_valid(repl, enc);
11950 rb_str_buf_cat(buf, RSTRING_PTR(repl), RSTRING_LEN(repl));
11953 }
11954 p += clen;
11955 p1 = p;
11956 p = search_nonascii(p, e);
11957 if (!p) {
11958 p = e;
11959 break;
11960 }
11961 }
11962 else {
11964 }
11965 }
11966 if (NIL_P(buf)) {
11967 if (p == e) {
11968 ENC_CODERANGE_SET(str, cr);
11969 return Qnil;
11970 }
11971 buf = rb_str_buf_new(RSTRING_LEN(str));
11972 }
11973 if (p1 < p) {
11974 rb_str_buf_cat(buf, p1, p - p1);
11975 }
11976 if (p < e) {
11977 if (rep) {
11978 rb_str_buf_cat(buf, rep, replen);
11979 if (!rep7bit_p) cr = ENC_CODERANGE_VALID;
11980 }
11981 else {
11982 repl = rb_yield(rb_enc_str_new(p, e-p, enc));
11983 str_mod_check(str, sp, slen);
11984 repl = str_compat_and_valid(repl, enc);
11985 rb_str_buf_cat(buf, RSTRING_PTR(repl), RSTRING_LEN(repl));
11988 }
11989 }
11990 }
11991 else {
11992 /* ASCII incompatible */
11993 long mbminlen = rb_enc_mbminlen(enc);
11994 if (!replen) {
11995 rep = NULL;
11996 }
11997 else if (!NIL_P(repl)) {
11998 rep = RSTRING_PTR(repl);
11999 replen = RSTRING_LEN(repl);
12000 }
12001 else if (encidx == ENCINDEX_UTF_16BE) {
12002 DEFAULT_REPLACE_CHAR("\xFF\xFD");
12003 }
12004 else if (encidx == ENCINDEX_UTF_16LE) {
12005 DEFAULT_REPLACE_CHAR("\xFD\xFF");
12006 }
12007 else if (encidx == ENCINDEX_UTF_32BE) {
12008 DEFAULT_REPLACE_CHAR("\x00\x00\xFF\xFD");
12009 }
12010 else if (encidx == ENCINDEX_UTF_32LE) {
12011 DEFAULT_REPLACE_CHAR("\xFD\xFF\x00\x00");
12012 }
12013 else {
12014 DEFAULT_REPLACE_CHAR("?");
12015 }
12016
12017 while (p < e) {
12018 int ret = rb_enc_precise_mbclen(p, e, enc);
12019 if (MBCLEN_NEEDMORE_P(ret)) {
12020 break;
12021 }
12022 else if (MBCLEN_CHARFOUND_P(ret)) {
12023 p += MBCLEN_CHARFOUND_LEN(ret);
12024 }
12025 else if (MBCLEN_INVALID_P(ret)) {
12026 const char *q = p;
12027 long clen = rb_enc_mbmaxlen(enc);
12028 if (NIL_P(buf)) buf = rb_str_buf_new(RSTRING_LEN(str));
12029 if (p > p1) rb_str_buf_cat(buf, p1, p - p1);
12030
12031 if (e - p < clen) clen = e - p;
12032 if (clen <= mbminlen * 2) {
12033 clen = mbminlen;
12034 }
12035 else {
12036 clen -= mbminlen;
12037 for (; clen > mbminlen; clen-=mbminlen) {
12038 ret = rb_enc_precise_mbclen(q, q + clen, enc);
12039 if (MBCLEN_NEEDMORE_P(ret)) break;
12040 if (MBCLEN_INVALID_P(ret)) continue;
12042 }
12043 }
12044 if (rep) {
12045 rb_str_buf_cat(buf, rep, replen);
12046 }
12047 else {
12048 repl = rb_yield(rb_enc_str_new(p, clen, enc));
12049 str_mod_check(str, sp, slen);
12050 repl = str_compat_and_valid(repl, enc);
12051 rb_str_buf_cat(buf, RSTRING_PTR(repl), RSTRING_LEN(repl));
12052 }
12053 p += clen;
12054 p1 = p;
12055 }
12056 else {
12058 }
12059 }
12060 if (NIL_P(buf)) {
12061 if (p == e) {
12063 return Qnil;
12064 }
12065 buf = rb_str_buf_new(RSTRING_LEN(str));
12066 }
12067 if (p1 < p) {
12068 rb_str_buf_cat(buf, p1, p - p1);
12069 }
12070 if (p < e) {
12071 if (rep) {
12072 rb_str_buf_cat(buf, rep, replen);
12073 }
12074 else {
12075 repl = rb_yield(rb_enc_str_new(p, e-p, enc));
12076 str_mod_check(str, sp, slen);
12077 repl = str_compat_and_valid(repl, enc);
12078 rb_str_buf_cat(buf, RSTRING_PTR(repl), RSTRING_LEN(repl));
12079 }
12080 }
12082 }
12083 rb_enc_associate(buf, enc);
12084 ENC_CODERANGE_SET(buf, cr);
12085 return buf;
12086}
12087
12088/*
12089 * call-seq:
12090 * scrub(replacement_string = default_replacement_string) -> new_string
12091 * scrub{|sequence| ... } -> new_string
12092 *
12093 * :include: doc/string/scrub.rdoc
12094 *
12095 */
12096static VALUE
12097str_scrub(int argc, VALUE *argv, VALUE str)
12098{
12099 VALUE repl = argc ? (rb_check_arity(argc, 0, 1), argv[0]) : Qnil;
12100 VALUE new = rb_str_scrub(str, repl);
12101 return NIL_P(new) ? str_duplicate(rb_cString, str): new;
12102}
12103
12104/*
12105 * call-seq:
12106 * scrub!(replacement_string = default_replacement_string) -> self
12107 * scrub!{|sequence| ... } -> self
12108 *
12109 * Like String#scrub, except that:
12110 *
12111 * - Any replacements are made in +self+.
12112 * - Returns +self+.
12113 *
12114 * Related: see {Modifying}[rdoc-ref:String@Modifying].
12115 *
12116 */
12117static VALUE
12118str_scrub_bang(int argc, VALUE *argv, VALUE str)
12119{
12120 VALUE repl = argc ? (rb_check_arity(argc, 0, 1), argv[0]) : Qnil;
12121 VALUE new = rb_str_scrub(str, repl);
12122 if (!NIL_P(new)) rb_str_replace(str, new);
12123 return str;
12124}
12125
12126static ID id_normalize;
12127static ID id_normalized_p;
12128static VALUE mUnicodeNormalize;
12129
12130static VALUE
12131unicode_normalize_common(int argc, VALUE *argv, VALUE str, ID id)
12132{
12133 static int UnicodeNormalizeRequired = 0;
12134 VALUE argv2[2];
12135
12136 if (!UnicodeNormalizeRequired) {
12137 rb_require("unicode_normalize/normalize.rb");
12138 UnicodeNormalizeRequired = 1;
12139 }
12140 argv2[0] = str;
12141 if (rb_check_arity(argc, 0, 1)) argv2[1] = argv[0];
12142 return rb_funcallv(mUnicodeNormalize, id, argc+1, argv2);
12143}
12144
12145/*
12146 * call-seq:
12147 * unicode_normalize(form = :nfc) -> string
12148 *
12149 * :include: doc/string/unicode_normalize.rdoc
12150 *
12151 */
12152static VALUE
12153rb_str_unicode_normalize(int argc, VALUE *argv, VALUE str)
12154{
12155 return unicode_normalize_common(argc, argv, str, id_normalize);
12156}
12157
12158/*
12159 * call-seq:
12160 * unicode_normalize!(form = :nfc) -> self
12161 *
12162 * Like String#unicode_normalize, except that the normalization
12163 * is performed on +self+ (not on a copy of +self+).
12164 *
12165 * Related: see {Modifying}[rdoc-ref:String@Modifying].
12166 *
12167 */
12168static VALUE
12169rb_str_unicode_normalize_bang(int argc, VALUE *argv, VALUE str)
12170{
12171 return rb_str_replace(str, unicode_normalize_common(argc, argv, str, id_normalize));
12172}
12173
12174/* call-seq:
12175 * unicode_normalized?(form = :nfc) -> true or false
12176 *
12177 * Returns whether +self+ is in the given +form+ of Unicode normalization;
12178 * see String#unicode_normalize.
12179 *
12180 * The +form+ must be one of +:nfc+, +:nfd+, +:nfkc+, or +:nfkd+.
12181 *
12182 * Examples:
12183 *
12184 * "a\u0300".unicode_normalized? # => false
12185 * "a\u0300".unicode_normalized?(:nfd) # => true
12186 * "\u00E0".unicode_normalized? # => true
12187 * "\u00E0".unicode_normalized?(:nfd) # => false
12188 *
12189 *
12190 * Raises an exception if +self+ is not in a Unicode encoding:
12191 *
12192 * s = "\xE0".force_encoding(Encoding::ISO_8859_1)
12193 * s.unicode_normalized? # Raises Encoding::CompatibilityError
12194 *
12195 * Related: see {Querying}[rdoc-ref:String@Querying].
12196 */
12197static VALUE
12198rb_str_unicode_normalized_p(int argc, VALUE *argv, VALUE str)
12199{
12200 return unicode_normalize_common(argc, argv, str, id_normalized_p);
12201}
12202
12203/**********************************************************************
12204 * Document-class: Symbol
12205 *
12206 * A +Symbol+ object represents a named identifier inside the Ruby interpreter.
12207 *
12208 * You can create a +Symbol+ object explicitly with:
12209 *
12210 * - A {symbol literal}[rdoc-ref:syntax/literals.rdoc@Symbol+Literals].
12211 *
12212 * The same +Symbol+ object will be
12213 * created for a given name or string for the duration of a program's
12214 * execution, regardless of the context or meaning of that name. Thus
12215 * if <code>Fred</code> is a constant in one context, a method in
12216 * another, and a class in a third, the +Symbol+ <code>:Fred</code>
12217 * will be the same object in all three contexts.
12218 *
12219 * module One
12220 * class Fred
12221 * end
12222 * $f1 = :Fred
12223 * end
12224 * module Two
12225 * Fred = 1
12226 * $f2 = :Fred
12227 * end
12228 * def Fred()
12229 * end
12230 * $f3 = :Fred
12231 * $f1.object_id #=> 2514190
12232 * $f2.object_id #=> 2514190
12233 * $f3.object_id #=> 2514190
12234 *
12235 * Constant, method, and variable names are returned as symbols:
12236 *
12237 * module One
12238 * Two = 2
12239 * def three; 3 end
12240 * @four = 4
12241 * @@five = 5
12242 * $six = 6
12243 * end
12244 * seven = 7
12245 *
12246 * One.constants
12247 * # => [:Two]
12248 * One.instance_methods(true)
12249 * # => [:three]
12250 * One.instance_variables
12251 * # => [:@four]
12252 * One.class_variables
12253 * # => [:@@five]
12254 * global_variables.grep(/six/)
12255 * # => [:$six]
12256 * local_variables
12257 * # => [:seven]
12258 *
12259 * A +Symbol+ object differs from a String object in that
12260 * a +Symbol+ object represents an identifier, while a String object
12261 * represents text or data.
12262 *
12263 * == What's Here
12264 *
12265 * First, what's elsewhere. Class +Symbol+:
12266 *
12267 * - Inherits from {class Object}[rdoc-ref:Object@What-27s+Here].
12268 * - Includes {module Comparable}[rdoc-ref:Comparable@What-27s+Here].
12269 *
12270 * Here, class +Symbol+ provides methods that are useful for:
12271 *
12272 * - {Querying}[rdoc-ref:Symbol@Methods+for+Querying]
12273 * - {Comparing}[rdoc-ref:Symbol@Methods+for+Comparing]
12274 * - {Converting}[rdoc-ref:Symbol@Methods+for+Converting]
12275 *
12276 * === Methods for Querying
12277 *
12278 * - ::all_symbols: Returns an array of the symbols currently in Ruby's symbol table.
12279 * - #=~: Returns the index of the first substring in symbol that matches a
12280 * given Regexp or other object; returns +nil+ if no match is found.
12281 * - #[], #slice : Returns a substring of symbol
12282 * determined by a given index, start/length, or range, or string.
12283 * - #empty?: Returns +true+ if +self.length+ is zero; +false+ otherwise.
12284 * - #encoding: Returns the Encoding object that represents the encoding
12285 * of symbol.
12286 * - #end_with?: Returns +true+ if symbol ends with
12287 * any of the given strings.
12288 * - #match: Returns a MatchData object if symbol
12289 * matches a given Regexp; +nil+ otherwise.
12290 * - #match?: Returns +true+ if symbol
12291 * matches a given Regexp; +false+ otherwise.
12292 * - #length, #size: Returns the number of characters in symbol.
12293 * - #start_with?: Returns +true+ if symbol starts with
12294 * any of the given strings.
12295 *
12296 * === Methods for Comparing
12297 *
12298 * - #<=>: Returns -1, 0, or 1 as a given symbol is smaller than, equal to,
12299 * or larger than symbol.
12300 * - #==, #===: Returns +true+ if a given symbol has the same content and
12301 * encoding.
12302 * - #casecmp: Ignoring case, returns -1, 0, or 1 as a given
12303 * symbol is smaller than, equal to, or larger than symbol.
12304 * - #casecmp?: Returns +true+ if symbol is equal to a given symbol
12305 * after Unicode case folding; +false+ otherwise.
12306 *
12307 * === Methods for Converting
12308 *
12309 * - #capitalize: Returns symbol with the first character upcased
12310 * and all other characters downcased.
12311 * - #downcase: Returns symbol with all characters downcased.
12312 * - #inspect: Returns the string representation of +self+ as a symbol literal.
12313 * - #name: Returns the frozen string corresponding to symbol.
12314 * - #succ, #next: Returns the symbol that is the successor to symbol.
12315 * - #swapcase: Returns symbol with all upcase characters downcased
12316 * and all downcase characters upcased.
12317 * - #to_proc: Returns a Proc object which responds to the method named by symbol.
12318 * - #to_s, #id2name: Returns the string corresponding to +self+.
12319 * - #to_sym, #intern: Returns +self+.
12320 * - #upcase: Returns symbol with all characters upcased.
12321 *
12322 */
12323
12324
12325/*
12326 * call-seq:
12327 * symbol == object -> true or false
12328 *
12329 * Returns +true+ if +object+ is the same object as +self+, +false+ otherwise.
12330 */
12331
12332#define sym_equal rb_obj_equal
12333
12334static int
12335sym_printable(const char *s, const char *send, rb_encoding *enc)
12336{
12337 while (s < send) {
12338 int n;
12339 int c = rb_enc_precise_mbclen(s, send, enc);
12340
12341 if (!MBCLEN_CHARFOUND_P(c)) return FALSE;
12342 n = MBCLEN_CHARFOUND_LEN(c);
12343 c = rb_enc_mbc_to_codepoint(s, send, enc);
12344 if (!rb_enc_isprint(c, enc)) return FALSE;
12345 s += n;
12346 }
12347 return TRUE;
12348}
12349
12350int
12351rb_str_symname_p(VALUE sym)
12352{
12353 rb_encoding *enc;
12354 const char *ptr;
12355 long len;
12356 rb_encoding *resenc = rb_default_internal_encoding();
12357
12358 if (resenc == NULL) resenc = rb_default_external_encoding();
12359 enc = STR_ENC_GET(sym);
12360 ptr = RSTRING_PTR(sym);
12361 len = RSTRING_LEN(sym);
12362 if ((resenc != enc && !rb_str_is_ascii_only_p(sym)) || len != (long)strlen(ptr) ||
12363 !rb_enc_symname2_p(ptr, len, enc) || !sym_printable(ptr, ptr + len, enc)) {
12364 return FALSE;
12365 }
12366 return TRUE;
12367}
12368
12369VALUE
12370rb_str_quote_unprintable(VALUE str)
12371{
12372 rb_encoding *enc;
12373 const char *ptr;
12374 long len;
12375 rb_encoding *resenc;
12376
12377 Check_Type(str, T_STRING);
12378 resenc = rb_default_internal_encoding();
12379 if (resenc == NULL) resenc = rb_default_external_encoding();
12380 enc = STR_ENC_GET(str);
12381 ptr = RSTRING_PTR(str);
12382 len = RSTRING_LEN(str);
12383 if ((resenc != enc && !rb_str_is_ascii_only_p(str)) ||
12384 !sym_printable(ptr, ptr + len, enc)) {
12385 return rb_str_escape(str);
12386 }
12387 return str;
12388}
12389
12390VALUE
12391rb_id_quote_unprintable(ID id)
12392{
12393 VALUE str = rb_id2str(id);
12394 if (!rb_str_symname_p(str)) {
12395 return rb_str_escape(str);
12396 }
12397 return str;
12398}
12399
12400/*
12401 * call-seq:
12402 * inspect -> string
12403 *
12404 * Returns a string representation of +self+ (including the leading colon):
12405 *
12406 * :foo.inspect # => ":foo"
12407 *
12408 * Related: Symbol#to_s, Symbol#name.
12409 *
12410 */
12411
12412static VALUE
12413sym_inspect(VALUE sym)
12414{
12415 VALUE str = rb_sym2str(sym);
12416 const char *ptr;
12417 long len;
12418 char *dest;
12419
12420 if (!rb_str_symname_p(str)) {
12421 str = rb_str_inspect(str);
12422 len = RSTRING_LEN(str);
12423 rb_str_resize(str, len + 1);
12424 dest = RSTRING_PTR(str);
12425 memmove(dest + 1, dest, len);
12426 }
12427 else {
12428 rb_encoding *enc = STR_ENC_GET(str);
12429 VALUE orig_str = str;
12430
12431 len = RSTRING_LEN(orig_str);
12432 str = rb_enc_str_new(0, len + 1, enc);
12433
12434 // Get data pointer after allocation
12435 ptr = RSTRING_PTR(orig_str);
12436 dest = RSTRING_PTR(str);
12437 memcpy(dest + 1, ptr, len);
12438
12439 RB_GC_GUARD(orig_str);
12440 }
12441 dest[0] = ':';
12442
12444
12445 return str;
12446}
12447
12448VALUE
12450{
12451 VALUE str = str_new_shared(rb_cString, rb_sym2str(sym));
12452 FL_SET_RAW(str, STR_CHILLED_SYMBOL_TO_S);
12453 return str;
12454}
12455
12456VALUE
12457rb_sym_proc_call(ID mid, int argc, const VALUE *argv, int kw_splat, VALUE passed_proc)
12458{
12459 VALUE obj;
12460
12461 if (argc < 1) {
12462 rb_raise(rb_eArgError, "no receiver given");
12463 }
12464 obj = argv[0];
12465 return rb_funcall_with_block_kw(obj, mid, argc - 1, argv + 1, passed_proc, kw_splat);
12466}
12467
12468/*
12469 * call-seq:
12470 * succ
12471 *
12472 * Equivalent to <tt>self.to_s.succ.to_sym</tt>:
12473 *
12474 * :foo.succ # => :fop
12475 *
12476 * Related: String#succ.
12477 */
12478
12479static VALUE
12480sym_succ(VALUE sym)
12481{
12482 return rb_str_intern(rb_str_succ(rb_sym2str(sym)));
12483}
12484
12485/*
12486 * call-seq:
12487 * self <=> other -> -1, 0, 1, or nil
12488 *
12489 * Compares +self+ and +other+, using String#<=>.
12490 *
12491 * Returns:
12492 *
12493 * - <tt>self.to_s <=> other.to_s</tt>, if +other+ is a symbol.
12494 * - +nil+, otherwise.
12495 *
12496 * Examples:
12497 *
12498 * :bar <=> :foo # => -1
12499 * :foo <=> :foo # => 0
12500 * :foo <=> :bar # => 1
12501 * :foo <=> 'bar' # => nil
12502 *
12503 * \Class \Symbol includes module Comparable,
12504 * each of whose methods uses Symbol#<=> for comparison.
12505 *
12506 * Related: String#<=>.
12507 */
12508
12509static VALUE
12510sym_cmp(VALUE sym, VALUE other)
12511{
12512 if (!SYMBOL_P(other)) {
12513 return Qnil;
12514 }
12515 return rb_str_cmp_m(rb_sym2str(sym), rb_sym2str(other));
12516}
12517
12518/*
12519 * call-seq:
12520 * casecmp(object) -> -1, 0, 1, or nil
12521 *
12522 * :include: doc/symbol/casecmp.rdoc
12523 *
12524 */
12525
12526static VALUE
12527sym_casecmp(VALUE sym, VALUE other)
12528{
12529 if (!SYMBOL_P(other)) {
12530 return Qnil;
12531 }
12532 return str_casecmp(rb_sym2str(sym), rb_sym2str(other));
12533}
12534
12535/*
12536 * call-seq:
12537 * casecmp?(object) -> true, false, or nil
12538 *
12539 * :include: doc/symbol/casecmp_p.rdoc
12540 *
12541 */
12542
12543static VALUE
12544sym_casecmp_p(VALUE sym, VALUE other)
12545{
12546 if (!SYMBOL_P(other)) {
12547 return Qnil;
12548 }
12549 return str_casecmp_p(rb_sym2str(sym), rb_sym2str(other));
12550}
12551
12552/*
12553 * call-seq:
12554 * symbol =~ object -> integer or nil
12555 *
12556 * Equivalent to <tt>symbol.to_s =~ object</tt>,
12557 * including possible updates to global variables;
12558 * see String#=~.
12559 *
12560 */
12561
12562static VALUE
12563sym_match(VALUE sym, VALUE other)
12564{
12565 return rb_str_match(rb_sym2str(sym), other);
12566}
12567
12568/*
12569 * call-seq:
12570 * match(pattern, offset = 0) -> matchdata or nil
12571 * match(pattern, offset = 0) {|matchdata| } -> object
12572 *
12573 * Equivalent to <tt>self.to_s.match</tt>,
12574 * including possible updates to global variables;
12575 * see String#match.
12576 *
12577 */
12578
12579static VALUE
12580sym_match_m(int argc, VALUE *argv, VALUE sym)
12581{
12582 return rb_str_match_m(argc, argv, rb_sym2str(sym));
12583}
12584
12585/*
12586 * call-seq:
12587 * match?(pattern, offset) -> true or false
12588 *
12589 * Equivalent to <tt>sym.to_s.match?</tt>;
12590 * see String#match.
12591 *
12592 */
12593
12594static VALUE
12595sym_match_m_p(int argc, VALUE *argv, VALUE sym)
12596{
12597 return rb_str_match_m_p(argc, argv, sym);
12598}
12599
12600/*
12601 * call-seq:
12602 * symbol[index] -> string or nil
12603 * symbol[start, length] -> string or nil
12604 * symbol[range] -> string or nil
12605 * symbol[regexp, capture = 0] -> string or nil
12606 * symbol[substring] -> string or nil
12607 *
12608 * Equivalent to <tt>symbol.to_s[]</tt>; see String#[].
12609 *
12610 */
12611
12612static VALUE
12613sym_aref(int argc, VALUE *argv, VALUE sym)
12614{
12615 return rb_str_aref_m(argc, argv, rb_sym2str(sym));
12616}
12617
12618/*
12619 * call-seq:
12620 * length -> integer
12621 *
12622 * Equivalent to <tt>self.to_s.length</tt>; see String#length.
12623 */
12624
12625static VALUE
12626sym_length(VALUE sym)
12627{
12628 return rb_str_length(rb_sym2str(sym));
12629}
12630
12631/*
12632 * call-seq:
12633 * empty? -> true or false
12634 *
12635 * Returns +true+ if +self+ is <tt>:''</tt>, +false+ otherwise.
12636 *
12637 */
12638
12639static VALUE
12640sym_empty(VALUE sym)
12641{
12642 return rb_str_empty(rb_sym2str(sym));
12643}
12644
12645/*
12646 * call-seq:
12647 * upcase(mapping) -> symbol
12648 *
12649 * Equivalent to <tt>sym.to_s.upcase.to_sym</tt>.
12650 *
12651 * See String#upcase.
12652 *
12653 */
12654
12655static VALUE
12656sym_upcase(int argc, VALUE *argv, VALUE sym)
12657{
12658 return rb_str_intern(rb_str_upcase(argc, argv, rb_sym2str(sym)));
12659}
12660
12661/*
12662 * call-seq:
12663 * downcase(mapping) -> symbol
12664 *
12665 * Equivalent to <tt>sym.to_s.downcase.to_sym</tt>.
12666 *
12667 * See String#downcase.
12668 *
12669 * Related: Symbol#upcase.
12670 *
12671 */
12672
12673static VALUE
12674sym_downcase(int argc, VALUE *argv, VALUE sym)
12675{
12676 return rb_str_intern(rb_str_downcase(argc, argv, rb_sym2str(sym)));
12677}
12678
12679/*
12680 * call-seq:
12681 * capitalize(mapping) -> symbol
12682 *
12683 * Equivalent to <tt>sym.to_s.capitalize.to_sym</tt>.
12684 *
12685 * See String#capitalize.
12686 *
12687 */
12688
12689static VALUE
12690sym_capitalize(int argc, VALUE *argv, VALUE sym)
12691{
12692 return rb_str_intern(rb_str_capitalize(argc, argv, rb_sym2str(sym)));
12693}
12694
12695/*
12696 * call-seq:
12697 * swapcase(mapping) -> symbol
12698 *
12699 * Equivalent to <tt>sym.to_s.swapcase.to_sym</tt>.
12700 *
12701 * See String#swapcase.
12702 *
12703 */
12704
12705static VALUE
12706sym_swapcase(int argc, VALUE *argv, VALUE sym)
12707{
12708 return rb_str_intern(rb_str_swapcase(argc, argv, rb_sym2str(sym)));
12709}
12710
12711/*
12712 * call-seq:
12713 * start_with?(*string_or_regexp) -> true or false
12714 *
12715 * Equivalent to <tt>self.to_s.start_with?</tt>; see String#start_with?.
12716 *
12717 */
12718
12719static VALUE
12720sym_start_with(int argc, VALUE *argv, VALUE sym)
12721{
12722 return rb_str_start_with(argc, argv, rb_sym2str(sym));
12723}
12724
12725/*
12726 * call-seq:
12727 * end_with?(*strings) -> true or false
12728 *
12729 *
12730 * Equivalent to <tt>self.to_s.end_with?</tt>; see String#end_with?.
12731 *
12732 */
12733
12734static VALUE
12735sym_end_with(int argc, VALUE *argv, VALUE sym)
12736{
12737 return rb_str_end_with(argc, argv, rb_sym2str(sym));
12738}
12739
12740/*
12741 * call-seq:
12742 * encoding -> encoding
12743 *
12744 * Equivalent to <tt>self.to_s.encoding</tt>; see String#encoding.
12745 *
12746 */
12747
12748static VALUE
12749sym_encoding(VALUE sym)
12750{
12751 return rb_obj_encoding(rb_sym2str(sym));
12752}
12753
12754static VALUE
12755string_for_symbol(VALUE name)
12756{
12757 if (!RB_TYPE_P(name, T_STRING)) {
12758 VALUE tmp = rb_check_string_type(name);
12759 if (NIL_P(tmp)) {
12760 rb_raise(rb_eTypeError, "%+"PRIsVALUE" is not a symbol nor a string",
12761 name);
12762 }
12763 name = tmp;
12764 }
12765 return name;
12766}
12767
12768ID
12770{
12771 if (SYMBOL_P(name)) {
12772 return SYM2ID(name);
12773 }
12774 name = string_for_symbol(name);
12775 return rb_intern_str(name);
12776}
12777
12778VALUE
12780{
12781 if (SYMBOL_P(name)) {
12782 return name;
12783 }
12784 name = string_for_symbol(name);
12785 return rb_str_intern(name);
12786}
12787
12788/*
12789 * call-seq:
12790 * Symbol.all_symbols -> array_of_symbols
12791 *
12792 * Returns an array of all symbols currently in Ruby's symbol table:
12793 *
12794 * Symbol.all_symbols.size # => 9334
12795 * Symbol.all_symbols.take(3) # => [:!, :"\"", :"#"]
12796 *
12797 */
12798
12799static VALUE
12800sym_all_symbols(VALUE _)
12801{
12802 return rb_sym_all_symbols();
12803}
12804
12805VALUE
12806rb_str_to_interned_str(VALUE str)
12807{
12808 return rb_fstring(str);
12809}
12810
12811VALUE
12812rb_interned_str(const char *ptr, long len)
12813{
12814 struct RString fake_str = {RBASIC_INIT};
12815 int encidx = ENCINDEX_US_ASCII;
12816 int coderange = ENC_CODERANGE_7BIT;
12817 if (len > 0 && search_nonascii(ptr, ptr + len)) {
12818 encidx = ENCINDEX_ASCII_8BIT;
12819 coderange = ENC_CODERANGE_VALID;
12820 }
12821 VALUE str = setup_fake_str(&fake_str, ptr, len, encidx);
12822 ENC_CODERANGE_SET(str, coderange);
12823 return register_fstring(str, true, false);
12824}
12825
12826VALUE
12828{
12829 return rb_interned_str(ptr, strlen(ptr));
12830}
12831
12832VALUE
12833rb_enc_interned_str(const char *ptr, long len, rb_encoding *enc)
12834{
12835 if (enc != NULL && UNLIKELY(rb_enc_autoload_p(enc))) {
12836 rb_enc_autoload(enc);
12837 }
12838
12839 struct RString fake_str = {RBASIC_INIT};
12840 return register_fstring(rb_setup_fake_str(&fake_str, ptr, len, enc), true, false);
12841}
12842
12843VALUE
12844rb_enc_literal_str(const char *ptr, long len, rb_encoding *enc)
12845{
12846 if (enc != NULL && UNLIKELY(rb_enc_autoload_p(enc))) {
12847 rb_enc_autoload(enc);
12848 }
12849
12850 struct RString fake_str = {RBASIC_INIT};
12851 VALUE str = register_fstring(rb_setup_fake_str(&fake_str, ptr, len, enc), true, true);
12852 RUBY_ASSERT(RB_OBJ_SHAREABLE_P(str) && (rb_gc_verify_shareable(str), 1));
12853 return str;
12854}
12855
12856VALUE
12857rb_enc_interned_str_cstr(const char *ptr, rb_encoding *enc)
12858{
12859 return rb_enc_interned_str(ptr, strlen(ptr), enc);
12860}
12861
12862#if USE_YJIT || USE_ZJIT
12863void
12864rb_jit_str_concat_codepoint(VALUE str, VALUE codepoint)
12865{
12866 if (RB_LIKELY(ENCODING_GET_INLINED(str) == rb_ascii8bit_encindex())) {
12867 ssize_t code = RB_NUM2SSIZE(codepoint);
12868
12869 if (RB_LIKELY(code >= 0 && code < 0xff)) {
12870 rb_str_buf_cat_byte(str, (char) code);
12871 return;
12872 }
12873 }
12874
12875 rb_str_concat(str, codepoint);
12876}
12877#endif
12878
12879static int
12880fstring_set_class_i(VALUE *str, void *data)
12881{
12882 RBASIC_SET_CLASS(*str, rb_cString);
12883
12884 return ST_CONTINUE;
12885}
12886
12887void
12888Init_String(void)
12889{
12890 rb_cString = rb_define_class("String", rb_cObject);
12891
12892 rb_concurrent_set_foreach_with_replace(fstring_table_obj, fstring_set_class_i, NULL);
12893
12895 rb_define_alloc_func(rb_cString, empty_str_alloc);
12896 rb_define_singleton_method(rb_cString, "new", rb_str_s_new, -1);
12897 rb_define_singleton_method(rb_cString, "try_convert", rb_str_s_try_convert, 1);
12898 rb_define_method(rb_cString, "initialize", rb_str_init, -1);
12900 rb_define_method(rb_cString, "initialize_copy", rb_str_replace, 1);
12901 rb_define_method(rb_cString, "<=>", rb_str_cmp_m, 1);
12904 rb_define_method(rb_cString, "eql?", rb_str_eql, 1);
12905 rb_define_method(rb_cString, "hash", rb_str_hash_m, 0);
12906 rb_define_method(rb_cString, "casecmp", rb_str_casecmp, 1);
12907 rb_define_method(rb_cString, "casecmp?", rb_str_casecmp_p, 1);
12910 rb_define_method(rb_cString, "%", rb_str_format_m, 1);
12911 rb_define_method(rb_cString, "[]", rb_str_aref_m, -1);
12912 rb_define_method(rb_cString, "[]=", rb_str_aset_m, -1);
12913 rb_define_method(rb_cString, "insert", rb_str_insert, 2);
12916 rb_define_method(rb_cString, "bytesize", rb_str_bytesize, 0);
12917 rb_define_method(rb_cString, "empty?", rb_str_empty, 0);
12918 rb_define_method(rb_cString, "=~", rb_str_match, 1);
12919 rb_define_method(rb_cString, "match", rb_str_match_m, -1);
12920 rb_define_method(rb_cString, "match?", rb_str_match_m_p, -1);
12922 rb_define_method(rb_cString, "succ!", rb_str_succ_bang, 0);
12924 rb_define_method(rb_cString, "next!", rb_str_succ_bang, 0);
12925 rb_define_method(rb_cString, "upto", rb_str_upto, -1);
12926 rb_define_method(rb_cString, "index", rb_str_index_m, -1);
12927 rb_define_method(rb_cString, "byteindex", rb_str_byteindex_m, -1);
12928 rb_define_method(rb_cString, "rindex", rb_str_rindex_m, -1);
12929 rb_define_method(rb_cString, "byterindex", rb_str_byterindex_m, -1);
12930 rb_define_method(rb_cString, "clear", rb_str_clear, 0);
12931 rb_define_method(rb_cString, "chr", rb_str_chr, 0);
12932 rb_define_method(rb_cString, "getbyte", rb_str_getbyte, 1);
12933 rb_define_method(rb_cString, "setbyte", rb_str_setbyte, 2);
12934 rb_define_method(rb_cString, "byteslice", rb_str_byteslice, -1);
12935 rb_define_method(rb_cString, "bytesplice", rb_str_bytesplice, -1);
12936 rb_define_method(rb_cString, "scrub", str_scrub, -1);
12937 rb_define_method(rb_cString, "scrub!", str_scrub_bang, -1);
12939 rb_define_method(rb_cString, "+@", str_uplus, 0);
12940 rb_define_method(rb_cString, "-@", str_uminus, 0);
12941 rb_define_method(rb_cString, "dup", rb_str_dup_m, 0);
12942 rb_define_alias(rb_cString, "dedup", "-@");
12943
12944 rb_define_method(rb_cString, "to_i", rb_str_to_i, -1);
12945 rb_define_method(rb_cString, "to_f", rb_str_to_f, 0);
12946 rb_define_method(rb_cString, "to_s", rb_str_to_s, 0);
12947 rb_define_method(rb_cString, "to_str", rb_str_to_s, 0);
12950 rb_define_method(rb_cString, "undump", str_undump, 0);
12951
12952 sym_ascii = ID2SYM(rb_intern_const("ascii"));
12953 sym_turkic = ID2SYM(rb_intern_const("turkic"));
12954 sym_lithuanian = ID2SYM(rb_intern_const("lithuanian"));
12955 sym_fold = ID2SYM(rb_intern_const("fold"));
12956
12957 rb_define_method(rb_cString, "upcase", rb_str_upcase, -1);
12958 rb_define_method(rb_cString, "downcase", rb_str_downcase, -1);
12959 rb_define_method(rb_cString, "capitalize", rb_str_capitalize, -1);
12960 rb_define_method(rb_cString, "swapcase", rb_str_swapcase, -1);
12961
12962 rb_define_method(rb_cString, "upcase!", rb_str_upcase_bang, -1);
12963 rb_define_method(rb_cString, "downcase!", rb_str_downcase_bang, -1);
12964 rb_define_method(rb_cString, "capitalize!", rb_str_capitalize_bang, -1);
12965 rb_define_method(rb_cString, "swapcase!", rb_str_swapcase_bang, -1);
12966
12967 rb_define_method(rb_cString, "hex", rb_str_hex, 0);
12968 rb_define_method(rb_cString, "oct", rb_str_oct, 0);
12969 rb_define_method(rb_cString, "split", rb_str_split_m, -1);
12970 rb_define_method(rb_cString, "lines", rb_str_lines, -1);
12971 rb_define_method(rb_cString, "bytes", rb_str_bytes, 0);
12972 rb_define_method(rb_cString, "chars", rb_str_chars, 0);
12973 rb_define_method(rb_cString, "codepoints", rb_str_codepoints, 0);
12974 rb_define_method(rb_cString, "grapheme_clusters", rb_str_grapheme_clusters, 0);
12975 rb_define_method(rb_cString, "reverse", rb_str_reverse, 0);
12976 rb_define_method(rb_cString, "reverse!", rb_str_reverse_bang, 0);
12977 rb_define_method(rb_cString, "concat", rb_str_concat_multi, -1);
12978 rb_define_method(rb_cString, "append_as_bytes", rb_str_append_as_bytes, -1);
12980 rb_define_method(rb_cString, "prepend", rb_str_prepend_multi, -1);
12981 rb_define_method(rb_cString, "crypt", rb_str_crypt, 1);
12982 rb_define_method(rb_cString, "intern", rb_str_intern, 0); /* in symbol.c */
12983 rb_define_method(rb_cString, "to_sym", rb_str_intern, 0); /* in symbol.c */
12984 rb_define_method(rb_cString, "ord", rb_str_ord, 0);
12985
12986 rb_define_method(rb_cString, "include?", rb_str_include, 1);
12987 rb_define_method(rb_cString, "start_with?", rb_str_start_with, -1);
12988 rb_define_method(rb_cString, "end_with?", rb_str_end_with, -1);
12989
12990 rb_define_method(rb_cString, "scan", rb_str_scan, 1);
12991
12992 rb_define_method(rb_cString, "ljust", rb_str_ljust, -1);
12993 rb_define_method(rb_cString, "rjust", rb_str_rjust, -1);
12994 rb_define_method(rb_cString, "center", rb_str_center, -1);
12995
12996 rb_define_method(rb_cString, "sub", rb_str_sub, -1);
12997 rb_define_method(rb_cString, "gsub", rb_str_gsub, -1);
12998 rb_define_method(rb_cString, "chop", rb_str_chop, 0);
12999 rb_define_method(rb_cString, "chomp", rb_str_chomp, -1);
13000 rb_define_method(rb_cString, "strip", rb_str_strip, -1);
13001 rb_define_method(rb_cString, "lstrip", rb_str_lstrip, -1);
13002 rb_define_method(rb_cString, "rstrip", rb_str_rstrip, -1);
13003 rb_define_method(rb_cString, "delete_prefix", rb_str_delete_prefix, 1);
13004 rb_define_method(rb_cString, "delete_suffix", rb_str_delete_suffix, 1);
13005
13006 rb_define_method(rb_cString, "sub!", rb_str_sub_bang, -1);
13007 rb_define_method(rb_cString, "gsub!", rb_str_gsub_bang, -1);
13008 rb_define_method(rb_cString, "chop!", rb_str_chop_bang, 0);
13009 rb_define_method(rb_cString, "chomp!", rb_str_chomp_bang, -1);
13010 rb_define_method(rb_cString, "strip!", rb_str_strip_bang, -1);
13011 rb_define_method(rb_cString, "lstrip!", rb_str_lstrip_bang, -1);
13012 rb_define_method(rb_cString, "rstrip!", rb_str_rstrip_bang, -1);
13013 rb_define_method(rb_cString, "delete_prefix!", rb_str_delete_prefix_bang, 1);
13014 rb_define_method(rb_cString, "delete_suffix!", rb_str_delete_suffix_bang, 1);
13015
13016 rb_define_method(rb_cString, "tr", rb_str_tr, 2);
13017 rb_define_method(rb_cString, "tr_s", rb_str_tr_s, 2);
13018 rb_define_method(rb_cString, "delete", rb_str_delete, -1);
13019 rb_define_method(rb_cString, "squeeze", rb_str_squeeze, -1);
13020 rb_define_method(rb_cString, "count", rb_str_count, -1);
13021
13022 rb_define_method(rb_cString, "tr!", rb_str_tr_bang, 2);
13023 rb_define_method(rb_cString, "tr_s!", rb_str_tr_s_bang, 2);
13024 rb_define_method(rb_cString, "delete!", rb_str_delete_bang, -1);
13025 rb_define_method(rb_cString, "squeeze!", rb_str_squeeze_bang, -1);
13026
13027 rb_define_method(rb_cString, "each_line", rb_str_each_line, -1);
13028 rb_define_method(rb_cString, "each_byte", rb_str_each_byte, 0);
13029 rb_define_method(rb_cString, "each_char", rb_str_each_char, 0);
13030 rb_define_method(rb_cString, "each_codepoint", rb_str_each_codepoint, 0);
13031 rb_define_method(rb_cString, "each_grapheme_cluster", rb_str_each_grapheme_cluster, 0);
13032
13033 rb_define_method(rb_cString, "sum", rb_str_sum, -1);
13034
13035 rb_define_method(rb_cString, "slice", rb_str_aref_m, -1);
13036 rb_define_method(rb_cString, "slice!", rb_str_slice_bang, -1);
13037
13038 rb_define_method(rb_cString, "partition", rb_str_partition, 1);
13039 rb_define_method(rb_cString, "rpartition", rb_str_rpartition, 1);
13040
13041 rb_define_method(rb_cString, "encoding", rb_obj_encoding, 0); /* in encoding.c */
13042 rb_define_method(rb_cString, "force_encoding", rb_str_force_encoding, 1);
13043 rb_define_method(rb_cString, "b", rb_str_b, 0);
13044 rb_define_method(rb_cString, "valid_encoding?", rb_str_valid_encoding_p, 0);
13045 rb_define_method(rb_cString, "ascii_only?", rb_str_is_ascii_only_p, 0);
13046
13047 /* define UnicodeNormalize module here so that we don't have to look it up */
13048 mUnicodeNormalize = rb_define_module("UnicodeNormalize");
13049 id_normalize = rb_intern_const("normalize");
13050 id_normalized_p = rb_intern_const("normalized?");
13051
13052 rb_define_method(rb_cString, "unicode_normalize", rb_str_unicode_normalize, -1);
13053 rb_define_method(rb_cString, "unicode_normalize!", rb_str_unicode_normalize_bang, -1);
13054 rb_define_method(rb_cString, "unicode_normalized?", rb_str_unicode_normalized_p, -1);
13055
13056 rb_fs = Qnil;
13057 rb_define_hooked_variable("$;", &rb_fs, 0, rb_fs_setter);
13058 rb_define_hooked_variable("$-F", &rb_fs, 0, rb_fs_setter);
13059 rb_gc_register_address(&rb_fs);
13060
13061 rb_cSymbol = rb_define_class("Symbol", rb_cObject);
13065 rb_define_singleton_method(rb_cSymbol, "all_symbols", sym_all_symbols, 0);
13066
13067 rb_define_method(rb_cSymbol, "==", sym_equal, 1);
13068 rb_define_method(rb_cSymbol, "===", sym_equal, 1);
13069 rb_define_method(rb_cSymbol, "inspect", sym_inspect, 0);
13070 rb_define_method(rb_cSymbol, "to_proc", rb_sym_to_proc, 0); /* in proc.c */
13071 rb_define_method(rb_cSymbol, "succ", sym_succ, 0);
13072 rb_define_method(rb_cSymbol, "next", sym_succ, 0);
13073
13074 rb_define_method(rb_cSymbol, "<=>", sym_cmp, 1);
13075 rb_define_method(rb_cSymbol, "casecmp", sym_casecmp, 1);
13076 rb_define_method(rb_cSymbol, "casecmp?", sym_casecmp_p, 1);
13077 rb_define_method(rb_cSymbol, "=~", sym_match, 1);
13078
13079 rb_define_method(rb_cSymbol, "[]", sym_aref, -1);
13080 rb_define_method(rb_cSymbol, "slice", sym_aref, -1);
13081 rb_define_method(rb_cSymbol, "length", sym_length, 0);
13082 rb_define_method(rb_cSymbol, "size", sym_length, 0);
13083 rb_define_method(rb_cSymbol, "empty?", sym_empty, 0);
13084 rb_define_method(rb_cSymbol, "match", sym_match_m, -1);
13085 rb_define_method(rb_cSymbol, "match?", sym_match_m_p, -1);
13086
13087 rb_define_method(rb_cSymbol, "upcase", sym_upcase, -1);
13088 rb_define_method(rb_cSymbol, "downcase", sym_downcase, -1);
13089 rb_define_method(rb_cSymbol, "capitalize", sym_capitalize, -1);
13090 rb_define_method(rb_cSymbol, "swapcase", sym_swapcase, -1);
13091
13092 rb_define_method(rb_cSymbol, "start_with?", sym_start_with, -1);
13093 rb_define_method(rb_cSymbol, "end_with?", sym_end_with, -1);
13094
13095 rb_define_method(rb_cSymbol, "encoding", sym_encoding, 0);
13096}
13097
#define RUBY_ASSERT_ALWAYS(expr,...)
A variant of RUBY_ASSERT that does not interface with RUBY_DEBUG.
Definition assert.h:199
#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
Atomic operations.
@ RUBY_ENC_CODERANGE_7BIT
The object holds 0 to 127 inclusive and nothing else.
Definition coderange.h:39
#define ISSPACE
@old{rb_isspace}
Definition ctype.h:88
#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.
VALUE rb_enc_sprintf(rb_encoding *enc, const char *fmt,...)
Identical to rb_sprintf(), except it additionally takes an encoding.
Definition sprintf.c:1232
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
Definition class.c:1695
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:1488
VALUE rb_define_module(const char *name)
Defines a top-level module.
Definition class.c:1601
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition class.c:2868
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition class.c:2680
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:3158
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition eval.c:1027
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
Definition class.c:2947
#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:133
#define rb_str_buf_cat2
Old name of rb_usascii_str_new_cstr.
Definition string.h:1683
#define ALLOCV
Old name of RB_ALLOCV.
Definition memory.h:404
#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 xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
#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:136
#define rb_str_cat2
Old name of rb_str_cat_cstr.
Definition string.h:1684
#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:134
#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:401
#define ALLOC_N
Old name of RB_ALLOC_N.
Definition memory.h:399
#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:131
#define FL_SET
Old name of RB_FL_SET.
Definition fl_type.h:128
#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:125
#define MBCLEN_INVALID_P(ret)
Old name of ONIGENC_MBCLEN_INVALID_P.
Definition encoding.h:518
#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:405
#define MBCLEN_CHARFOUND_P(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_P.
Definition encoding.h:516
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
Definition fl_type.h:59
#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:130
#define FL_FREEZE
Old name of RUBY_FL_FREEZE.
Definition fl_type.h:66
#define NUM2LONG
Old name of RB_NUM2LONG.
Definition long.h:51
#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:132
#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 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:129
#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:137
#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:476
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:664
void rb_syserr_fail(int e, const char *mesg)
Raises appropriate exception that represents a C errno.
Definition error.c:3911
VALUE rb_eRangeError
RangeError exception.
Definition error.c:1435
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1431
VALUE rb_eEncCompatError
Encoding::CompatibilityError exception.
Definition error.c:1438
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1429
VALUE rb_eIndexError
IndexError exception.
Definition error.c:1433
VALUE rb_ensure(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*e_proc)(VALUE), VALUE data2)
An equivalent to ensure clause.
Definition eval.c:1178
@ RB_WARN_CATEGORY_DEPRECATED
Warning is for deprecated features.
Definition error.h:48
VALUE rb_any_to_s(VALUE obj)
Generates a textual representation of the given object.
Definition object.c:675
VALUE rb_obj_alloc(VALUE klass)
Allocates an instance of the given class.
Definition object.c:2208
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:2226
VALUE rb_obj_frozen_p(VALUE obj)
Just calls RB_OBJ_FROZEN() inside.
Definition object.c:1354
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:3622
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:264
VALUE rb_obj_dup(VALUE obj)
Duplicates the given object.
Definition object.c:582
VALUE rb_cSymbol
Symbol class.
Definition string.c:85
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
Definition object.c:176
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
Definition object.c:1342
VALUE rb_mComparable
Comparable module.
Definition compar.c:19
VALUE rb_cString
String class.
Definition string.c:84
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
Definition object.c:3306
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:603
Encoding relates APIs.
VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
Encoding conversion main routine.
Definition string.c:1359
int rb_enc_str_coderange(VALUE str)
Scans the passed string to collect its code range.
Definition string.c:964
VALUE rb_enc_str_new_static(const char *ptr, long len, rb_encoding *enc)
Identical to rb_enc_str_new(), except it takes a C string literal.
Definition string.c:1224
char * rb_enc_nth(const char *head, const char *tail, long nth, rb_encoding *enc)
Queries the n-th character.
Definition string.c:3059
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:1243
VALUE rb_enc_interned_str(const char *ptr, long len, rb_encoding *enc)
Identical to rb_enc_str_new(), except it returns a "f"string.
Definition string.c:12833
long rb_memsearch(const void *x, long m, const void *y, long n, rb_encoding *enc)
Looks for the passed string in the passed buffer.
Definition re.c:256
long 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:2363
VALUE rb_enc_str_buf_cat(VALUE str, const char *ptr, long len, rb_encoding *enc)
Identical to rb_str_cat(), except it additionally takes an encoding.
Definition string.c:3766
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:1172
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:1464
VALUE rb_external_str_new_with_enc(const char *ptr, long len, rb_encoding *enc)
Identical to rb_external_str_new(), except it additionally takes an encoding.
Definition string.c:1365
int rb_enc_str_asciionly_p(VALUE str)
Queries if the passed string is "ASCII only".
Definition string.c:983
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:12857
int rb_enc_symname2_p(const char *name, long len, rb_encoding *enc)
Identical to rb_enc_symname_p(), except it additionally takes the passed string's length.
Definition symbol.c:703
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:2729
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:2992
void rb_econv_close(rb_econv_t *ec)
Destructs a converter.
Definition transcode.c:1743
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1119
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:1206
#define RGENGC_WB_PROTECTED_STRING
This is a compile-time flag to enable/disable write barrier for struct RString.
Definition gc.h:479
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_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
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
VALUE rb_fs
The field separator character for inputs, or the $;.
Definition string.c:728
VALUE rb_default_rs
This is the default value of rb_rs, i.e.
Definition io.c:212
VALUE rb_backref_get(void)
Queries the last match, or Regexp.last_match, or the $~.
Definition vm.c:2056
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:1061
void rb_backref_set(VALUE md)
Updates $~.
Definition vm.c:2062
VALUE rb_range_beg_len(VALUE range, long *begp, long *lenp, long len, int err)
Deconstructs a numerical range.
Definition range.c:1954
int rb_reg_backref_number(VALUE match, VALUE backref)
Queries the index of the given named capture.
Definition re.c:1247
int rb_reg_options(VALUE re)
Queries the options of the passed regular expression.
Definition re.c:4242
VALUE rb_reg_match(VALUE re, VALUE str)
This is the match operator.
Definition re.c:3739
void rb_match_busy(VALUE md)
Asserts that the given MatchData is "occupied".
Definition re.c:1501
VALUE rb_reg_nth_match(int n, VALUE md)
Queries the nth captured substring.
Definition re.c:1937
void rb_str_free(VALUE str)
Destroys the given string for no reason.
Definition string.c:1769
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:1529
VALUE rb_str_plus(VALUE lhs, VALUE rhs)
Generates a new string, concatenating the former to the latter.
Definition string.c:2516
#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:1584
#define rb_hash_end(h)
Just another name of st_hash_end.
Definition string.h:946
#define rb_hash_uint32(h, i)
Just another name of st_hash_uint32.
Definition string.h:940
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:3833
VALUE rb_filesystem_str_new(const char *ptr, long len)
Identical to rb_str_new(), except it generates a string of "filesystem" encoding.
Definition string.c:1440
VALUE rb_sym_to_s(VALUE sym)
This is an rb_sym2str() + rb_str_dup() combo.
Definition string.c:12449
VALUE rb_str_times(VALUE str, VALUE num)
Repetition of a string.
Definition string.c:2589
VALUE rb_external_str_new(const char *ptr, long len)
Identical to rb_str_new(), except it generates a string of "defaultexternal" encoding.
Definition string.c:1416
VALUE rb_str_tmp_new(long len)
Allocates a "temporary" string.
Definition string.c:1763
long rb_str_offset(VALUE str, long pos)
"Inverse" of rb_str_sublen().
Definition string.c:3087
VALUE rb_str_succ(VALUE orig)
Searches for the "successor" of a string.
Definition string.c:5389
int rb_str_hash_cmp(VALUE str1, VALUE str2)
Compares two strings.
Definition string.c:4205
VALUE rb_str_subseq(VALUE str, long beg, long len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
Definition string.c:3184
VALUE rb_str_ellipsize(VALUE str, long len)
Shortens str and adds three dots, an ellipsis, if it is longer than len characters.
Definition string.c:11769
st_index_t rb_memhash(const void *ptr, long len)
This is a universal hash function.
Definition random.c:1790
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1499
void rb_str_shared_replace(VALUE dst, VALUE src)
Replaces the contents of the former with the latter.
Definition string.c:1805
#define rb_str_buf_cat
Just another name of rb_str_cat.
Definition string.h:1682
VALUE rb_str_new_static(const char *ptr, long len)
Identical to rb_str_new(), except it takes a C string literal.
Definition string.c:1206
#define rb_usascii_str_new(str, len)
Identical to rb_str_new, except it generates a string of "US ASCII" encoding.
Definition string.h:1533
size_t rb_str_capacity(VALUE str)
Queries the capacity of the given string.
Definition string.c:1018
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
Definition string.c:1535
VALUE rb_str_dup(VALUE str)
Duplicates a string.
Definition string.c:2014
st_index_t rb_str_hash(VALUE str)
Calculates a hash value of a string.
Definition string.c:4191
VALUE rb_str_cat(VALUE dst, const char *src, long srclen)
Destructively appends the passed contents to the string.
Definition string.c:3599
VALUE rb_str_locktmp(VALUE str)
Obtains a "temporary lock" of the string.
long rb_str_strlen(VALUE str)
Counts the number of characters (not bytes) that are stored inside of the given string.
Definition string.c:2452
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:2032
#define rb_str_buf_new_cstr(str)
Identical to rb_str_new_cstr, except done differently.
Definition string.h:1640
#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:1568
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:6606
char * rb_str_subpos(VALUE str, long beg, long *len)
Identical to rb_str_substr(), except it returns a C's string instead of Ruby's.
Definition string.c:3192
rb_gvar_setter_t rb_str_setter
This is a rb_gvar_setter_t that refutes non-string assignments.
Definition string.h:1147
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:12827
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:1446
#define rb_external_str_new_cstr(str)
Identical to rb_str_new_cstr, except it generates a string of "defaultexternal" encoding.
Definition string.h:1605
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:3797
long rb_str_sublen(VALUE str, long pos)
Byte offset to character offset conversion.
Definition string.c:3134
VALUE rb_str_equal(VALUE str1, VALUE str2)
Equality of two strings.
Definition string.c:4312
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
Definition string.c:3418
VALUE rb_str_inspect(VALUE str)
Generates a "readable" version of the receiver.
Definition string.c:7285
void rb_must_asciicompat(VALUE obj)
Asserts that the given string's encoding is (Ruby's definition of) ASCII compatible.
Definition string.c:2821
VALUE rb_interned_str(const char *ptr, long len)
Identical to rb_str_new(), except it returns an infamous "f"string.
Definition string.c:12812
int rb_str_cmp(VALUE lhs, VALUE rhs)
Compares two strings, as in strcmp(3).
Definition string.c:4259
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:4073
int rb_str_comparable(VALUE str1, VALUE str2)
Checks if two strings are comparable each other or not.
Definition string.c:4234
#define rb_strlen_lit(str)
Length of a string literal.
Definition string.h:1693
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:3773
VALUE rb_str_freeze(VALUE str)
This is the implementation of String#freeze.
Definition string.c:3309
void rb_str_update(VALUE dst, long beg, long len, VALUE src)
Replaces some (or all) of the contents of the given string.
Definition string.c:5874
VALUE rb_str_scrub(VALUE str, VALUE repl)
"Cleanses" the string.
Definition string.c:11827
#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:1626
VALUE rb_str_new_with_class(VALUE obj, const char *ptr, long len)
Identical to rb_str_new(), except it takes the class of the allocating object.
Definition string.c:1719
#define rb_str_dup_frozen
Just another name of rb_str_new_frozen.
Definition string.h:632
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
Definition string.c:2981
VALUE rb_str_substr(VALUE str, long beg, long len)
This is the implementation of two-argumented String#slice.
Definition string.c:3281
#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:1657
VALUE rb_str_unlocktmp(VALUE str)
Releases a lock formerly obtained by rb_str_locktmp().
Definition string.c:3400
VALUE rb_utf8_str_new_static(const char *ptr, long len)
Identical to rb_str_new_static(), except it generates a string of "UTF-8" encoding instead of "binary...
Definition string.c:1218
#define rb_utf8_str_new(str, len)
Identical to rb_str_new, except it generates a string of "UTF-8" encoding.
Definition string.h:1550
void rb_str_modify_expand(VALUE str, long capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
Definition string.c:2775
VALUE rb_str_dump(VALUE str)
"Inverse" of rb_eval_string().
Definition string.c:7392
VALUE rb_locale_str_new(const char *ptr, long len)
Identical to rb_str_new(), except it generates a string of "locale" encoding.
Definition string.c:1428
VALUE rb_str_buf_new(long capa)
Allocates a "string buffer".
Definition string.c:1735
VALUE rb_str_length(VALUE)
Identical to rb_str_strlen(), except it returns the value in rb_cInteger.
Definition string.c:2466
#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:1515
VALUE rb_str_drop_bytes(VALUE str, long len)
Shrinks the given string for the given number of bytes.
Definition string.c:5791
VALUE rb_str_split(VALUE str, const char *delim)
Divides the given string based on the given delimiter.
Definition string.c:9405
VALUE rb_usascii_str_new_static(const char *ptr, long len)
Identical to rb_str_new_static(), except it generates a string of "US ASCII" encoding instead of "bin...
Definition string.c:1212
VALUE rb_str_intern(VALUE str)
Identical to rb_to_symbol(), except it assumes the receiver being an instance of RString.
Definition symbol.c:937
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:1868
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:2030
VALUE rb_ivar_defined(VALUE obj, ID name)
Queries if the instance variable is defined at the object.
Definition variable.c:2109
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
Definition vm_method.c:3480
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition vm_method.c:1719
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
Definition symbol.c:994
VALUE rb_to_symbol(VALUE name)
Identical to rb_intern_str(), except it generates a dynamic symbol if necessary.
Definition string.c:12779
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:12769
int capa
Designed capacity of the buffer.
Definition io.h:11
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
Definition io.h:2
int off
Offset inside of ptr.
Definition io.h:5
int len
Length of the buffer.
Definition io.h:8
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
Definition ractor.h:235
long rb_reg_search(VALUE re, VALUE str, long pos, int dir)
Runs the passed regular expression over the passed string.
Definition re.c:1876
VALUE rb_reg_regcomp(VALUE str)
Creates a new instance of rb_cRegexp.
Definition re.c:3518
VALUE rb_reg_regsub(VALUE repl, VALUE src, struct re_registers *regs, VALUE rexp)
Substitution.
Definition re.c:4486
VALUE rb_str_format(int argc, const VALUE *argv, VALUE fmt)
Formats a string.
Definition sprintf.c:979
VALUE rb_yield(VALUE val)
Yields the block.
Definition vm_eval.c:1374
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:372
#define ALLOCA_N(type, n)
Definition memory.h:292
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
Definition memory.h:360
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:167
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.
Defines RBIMPL_ATTR_NONSTRING.
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
Definition rarray.h:52
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
#define DATA_PTR(obj)
Convenient getter macro.
Definition rdata.h:67
#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:1458
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:2958
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
Definition rstring.h:450
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:2840
#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:1452
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:2853
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:1796
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
Definition rstring.h:89
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
Definition rtypeddata.h:461
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:205
VALUE rb_require(const char *feature)
Identical to rb_require_string(), except it takes C's string instead of Ruby's.
Definition load.c:1455
#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
struct RBasic basic
Basic part, including flags and class.
Definition rstring.h:199
long len
Length of the string, not including terminating NUL character.
Definition rstring.h:206
Definition string.c:8285
void rb_nativethread_lock_lock(rb_nativethread_lock_t *lock)
Blocks until the current thread obtains a lock.
Definition thread.c:308
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
ruby_value_type
C-level type of an object.
Definition value_type.h:113