summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
Diffstat (limited to 'string.c')
-rw-r--r--string.c9523
1 files changed, 4752 insertions, 4771 deletions
diff --git a/string.c b/string.c
index 303cd9b98b..5c29718dff 100644
--- a/string.c
+++ b/string.c
@@ -23,7 +23,6 @@
#include "debug_counter.h"
#include "encindex.h"
-#include "gc.h"
#include "id.h"
#include "internal.h"
#include "internal/array.h"
@@ -63,7 +62,6 @@
#undef rb_utf8_str_new
#undef rb_enc_str_new
#undef rb_str_new_cstr
-#undef rb_tainted_str_new_cstr
#undef rb_usascii_str_new_cstr
#undef rb_utf8_str_new_cstr
#undef rb_enc_str_new_cstr
@@ -80,25 +78,39 @@
VALUE rb_cString;
VALUE rb_cSymbol;
-/* FLAGS of RString
+/* Flags of RString
*
* 1: RSTRING_NOEMBED
- * 2: STR_SHARED (== ELTS_SHARED)
- * 2-6: RSTRING_EMBED_LEN (5 bits == 32)
- * 5: STR_SHARED_ROOT (RSTRING_NOEMBED==1 && STR_SHARED == 0, there may be
- * other strings that rely on this string's buffer)
- * 6: STR_BORROWED (when RSTRING_NOEMBED==1 && klass==0, unsafe to recycle
- * early, specific to rb_str_tmp_frozen_{acquire,release})
- * 7: STR_TMPLOCK (set when a pointer to the buffer is passed to syscall
- * such as read(2). Any modification and realloc is prohibited)
- *
- * 8-9: ENC_CODERANGE (2 bits)
- * 10-16: ENCODING (7 bits == 128)
+ * The string is not embedded. When a string is embedded, the contents
+ * follow the header. When a string is not embedded, the contents is
+ * on a separately allocated buffer.
+ * 2: STR_SHARED (equal to ELTS_SHARED)
+ * The string is shared. The buffer this string points to is owned by
+ * another string (the shared root).
+ * 5: STR_SHARED_ROOT
+ * Other strings may point to the contents of this string. When this
+ * flag is set, STR_SHARED must not be set.
+ * 6: STR_BORROWED
+ * When RSTRING_NOEMBED is set and klass is 0, this string is unsafe
+ * to be unshared by rb_str_tmp_frozen_release.
+ * 7: STR_TMPLOCK
+ * The pointer to the buffer is passed to a system call such as
+ * read(2). Any modification and realloc is prohibited.
+ * 8-9: ENC_CODERANGE
+ * Stores the coderange of the string.
+ * 10-16: ENCODING
+ * Stores the encoding of the string.
* 17: RSTRING_FSTR
- * 18: STR_NOFREE (do not free this string's buffer when a String is freed.
- * used for a string object based on C string literal)
- * 19: STR_FAKESTR (when RVALUE is not managed by GC. Typically, the string
- * object header is temporarily allocated on C stack)
+ * The string is a fstring. The string is deduplicated in the fstring
+ * table.
+ * 18: STR_NOFREE
+ * Do not free this string's buffer when the string is reclaimed
+ * by the garbage collector. Used for when the string buffer is a C
+ * string literal.
+ * 19: STR_FAKESTR
+ * The string is not allocated or managed by the garbage collector.
+ * Typically, the string object header (struct RString) is temporarily
+ * allocated on C stack.
*/
#define RUBY_MAX_CHAR_LEN 16
@@ -110,54 +122,35 @@ VALUE rb_cSymbol;
#define STR_SET_NOEMBED(str) do {\
FL_SET((str), STR_NOEMBED);\
- if (USE_RVARGC) {\
- FL_UNSET((str), STR_SHARED | STR_SHARED_ROOT | STR_BORROWED);\
- }\
- else {\
- STR_SET_EMBED_LEN((str), 0);\
- }\
-} while (0)
-#define STR_SET_EMBED(str) FL_UNSET((str), (STR_NOEMBED|STR_NOFREE))
-#if USE_RVARGC
-# define STR_SET_EMBED_LEN(str, n) do { \
- assert(str_embed_capa(str) > (n));\
- RSTRING(str)->as.embed.len = (n);\
+ FL_UNSET((str), STR_SHARED | STR_SHARED_ROOT | STR_BORROWED);\
} while (0)
-#else
-# define STR_SET_EMBED_LEN(str, n) do { \
- long tmp_n = (n);\
- RBASIC(str)->flags &= ~RSTRING_EMBED_LEN_MASK;\
- RBASIC(str)->flags |= (tmp_n) << RSTRING_EMBED_LEN_SHIFT;\
-} while (0)
-#endif
+#define STR_SET_EMBED(str) FL_UNSET((str), STR_NOEMBED | STR_SHARED | STR_NOFREE)
#define STR_SET_LEN(str, n) do { \
- if (STR_EMBED_P(str)) {\
- STR_SET_EMBED_LEN((str), (n));\
- }\
- else {\
- RSTRING(str)->as.heap.len = (n);\
- }\
+ RSTRING(str)->len = (n); \
} while (0)
-#define STR_DEC_LEN(str) do {\
- if (STR_EMBED_P(str)) {\
- long n = RSTRING_LEN(str);\
- n--;\
- STR_SET_EMBED_LEN((str), n);\
- }\
- else {\
- RSTRING(str)->as.heap.len--;\
- }\
-} while (0)
+static inline bool
+str_enc_fastpath(VALUE str)
+{
+ // The overwhelming majority of strings are in one of these 3 encodings.
+ switch (ENCODING_GET_INLINED(str)) {
+ case ENCINDEX_ASCII_8BIT:
+ case ENCINDEX_UTF_8:
+ case ENCINDEX_US_ASCII:
+ return true;
+ default:
+ return false;
+ }
+}
-#define TERM_LEN(str) rb_enc_mbminlen(rb_enc_get(str))
+#define TERM_LEN(str) (str_enc_fastpath(str) ? 1 : rb_enc_mbminlen(rb_enc_from_index(ENCODING_GET(str))))
#define TERM_FILL(ptr, termlen) do {\
char *const term_fill_ptr = (ptr);\
const int term_fill_len = (termlen);\
*term_fill_ptr = '\0';\
if (UNLIKELY(term_fill_len > 1))\
- memset(term_fill_ptr, 0, term_fill_len);\
+ memset(term_fill_ptr, 0, term_fill_len);\
} while (0)
#define RESIZE_CAPA(str,capacity) do {\
@@ -166,33 +159,33 @@ VALUE rb_cSymbol;
} while (0)
#define RESIZE_CAPA_TERM(str,capacity,termlen) do {\
if (STR_EMBED_P(str)) {\
- if (str_embed_capa(str) < capacity + termlen) {\
- char *const tmp = ALLOC_N(char, (size_t)(capacity) + (termlen));\
- const long tlen = RSTRING_LEN(str);\
- memcpy(tmp, RSTRING_PTR(str), tlen);\
- RSTRING(str)->as.heap.ptr = tmp;\
- RSTRING(str)->as.heap.len = tlen;\
+ if (str_embed_capa(str) < capacity + termlen) {\
+ char *const tmp = ALLOC_N(char, (size_t)(capacity) + (termlen));\
+ const long tlen = RSTRING_LEN(str);\
+ memcpy(tmp, RSTRING_PTR(str), tlen);\
+ RSTRING(str)->as.heap.ptr = tmp;\
+ RSTRING(str)->len = tlen;\
STR_SET_NOEMBED(str);\
- RSTRING(str)->as.heap.aux.capa = (capacity);\
- }\
+ RSTRING(str)->as.heap.aux.capa = (capacity);\
+ }\
}\
else {\
- assert(!FL_TEST((str), STR_SHARED)); \
- SIZED_REALLOC_N(RSTRING(str)->as.heap.ptr, char, \
- (size_t)(capacity) + (termlen), STR_HEAP_SIZE(str)); \
- RSTRING(str)->as.heap.aux.capa = (capacity);\
+ RUBY_ASSERT(!FL_TEST((str), STR_SHARED)); \
+ SIZED_REALLOC_N(RSTRING(str)->as.heap.ptr, char, \
+ (size_t)(capacity) + (termlen), STR_HEAP_SIZE(str)); \
+ RSTRING(str)->as.heap.aux.capa = (capacity);\
}\
} while (0)
#define STR_SET_SHARED(str, shared_str) do { \
if (!FL_TEST(str, STR_FAKESTR)) { \
- assert(RSTRING_PTR(shared_str) <= RSTRING_PTR(str)); \
- assert(RSTRING_PTR(str) <= RSTRING_PTR(shared_str) + RSTRING_LEN(shared_str)); \
- RB_OBJ_WRITE((str), &RSTRING(str)->as.heap.aux.shared, (shared_str)); \
- FL_SET((str), STR_SHARED); \
+ RUBY_ASSERT(RSTRING_PTR(shared_str) <= RSTRING_PTR(str)); \
+ RUBY_ASSERT(RSTRING_PTR(str) <= RSTRING_PTR(shared_str) + RSTRING_LEN(shared_str)); \
+ RB_OBJ_WRITE((str), &RSTRING(str)->as.heap.aux.shared, (shared_str)); \
+ FL_SET((str), STR_SHARED); \
FL_SET((shared_str), STR_SHARED_ROOT); \
- if (RBASIC_CLASS((shared_str)) == 0) /* for CoW-friendliness */ \
- FL_SET_RAW((shared_str), STR_BORROWED); \
+ if (RBASIC_CLASS((shared_str)) == 0) /* for CoW-friendliness */ \
+ FL_SET_RAW((shared_str), STR_BORROWED); \
} \
} while (0)
@@ -215,27 +208,43 @@ VALUE rb_cSymbol;
static inline long
str_embed_capa(VALUE str)
{
-#if USE_RVARGC
return rb_gc_obj_slot_size(str) - offsetof(struct RString, as.embed.ary);
-#else
- return RSTRING_EMBED_LEN_MAX + 1;
-#endif
+}
+
+bool
+rb_str_reembeddable_p(VALUE str)
+{
+ return !FL_TEST(str, STR_NOFREE|STR_SHARED_ROOT|STR_SHARED);
}
static inline size_t
-str_embed_size(long capa)
+rb_str_embed_size(long capa)
{
return offsetof(struct RString, as.embed.ary) + capa;
}
+size_t
+rb_str_size_as_embedded(VALUE str)
+{
+ size_t real_size;
+ if (STR_EMBED_P(str)) {
+ real_size = rb_str_embed_size(RSTRING(str)->len) + TERM_LEN(str);
+ }
+ /* if the string is not currently embedded, but it can be embedded, how
+ * much space would it require */
+ else if (rb_str_reembeddable_p(str)) {
+ real_size = rb_str_embed_size(RSTRING(str)->as.heap.aux.capa) + TERM_LEN(str);
+ }
+ else {
+ real_size = sizeof(struct RString);
+ }
+ return real_size;
+}
+
static inline bool
STR_EMBEDDABLE_P(long len, long termlen)
{
-#if USE_RVARGC
- return rb_gc_size_allocatable_p(str_embed_size(len + termlen));
-#else
- return len <= RSTRING_EMBED_LEN_MAX + 1 - termlen;
-#endif
+ return rb_gc_size_allocatable_p(rb_str_embed_size(len + termlen));
}
static VALUE str_replace_shared_without_enc(VALUE str2, VALUE str);
@@ -266,59 +275,49 @@ rb_str_make_independent(VALUE str)
}
void
+rb_str_make_embedded(VALUE str)
+{
+ RUBY_ASSERT(rb_str_reembeddable_p(str));
+ RUBY_ASSERT(!STR_EMBED_P(str));
+
+ char *buf = RSTRING(str)->as.heap.ptr;
+ long len = RSTRING(str)->len;
+
+ STR_SET_EMBED(str);
+ STR_SET_LEN(str, len);
+
+ if (len > 0) {
+ memcpy(RSTRING_PTR(str), buf, len);
+ ruby_xfree(buf);
+ }
+
+ TERM_FILL(RSTRING(str)->as.embed.ary + len, TERM_LEN(str));
+}
+
+void
rb_debug_rstring_null_ptr(const char *func)
{
fprintf(stderr, "%s is returning NULL!! "
- "SIGSEGV is highly expected to follow immediately. "
+ "SIGSEGV is highly expected to follow immediately.\n"
"If you could reproduce, attach your debugger here, "
- "and look at the passed string.",
- func);
+ "and look at the passed string.\n",
+ func);
}
/* symbols for [up|down|swap]case/capitalize options */
static VALUE sym_ascii, sym_turkic, sym_lithuanian, sym_fold;
static rb_encoding *
-get_actual_encoding(const int encidx, VALUE str)
-{
- const unsigned char *q;
-
- switch (encidx) {
- case ENCINDEX_UTF_16:
- if (RSTRING_LEN(str) < 2) break;
- q = (const unsigned char *)RSTRING_PTR(str);
- if (q[0] == 0xFE && q[1] == 0xFF) {
- return rb_enc_get_from_index(ENCINDEX_UTF_16BE);
- }
- if (q[0] == 0xFF && q[1] == 0xFE) {
- return rb_enc_get_from_index(ENCINDEX_UTF_16LE);
- }
- return rb_ascii8bit_encoding();
- case ENCINDEX_UTF_32:
- if (RSTRING_LEN(str) < 4) break;
- q = (const unsigned char *)RSTRING_PTR(str);
- if (q[0] == 0 && q[1] == 0 && q[2] == 0xFE && q[3] == 0xFF) {
- return rb_enc_get_from_index(ENCINDEX_UTF_32BE);
- }
- if (q[3] == 0 && q[2] == 0 && q[1] == 0xFE && q[0] == 0xFF) {
- return rb_enc_get_from_index(ENCINDEX_UTF_32LE);
- }
- return rb_ascii8bit_encoding();
- }
- return rb_enc_from_index(encidx);
-}
-
-static rb_encoding *
get_encoding(VALUE str)
{
- return get_actual_encoding(ENCODING_GET(str), str);
+ return rb_enc_from_index(ENCODING_GET(str));
}
static void
mustnot_broken(VALUE str)
{
if (is_broken_string(str)) {
- rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(STR_ENC_GET(str)));
+ rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(STR_ENC_GET(str)));
}
}
@@ -327,7 +326,7 @@ mustnot_wchar(VALUE str)
{
rb_encoding *enc = STR_ENC_GET(str);
if (rb_enc_mbminlen(enc) > 1) {
- rb_raise(rb_eArgError, "wide char encoding: %s", rb_enc_name(enc));
+ rb_raise(rb_eArgError, "wide char encoding: %s", rb_enc_name(enc));
}
}
@@ -355,51 +354,51 @@ fstr_update_callback(st_data_t *key, st_data_t *value, st_data_t data, int exist
VALUE str = (VALUE)*key;
if (existing) {
- /* because of lazy sweep, str may be unmarked already and swept
- * at next time */
+ /* because of lazy sweep, str may be unmarked already and swept
+ * at next time */
- if (rb_objspace_garbage_object_p(str)) {
- arg->fstr = Qundef;
- return ST_DELETE;
- }
+ if (rb_objspace_garbage_object_p(str)) {
+ arg->fstr = Qundef;
+ return ST_DELETE;
+ }
- arg->fstr = str;
- return ST_STOP;
+ arg->fstr = str;
+ return ST_STOP;
}
else {
- if (FL_TEST_RAW(str, STR_FAKESTR)) {
- if (arg->copy) {
- VALUE new_str = str_new(rb_cString, RSTRING(str)->as.heap.ptr, RSTRING(str)->as.heap.len);
- rb_enc_copy(new_str, str);
- str = new_str;
- }
- else {
- str = str_new_static(rb_cString, RSTRING(str)->as.heap.ptr,
- RSTRING(str)->as.heap.len,
- ENCODING_GET(str));
- }
- OBJ_FREEZE_RAW(str);
- }
- else {
- if (!OBJ_FROZEN(str))
- str = str_new_frozen(rb_cString, str);
- if (STR_SHARED_P(str)) { /* str should not be shared */
- /* shared substring */
+ if (FL_TEST_RAW(str, STR_FAKESTR)) {
+ if (arg->copy) {
+ VALUE new_str = str_new(rb_cString, RSTRING(str)->as.heap.ptr, RSTRING(str)->len);
+ rb_enc_copy(new_str, str);
+ str = new_str;
+ }
+ else {
+ str = str_new_static(rb_cString, RSTRING(str)->as.heap.ptr,
+ RSTRING(str)->len,
+ ENCODING_GET(str));
+ }
+ OBJ_FREEZE_RAW(str);
+ }
+ else {
+ if (!OBJ_FROZEN(str) || CHILLED_STRING_P(str)) {
+ str = str_new_frozen(rb_cString, str);
+ }
+ if (STR_SHARED_P(str)) { /* str should not be shared */
+ /* shared substring */
str_make_independent(str);
- assert(OBJ_FROZEN(str));
- }
- if (!BARE_STRING_P(str)) {
- str = str_new_frozen(rb_cString, str);
- }
- }
- RBASIC(str)->flags |= RSTRING_FSTR;
+ RUBY_ASSERT(OBJ_FROZEN(str));
+ }
+ if (!BARE_STRING_P(str)) {
+ str = str_new_frozen(rb_cString, str);
+ }
+ }
+ RBASIC(str)->flags |= RSTRING_FSTR;
- *key = *value = arg->fstr = str;
- return ST_CONTINUE;
+ *key = *value = arg->fstr = str;
+ return ST_CONTINUE;
}
}
-RUBY_FUNC_EXPORTED
VALUE
rb_fstring(VALUE str)
{
@@ -409,7 +408,7 @@ rb_fstring(VALUE str)
Check_Type(str, T_STRING);
if (FL_TEST(str, RSTRING_FSTR))
- return str;
+ return str;
bare = BARE_STRING_P(str);
if (!bare) {
@@ -417,21 +416,22 @@ rb_fstring(VALUE str)
OBJ_FREEZE_RAW(str);
return str;
}
- if (FL_TEST_RAW(str, STR_NOEMBED|STR_SHARED_ROOT|STR_SHARED) == (STR_NOEMBED|STR_SHARED_ROOT)) {
- assert(OBJ_FROZEN(str));
+
+ if (FL_TEST_RAW(str, STR_SHARED_ROOT | STR_SHARED) == STR_SHARED_ROOT) {
+ RUBY_ASSERT(OBJ_FROZEN(str));
return str;
}
}
- if (!OBJ_FROZEN(str))
+ if (!FL_TEST_RAW(str, FL_FREEZE | STR_NOFREE | STR_CHILLED))
rb_str_resize(str, RSTRING_LEN(str));
fstr = register_fstring(str, FALSE);
if (!bare) {
- str_replace_shared_without_enc(str, fstr);
- OBJ_FREEZE_RAW(str);
- return str;
+ str_replace_shared_without_enc(str, fstr);
+ OBJ_FREEZE_RAW(str);
+ return str;
}
return fstr;
}
@@ -448,14 +448,15 @@ register_fstring(VALUE str, bool copy)
do {
args.fstr = str;
st_update(frozen_strings, (st_data_t)str, fstr_update_callback, (st_data_t)&args);
- } while (args.fstr == Qundef);
+ } while (UNDEF_P(args.fstr));
}
RB_VM_LOCK_LEAVE();
- assert(OBJ_FROZEN(args.fstr));
- assert(!FL_TEST_RAW(args.fstr, STR_FAKESTR));
- assert(!FL_TEST_RAW(args.fstr, FL_EXIVAR));
- assert(RBASIC_CLASS(args.fstr) == rb_cString);
+ RUBY_ASSERT(OBJ_FROZEN(args.fstr));
+ RUBY_ASSERT(!FL_TEST_RAW(args.fstr, STR_FAKESTR));
+ RUBY_ASSERT(!FL_TEST_RAW(args.fstr, FL_EXIVAR));
+ RUBY_ASSERT(RBASIC_CLASS(args.fstr) == rb_cString);
+
return args.fstr;
}
@@ -466,14 +467,14 @@ setup_fake_str(struct RString *fake_str, const char *name, long len, int encidx)
/* SHARED to be allocated by the callback */
if (!name) {
- RUBY_ASSERT_ALWAYS(len == 0);
- name = "";
+ RUBY_ASSERT_ALWAYS(len == 0);
+ name = "";
}
ENCODING_SET_INLINED((VALUE)fake_str, encidx);
RBASIC_SET_CLASS_RAW((VALUE)fake_str, rb_cString);
- fake_str->as.heap.len = len;
+ fake_str->len = len;
fake_str->as.heap.ptr = (char *)name;
fake_str->as.heap.aux.capa = len;
return (VALUE)fake_str;
@@ -493,7 +494,7 @@ rb_setup_fake_str(struct RString *fake_str, const char *name, long len, rb_encod
* shared string which refers a static string literal. `ptr` must
* point a constant string.
*/
-MJIT_FUNC_EXPORTED VALUE
+VALUE
rb_fstring_new(const char *ptr, long len)
{
struct RString fake_str;
@@ -528,8 +529,8 @@ fstring_cmp(VALUE a, VALUE b)
RSTRING_GETMEM(a, aptr, alen);
RSTRING_GETMEM(b, bptr, blen);
return (alen != blen ||
- ENCODING_GET(a) != ENCODING_GET(b) ||
- memcmp(aptr, bptr, alen) != 0);
+ ENCODING_GET(a) != ENCODING_GET(b) ||
+ memcmp(aptr, bptr, alen) != 0);
}
static inline int
@@ -577,23 +578,23 @@ search_nonascii(const char *p, const char *e)
if (UNALIGNED_WORD_ACCESS || e - p >= SIZEOF_VOIDP) {
#if !UNALIGNED_WORD_ACCESS
- if ((uintptr_t)p % SIZEOF_VOIDP) {
- int l = SIZEOF_VOIDP - (uintptr_t)p % SIZEOF_VOIDP;
- p += l;
- switch (l) {
- default: UNREACHABLE;
+ if ((uintptr_t)p % SIZEOF_VOIDP) {
+ int l = SIZEOF_VOIDP - (uintptr_t)p % SIZEOF_VOIDP;
+ p += l;
+ switch (l) {
+ default: UNREACHABLE;
#if SIZEOF_VOIDP > 4
- case 7: if (p[-7]&0x80) return p-7;
- case 6: if (p[-6]&0x80) return p-6;
- case 5: if (p[-5]&0x80) return p-5;
- case 4: if (p[-4]&0x80) return p-4;
+ case 7: if (p[-7]&0x80) return p-7;
+ case 6: if (p[-6]&0x80) return p-6;
+ case 5: if (p[-5]&0x80) return p-5;
+ case 4: if (p[-4]&0x80) return p-4;
#endif
- case 3: if (p[-3]&0x80) return p-3;
- case 2: if (p[-2]&0x80) return p-2;
- case 1: if (p[-1]&0x80) return p-1;
- case 0: break;
- }
- }
+ case 3: if (p[-3]&0x80) return p-3;
+ case 2: if (p[-2]&0x80) return p-2;
+ case 1: if (p[-1]&0x80) return p-1;
+ case 0: break;
+ }
+ }
#endif
#if defined(HAVE_BUILTIN___BUILTIN_ASSUME_ALIGNED) &&! UNALIGNED_WORD_ACCESS
#define aligned_ptr(value) \
@@ -601,19 +602,19 @@ search_nonascii(const char *p, const char *e)
#else
#define aligned_ptr(value) (uintptr_t *)(value)
#endif
- s = aligned_ptr(p);
- t = (uintptr_t *)(e - (SIZEOF_VOIDP-1));
+ s = aligned_ptr(p);
+ t = (uintptr_t *)(e - (SIZEOF_VOIDP-1));
#undef aligned_ptr
- for (;s < t; s++) {
- if (*s & NONASCII_MASK) {
+ for (;s < t; s++) {
+ if (*s & NONASCII_MASK) {
#ifdef WORDS_BIGENDIAN
- return (const char *)s + (nlz_intptr(*s&NONASCII_MASK)>>3);
+ return (const char *)s + (nlz_intptr(*s&NONASCII_MASK)>>3);
#else
- return (const char *)s + (ntz_intptr(*s&NONASCII_MASK)>>3);
+ return (const char *)s + (ntz_intptr(*s&NONASCII_MASK)>>3);
#endif
- }
- }
- p = (const char *)s;
+ }
+ }
+ p = (const char *)s;
}
switch (e - p) {
@@ -670,42 +671,42 @@ rb_str_coderange_scan_restartable(const char *s, const char *e, rb_encoding *enc
const char *p = s;
if (*cr == ENC_CODERANGE_BROKEN)
- return e - s;
+ return e - s;
if (rb_enc_to_index(enc) == rb_ascii8bit_encindex()) {
- /* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
- if (*cr == ENC_CODERANGE_VALID) return e - s;
- p = search_nonascii(p, e);
+ /* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
+ if (*cr == ENC_CODERANGE_VALID) return e - s;
+ p = search_nonascii(p, e);
*cr = p ? ENC_CODERANGE_VALID : ENC_CODERANGE_7BIT;
- return e - s;
+ return e - s;
}
else if (rb_enc_asciicompat(enc)) {
- p = search_nonascii(p, e);
- if (!p) {
- if (*cr != ENC_CODERANGE_VALID) *cr = ENC_CODERANGE_7BIT;
- return e - s;
- }
- for (;;) {
- int ret = rb_enc_precise_mbclen(p, e, enc);
- if (!MBCLEN_CHARFOUND_P(ret)) {
- *cr = MBCLEN_INVALID_P(ret) ? ENC_CODERANGE_BROKEN: ENC_CODERANGE_UNKNOWN;
- return p - s;
- }
- p += MBCLEN_CHARFOUND_LEN(ret);
- if (p == e) break;
- p = search_nonascii(p, e);
- if (!p) break;
- }
+ p = search_nonascii(p, e);
+ if (!p) {
+ if (*cr != ENC_CODERANGE_VALID) *cr = ENC_CODERANGE_7BIT;
+ return e - s;
+ }
+ for (;;) {
+ int ret = rb_enc_precise_mbclen(p, e, enc);
+ if (!MBCLEN_CHARFOUND_P(ret)) {
+ *cr = MBCLEN_INVALID_P(ret) ? ENC_CODERANGE_BROKEN: ENC_CODERANGE_UNKNOWN;
+ return p - s;
+ }
+ p += MBCLEN_CHARFOUND_LEN(ret);
+ if (p == e) break;
+ p = search_nonascii(p, e);
+ if (!p) break;
+ }
}
else {
- while (p < e) {
- int ret = rb_enc_precise_mbclen(p, e, enc);
- if (!MBCLEN_CHARFOUND_P(ret)) {
- *cr = MBCLEN_INVALID_P(ret) ? ENC_CODERANGE_BROKEN: ENC_CODERANGE_UNKNOWN;
- return p - s;
- }
- p += MBCLEN_CHARFOUND_LEN(ret);
- }
+ while (p < e) {
+ int ret = rb_enc_precise_mbclen(p, e, enc);
+ if (!MBCLEN_CHARFOUND_P(ret)) {
+ *cr = MBCLEN_INVALID_P(ret) ? ENC_CODERANGE_BROKEN: ENC_CODERANGE_UNKNOWN;
+ return p - s;
+ }
+ p += MBCLEN_CHARFOUND_LEN(ret);
+ }
}
*cr = ENC_CODERANGE_VALID;
return e - s;
@@ -717,6 +718,20 @@ str_enc_copy(VALUE str1, VALUE str2)
rb_enc_set_index(str1, ENCODING_GET(str2));
}
+/* Like str_enc_copy, but does not check frozen status of str1.
+ * You should use this only if you're certain that str1 is not frozen. */
+static inline void
+str_enc_copy_direct(VALUE str1, VALUE str2)
+{
+ int inlined_encoding = RB_ENCODING_GET_INLINED(str2);
+ if (inlined_encoding == ENCODING_INLINE_MAX) {
+ rb_enc_set_index(str1, rb_enc_get_index(str2));
+ }
+ else {
+ ENCODING_SET_INLINED(str1, inlined_encoding);
+ }
+}
+
static void
rb_enc_cr_str_copy_for_substr(VALUE dest, VALUE src)
{
@@ -725,25 +740,25 @@ rb_enc_cr_str_copy_for_substr(VALUE dest, VALUE src)
*/
str_enc_copy(dest, src);
if (RSTRING_LEN(dest) == 0) {
- if (!rb_enc_asciicompat(STR_ENC_GET(src)))
- ENC_CODERANGE_SET(dest, ENC_CODERANGE_VALID);
- else
- ENC_CODERANGE_SET(dest, ENC_CODERANGE_7BIT);
- return;
+ if (!rb_enc_asciicompat(STR_ENC_GET(src)))
+ ENC_CODERANGE_SET(dest, ENC_CODERANGE_VALID);
+ else
+ ENC_CODERANGE_SET(dest, ENC_CODERANGE_7BIT);
+ return;
}
switch (ENC_CODERANGE(src)) {
case ENC_CODERANGE_7BIT:
- ENC_CODERANGE_SET(dest, ENC_CODERANGE_7BIT);
- break;
+ ENC_CODERANGE_SET(dest, ENC_CODERANGE_7BIT);
+ break;
case ENC_CODERANGE_VALID:
- if (!rb_enc_asciicompat(STR_ENC_GET(src)) ||
- search_nonascii(RSTRING_PTR(dest), RSTRING_END(dest)))
- ENC_CODERANGE_SET(dest, ENC_CODERANGE_VALID);
- else
- ENC_CODERANGE_SET(dest, ENC_CODERANGE_7BIT);
- break;
+ if (!rb_enc_asciicompat(STR_ENC_GET(src)) ||
+ search_nonascii(RSTRING_PTR(dest), RSTRING_END(dest)))
+ ENC_CODERANGE_SET(dest, ENC_CODERANGE_VALID);
+ else
+ ENC_CODERANGE_SET(dest, ENC_CODERANGE_7BIT);
+ break;
default:
- break;
+ break;
}
}
@@ -755,21 +770,15 @@ rb_enc_cr_str_exact_copy(VALUE dest, VALUE src)
}
static int
-enc_coderange_scan(VALUE str, rb_encoding *enc, int encidx)
+enc_coderange_scan(VALUE str, rb_encoding *enc)
{
- if (rb_enc_mbminlen(enc) > 1 && rb_enc_dummy_p(enc) &&
- rb_enc_mbminlen(enc = get_actual_encoding(encidx, str)) == 1) {
- return ENC_CODERANGE_BROKEN;
- }
- else {
- return coderange_scan(RSTRING_PTR(str), RSTRING_LEN(str), enc);
- }
+ return coderange_scan(RSTRING_PTR(str), RSTRING_LEN(str), enc);
}
int
rb_enc_str_coderange_scan(VALUE str, rb_encoding *enc)
{
- return enc_coderange_scan(str, enc, rb_enc_to_index(enc));
+ return enc_coderange_scan(str, enc);
}
int
@@ -778,9 +787,7 @@ rb_enc_str_coderange(VALUE str)
int cr = ENC_CODERANGE(str);
if (cr == ENC_CODERANGE_UNKNOWN) {
- int encidx = ENCODING_GET(str);
- rb_encoding *enc = rb_enc_from_index(encidx);
- cr = enc_coderange_scan(str, enc, encidx);
+ cr = enc_coderange_scan(str, get_encoding(str));
ENC_CODERANGE_SET(str, cr);
}
return cr;
@@ -793,7 +800,7 @@ rb_enc_str_asciionly_p(VALUE str)
if (!rb_enc_asciicompat(enc))
return FALSE;
- else if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT)
+ else if (is_ascii_string(str))
return TRUE;
return FALSE;
}
@@ -802,7 +809,7 @@ static inline void
str_mod_check(VALUE s, const char *p, long len)
{
if (RSTRING_PTR(s) != p || RSTRING_LEN(s) != len){
- rb_raise(rb_eRuntimeError, "string modified");
+ rb_raise(rb_eRuntimeError, "string modified");
}
}
@@ -810,17 +817,13 @@ static size_t
str_capacity(VALUE str, const int termlen)
{
if (STR_EMBED_P(str)) {
-#if USE_RVARGC
return str_embed_capa(str) - termlen;
-#else
- return (RSTRING_EMBED_LEN_MAX + 1 - termlen);
-#endif
}
else if (FL_TEST(str, STR_SHARED|STR_NOFREE)) {
- return RSTRING(str)->as.heap.len;
+ return RSTRING(str)->len;
}
else {
- return RSTRING(str)->as.heap.aux.capa;
+ return RSTRING(str)->as.heap.aux.capa;
}
}
@@ -834,34 +837,30 @@ static inline void
must_not_null(const char *ptr)
{
if (!ptr) {
- rb_raise(rb_eArgError, "NULL pointer given");
+ rb_raise(rb_eArgError, "NULL pointer given");
}
}
static inline VALUE
-str_alloc(VALUE klass, size_t size)
-{
- assert(size > 0);
- RVARGC_NEWOBJ_OF(str, struct RString, klass,
- T_STRING | (RGENGC_WB_PROTECTED_STRING ? FL_WB_PROTECTED : 0), size);
- return (VALUE)str;
-}
-
-static inline VALUE
str_alloc_embed(VALUE klass, size_t capa)
{
- size_t size = str_embed_size(capa);
- assert(rb_gc_size_allocatable_p(size));
-#if !USE_RVARGC
- assert(size <= sizeof(struct RString));
-#endif
- return str_alloc(klass, size);
+ size_t size = rb_str_embed_size(capa);
+ RUBY_ASSERT(size > 0);
+ RUBY_ASSERT(rb_gc_size_allocatable_p(size));
+
+ NEWOBJ_OF(str, struct RString, klass,
+ T_STRING | (RGENGC_WB_PROTECTED_STRING ? FL_WB_PROTECTED : 0), size, 0);
+
+ return (VALUE)str;
}
static inline VALUE
str_alloc_heap(VALUE klass)
{
- return str_alloc(klass, sizeof(struct RString));
+ NEWOBJ_OF(str, struct RString, klass,
+ T_STRING | STR_NOEMBED | (RGENGC_WB_PROTECTED_STRING ? FL_WB_PROTECTED : 0), sizeof(struct RString), 0);
+
+ return (VALUE)str;
}
static inline VALUE
@@ -879,7 +878,7 @@ str_new0(VALUE klass, const char *ptr, long len, int termlen)
VALUE str;
if (len < 0) {
- rb_raise(rb_eArgError, "negative string size (or size too big)");
+ rb_raise(rb_eArgError, "negative string size (or size too big)");
}
RUBY_DTRACE_CREATE_HOOK(STRING, len);
@@ -892,16 +891,15 @@ str_new0(VALUE klass, const char *ptr, long len, int termlen)
}
else {
str = str_alloc_heap(klass);
- RSTRING(str)->as.heap.aux.capa = len;
+ RSTRING(str)->as.heap.aux.capa = len;
/* :FIXME: @shyouhei guesses `len + termlen` is guaranteed to never
* integer overflow. If we can STATIC_ASSERT that, the following
* mul_add_mul can be reverted to a simple ALLOC_N. */
RSTRING(str)->as.heap.ptr =
rb_xmalloc_mul_add_mul(sizeof(char), len, sizeof(char), termlen);
- STR_SET_NOEMBED(str);
}
if (ptr) {
- memcpy(RSTRING_PTR(str), ptr, len);
+ memcpy(RSTRING_PTR(str), ptr, len);
}
STR_SET_LEN(str, len);
TERM_FILL(RSTRING_PTR(str) + len, termlen);
@@ -981,7 +979,7 @@ rb_enc_str_new_cstr(const char *ptr, rb_encoding *enc)
{
must_not_null(ptr);
if (rb_enc_mbminlen(enc) != 1) {
- rb_raise(rb_eArgError, "wchar encoding given");
+ rb_raise(rb_eArgError, "wchar encoding given");
}
return rb_enc_str_new(ptr, strlen(ptr), enc);
}
@@ -992,21 +990,20 @@ str_new_static(VALUE klass, const char *ptr, long len, int encindex)
VALUE str;
if (len < 0) {
- rb_raise(rb_eArgError, "negative string size (or size too big)");
+ rb_raise(rb_eArgError, "negative string size (or size too big)");
}
if (!ptr) {
- rb_encoding *enc = rb_enc_get_from_index(encindex);
- str = str_new0(klass, ptr, len, rb_enc_mbminlen(enc));
+ rb_encoding *enc = rb_enc_get_from_index(encindex);
+ str = str_new0(klass, ptr, len, rb_enc_mbminlen(enc));
}
else {
- RUBY_DTRACE_CREATE_HOOK(STRING, len);
+ RUBY_DTRACE_CREATE_HOOK(STRING, len);
str = str_alloc_heap(klass);
- RSTRING(str)->as.heap.len = len;
- RSTRING(str)->as.heap.ptr = (char *)ptr;
- RSTRING(str)->as.heap.aux.capa = len;
- STR_SET_NOEMBED(str);
- RBASIC(str)->flags |= STR_NOFREE;
+ RSTRING(str)->len = len;
+ RSTRING(str)->as.heap.ptr = (char *)ptr;
+ RSTRING(str)->as.heap.aux.capa = len;
+ RBASIC(str)->flags |= STR_NOFREE;
}
rb_enc_associate_index(str, encindex);
return str;
@@ -1036,31 +1033,17 @@ rb_enc_str_new_static(const char *ptr, long len, rb_encoding *enc)
return str_new_static(rb_cString, ptr, len, rb_enc_to_index(enc));
}
-VALUE
-rb_tainted_str_new(const char *ptr, long len)
-{
- rb_warn_deprecated_to_remove_at(3.2, "rb_tainted_str_new", NULL);
- return rb_str_new(ptr, len);
-}
-
-VALUE
-rb_tainted_str_new_cstr(const char *ptr)
-{
- rb_warn_deprecated_to_remove_at(3.2, "rb_tainted_str_new_cstr", NULL);
- return rb_str_new_cstr(ptr);
-}
-
static VALUE str_cat_conv_enc_opts(VALUE newstr, long ofs, const char *ptr, long len,
- rb_encoding *from, rb_encoding *to,
- int ecflags, VALUE ecopts);
+ rb_encoding *from, rb_encoding *to,
+ int ecflags, VALUE ecopts);
static inline bool
is_enc_ascii_string(VALUE str, rb_encoding *enc)
{
int encidx = rb_enc_to_index(enc);
if (rb_enc_get_index(str) == encidx)
- return is_ascii_string(str);
- return enc_coderange_scan(str, enc, encidx) == ENC_CODERANGE_7BIT;
+ return is_ascii_string(str);
+ return enc_coderange_scan(str, enc) == ENC_CODERANGE_7BIT;
}
VALUE
@@ -1074,27 +1057,27 @@ rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags,
if (!from) from = rb_enc_get(str);
if (from == to) return str;
if ((rb_enc_asciicompat(to) && is_enc_ascii_string(str, from)) ||
- to == rb_ascii8bit_encoding()) {
- if (STR_ENC_GET(str) != to) {
- str = rb_str_dup(str);
- rb_enc_associate(str, to);
- }
- return str;
+ rb_is_ascii8bit_enc(to)) {
+ if (STR_ENC_GET(str) != to) {
+ str = rb_str_dup(str);
+ rb_enc_associate(str, to);
+ }
+ return str;
}
RSTRING_GETMEM(str, ptr, len);
newstr = str_cat_conv_enc_opts(rb_str_buf_new(len), 0, ptr, len,
- from, to, ecflags, ecopts);
+ from, to, ecflags, ecopts);
if (NIL_P(newstr)) {
- /* some error, return original */
- return str;
+ /* some error, return original */
+ return str;
}
return newstr;
}
VALUE
rb_str_cat_conv_enc_opts(VALUE newstr, long ofs, const char *ptr, long len,
- rb_encoding *from, int ecflags, VALUE ecopts)
+ rb_encoding *from, int ecflags, VALUE ecopts)
{
long olen;
@@ -1103,14 +1086,14 @@ rb_str_cat_conv_enc_opts(VALUE newstr, long ofs, const char *ptr, long len,
rb_raise(rb_eIndexError, "index %ld out of string", ofs);
if (ofs < 0) ofs += olen;
if (!from) {
- STR_SET_LEN(newstr, ofs);
- return rb_str_cat(newstr, ptr, len);
+ STR_SET_LEN(newstr, ofs);
+ return rb_str_cat(newstr, ptr, len);
}
rb_str_modify(newstr);
return str_cat_conv_enc_opts(newstr, ofs, ptr, len, from,
- rb_enc_get(newstr),
- ecflags, ecopts);
+ rb_enc_get(newstr),
+ ecflags, ecopts);
}
VALUE
@@ -1124,8 +1107,8 @@ rb_str_initialize(VALUE str, const char *ptr, long len, rb_encoding *enc)
static VALUE
str_cat_conv_enc_opts(VALUE newstr, long ofs, const char *ptr, long len,
- rb_encoding *from, rb_encoding *to,
- int ecflags, VALUE ecopts)
+ rb_encoding *from, rb_encoding *to,
+ int ecflags, VALUE ecopts)
{
rb_econv_t *ec;
rb_econv_result_t ret;
@@ -1146,35 +1129,35 @@ str_cat_conv_enc_opts(VALUE newstr, long ofs, const char *ptr, long len,
sp = (unsigned char*)ptr;
start = sp;
while ((dest = (unsigned char*)RSTRING_PTR(newstr)),
- (dp = dest + converted_output),
- (ret = rb_econv_convert(ec, &sp, start + len, &dp, dest + olen, 0)),
- ret == econv_destination_buffer_full) {
- /* destination buffer short */
- size_t converted_input = sp - start;
- size_t rest = len - converted_input;
- converted_output = dp - dest;
- rb_str_set_len(newstr, converted_output);
- if (converted_input && converted_output &&
- rest < (LONG_MAX / converted_output)) {
- rest = (rest * converted_output) / converted_input;
- }
- else {
- rest = olen;
- }
- olen += rest < 2 ? 2 : rest;
- rb_str_resize(newstr, olen);
+ (dp = dest + converted_output),
+ (ret = rb_econv_convert(ec, &sp, start + len, &dp, dest + olen, 0)),
+ ret == econv_destination_buffer_full) {
+ /* destination buffer short */
+ size_t converted_input = sp - start;
+ size_t rest = len - converted_input;
+ converted_output = dp - dest;
+ rb_str_set_len(newstr, converted_output);
+ if (converted_input && converted_output &&
+ rest < (LONG_MAX / converted_output)) {
+ rest = (rest * converted_output) / converted_input;
+ }
+ else {
+ rest = olen;
+ }
+ olen += rest < 2 ? 2 : rest;
+ rb_str_resize(newstr, olen);
}
DATA_PTR(econv_wrapper) = 0;
rb_econv_close(ec);
switch (ret) {
case econv_finished:
- len = dp - (unsigned char*)RSTRING_PTR(newstr);
- rb_str_set_len(newstr, len);
- rb_enc_associate(newstr, to);
- return newstr;
+ len = dp - (unsigned char*)RSTRING_PTR(newstr);
+ rb_str_set_len(newstr, len);
+ rb_enc_associate(newstr, to);
+ return newstr;
default:
- return Qnil;
+ return Qnil;
}
}
@@ -1197,7 +1180,7 @@ rb_external_str_new_with_enc(const char *ptr, long len, rb_encoding *eenc)
/* ASCII-8BIT case, no conversion */
if ((eidx == rb_ascii8bit_encindex()) ||
- (eidx == rb_usascii_encindex() && search_nonascii(ptr, ptr + len))) {
+ (eidx == rb_usascii_encindex() && search_nonascii(ptr, ptr + len))) {
return rb_str_new(ptr, len);
}
/* no default_internal or same encoding, no conversion */
@@ -1208,8 +1191,8 @@ rb_external_str_new_with_enc(const char *ptr, long len, rb_encoding *eenc)
/* ASCII compatible, and ASCII only string, no conversion in
* default_internal */
if ((eidx == rb_ascii8bit_encindex()) ||
- (eidx == rb_usascii_encindex()) ||
- (rb_enc_asciicompat(eenc) && !search_nonascii(ptr, ptr + len))) {
+ (eidx == rb_usascii_encindex()) ||
+ (rb_enc_asciicompat(eenc) && !search_nonascii(ptr, ptr + len))) {
return rb_enc_str_new(ptr, len, ienc);
}
/* convert from the given encoding to default_internal */
@@ -1217,7 +1200,7 @@ rb_external_str_new_with_enc(const char *ptr, long len, rb_encoding *eenc)
/* when the conversion failed for some reason, just ignore the
* default_internal and result in the given encoding as-is. */
if (NIL_P(rb_str_cat_conv_enc_opts(str, 0, ptr, len, eenc, 0, Qnil))) {
- rb_str_initialize(str, ptr, len, eenc);
+ rb_str_initialize(str, ptr, len, eenc);
}
return str;
}
@@ -1227,9 +1210,9 @@ rb_external_str_with_enc(VALUE str, rb_encoding *eenc)
{
int eidx = rb_enc_to_index(eenc);
if (eidx == rb_usascii_encindex() &&
- rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
- rb_enc_associate_index(str, rb_ascii8bit_encindex());
- return str;
+ !is_ascii_string(str)) {
+ rb_enc_associate_index(str, rb_ascii8bit_encindex());
+ return str;
}
rb_enc_associate_index(str, eidx);
return rb_str_conv_enc(str, eenc, rb_default_internal_encoding());
@@ -1299,10 +1282,9 @@ str_replace_shared_without_enc(VALUE str2, VALUE str)
RSTRING_GETMEM(str, ptr, len);
if (str_embed_capa(str2) >= len + termlen) {
char *ptr2 = RSTRING(str2)->as.embed.ary;
- STR_SET_EMBED(str2);
- memcpy(ptr2, RSTRING_PTR(str), len);
- STR_SET_EMBED_LEN(str2, len);
- TERM_FILL(ptr2+len, termlen);
+ STR_SET_EMBED(str2);
+ memcpy(ptr2, RSTRING_PTR(str), len);
+ TERM_FILL(ptr2+len, termlen);
}
else {
VALUE root;
@@ -1314,7 +1296,8 @@ str_replace_shared_without_enc(VALUE str2, VALUE str)
root = rb_str_new_frozen(str);
RSTRING_GETMEM(root, ptr, len);
}
- assert(OBJ_FROZEN(root));
+ RUBY_ASSERT(OBJ_FROZEN(root));
+
if (!STR_EMBED_P(str2) && !FL_TEST_RAW(str2, STR_SHARED|STR_NOFREE)) {
if (FL_TEST_RAW(str2, STR_SHARED_ROOT)) {
rb_fatal("about to free a possible shared root");
@@ -1324,11 +1307,13 @@ str_replace_shared_without_enc(VALUE str2, VALUE str)
ruby_sized_xfree(ptr2, STR_HEAP_SIZE(str2));
}
}
- FL_SET(str2, STR_NOEMBED);
- RSTRING(str2)->as.heap.len = len;
- RSTRING(str2)->as.heap.ptr = ptr;
+ FL_SET(str2, STR_NOEMBED);
+ RSTRING(str2)->as.heap.ptr = ptr;
STR_SET_SHARED(str2, root);
}
+
+ STR_SET_LEN(str2, len);
+
return str2;
}
@@ -1355,7 +1340,7 @@ rb_str_new_shared(VALUE str)
VALUE
rb_str_new_frozen(VALUE orig)
{
- if (OBJ_FROZEN(orig)) return orig;
+ if (RB_FL_TEST_RAW(orig, FL_FREEZE | STR_CHILLED) == FL_FREEZE) return orig;
return str_new_frozen(rb_obj_class(orig), orig);
}
@@ -1373,33 +1358,69 @@ rb_str_tmp_frozen_acquire(VALUE orig)
return str_new_frozen_buffer(0, orig, FALSE);
}
+VALUE
+rb_str_tmp_frozen_no_embed_acquire(VALUE orig)
+{
+ if (OBJ_FROZEN_RAW(orig) && !STR_EMBED_P(orig) && !rb_str_reembeddable_p(orig)) return orig;
+ if (STR_SHARED_P(orig) && !STR_EMBED_P(RSTRING(orig)->as.heap.aux.shared)) return rb_str_tmp_frozen_acquire(orig);
+
+ VALUE str = str_alloc_heap(0);
+ OBJ_FREEZE(str);
+ /* Always set the STR_SHARED_ROOT to ensure it does not get re-embedded. */
+ FL_SET(str, STR_SHARED_ROOT);
+
+ size_t capa = str_capacity(orig, TERM_LEN(orig));
+
+ /* If the string is embedded then we want to create a copy that is heap
+ * allocated. If the string is shared then the shared root must be
+ * embedded, so we want to create a copy. If the string is a shared root
+ * then it must be embedded, so we want to create a copy. */
+ if (STR_EMBED_P(orig) || FL_TEST_RAW(orig, STR_SHARED | STR_SHARED_ROOT)) {
+ RSTRING(str)->as.heap.ptr = rb_xmalloc_mul_add_mul(sizeof(char), capa, sizeof(char), TERM_LEN(orig));
+ memcpy(RSTRING(str)->as.heap.ptr, RSTRING_PTR(orig), capa);
+ }
+ else {
+ /* orig must be heap allocated and not shared, so we can safely transfer
+ * the pointer to str. */
+ RSTRING(str)->as.heap.ptr = RSTRING(orig)->as.heap.ptr;
+ RBASIC(str)->flags |= RBASIC(orig)->flags & STR_NOFREE;
+ RBASIC(orig)->flags &= ~STR_NOFREE;
+ STR_SET_SHARED(orig, str);
+ }
+
+ RSTRING(str)->len = RSTRING(orig)->len;
+ RSTRING(str)->as.heap.aux.capa = capa;
+
+ return str;
+}
+
void
rb_str_tmp_frozen_release(VALUE orig, VALUE tmp)
{
if (RBASIC_CLASS(tmp) != 0)
- return;
+ return;
if (STR_EMBED_P(tmp)) {
- assert(OBJ_FROZEN_RAW(tmp));
+ RUBY_ASSERT(OBJ_FROZEN_RAW(tmp));
}
else if (FL_TEST_RAW(orig, STR_SHARED) &&
- !FL_TEST_RAW(orig, STR_TMPLOCK|RUBY_FL_FREEZE)) {
- VALUE shared = RSTRING(orig)->as.heap.aux.shared;
+ !FL_TEST_RAW(orig, STR_TMPLOCK|RUBY_FL_FREEZE)) {
+ VALUE shared = RSTRING(orig)->as.heap.aux.shared;
- if (shared == tmp && !FL_TEST_RAW(tmp, STR_BORROWED)) {
- assert(RSTRING(orig)->as.heap.ptr == RSTRING(tmp)->as.heap.ptr);
- assert(RSTRING(orig)->as.heap.len == RSTRING(tmp)->as.heap.len);
+ if (shared == tmp && !FL_TEST_RAW(tmp, STR_BORROWED)) {
+ RUBY_ASSERT(RSTRING(orig)->as.heap.ptr == RSTRING(tmp)->as.heap.ptr);
+ RUBY_ASSERT(RSTRING_LEN(orig) == RSTRING_LEN(tmp));
/* Unshare orig since the root (tmp) only has this one child. */
- FL_UNSET_RAW(orig, STR_SHARED);
- RSTRING(orig)->as.heap.aux.capa = RSTRING(tmp)->as.heap.aux.capa;
- RBASIC(orig)->flags |= RBASIC(tmp)->flags & STR_NOFREE;
- assert(OBJ_FROZEN_RAW(tmp));
+ FL_UNSET_RAW(orig, STR_SHARED);
+ RSTRING(orig)->as.heap.aux.capa = RSTRING(tmp)->as.heap.aux.capa;
+ RBASIC(orig)->flags |= RBASIC(tmp)->flags & STR_NOFREE;
+ RUBY_ASSERT(OBJ_FROZEN_RAW(tmp));
/* Make tmp embedded and empty so it is safe for sweeping. */
STR_SET_EMBED(tmp);
- STR_SET_EMBED_LEN(tmp, 0);
- }
+ STR_SET_LEN(tmp, 0);
+ }
}
}
@@ -1412,12 +1433,11 @@ str_new_frozen(VALUE klass, VALUE orig)
static VALUE
heap_str_make_shared(VALUE klass, VALUE orig)
{
- assert(!STR_EMBED_P(orig));
- assert(!STR_SHARED_P(orig));
+ RUBY_ASSERT(!STR_EMBED_P(orig));
+ RUBY_ASSERT(!STR_SHARED_P(orig));
VALUE str = str_alloc_heap(klass);
- STR_SET_NOEMBED(str);
- RSTRING(str)->as.heap.len = RSTRING_LEN(orig);
+ STR_SET_LEN(str, RSTRING_LEN(orig));
RSTRING(str)->as.heap.ptr = RSTRING_PTR(orig);
RSTRING(str)->as.heap.aux.capa = RSTRING(orig)->as.heap.aux.capa;
RBASIC(str)->flags |= RBASIC(orig)->flags & STR_NOFREE;
@@ -1434,48 +1454,46 @@ str_new_frozen_buffer(VALUE klass, VALUE orig, int copy_encoding)
VALUE str;
long len = RSTRING_LEN(orig);
+ int termlen = copy_encoding ? TERM_LEN(orig) : 1;
- if (STR_EMBED_P(orig) || STR_EMBEDDABLE_P(len, 1)) {
- str = str_new(klass, RSTRING_PTR(orig), len);
- assert(STR_EMBED_P(str));
+ if (STR_EMBED_P(orig) || STR_EMBEDDABLE_P(len, termlen)) {
+ str = str_new0(klass, RSTRING_PTR(orig), len, termlen);
+ RUBY_ASSERT(STR_EMBED_P(str));
}
else {
- if (FL_TEST_RAW(orig, STR_SHARED)) {
- VALUE shared = RSTRING(orig)->as.heap.aux.shared;
+ if (FL_TEST_RAW(orig, STR_SHARED)) {
+ VALUE shared = RSTRING(orig)->as.heap.aux.shared;
long ofs = RSTRING(orig)->as.heap.ptr - RSTRING_PTR(shared);
- long rest = RSTRING_LEN(shared) - ofs - RSTRING(orig)->as.heap.len;
- assert(ofs >= 0);
- assert(rest >= 0);
- assert(ofs + rest <= RSTRING_LEN(shared));
-#if !USE_RVARGC
- assert(!STR_EMBED_P(shared));
-#endif
- assert(OBJ_FROZEN(shared));
-
- if ((ofs > 0) || (rest > 0) ||
- (klass != RBASIC(shared)->klass) ||
- ENCODING_GET(shared) != ENCODING_GET(orig)) {
- str = str_new_shared(klass, shared);
- assert(!STR_EMBED_P(str));
- RSTRING(str)->as.heap.ptr += ofs;
- RSTRING(str)->as.heap.len -= ofs + rest;
- }
- else {
- if (RBASIC_CLASS(shared) == 0)
- FL_SET_RAW(shared, STR_BORROWED);
- return shared;
- }
- }
+ long rest = RSTRING_LEN(shared) - ofs - RSTRING_LEN(orig);
+ RUBY_ASSERT(ofs >= 0);
+ RUBY_ASSERT(rest >= 0);
+ RUBY_ASSERT(ofs + rest <= RSTRING_LEN(shared));
+ RUBY_ASSERT(OBJ_FROZEN(shared));
+
+ if ((ofs > 0) || (rest > 0) ||
+ (klass != RBASIC(shared)->klass) ||
+ ENCODING_GET(shared) != ENCODING_GET(orig)) {
+ str = str_new_shared(klass, shared);
+ RUBY_ASSERT(!STR_EMBED_P(str));
+ RSTRING(str)->as.heap.ptr += ofs;
+ STR_SET_LEN(str, RSTRING_LEN(str) - (ofs + rest));
+ }
+ else {
+ if (RBASIC_CLASS(shared) == 0)
+ FL_SET_RAW(shared, STR_BORROWED);
+ return shared;
+ }
+ }
else if (STR_EMBEDDABLE_P(RSTRING_LEN(orig), TERM_LEN(orig))) {
str = str_alloc_embed(klass, RSTRING_LEN(orig) + TERM_LEN(orig));
- STR_SET_EMBED(str);
- memcpy(RSTRING_PTR(str), RSTRING_PTR(orig), RSTRING_LEN(orig));
- STR_SET_EMBED_LEN(str, RSTRING_LEN(orig));
- TERM_FILL(RSTRING_END(str), TERM_LEN(orig));
- }
- else {
+ STR_SET_EMBED(str);
+ memcpy(RSTRING_PTR(str), RSTRING_PTR(orig), RSTRING_LEN(orig));
+ STR_SET_LEN(str, RSTRING_LEN(orig));
+ TERM_FILL(RSTRING_END(str), TERM_LEN(orig));
+ }
+ else {
str = heap_str_make_shared(klass, orig);
- }
+ }
}
if (copy_encoding) rb_enc_cr_str_exact_copy(str, orig);
@@ -1498,9 +1516,6 @@ str_new_empty_String(VALUE str)
}
#define STR_BUF_MIN_SIZE 63
-#if !USE_RVARGC
-STATIC_ASSERT(STR_BUF_MIN_SIZE, STR_BUF_MIN_SIZE > RSTRING_EMBED_LEN_MAX);
-#endif
VALUE
rb_str_buf_new(long capa)
@@ -1511,12 +1526,6 @@ rb_str_buf_new(long capa)
VALUE str = str_alloc_heap(rb_cString);
-#if !USE_RVARGC
- if (capa < STR_BUF_MIN_SIZE) {
- capa = STR_BUF_MIN_SIZE;
- }
-#endif
- FL_SET(str, STR_NOEMBED);
RSTRING(str)->as.heap.aux.capa = capa;
RSTRING(str)->as.heap.ptr = ALLOC_N(char, (size_t)capa + 1);
RSTRING(str)->as.heap.ptr[0] = '\0';
@@ -1546,7 +1555,7 @@ void
rb_str_free(VALUE str)
{
if (FL_TEST(str, RSTRING_FSTR)) {
- st_data_t fstr = (st_data_t)str;
+ st_data_t fstr = (st_data_t)str;
RB_VM_LOCK_ENTER();
{
@@ -1557,26 +1566,26 @@ rb_str_free(VALUE str)
}
if (STR_EMBED_P(str)) {
- RB_DEBUG_COUNTER_INC(obj_str_embed);
+ RB_DEBUG_COUNTER_INC(obj_str_embed);
}
else if (FL_TEST(str, STR_SHARED | STR_NOFREE)) {
- (void)RB_DEBUG_COUNTER_INC_IF(obj_str_shared, FL_TEST(str, STR_SHARED));
- (void)RB_DEBUG_COUNTER_INC_IF(obj_str_shared, FL_TEST(str, STR_NOFREE));
+ (void)RB_DEBUG_COUNTER_INC_IF(obj_str_shared, FL_TEST(str, STR_SHARED));
+ (void)RB_DEBUG_COUNTER_INC_IF(obj_str_shared, FL_TEST(str, STR_NOFREE));
}
else {
- RB_DEBUG_COUNTER_INC(obj_str_ptr);
- ruby_sized_xfree(STR_HEAP_PTR(str), STR_HEAP_SIZE(str));
+ RB_DEBUG_COUNTER_INC(obj_str_ptr);
+ ruby_sized_xfree(STR_HEAP_PTR(str), STR_HEAP_SIZE(str));
}
}
-RUBY_FUNC_EXPORTED size_t
+size_t
rb_str_memsize(VALUE str)
{
if (FL_TEST(str, STR_NOEMBED|STR_SHARED|STR_NOFREE) == STR_NOEMBED) {
- return STR_HEAP_SIZE(str);
+ return STR_HEAP_SIZE(str);
}
else {
- return 0;
+ return 0;
}
}
@@ -1608,48 +1617,46 @@ str_shared_replace(VALUE str, VALUE str2)
str_discard(str);
termlen = rb_enc_mbminlen(enc);
+ STR_SET_LEN(str, RSTRING_LEN(str2));
+
if (str_embed_capa(str) >= RSTRING_LEN(str2) + termlen) {
- STR_SET_EMBED(str);
- memcpy(RSTRING_PTR(str), RSTRING_PTR(str2), (size_t)RSTRING_LEN(str2) + termlen);
- STR_SET_EMBED_LEN(str, RSTRING_LEN(str2));
+ STR_SET_EMBED(str);
+ memcpy(RSTRING_PTR(str), RSTRING_PTR(str2), (size_t)RSTRING_LEN(str2) + termlen);
rb_enc_associate(str, enc);
ENC_CODERANGE_SET(str, cr);
}
else {
-#if USE_RVARGC
if (STR_EMBED_P(str2)) {
- assert(!FL_TEST(str2, STR_SHARED));
- long len = RSTRING(str2)->as.embed.len;
- assert(len + termlen <= str_embed_capa(str2));
+ RUBY_ASSERT(!FL_TEST(str2, STR_SHARED));
+ long len = RSTRING_LEN(str2);
+ RUBY_ASSERT(len + termlen <= str_embed_capa(str2));
char *new_ptr = ALLOC_N(char, len + termlen);
memcpy(new_ptr, RSTRING(str2)->as.embed.ary, len + termlen);
RSTRING(str2)->as.heap.ptr = new_ptr;
- RSTRING(str2)->as.heap.len = len;
+ STR_SET_LEN(str2, len);
RSTRING(str2)->as.heap.aux.capa = len;
STR_SET_NOEMBED(str2);
}
-#endif
- STR_SET_NOEMBED(str);
- FL_UNSET(str, STR_SHARED);
- RSTRING(str)->as.heap.ptr = RSTRING_PTR(str2);
- RSTRING(str)->as.heap.len = RSTRING_LEN(str2);
+ STR_SET_NOEMBED(str);
+ FL_UNSET(str, STR_SHARED);
+ RSTRING(str)->as.heap.ptr = RSTRING_PTR(str2);
- if (FL_TEST(str2, STR_SHARED)) {
- VALUE shared = RSTRING(str2)->as.heap.aux.shared;
- STR_SET_SHARED(str, shared);
- }
- else {
- RSTRING(str)->as.heap.aux.capa = RSTRING(str2)->as.heap.aux.capa;
- }
+ if (FL_TEST(str2, STR_SHARED)) {
+ VALUE shared = RSTRING(str2)->as.heap.aux.shared;
+ STR_SET_SHARED(str, shared);
+ }
+ else {
+ RSTRING(str)->as.heap.aux.capa = RSTRING(str2)->as.heap.aux.capa;
+ }
- /* abandon str2 */
- STR_SET_EMBED(str2);
- RSTRING_PTR(str2)[0] = 0;
- STR_SET_EMBED_LEN(str2, 0);
- rb_enc_associate(str, enc);
- ENC_CODERANGE_SET(str, cr);
+ /* abandon str2 */
+ STR_SET_EMBED(str2);
+ RSTRING_PTR(str2)[0] = 0;
+ STR_SET_LEN(str2, 0);
+ rb_enc_associate(str, enc);
+ ENC_CODERANGE_SET(str, cr);
}
}
@@ -1659,17 +1666,17 @@ rb_obj_as_string(VALUE obj)
VALUE str;
if (RB_TYPE_P(obj, T_STRING)) {
- return obj;
+ return obj;
}
str = rb_funcall(obj, idTo_s, 0);
return rb_obj_as_string_result(str, obj);
}
-MJIT_FUNC_EXPORTED VALUE
+VALUE
rb_obj_as_string_result(VALUE str, VALUE obj)
{
if (!RB_TYPE_P(str, T_STRING))
- return rb_any_to_s(obj);
+ return rb_any_to_s(obj);
return str;
}
@@ -1680,64 +1687,57 @@ str_replace(VALUE str, VALUE str2)
len = RSTRING_LEN(str2);
if (STR_SHARED_P(str2)) {
- VALUE shared = RSTRING(str2)->as.heap.aux.shared;
- assert(OBJ_FROZEN(shared));
- STR_SET_NOEMBED(str);
- RSTRING(str)->as.heap.len = len;
- RSTRING(str)->as.heap.ptr = RSTRING_PTR(str2);
- STR_SET_SHARED(str, shared);
- rb_enc_cr_str_exact_copy(str, str2);
+ VALUE shared = RSTRING(str2)->as.heap.aux.shared;
+ RUBY_ASSERT(OBJ_FROZEN(shared));
+ STR_SET_NOEMBED(str);
+ STR_SET_LEN(str, len);
+ RSTRING(str)->as.heap.ptr = RSTRING_PTR(str2);
+ STR_SET_SHARED(str, shared);
+ rb_enc_cr_str_exact_copy(str, str2);
}
else {
- str_replace_shared(str, str2);
+ str_replace_shared(str, str2);
}
return str;
}
static inline VALUE
-ec_str_alloc(struct rb_execution_context_struct *ec, VALUE klass, size_t size)
-{
- assert(size > 0);
- RB_RVARGC_EC_NEWOBJ_OF(ec, str, struct RString, klass,
- T_STRING | (RGENGC_WB_PROTECTED_STRING ? FL_WB_PROTECTED : 0), size);
- return (VALUE)str;
-}
-
-static inline VALUE
ec_str_alloc_embed(struct rb_execution_context_struct *ec, VALUE klass, size_t capa)
{
- size_t size = str_embed_size(capa);
- assert(rb_gc_size_allocatable_p(size));
-#if !USE_RVARGC
- assert(size <= sizeof(struct RString));
-#endif
- return ec_str_alloc(ec, klass, size);
+ size_t size = rb_str_embed_size(capa);
+ RUBY_ASSERT(size > 0);
+ RUBY_ASSERT(rb_gc_size_allocatable_p(size));
+
+ NEWOBJ_OF(str, struct RString, klass,
+ T_STRING | (RGENGC_WB_PROTECTED_STRING ? FL_WB_PROTECTED : 0), size, ec);
+
+ return (VALUE)str;
}
static inline VALUE
ec_str_alloc_heap(struct rb_execution_context_struct *ec, VALUE klass)
{
- return ec_str_alloc(ec, klass, sizeof(struct RString));
+ NEWOBJ_OF(str, struct RString, klass,
+ T_STRING | STR_NOEMBED | (RGENGC_WB_PROTECTED_STRING ? FL_WB_PROTECTED : 0), sizeof(struct RString), ec);
+
+ return (VALUE)str;
}
static inline VALUE
str_duplicate_setup(VALUE klass, VALUE str, VALUE dup)
{
const VALUE flag_mask =
-#if !USE_RVARGC
- RSTRING_NOEMBED | RSTRING_EMBED_LEN_MASK |
-#endif
ENC_CODERANGE_MASK | ENCODING_MASK |
FL_FREEZE
- ;
+ ;
VALUE flags = FL_TEST_RAW(str, flag_mask);
int encidx = 0;
if (STR_EMBED_P(str)) {
- long len = RSTRING_EMBED_LEN(str);
+ long len = RSTRING_LEN(str);
- assert(str_embed_capa(dup) >= len + 1);
- STR_SET_EMBED_LEN(dup, len);
+ RUBY_ASSERT(STR_EMBED_P(dup));
+ RUBY_ASSERT(str_embed_capa(dup) >= len + 1);
MEMCPY(RSTRING(dup)->as.embed.ary, RSTRING(str)->as.embed.ary, char, len + 1);
}
else {
@@ -1749,27 +1749,20 @@ str_duplicate_setup(VALUE klass, VALUE str, VALUE dup)
root = str = str_new_frozen(klass, str);
flags = FL_TEST_RAW(str, flag_mask);
}
- assert(!STR_SHARED_P(root));
- assert(RB_OBJ_FROZEN_RAW(root));
-#if USE_RVARGC
- if (1) {
-#else
- if (STR_EMBED_P(root)) {
- MEMCPY(RSTRING(dup)->as.embed.ary, RSTRING(root)->as.embed.ary,
- char, RSTRING_EMBED_LEN_MAX + 1);
- }
- else {
-#endif
- RSTRING(dup)->as.heap.len = RSTRING_LEN(str);
- RSTRING(dup)->as.heap.ptr = RSTRING_PTR(str);
- RB_OBJ_WRITE(dup, &RSTRING(dup)->as.heap.aux.shared, root);
- flags |= RSTRING_NOEMBED | STR_SHARED;
- }
+ RUBY_ASSERT(!STR_SHARED_P(root));
+ RUBY_ASSERT(RB_OBJ_FROZEN_RAW(root));
+
+ RSTRING(dup)->as.heap.ptr = RSTRING_PTR(str);
+ FL_SET(root, STR_SHARED_ROOT);
+ RB_OBJ_WRITE(dup, &RSTRING(dup)->as.heap.aux.shared, root);
+ flags |= RSTRING_NOEMBED | STR_SHARED;
}
+ STR_SET_LEN(dup, RSTRING_LEN(str));
+
if ((flags & ENCODING_MASK) == (ENCODING_INLINE_MAX<<ENCODING_SHIFT)) {
- encidx = rb_enc_get_index(str);
- flags &= ~ENCODING_MASK;
+ encidx = rb_enc_get_index(str);
+ flags &= ~ENCODING_MASK;
}
FL_SET_RAW(dup, flags & ~FL_FREEZE);
if (encidx) rb_enc_associate_index(dup, encidx);
@@ -1780,11 +1773,11 @@ static inline VALUE
ec_str_duplicate(struct rb_execution_context_struct *ec, VALUE klass, VALUE str)
{
VALUE dup;
- if (!USE_RVARGC || FL_TEST(str, STR_NOEMBED)) {
- dup = ec_str_alloc_heap(ec, klass);
+ if (STR_EMBED_P(str)) {
+ dup = ec_str_alloc_embed(ec, klass, RSTRING_LEN(str) + TERM_LEN(str));
}
else {
- dup = ec_str_alloc_embed(ec, klass, RSTRING_EMBED_LEN(str) + TERM_LEN(str));
+ dup = ec_str_alloc_heap(ec, klass);
}
return str_duplicate_setup(klass, str, dup);
@@ -1794,11 +1787,11 @@ static inline VALUE
str_duplicate(VALUE klass, VALUE str)
{
VALUE dup;
- if (!USE_RVARGC || FL_TEST(str, STR_NOEMBED)) {
- dup = str_alloc_heap(klass);
+ if (STR_EMBED_P(str)) {
+ dup = str_alloc_embed(klass, RSTRING_LEN(str) + TERM_LEN(str));
}
else {
- dup = str_alloc_embed(klass, RSTRING_EMBED_LEN(str) + TERM_LEN(str));
+ dup = str_alloc_heap(klass);
}
return str_duplicate_setup(klass, str, dup);
@@ -1810,6 +1803,18 @@ rb_str_dup(VALUE str)
return str_duplicate(rb_obj_class(str), str);
}
+/* :nodoc: */
+VALUE
+rb_str_dup_m(VALUE str)
+{
+ if (LIKELY(BARE_STRING_P(str))) {
+ return str_duplicate(rb_obj_class(str), str);
+ }
+ else {
+ return rb_obj_dup(str);
+ }
+}
+
VALUE
rb_str_resurrect(VALUE str)
{
@@ -1818,55 +1823,28 @@ rb_str_resurrect(VALUE str)
}
VALUE
-rb_ec_str_resurrect(struct rb_execution_context_struct *ec, VALUE str)
+rb_ec_str_resurrect(struct rb_execution_context_struct *ec, VALUE str, bool chilled)
{
RUBY_DTRACE_CREATE_HOOK(STRING, RSTRING_LEN(str));
- return ec_str_duplicate(ec, rb_cString, str);
+ VALUE new_str = ec_str_duplicate(ec, rb_cString, str);
+ if (chilled) {
+ STR_CHILL_RAW(new_str);
+ }
+ return new_str;
+}
+
+bool
+rb_str_chilled_p(VALUE str)
+{
+ return CHILLED_STRING_P(str);
}
/*
- * call-seq:
- * String.new(string = '') -> new_string
- * String.new(string = '', encoding: encoding) -> new_string
- * String.new(string = '', capacity: size) -> new_string
- *
- * Returns a new \String that is a copy of +string+.
- *
- * With no arguments, returns the empty string with the Encoding <tt>ASCII-8BIT</tt>:
- * s = String.new
- * s # => ""
- * s.encoding # => #<Encoding:ASCII-8BIT>
*
- * With the single \String argument +string+, returns a copy of +string+
- * with the same encoding as +string+:
- * s = String.new("Que veut dire \u{e7}a?")
- * s # => "Que veut dire \u{e7}a?"
- * s.encoding # => #<Encoding:UTF-8>
- *
- * Literal strings like <tt>""</tt> or here-documents always use
- * {script encoding}[Encoding.html#class-Encoding-label-Script+encoding], unlike String.new.
- *
- * With keyword +encoding+, returns a copy of +str+
- * with the specified encoding:
- * s = String.new(encoding: 'ASCII')
- * s.encoding # => #<Encoding:US-ASCII>
- * s = String.new('foo', encoding: 'ASCII')
- * s.encoding # => #<Encoding:US-ASCII>
- *
- * Note that these are equivalent:
- * s0 = String.new('foo', encoding: 'ASCII')
- * s1 = 'foo'.force_encoding('ASCII')
- * s0.encoding == s1.encoding # => true
- *
- * With keyword +capacity+, returns a copy of +str+;
- * the given +capacity+ may set the size of the internal buffer,
- * which may affect performance:
- * String.new(capacity: 1) # => ""
- * String.new(capacity: 4096) # => ""
- *
- * The +string+, +encoding+, and +capacity+ arguments may all be used together:
+ * call-seq:
+ * String.new(string = '', **opts) -> new_string
*
- * String.new('hello', encoding: 'UTF-8', capacity: 25)
+ * :include: doc/string/new.rdoc
*
*/
@@ -1880,81 +1858,165 @@ rb_str_init(int argc, VALUE *argv, VALUE str)
int n;
if (!keyword_ids[0]) {
- keyword_ids[0] = rb_id_encoding();
- CONST_ID(keyword_ids[1], "capacity");
+ keyword_ids[0] = rb_id_encoding();
+ CONST_ID(keyword_ids[1], "capacity");
}
n = rb_scan_args(argc, argv, "01:", &orig, &opt);
if (!NIL_P(opt)) {
- rb_get_kwargs(opt, keyword_ids, 0, 2, kwargs);
- venc = kwargs[0];
- vcapa = kwargs[1];
- if (venc != Qundef && !NIL_P(venc)) {
- enc = rb_to_encoding(venc);
- }
- if (vcapa != Qundef && !NIL_P(vcapa)) {
- long capa = NUM2LONG(vcapa);
- long len = 0;
- int termlen = enc ? rb_enc_mbminlen(enc) : 1;
-
- if (capa < STR_BUF_MIN_SIZE) {
- capa = STR_BUF_MIN_SIZE;
- }
- if (n == 1) {
- StringValue(orig);
- len = RSTRING_LEN(orig);
- if (capa < len) {
- capa = len;
- }
- if (orig == str) n = 0;
- }
- str_modifiable(str);
- if (STR_EMBED_P(str)) { /* make noembed always */
- char *new_ptr = ALLOC_N(char, (size_t)capa + termlen);
-#if USE_RVARGC
- assert(RSTRING(str)->as.embed.len + 1 <= str_embed_capa(str));
- memcpy(new_ptr, RSTRING(str)->as.embed.ary, RSTRING(str)->as.embed.len + 1);
-#else
- memcpy(new_ptr, RSTRING(str)->as.embed.ary, RSTRING_EMBED_LEN_MAX + 1);
-#endif
- RSTRING(str)->as.heap.ptr = new_ptr;
+ rb_get_kwargs(opt, keyword_ids, 0, 2, kwargs);
+ venc = kwargs[0];
+ vcapa = kwargs[1];
+ if (!UNDEF_P(venc) && !NIL_P(venc)) {
+ enc = rb_to_encoding(venc);
+ }
+ if (!UNDEF_P(vcapa) && !NIL_P(vcapa)) {
+ long capa = NUM2LONG(vcapa);
+ long len = 0;
+ int termlen = enc ? rb_enc_mbminlen(enc) : 1;
+
+ if (capa < STR_BUF_MIN_SIZE) {
+ capa = STR_BUF_MIN_SIZE;
+ }
+ if (n == 1) {
+ StringValue(orig);
+ len = RSTRING_LEN(orig);
+ if (capa < len) {
+ capa = len;
+ }
+ if (orig == str) n = 0;
}
- else if (FL_TEST(str, STR_SHARED|STR_NOFREE)) {
+ str_modifiable(str);
+ if (STR_EMBED_P(str) || FL_TEST(str, STR_SHARED|STR_NOFREE)) {
+ /* make noembed always */
const size_t size = (size_t)capa + termlen;
const char *const old_ptr = RSTRING_PTR(str);
- const size_t osize = RSTRING(str)->as.heap.len + TERM_LEN(str);
- char *new_ptr = ALLOC_N(char, (size_t)capa + termlen);
+ const size_t osize = RSTRING_LEN(str) + TERM_LEN(str);
+ char *new_ptr = ALLOC_N(char, size);
+ if (STR_EMBED_P(str)) RUBY_ASSERT((long)osize <= str_embed_capa(str));
memcpy(new_ptr, old_ptr, osize < size ? osize : size);
FL_UNSET_RAW(str, STR_SHARED|STR_NOFREE);
RSTRING(str)->as.heap.ptr = new_ptr;
- }
- else if (STR_HEAP_SIZE(str) != (size_t)capa + termlen) {
- SIZED_REALLOC_N(RSTRING(str)->as.heap.ptr, char,
- (size_t)capa + termlen, STR_HEAP_SIZE(str));
- }
- RSTRING(str)->as.heap.len = len;
- TERM_FILL(&RSTRING(str)->as.heap.ptr[len], termlen);
- if (n == 1) {
- memcpy(RSTRING(str)->as.heap.ptr, RSTRING_PTR(orig), len);
- rb_enc_cr_str_exact_copy(str, orig);
- }
- FL_SET(str, STR_NOEMBED);
- RSTRING(str)->as.heap.aux.capa = capa;
- }
- else if (n == 1) {
- rb_str_replace(str, orig);
- }
- if (enc) {
- rb_enc_associate(str, enc);
- ENC_CODERANGE_CLEAR(str);
- }
+ }
+ else if (STR_HEAP_SIZE(str) != (size_t)capa + termlen) {
+ SIZED_REALLOC_N(RSTRING(str)->as.heap.ptr, char,
+ (size_t)capa + termlen, STR_HEAP_SIZE(str));
+ }
+ STR_SET_LEN(str, len);
+ TERM_FILL(&RSTRING(str)->as.heap.ptr[len], termlen);
+ if (n == 1) {
+ memcpy(RSTRING(str)->as.heap.ptr, RSTRING_PTR(orig), len);
+ rb_enc_cr_str_exact_copy(str, orig);
+ }
+ FL_SET(str, STR_NOEMBED);
+ RSTRING(str)->as.heap.aux.capa = capa;
+ }
+ else if (n == 1) {
+ rb_str_replace(str, orig);
+ }
+ if (enc) {
+ rb_enc_associate(str, enc);
+ ENC_CODERANGE_CLEAR(str);
+ }
}
else if (n == 1) {
- rb_str_replace(str, orig);
+ rb_str_replace(str, orig);
}
return str;
}
+/* :nodoc: */
+static VALUE
+rb_str_s_new(int argc, VALUE *argv, VALUE klass)
+{
+ if (klass != rb_cString) {
+ return rb_class_new_instance_pass_kw(argc, argv, klass);
+ }
+
+ static ID keyword_ids[2];
+ VALUE orig, opt, encoding = Qnil, capacity = Qnil;
+ VALUE kwargs[2];
+ rb_encoding *enc = NULL;
+
+ int n = rb_scan_args(argc, argv, "01:", &orig, &opt);
+ if (NIL_P(opt)) {
+ return rb_class_new_instance_pass_kw(argc, argv, klass);
+ }
+
+ keyword_ids[0] = rb_id_encoding();
+ CONST_ID(keyword_ids[1], "capacity");
+ rb_get_kwargs(opt, keyword_ids, 0, 2, kwargs);
+ encoding = kwargs[0];
+ capacity = kwargs[1];
+
+ int termlen = 1;
+
+ if (n == 1) {
+ orig = StringValue(orig);
+ }
+ else {
+ orig = Qnil;
+ }
+
+ if (UNDEF_P(encoding)) {
+ if (!NIL_P(orig)) {
+ encoding = rb_obj_encoding(orig);
+ }
+ }
+
+ if (!UNDEF_P(encoding)) {
+ enc = rb_to_encoding(encoding);
+ termlen = rb_enc_mbminlen(enc);
+ }
+
+ // If capacity is nil, we're basically just duping `orig`.
+ if (UNDEF_P(capacity)) {
+ if (NIL_P(orig)) {
+ VALUE empty_str = str_new(klass, "", 0);
+ if (enc) {
+ rb_enc_associate(empty_str, enc);
+ }
+ return empty_str;
+ }
+ VALUE copy = str_duplicate(klass, orig);
+ rb_enc_associate(copy, enc);
+ ENC_CODERANGE_CLEAR(copy);
+ return copy;
+ }
+
+ long capa = 0;
+ capa = NUM2LONG(capacity);
+ if (capa < 0) {
+ capa = 0;
+ }
+
+ if (!NIL_P(orig)) {
+ long orig_capa = rb_str_capacity(orig);
+ if (orig_capa > capa) {
+ capa = orig_capa;
+ }
+ }
+
+ long fake_len = capa - termlen;
+ if (fake_len < 0) {
+ fake_len = 0;
+ }
+
+ VALUE str = str_new0(klass, NULL, fake_len, termlen);
+ STR_SET_LEN(str, 0);
+ TERM_FILL(RSTRING_PTR(str), termlen);
+
+ if (enc) {
+ rb_enc_associate(str, enc);
+ }
+
+ if (!NIL_P(orig)) {
+ rb_str_buf_append(str, orig);
+ }
+
+ return str;
+}
+
#ifdef NONASCII_MASK
#define is_utf8_lead_byte(c) (((c)&0xC0) != 0x80)
@@ -2002,62 +2064,62 @@ enc_strlen(const char *p, const char *e, rb_encoding *enc, int cr)
const char *q;
if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) {
- long diff = (long)(e - p);
- return diff / rb_enc_mbminlen(enc) + !!(diff % rb_enc_mbminlen(enc));
+ long diff = (long)(e - p);
+ return diff / rb_enc_mbminlen(enc) + !!(diff % rb_enc_mbminlen(enc));
}
#ifdef NONASCII_MASK
else if (cr == ENC_CODERANGE_VALID && enc == rb_utf8_encoding()) {
- uintptr_t len = 0;
- if ((int)sizeof(uintptr_t) * 2 < e - p) {
- const uintptr_t *s, *t;
- const uintptr_t lowbits = sizeof(uintptr_t) - 1;
- s = (const uintptr_t*)(~lowbits & ((uintptr_t)p + lowbits));
- t = (const uintptr_t*)(~lowbits & (uintptr_t)e);
- while (p < (const char *)s) {
- if (is_utf8_lead_byte(*p)) len++;
- p++;
- }
- while (s < t) {
- len += count_utf8_lead_bytes_with_word(s);
- s++;
- }
- p = (const char *)s;
- }
- while (p < e) {
- if (is_utf8_lead_byte(*p)) len++;
- p++;
- }
- return (long)len;
+ uintptr_t len = 0;
+ if ((int)sizeof(uintptr_t) * 2 < e - p) {
+ const uintptr_t *s, *t;
+ const uintptr_t lowbits = sizeof(uintptr_t) - 1;
+ s = (const uintptr_t*)(~lowbits & ((uintptr_t)p + lowbits));
+ t = (const uintptr_t*)(~lowbits & (uintptr_t)e);
+ while (p < (const char *)s) {
+ if (is_utf8_lead_byte(*p)) len++;
+ p++;
+ }
+ while (s < t) {
+ len += count_utf8_lead_bytes_with_word(s);
+ s++;
+ }
+ p = (const char *)s;
+ }
+ while (p < e) {
+ if (is_utf8_lead_byte(*p)) len++;
+ p++;
+ }
+ return (long)len;
}
#endif
else if (rb_enc_asciicompat(enc)) {
c = 0;
- if (ENC_CODERANGE_CLEAN_P(cr)) {
- while (p < e) {
- if (ISASCII(*p)) {
- q = search_nonascii(p, e);
- if (!q)
- return c + (e - p);
- c += q - p;
- p = q;
- }
- p += rb_enc_fast_mbclen(p, e, enc);
- c++;
- }
- }
- else {
- while (p < e) {
- if (ISASCII(*p)) {
- q = search_nonascii(p, e);
- if (!q)
- return c + (e - p);
- c += q - p;
- p = q;
- }
- p += rb_enc_mbclen(p, e, enc);
- c++;
- }
- }
+ if (ENC_CODERANGE_CLEAN_P(cr)) {
+ while (p < e) {
+ if (ISASCII(*p)) {
+ q = search_nonascii(p, e);
+ if (!q)
+ return c + (e - p);
+ c += q - p;
+ p = q;
+ }
+ p += rb_enc_fast_mbclen(p, e, enc);
+ c++;
+ }
+ }
+ else {
+ while (p < e) {
+ if (ISASCII(*p)) {
+ q = search_nonascii(p, e);
+ if (!q)
+ return c + (e - p);
+ c += q - p;
+ p = q;
+ }
+ p += rb_enc_mbclen(p, e, enc);
+ c++;
+ }
+ }
return c;
}
@@ -2085,49 +2147,49 @@ rb_enc_strlen_cr(const char *p, const char *e, rb_encoding *enc, int *cr)
*cr = 0;
if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) {
- long diff = (long)(e - p);
- return diff / rb_enc_mbminlen(enc) + !!(diff % rb_enc_mbminlen(enc));
+ long diff = (long)(e - p);
+ return diff / rb_enc_mbminlen(enc) + !!(diff % rb_enc_mbminlen(enc));
}
else if (rb_enc_asciicompat(enc)) {
- c = 0;
- while (p < e) {
- if (ISASCII(*p)) {
- q = search_nonascii(p, e);
- if (!q) {
- if (!*cr) *cr = ENC_CODERANGE_7BIT;
- return c + (e - p);
- }
- c += q - p;
- p = q;
- }
- ret = rb_enc_precise_mbclen(p, e, enc);
- if (MBCLEN_CHARFOUND_P(ret)) {
- *cr |= ENC_CODERANGE_VALID;
- p += MBCLEN_CHARFOUND_LEN(ret);
- }
- else {
- *cr = ENC_CODERANGE_BROKEN;
- p++;
- }
- c++;
- }
- if (!*cr) *cr = ENC_CODERANGE_7BIT;
- return c;
+ c = 0;
+ while (p < e) {
+ if (ISASCII(*p)) {
+ q = search_nonascii(p, e);
+ if (!q) {
+ if (!*cr) *cr = ENC_CODERANGE_7BIT;
+ return c + (e - p);
+ }
+ c += q - p;
+ p = q;
+ }
+ ret = rb_enc_precise_mbclen(p, e, enc);
+ if (MBCLEN_CHARFOUND_P(ret)) {
+ *cr |= ENC_CODERANGE_VALID;
+ p += MBCLEN_CHARFOUND_LEN(ret);
+ }
+ else {
+ *cr = ENC_CODERANGE_BROKEN;
+ p++;
+ }
+ c++;
+ }
+ if (!*cr) *cr = ENC_CODERANGE_7BIT;
+ return c;
}
for (c=0; p<e; c++) {
- ret = rb_enc_precise_mbclen(p, e, enc);
- if (MBCLEN_CHARFOUND_P(ret)) {
- *cr |= ENC_CODERANGE_VALID;
- p += MBCLEN_CHARFOUND_LEN(ret);
- }
- else {
- *cr = ENC_CODERANGE_BROKEN;
+ ret = rb_enc_precise_mbclen(p, e, enc);
+ if (MBCLEN_CHARFOUND_P(ret)) {
+ *cr |= ENC_CODERANGE_VALID;
+ p += MBCLEN_CHARFOUND_LEN(ret);
+ }
+ else {
+ *cr = ENC_CODERANGE_BROKEN;
if (p + rb_enc_mbminlen(enc) <= e)
p += rb_enc_mbminlen(enc);
else
p = e;
- }
+ }
}
if (!*cr) *cr = ENC_CODERANGE_7BIT;
return c;
@@ -2147,12 +2209,12 @@ str_strlen(VALUE str, rb_encoding *enc)
cr = ENC_CODERANGE(str);
if (cr == ENC_CODERANGE_UNKNOWN) {
- long n = rb_enc_strlen_cr(p, e, enc, &cr);
- if (cr) ENC_CODERANGE_SET(str, cr);
- return n;
+ long n = rb_enc_strlen_cr(p, e, enc, &cr);
+ if (cr) ENC_CODERANGE_SET(str, cr);
+ return n;
}
else {
- return enc_strlen(p, e, enc, cr);
+ return enc_strlen(p, e, enc, cr);
}
}
@@ -2166,14 +2228,8 @@ rb_str_strlen(VALUE str)
* call-seq:
* length -> integer
*
- * Returns the count of characters (not bytes) in +self+:
- *
- * "\x80\u3042".length # => 2
- * "hello".length # => 5
+ * :include: doc/string/length.rdoc
*
- * String#size is an alias for String#length.
- *
- * Related: String#bytesize.
*/
VALUE
@@ -2186,15 +2242,11 @@ rb_str_length(VALUE str)
* call-seq:
* bytesize -> integer
*
- * Returns the count of bytes in +self+:
- *
- * "\x80\u3042".bytesize # => 4
- * "hello".bytesize # => 5
+ * :include: doc/string/bytesize.rdoc
*
- * Related: String#length.
*/
-static VALUE
+VALUE
rb_str_bytesize(VALUE str)
{
return LONG2NUM(RSTRING_LEN(str));
@@ -2222,7 +2274,7 @@ rb_str_empty(VALUE str)
* call-seq:
* string + other_string -> new_string
*
- * Returns a new \String containing +other_string+ concatenated to +self+:
+ * Returns a new +String+ containing +other_string+ concatenated to +self+:
*
* "Hello from " + self.to_s # => "Hello from main"
*
@@ -2243,7 +2295,7 @@ rb_str_plus(VALUE str1, VALUE str2)
RSTRING_GETMEM(str2, ptr2, len2);
termlen = rb_enc_mbminlen(enc);
if (len1 > LONG_MAX - len2) {
- rb_raise(rb_eArgError, "string size too big");
+ rb_raise(rb_eArgError, "string size too big");
}
str3 = str_new0(rb_cString, 0, len1+len2, termlen);
ptr3 = RSTRING_PTR(str3);
@@ -2252,18 +2304,18 @@ rb_str_plus(VALUE str1, VALUE str2)
TERM_FILL(&ptr3[len1+len2], termlen);
ENCODING_CODERANGE_SET(str3, rb_enc_to_index(enc),
- ENC_CODERANGE_AND(ENC_CODERANGE(str1), ENC_CODERANGE(str2)));
+ ENC_CODERANGE_AND(ENC_CODERANGE(str1), ENC_CODERANGE(str2)));
RB_GC_GUARD(str1);
RB_GC_GUARD(str2);
return str3;
}
/* A variant of rb_str_plus that does not raise but return Qundef instead. */
-MJIT_FUNC_EXPORTED VALUE
+VALUE
rb_str_opt_plus(VALUE str1, VALUE str2)
{
- assert(RBASIC_CLASS(str1) == rb_cString);
- assert(RBASIC_CLASS(str2) == rb_cString);
+ RUBY_ASSERT(RBASIC_CLASS(str1) == rb_cString);
+ RUBY_ASSERT(RBASIC_CLASS(str2) == rb_cString);
long len1, len2;
MAYBE_UNUSED(char) *ptr1, *ptr2;
RSTRING_GETMEM(str1, ptr1, len1);
@@ -2293,7 +2345,7 @@ rb_str_opt_plus(VALUE str1, VALUE str2)
* call-seq:
* string * integer -> new_string
*
- * Returns a new \String containing +integer+ copies of +self+:
+ * Returns a new +String+ containing +integer+ copies of +self+:
*
* "Ho! " * 3 # => "Ho! Ho! Ho! "
* "Ho! " * 0 # => ""
@@ -2313,12 +2365,12 @@ rb_str_times(VALUE str, VALUE times)
}
if (times == INT2FIX(0)) {
str2 = str_alloc_embed(rb_cString, 0);
- rb_enc_copy(str2, str);
- return str2;
+ rb_enc_copy(str2, str);
+ return str2;
}
len = NUM2LONG(times);
if (len < 0) {
- rb_raise(rb_eArgError, "negative argument");
+ rb_raise(rb_eArgError, "negative argument");
}
if (RSTRING_LEN(str) == 1 && RSTRING_PTR(str)[0] == 0) {
if (STR_EMBEDDABLE_P(len, 1)) {
@@ -2329,14 +2381,13 @@ rb_str_times(VALUE str, VALUE times)
str2 = str_alloc_heap(rb_cString);
RSTRING(str2)->as.heap.aux.capa = len;
RSTRING(str2)->as.heap.ptr = ZALLOC_N(char, (size_t)len + 1);
- STR_SET_NOEMBED(str2);
}
STR_SET_LEN(str2, len);
rb_enc_copy(str2, str);
return str2;
}
if (len && LONG_MAX/len < RSTRING_LEN(str)) {
- rb_raise(rb_eArgError, "argument too big");
+ rb_raise(rb_eArgError, "argument too big");
}
len *= RSTRING_LEN(str);
@@ -2369,7 +2420,7 @@ rb_str_times(VALUE str, VALUE times)
* "%05d" % 123 # => "00123"
*
* If +self+ contains multiple substitutions, +object+ must be
- * an \Array or \Hash containing the values to be substituted:
+ * an Array or Hash containing the values to be substituted:
*
* "%-5s: %016x" % [ "ID", self.object_id ] # => "ID : 00002b054ec93168"
* "foo = %{foo}" % {foo: 'bar'} # => "foo = bar"
@@ -2392,7 +2443,7 @@ static inline void
rb_check_lockedtmp(VALUE str)
{
if (FL_TEST(str, STR_TMPLOCK)) {
- rb_raise(rb_eRuntimeError, "can't modify string; temporarily locked");
+ rb_raise(rb_eRuntimeError, "can't modify string; temporarily locked");
}
}
@@ -2407,10 +2458,10 @@ static inline int
str_dependent_p(VALUE str)
{
if (STR_EMBED_P(str) || !FL_TEST(str, STR_SHARED|STR_NOFREE)) {
- return 0;
+ return 0;
}
else {
- return 1;
+ return 1;
}
}
@@ -2431,18 +2482,18 @@ str_make_independent_expand(VALUE str, long len, long expand, const int termlen)
if (len > capa) len = capa;
if (!STR_EMBED_P(str) && str_embed_capa(str) >= capa + termlen) {
- ptr = RSTRING(str)->as.heap.ptr;
- STR_SET_EMBED(str);
+ ptr = RSTRING(str)->as.heap.ptr;
+ STR_SET_EMBED(str);
memcpy(RSTRING(str)->as.embed.ary, ptr, len);
TERM_FILL(RSTRING(str)->as.embed.ary + len, termlen);
- STR_SET_EMBED_LEN(str, len);
- return;
+ STR_SET_LEN(str, len);
+ return;
}
ptr = ALLOC_N(char, (size_t)capa + termlen);
oldptr = RSTRING_PTR(str);
if (oldptr) {
- memcpy(ptr, oldptr, len);
+ memcpy(ptr, oldptr, len);
}
if (FL_TEST_RAW(str, STR_NOEMBED|STR_NOFREE|STR_SHARED) == STR_NOEMBED) {
xfree(oldptr);
@@ -2451,7 +2502,7 @@ str_make_independent_expand(VALUE str, long len, long expand, const int termlen)
FL_UNSET(str, STR_SHARED|STR_NOFREE);
TERM_FILL(ptr + len, termlen);
RSTRING(str)->as.heap.ptr = ptr;
- RSTRING(str)->as.heap.len = len;
+ STR_SET_LEN(str, len);
RSTRING(str)->as.heap.aux.capa = capa;
}
@@ -2459,7 +2510,7 @@ void
rb_str_modify(VALUE str)
{
if (!str_independent(str))
- str_make_independent(str);
+ str_make_independent(str);
ENC_CODERANGE_CLEAR(str);
}
@@ -2470,17 +2521,17 @@ rb_str_modify_expand(VALUE str, long expand)
long len = RSTRING_LEN(str);
if (expand < 0) {
- rb_raise(rb_eArgError, "negative expanding string size");
+ rb_raise(rb_eArgError, "negative expanding string size");
}
if (expand >= LONG_MAX - len) {
- rb_raise(rb_eArgError, "string size too big");
+ rb_raise(rb_eArgError, "string size too big");
}
if (!str_independent(str)) {
- str_make_independent_expand(str, len, expand, termlen);
+ str_make_independent_expand(str, len, expand, termlen);
}
else if (expand > 0) {
- RESIZE_CAPA_TERM(str, len + expand, termlen);
+ RESIZE_CAPA_TERM(str, len + expand, termlen);
}
ENC_CODERANGE_CLEAR(str);
}
@@ -2490,10 +2541,10 @@ static void
str_modify_keep_cr(VALUE str)
{
if (!str_independent(str))
- str_make_independent(str);
+ str_make_independent(str);
if (ENC_CODERANGE(str) == ENC_CODERANGE_BROKEN)
- /* Force re-scan later */
- ENC_CODERANGE_CLEAR(str);
+ /* Force re-scan later */
+ ENC_CODERANGE_CLEAR(str);
}
static inline void
@@ -2501,9 +2552,9 @@ str_discard(VALUE str)
{
str_modifiable(str);
if (!STR_EMBED_P(str) && !FL_TEST(str, STR_SHARED|STR_NOFREE)) {
- ruby_sized_xfree(STR_HEAP_PTR(str), STR_HEAP_SIZE(str));
- RSTRING(str)->as.heap.ptr = 0;
- RSTRING(str)->as.heap.len = 0;
+ ruby_sized_xfree(STR_HEAP_PTR(str), STR_HEAP_SIZE(str));
+ RSTRING(str)->as.heap.ptr = 0;
+ STR_SET_LEN(str, 0);
}
}
@@ -2511,8 +2562,11 @@ void
rb_must_asciicompat(VALUE str)
{
rb_encoding *enc = rb_enc_get(str);
+ if (!enc) {
+ rb_raise(rb_eTypeError, "not encoding capable object");
+ }
if (!rb_enc_asciicompat(enc)) {
- rb_raise(rb_eEncCompatError, "ASCII incompatible encoding: %s", rb_enc_name(enc));
+ rb_raise(rb_eEncCompatError, "ASCII incompatible encoding: %s", rb_enc_name(enc));
}
}
@@ -2521,8 +2575,8 @@ rb_string_value(volatile VALUE *ptr)
{
VALUE s = *ptr;
if (!RB_TYPE_P(s, T_STRING)) {
- s = rb_str_to_str(s);
- *ptr = s;
+ s = rb_str_to_str(s);
+ *ptr = s;
}
return s;
}
@@ -2538,7 +2592,7 @@ static int
zero_filled(const char *s, int n)
{
for (; n > 0; --n) {
- if (*s++) return 0;
+ if (*s++) return 0;
}
return 1;
}
@@ -2549,7 +2603,7 @@ str_null_char(const char *s, long len, const int minlen, rb_encoding *enc)
const char *e = s + len;
for (; s + minlen <= e; s += rb_enc_mbclen(s, e, enc)) {
- if (zero_filled(s, minlen)) return s;
+ if (zero_filled(s, minlen)) return s;
}
return 0;
}
@@ -2561,12 +2615,12 @@ str_fill_term(VALUE str, char *s, long len, int termlen)
* is allocated, like many other functions in this file.
*/
if (str_dependent_p(str)) {
- if (!zero_filled(s + len, termlen))
- str_make_independent_expand(str, len, 0L, termlen);
+ if (!zero_filled(s + len, termlen))
+ str_make_independent_expand(str, len, 0L, termlen);
}
else {
- TERM_FILL(s + len, termlen);
- return s;
+ TERM_FILL(s + len, termlen);
+ return s;
}
return RSTRING_PTR(str);
}
@@ -2577,24 +2631,24 @@ rb_str_change_terminator_length(VALUE str, const int oldtermlen, const int terml
long capa = str_capacity(str, oldtermlen) + oldtermlen;
long len = RSTRING_LEN(str);
- assert(capa >= len);
+ RUBY_ASSERT(capa >= len);
if (capa - len < termlen) {
- rb_check_lockedtmp(str);
- str_make_independent_expand(str, len, 0L, termlen);
+ rb_check_lockedtmp(str);
+ str_make_independent_expand(str, len, 0L, termlen);
}
else if (str_dependent_p(str)) {
- if (termlen > oldtermlen)
- str_make_independent_expand(str, len, 0L, termlen);
+ if (termlen > oldtermlen)
+ str_make_independent_expand(str, len, 0L, termlen);
}
else {
- if (!STR_EMBED_P(str)) {
- /* modify capa instead of realloc */
- assert(!FL_TEST((str), STR_SHARED));
- RSTRING(str)->as.heap.aux.capa = capa - termlen;
- }
- if (termlen > oldtermlen) {
- TERM_FILL(RSTRING_PTR(str) + len, termlen);
- }
+ if (!STR_EMBED_P(str)) {
+ /* modify capa instead of realloc */
+ RUBY_ASSERT(!FL_TEST((str), STR_SHARED));
+ RSTRING(str)->as.heap.aux.capa = capa - termlen;
+ }
+ if (termlen > oldtermlen) {
+ TERM_FILL(RSTRING_PTR(str) + len, termlen);
+ }
}
return;
@@ -2609,18 +2663,18 @@ str_null_check(VALUE str, int *w)
const int minlen = rb_enc_mbminlen(enc);
if (minlen > 1) {
- *w = 1;
- if (str_null_char(s, len, minlen, enc)) {
- return NULL;
- }
- return str_fill_term(str, s, len, minlen);
+ *w = 1;
+ if (str_null_char(s, len, minlen, enc)) {
+ return NULL;
+ }
+ return str_fill_term(str, s, len, minlen);
}
*w = 0;
if (!s || memchr(s, 0, len)) {
- return NULL;
+ return NULL;
}
if (s[len]) {
- s = str_fill_term(str, s, len, minlen);
+ s = str_fill_term(str, s, len, minlen);
}
return s;
}
@@ -2639,10 +2693,10 @@ rb_string_value_cstr(volatile VALUE *ptr)
int w;
char *s = str_null_check(str, &w);
if (!s) {
- if (w) {
- rb_raise(rb_eArgError, "string contains null char");
- }
- rb_raise(rb_eArgError, "string contains null byte");
+ if (w) {
+ rb_raise(rb_eArgError, "string contains null char");
+ }
+ rb_raise(rb_eArgError, "string contains null byte");
}
return s;
}
@@ -2666,14 +2720,14 @@ rb_check_string_type(VALUE str)
* call-seq:
* String.try_convert(object) -> object, new_string, or nil
*
- * If +object+ is a \String object, returns +object+.
+ * If +object+ is a +String+ object, returns +object+.
*
* Otherwise if +object+ responds to <tt>:to_str</tt>,
* calls <tt>object.to_str</tt> and returns the result.
*
* Returns +nil+ if +object+ does not respond to <tt>:to_str</tt>.
*
- * Raises an exception unless <tt>object.to_str</tt> returns a \String object.
+ * Raises an exception unless <tt>object.to_str</tt> returns a +String+ object.
*/
static VALUE
rb_str_s_try_convert(VALUE dummy, VALUE str)
@@ -2704,8 +2758,8 @@ str_nth_len(const char *p, const char *e, long *nthp, rb_encoding *enc)
if (ISASCII(*p)) {
p2 = search_nonascii(p, e2);
if (!p2) {
- nth -= e2 - p;
- *nthp = nth;
+ nth -= e2 - p;
+ *nthp = nth;
return (char *)e2;
}
nth -= p2 - p;
@@ -2741,9 +2795,9 @@ static char*
str_nth(const char *p, const char *e, long nth, rb_encoding *enc, int singlebyte)
{
if (singlebyte)
- p += nth;
+ p += nth;
else {
- p = str_nth_len(p, e, &nth, enc);
+ p = str_nth_len(p, e, &nth, enc);
}
if (!p) return 0;
if (p > e) p = e;
@@ -2763,7 +2817,7 @@ long
rb_str_offset(VALUE str, long pos)
{
return str_offset(RSTRING_PTR(str), RSTRING_END(str), pos,
- STR_ENC_GET(str), single_byte_optimizable(str));
+ STR_ENC_GET(str), single_byte_optimizable(str));
}
#ifdef NONASCII_MASK
@@ -2772,26 +2826,26 @@ str_utf8_nth(const char *p, const char *e, long *nthp)
{
long nth = *nthp;
if ((int)SIZEOF_VOIDP * 2 < e - p && (int)SIZEOF_VOIDP * 2 < nth) {
- const uintptr_t *s, *t;
- const uintptr_t lowbits = SIZEOF_VOIDP - 1;
- s = (const uintptr_t*)(~lowbits & ((uintptr_t)p + lowbits));
- t = (const uintptr_t*)(~lowbits & (uintptr_t)e);
- while (p < (const char *)s) {
- if (is_utf8_lead_byte(*p)) nth--;
- p++;
- }
- do {
- nth -= count_utf8_lead_bytes_with_word(s);
- s++;
- } while (s < t && (int)SIZEOF_VOIDP <= nth);
- p = (char *)s;
+ const uintptr_t *s, *t;
+ const uintptr_t lowbits = SIZEOF_VOIDP - 1;
+ s = (const uintptr_t*)(~lowbits & ((uintptr_t)p + lowbits));
+ t = (const uintptr_t*)(~lowbits & (uintptr_t)e);
+ while (p < (const char *)s) {
+ if (is_utf8_lead_byte(*p)) nth--;
+ p++;
+ }
+ do {
+ nth -= count_utf8_lead_bytes_with_word(s);
+ s++;
+ } while (s < t && (int)SIZEOF_VOIDP <= nth);
+ p = (char *)s;
}
while (p < e) {
- if (is_utf8_lead_byte(*p)) {
- if (nth == 0) break;
- nth--;
- }
- p++;
+ if (is_utf8_lead_byte(*p)) {
+ if (nth == 0) break;
+ nth--;
+ }
+ p++;
}
*nthp = nth;
return (char *)p;
@@ -2812,31 +2866,55 @@ rb_str_sublen(VALUE str, long pos)
if (single_byte_optimizable(str) || pos < 0)
return pos;
else {
- char *p = RSTRING_PTR(str);
+ char *p = RSTRING_PTR(str);
return enc_strlen(p, p + pos, STR_ENC_GET(str), ENC_CODERANGE(str));
}
}
-VALUE
-rb_str_subseq(VALUE str, long beg, long len)
+static VALUE
+str_subseq(VALUE str, long beg, long len)
{
VALUE str2;
- if (!STR_EMBEDDABLE_P(len, TERM_LEN(str)) &&
- SHARABLE_SUBSTRING_P(beg, len, RSTRING_LEN(str))) {
- long olen;
- str2 = rb_str_new_shared(rb_str_new_frozen_String(str));
- RSTRING(str2)->as.heap.ptr += beg;
- olen = RSTRING(str2)->as.heap.len;
- if (olen > len) RSTRING(str2)->as.heap.len = len;
+ RUBY_ASSERT(beg >= 0);
+ RUBY_ASSERT(len >= 0);
+ RUBY_ASSERT(beg+len <= RSTRING_LEN(str));
+
+ const int termlen = TERM_LEN(str);
+ if (!SHARABLE_SUBSTRING_P(beg, len, RSTRING_LEN(str))) {
+ str2 = rb_str_new(RSTRING_PTR(str) + beg, len);
+ RB_GC_GUARD(str);
+ return str2;
}
- else {
- str2 = rb_str_new(RSTRING_PTR(str)+beg, len);
+
+ str2 = str_alloc_heap(rb_cString);
+ if (str_embed_capa(str2) >= len + termlen) {
+ char *ptr2 = RSTRING(str2)->as.embed.ary;
+ STR_SET_EMBED(str2);
+ memcpy(ptr2, RSTRING_PTR(str) + beg, len);
+ TERM_FILL(ptr2+len, termlen);
+
+ STR_SET_LEN(str2, len);
RB_GC_GUARD(str);
}
+ else {
+ str_replace_shared(str2, str);
+ RUBY_ASSERT(!STR_EMBED_P(str2));
+ ENC_CODERANGE_CLEAR(str2);
+ RSTRING(str2)->as.heap.ptr += beg;
+ if (RSTRING_LEN(str2) > len) {
+ STR_SET_LEN(str2, len);
+ }
+ }
- rb_enc_cr_str_copy_for_substr(str2, str);
+ return str2;
+}
+VALUE
+rb_str_subseq(VALUE str, long beg, long len)
+{
+ VALUE str2 = str_subseq(str, beg, len);
+ rb_enc_cr_str_copy_for_substr(str2, str);
return str2;
}
@@ -2851,46 +2929,46 @@ rb_str_subpos(VALUE str, long beg, long *lenp)
if (len < 0) return 0;
if (!blen) {
- len = 0;
+ len = 0;
}
if (single_byte_optimizable(str)) {
- if (beg > blen) return 0;
- if (beg < 0) {
- beg += blen;
- if (beg < 0) return 0;
- }
- if (len > blen - beg)
- len = blen - beg;
- if (len < 0) return 0;
- p = s + beg;
- goto end;
+ if (beg > blen) return 0;
+ if (beg < 0) {
+ beg += blen;
+ if (beg < 0) return 0;
+ }
+ if (len > blen - beg)
+ len = blen - beg;
+ if (len < 0) return 0;
+ p = s + beg;
+ goto end;
}
if (beg < 0) {
- if (len > -beg) len = -beg;
- if (-beg * rb_enc_mbmaxlen(enc) < RSTRING_LEN(str) / 8) {
- beg = -beg;
- while (beg-- > len && (e = rb_enc_prev_char(s, e, e, enc)) != 0);
- p = e;
- if (!p) return 0;
- while (len-- > 0 && (p = rb_enc_prev_char(s, p, e, enc)) != 0);
- if (!p) return 0;
- len = e - p;
- goto end;
- }
- else {
- slen = str_strlen(str, enc);
- beg += slen;
- if (beg < 0) return 0;
- p = s + beg;
- if (len == 0) goto end;
- }
+ if (len > -beg) len = -beg;
+ if (-beg * rb_enc_mbmaxlen(enc) < RSTRING_LEN(str) / 8) {
+ beg = -beg;
+ while (beg-- > len && (e = rb_enc_prev_char(s, e, e, enc)) != 0);
+ p = e;
+ if (!p) return 0;
+ while (len-- > 0 && (p = rb_enc_prev_char(s, p, e, enc)) != 0);
+ if (!p) return 0;
+ len = e - p;
+ goto end;
+ }
+ else {
+ slen = str_strlen(str, enc);
+ beg += slen;
+ if (beg < 0) return 0;
+ p = s + beg;
+ if (len == 0) goto end;
+ }
}
else if (beg > 0 && beg > RSTRING_LEN(str)) {
- return 0;
+ return 0;
}
if (len == 0) {
- if (beg > str_strlen(str, enc)) return 0; /* str's enc */
- p = s + beg;
+ if (beg > str_strlen(str, enc)) return 0; /* str's enc */
+ p = s + beg;
}
#ifdef NONASCII_MASK
else if (ENC_CODERANGE(str) == ENC_CODERANGE_VALID &&
@@ -2901,23 +2979,23 @@ rb_str_subpos(VALUE str, long beg, long *lenp)
}
#endif
else if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) {
- int char_sz = rb_enc_mbmaxlen(enc);
+ int char_sz = rb_enc_mbmaxlen(enc);
- p = s + beg * char_sz;
- if (p > e) {
- return 0;
- }
+ p = s + beg * char_sz;
+ if (p > e) {
+ return 0;
+ }
else if (len * char_sz > e - p)
len = e - p;
else
- len *= char_sz;
+ len *= char_sz;
}
else if ((p = str_nth_len(s, e, &beg, enc)) == e) {
- if (beg > 0) return 0;
- len = 0;
+ if (beg > 0) return 0;
+ len = 0;
}
else {
- len = str_offset(p, e, len, enc, 0);
+ len = str_offset(p, e, len, enc, 0);
}
end:
*lenp = len;
@@ -2936,65 +3014,70 @@ rb_str_substr(VALUE str, long beg, long len)
static VALUE
str_substr(VALUE str, long beg, long len, int empty)
{
- VALUE str2;
char *p = rb_str_subpos(str, beg, &len);
if (!p) return Qnil;
- if (!STR_EMBEDDABLE_P(len, TERM_LEN(str)) &&
- SHARABLE_SUBSTRING_P(p, len, RSTRING_END(str))) {
- long ofs = p - RSTRING_PTR(str);
- str2 = rb_str_new_frozen(str);
- str2 = str_new_shared(rb_cString, str2);
- RSTRING(str2)->as.heap.ptr += ofs;
- RSTRING(str2)->as.heap.len = len;
- ENC_CODERANGE_CLEAR(str2);
- }
- else {
- if (!len && !empty) return Qnil;
- str2 = rb_str_new(p, len);
- RB_GC_GUARD(str);
- }
- rb_enc_cr_str_copy_for_substr(str2, str);
+ if (!len && !empty) return Qnil;
+
+ beg = p - RSTRING_PTR(str);
+ VALUE str2 = str_subseq(str, beg, len);
+ rb_enc_cr_str_copy_for_substr(str2, str);
return str2;
}
+/* :nodoc: */
VALUE
rb_str_freeze(VALUE str)
{
+ if (CHILLED_STRING_P(str)) {
+ FL_UNSET_RAW(str, STR_CHILLED);
+ }
+
if (OBJ_FROZEN(str)) return str;
rb_str_resize(str, RSTRING_LEN(str));
return rb_obj_freeze(str);
}
-
/*
* call-seq:
* +string -> new_string or self
*
* Returns +self+ if +self+ is not frozen.
*
- * Otherwise. returns <tt>self.dup</tt>, which is not frozen.
+ * Otherwise returns <tt>self.dup</tt>, which is not frozen.
*/
static VALUE
str_uplus(VALUE str)
{
if (OBJ_FROZEN(str)) {
- return rb_str_dup(str);
+ return rb_str_dup(str);
}
else {
- return str;
+ return str;
}
}
/*
* call-seq:
* -string -> frozen_string
+ * dedup -> frozen_string
*
* Returns a frozen, possibly pre-existing copy of the string.
*
- * The returned \String will be deduplicated as long as it does not have
- * any instance variables set on it.
+ * The returned +String+ will be deduplicated as long as it does not have
+ * any instance variables set on it and is not a String subclass.
+ *
+ * Note that <tt>-string</tt> variant is more convenient for defining
+ * constants:
+ *
+ * FILENAME = -'config/database.yml'
+ *
+ * while +dedup+ is better suitable for using the method in chains
+ * of calculations:
+ *
+ * @url_list.concat(urls.map(&:dedup))
+ *
*/
static VALUE
str_uminus(VALUE str)
@@ -3012,7 +3095,7 @@ VALUE
rb_str_locktmp(VALUE str)
{
if (FL_TEST(str, STR_TMPLOCK)) {
- rb_raise(rb_eRuntimeError, "temporal locking already locked string");
+ rb_raise(rb_eRuntimeError, "temporal locking already locked string");
}
FL_SET(str, STR_TMPLOCK);
return str;
@@ -3022,13 +3105,13 @@ VALUE
rb_str_unlocktmp(VALUE str)
{
if (!FL_TEST(str, STR_TMPLOCK)) {
- rb_raise(rb_eRuntimeError, "temporal unlocking already unlocked string");
+ rb_raise(rb_eRuntimeError, "temporal unlocking already unlocked string");
}
FL_UNSET(str, STR_TMPLOCK);
return str;
}
-RUBY_FUNC_EXPORTED VALUE
+VALUE
rb_str_locktmp_ensure(VALUE str, VALUE (*func)(VALUE), VALUE arg)
{
rb_str_locktmp(str);
@@ -3043,11 +3126,38 @@ rb_str_set_len(VALUE str, long len)
str_modifiable(str);
if (STR_SHARED_P(str)) {
- rb_raise(rb_eRuntimeError, "can't set length of shared string");
+ rb_raise(rb_eRuntimeError, "can't set length of shared string");
}
if (len > (capa = (long)str_capacity(str, termlen)) || len < 0) {
- rb_bug("probable buffer overflow: %ld for %ld", len, capa);
+ rb_bug("probable buffer overflow: %ld for %ld", len, capa);
+ }
+
+ int cr = ENC_CODERANGE(str);
+ if (cr == ENC_CODERANGE_UNKNOWN) {
+ /* Leave unknown. */
+ }
+ else if (len > RSTRING_LEN(str)) {
+ if (ENC_CODERANGE_CLEAN_P(cr)) {
+ /* Update the coderange regarding the extended part. */
+ const char *const prev_end = RSTRING_END(str);
+ const char *const new_end = RSTRING_PTR(str) + len;
+ rb_encoding *enc = rb_enc_get(str);
+ rb_str_coderange_scan_restartable(prev_end, new_end, enc, &cr);
+ ENC_CODERANGE_SET(str, cr);
+ }
+ else if (cr == ENC_CODERANGE_BROKEN) {
+ /* May be valid now, by appended part. */
+ ENC_CODERANGE_SET(str, ENC_CODERANGE_UNKNOWN);
+ }
}
+ else if (len < RSTRING_LEN(str)) {
+ if (cr != ENC_CODERANGE_7BIT) {
+ /* ASCII-only string is keeping after truncated. Valid
+ * and broken may be invalid or valid, leave unknown. */
+ ENC_CODERANGE_SET(str, ENC_CODERANGE_UNKNOWN);
+ }
+ }
+
STR_SET_LEN(str, len);
TERM_FILL(&RSTRING_PTR(str)[len], termlen);
}
@@ -3055,95 +3165,91 @@ rb_str_set_len(VALUE str, long len)
VALUE
rb_str_resize(VALUE str, long len)
{
- long slen;
- int independent;
-
if (len < 0) {
- rb_raise(rb_eArgError, "negative string size (or size too big)");
+ rb_raise(rb_eArgError, "negative string size (or size too big)");
}
- independent = str_independent(str);
- ENC_CODERANGE_CLEAR(str);
- slen = RSTRING_LEN(str);
+ int independent = str_independent(str);
+ long slen = RSTRING_LEN(str);
+
+ if (slen > len && ENC_CODERANGE(str) != ENC_CODERANGE_7BIT) {
+ ENC_CODERANGE_CLEAR(str);
+ }
{
- long capa;
- const int termlen = TERM_LEN(str);
- if (STR_EMBED_P(str)) {
- if (len == slen) return str;
+ long capa;
+ const int termlen = TERM_LEN(str);
+ if (STR_EMBED_P(str)) {
+ if (len == slen) return str;
if (str_embed_capa(str) >= len + termlen) {
- STR_SET_EMBED_LEN(str, len);
+ STR_SET_LEN(str, len);
TERM_FILL(RSTRING(str)->as.embed.ary + len, termlen);
- return str;
- }
- str_make_independent_expand(str, slen, len - slen, termlen);
- }
+ return str;
+ }
+ str_make_independent_expand(str, slen, len - slen, termlen);
+ }
else if (str_embed_capa(str) >= len + termlen) {
- char *ptr = STR_HEAP_PTR(str);
- STR_SET_EMBED(str);
- if (slen > len) slen = len;
+ char *ptr = STR_HEAP_PTR(str);
+ STR_SET_EMBED(str);
+ if (slen > len) slen = len;
if (slen > 0) MEMCPY(RSTRING(str)->as.embed.ary, ptr, char, slen);
TERM_FILL(RSTRING(str)->as.embed.ary + len, termlen);
- STR_SET_EMBED_LEN(str, len);
- if (independent) ruby_xfree(ptr);
- return str;
- }
- else if (!independent) {
- if (len == slen) return str;
- str_make_independent_expand(str, slen, len - slen, termlen);
- }
- else if ((capa = RSTRING(str)->as.heap.aux.capa) < len ||
- (capa - len) > (len < 1024 ? len : 1024)) {
- SIZED_REALLOC_N(RSTRING(str)->as.heap.ptr, char,
- (size_t)len + termlen, STR_HEAP_SIZE(str));
- RSTRING(str)->as.heap.aux.capa = len;
- }
- else if (len == slen) return str;
- RSTRING(str)->as.heap.len = len;
- TERM_FILL(RSTRING(str)->as.heap.ptr + len, termlen); /* sentinel */
+ STR_SET_LEN(str, len);
+ if (independent) ruby_xfree(ptr);
+ return str;
+ }
+ else if (!independent) {
+ if (len == slen) return str;
+ str_make_independent_expand(str, slen, len - slen, termlen);
+ }
+ else if ((capa = RSTRING(str)->as.heap.aux.capa) < len ||
+ (capa - len) > (len < 1024 ? len : 1024)) {
+ SIZED_REALLOC_N(RSTRING(str)->as.heap.ptr, char,
+ (size_t)len + termlen, STR_HEAP_SIZE(str));
+ RSTRING(str)->as.heap.aux.capa = len;
+ }
+ else if (len == slen) return str;
+ STR_SET_LEN(str, len);
+ TERM_FILL(RSTRING(str)->as.heap.ptr + len, termlen); /* sentinel */
}
return str;
}
static VALUE
-str_buf_cat(VALUE str, const char *ptr, long len)
+str_buf_cat4(VALUE str, const char *ptr, long len, bool keep_cr)
{
- long capa, total, olen, off = -1;
+ if (keep_cr) {
+ str_modify_keep_cr(str);
+ }
+ else {
+ rb_str_modify(str);
+ }
+ if (len == 0) return 0;
+
+ long total, olen, off = -1;
char *sptr;
const int termlen = TERM_LEN(str);
-#if !USE_RVARGC
- assert(termlen < RSTRING_EMBED_LEN_MAX + 1); /* < (LONG_MAX/2) */
-#endif
RSTRING_GETMEM(str, sptr, olen);
if (ptr >= sptr && ptr <= sptr + olen) {
off = ptr - sptr;
}
- rb_str_modify(str);
- if (len == 0) return 0;
- if (STR_EMBED_P(str)) {
- capa = str_embed_capa(str) - termlen;
- sptr = RSTRING(str)->as.embed.ary;
- olen = RSTRING_EMBED_LEN(str);
- }
- else {
- capa = RSTRING(str)->as.heap.aux.capa;
- sptr = RSTRING(str)->as.heap.ptr;
- olen = RSTRING(str)->as.heap.len;
- }
+
+ long capa = str_capacity(str, termlen);
+
if (olen > LONG_MAX - len) {
- rb_raise(rb_eArgError, "string sizes too big");
+ rb_raise(rb_eArgError, "string sizes too big");
}
total = olen + len;
if (capa < total) {
- if (total >= LONG_MAX / 2) {
- capa = total;
- }
- while (total > capa) {
- capa = 2 * capa + termlen; /* == 2*(capa+termlen)-termlen */
- }
- RESIZE_CAPA_TERM(str, capa, termlen);
- sptr = RSTRING_PTR(str);
+ if (total >= LONG_MAX / 2) {
+ capa = total;
+ }
+ while (total > capa) {
+ capa = 2 * capa + termlen; /* == 2*(capa+termlen)-termlen */
+ }
+ RESIZE_CAPA_TERM(str, capa, termlen);
+ sptr = RSTRING_PTR(str);
}
if (off != -1) {
ptr = sptr + off;
@@ -3155,14 +3261,15 @@ str_buf_cat(VALUE str, const char *ptr, long len)
return str;
}
-#define str_buf_cat2(str, ptr) str_buf_cat((str), (ptr), strlen(ptr))
+#define str_buf_cat(str, ptr, len) str_buf_cat4((str), (ptr), len, false)
+#define str_buf_cat2(str, ptr) str_buf_cat4((str), (ptr), rb_strlen_lit(ptr), false)
VALUE
rb_str_cat(VALUE str, const char *ptr, long len)
{
if (len == 0) return str;
if (len < 0) {
- rb_raise(rb_eArgError, "negative string size (or size too big)");
+ rb_raise(rb_eArgError, "negative string size (or size too big)");
}
return str_buf_cat(str, ptr, len);
}
@@ -3190,26 +3297,27 @@ rb_enc_cr_str_buf_cat(VALUE str, const char *ptr, long len,
str_cr = RSTRING_LEN(str) ? ENC_CODERANGE(str) : ENC_CODERANGE_7BIT;
if (str_encindex == ptr_encindex) {
- if (str_cr != ENC_CODERANGE_UNKNOWN && ptr_cr == ENC_CODERANGE_UNKNOWN) {
+ if (str_cr != ENC_CODERANGE_UNKNOWN && ptr_cr == ENC_CODERANGE_UNKNOWN) {
ptr_cr = coderange_scan(ptr, len, rb_enc_from_index(ptr_encindex));
}
}
else {
- str_enc = rb_enc_from_index(str_encindex);
- ptr_enc = rb_enc_from_index(ptr_encindex);
+ str_enc = rb_enc_from_index(str_encindex);
+ ptr_enc = rb_enc_from_index(ptr_encindex);
if (!rb_enc_asciicompat(str_enc) || !rb_enc_asciicompat(ptr_enc)) {
if (len == 0)
return str;
if (RSTRING_LEN(str) == 0) {
rb_str_buf_cat(str, ptr, len);
ENCODING_CODERANGE_SET(str, ptr_encindex, ptr_cr);
+ rb_str_change_terminator_length(str, rb_enc_mbminlen(str_enc), rb_enc_mbminlen(ptr_enc));
return str;
}
goto incompatible;
}
- if (ptr_cr == ENC_CODERANGE_UNKNOWN) {
- ptr_cr = coderange_scan(ptr, len, ptr_enc);
- }
+ if (ptr_cr == ENC_CODERANGE_UNKNOWN) {
+ ptr_cr = coderange_scan(ptr, len, ptr_enc);
+ }
if (str_cr == ENC_CODERANGE_UNKNOWN) {
if (ENCODING_IS_ASCII8BIT(str) || ptr_cr != ENC_CODERANGE_7BIT) {
str_cr = rb_enc_str_coderange(str);
@@ -3222,8 +3330,8 @@ rb_enc_cr_str_buf_cat(VALUE str, const char *ptr, long len,
if (str_encindex != ptr_encindex &&
str_cr != ENC_CODERANGE_7BIT &&
ptr_cr != ENC_CODERANGE_7BIT) {
- str_enc = rb_enc_from_index(str_encindex);
- ptr_enc = rb_enc_from_index(ptr_encindex);
+ str_enc = rb_enc_from_index(str_encindex);
+ ptr_enc = rb_enc_from_index(ptr_encindex);
goto incompatible;
}
@@ -3243,10 +3351,10 @@ rb_enc_cr_str_buf_cat(VALUE str, const char *ptr, long len,
}
else if (str_cr == ENC_CODERANGE_VALID) {
res_encindex = str_encindex;
- if (ENC_CODERANGE_CLEAN_P(ptr_cr))
- res_cr = str_cr;
- else
- res_cr = ptr_cr;
+ if (ENC_CODERANGE_CLEAN_P(ptr_cr))
+ res_cr = str_cr;
+ else
+ res_cr = ptr_cr;
}
else { /* str_cr == ENC_CODERANGE_BROKEN */
res_encindex = str_encindex;
@@ -3255,7 +3363,7 @@ rb_enc_cr_str_buf_cat(VALUE str, const char *ptr, long len,
}
if (len < 0) {
- rb_raise(rb_eArgError, "negative string size (or size too big)");
+ rb_raise(rb_eArgError, "negative string size (or size too big)");
}
str_buf_cat(str, ptr, len);
ENCODING_CODERANGE_SET(str, res_encindex, res_cr);
@@ -3301,9 +3409,28 @@ rb_str_buf_cat_ascii(VALUE str, const char *ptr)
VALUE
rb_str_buf_append(VALUE str, VALUE str2)
{
- int str2_cr;
+ int str2_cr = rb_enc_str_coderange(str2);
- str2_cr = ENC_CODERANGE(str2);
+ if (str_enc_fastpath(str)) {
+ switch (str2_cr) {
+ case ENC_CODERANGE_7BIT:
+ // If RHS is 7bit we can do simple concatenation
+ str_buf_cat4(str, RSTRING_PTR(str2), RSTRING_LEN(str2), true);
+ RB_GC_GUARD(str2);
+ return str;
+ case ENC_CODERANGE_VALID:
+ // If RHS is valid, we can do simple concatenation if encodings are the same
+ if (ENCODING_GET_INLINED(str) == ENCODING_GET_INLINED(str2)) {
+ str_buf_cat4(str, RSTRING_PTR(str2), RSTRING_LEN(str2), true);
+ int str_cr = ENC_CODERANGE(str);
+ if (UNLIKELY(str_cr != ENC_CODERANGE_VALID)) {
+ ENC_CODERANGE_SET(str, RB_ENC_CODERANGE_AND(str_cr, str2_cr));
+ }
+ RB_GC_GUARD(str2);
+ return str;
+ }
+ }
+ }
rb_enc_cr_str_buf_cat(str, RSTRING_PTR(str2), RSTRING_LEN(str2),
ENCODING_GET(str2), str2_cr, &str2_cr);
@@ -3320,39 +3447,29 @@ rb_str_append(VALUE str, VALUE str2)
return rb_str_buf_append(str, str2);
}
-#define MIN_PRE_ALLOC_SIZE 48
-
-MJIT_FUNC_EXPORTED VALUE
+VALUE
rb_str_concat_literals(size_t num, const VALUE *strary)
{
VALUE str;
- size_t i, s;
- long len = 1;
+ size_t i, s = 0;
+ unsigned long len = 1;
if (UNLIKELY(!num)) return rb_str_new(0, 0);
if (UNLIKELY(num == 1)) return rb_str_resurrect(strary[0]);
for (i = 0; i < num; ++i) { len += RSTRING_LEN(strary[i]); }
- if (LIKELY(len < MIN_PRE_ALLOC_SIZE)) {
- str = rb_str_resurrect(strary[0]);
- s = 1;
- }
- else {
- str = rb_str_buf_new(len);
- rb_enc_copy(str, strary[0]);
- s = 0;
- }
+ str = rb_str_buf_new(len);
+ str_enc_copy_direct(str, strary[0]);
for (i = s; i < num; ++i) {
- const VALUE v = strary[i];
- int encidx = ENCODING_GET(v);
+ const VALUE v = strary[i];
+ int encidx = ENCODING_GET(v);
- rb_enc_cr_str_buf_cat(str, RSTRING_PTR(v), RSTRING_LEN(v),
- encidx, ENC_CODERANGE(v), NULL);
- if (encidx != ENCINDEX_US_ASCII) {
- if (ENCODING_GET_INLINED(str) == ENCINDEX_US_ASCII)
- rb_enc_set_index(str, encidx);
- }
+ rb_str_buf_append(str, v);
+ if (encidx != ENCINDEX_US_ASCII) {
+ if (ENCODING_GET_INLINED(str) == ENCINDEX_US_ASCII)
+ rb_enc_set_index(str, encidx);
+ }
}
return str;
}
@@ -3367,7 +3484,7 @@ rb_str_concat_literals(size_t num, const VALUE *strary)
* s.concat('bar', 'baz') # => "foobarbaz"
* s # => "foobarbaz"
*
- * For each given object +object+ that is an \Integer,
+ * For each given object +object+ that is an Integer,
* the value is considered a codepoint and converted to a character before concatenation:
*
* s = 'foo'
@@ -3381,16 +3498,16 @@ rb_str_concat_multi(int argc, VALUE *argv, VALUE str)
str_modifiable(str);
if (argc == 1) {
- return rb_str_concat(str, argv[0]);
+ return rb_str_concat(str, argv[0]);
}
else if (argc > 1) {
- int i;
- VALUE arg_str = rb_str_tmp_new(0);
- rb_enc_copy(arg_str, str);
- for (i = 0; i < argc; i++) {
- rb_str_concat(arg_str, argv[i]);
- }
- rb_str_buf_append(str, arg_str);
+ int i;
+ VALUE arg_str = rb_str_tmp_new(0);
+ rb_enc_copy(arg_str, str);
+ for (i = 0; i < argc; i++) {
+ rb_str_concat(arg_str, argv[i]);
+ }
+ rb_str_buf_append(str, arg_str);
}
return str;
@@ -3406,7 +3523,7 @@ rb_str_concat_multi(int argc, VALUE *argv, VALUE str)
* s << 'bar' # => "foobar"
* s # => "foobar"
*
- * If +object+ is an \Integer,
+ * If +object+ is an Integer,
* the value is considered a codepoint and converted to a character before concatenation:
*
* s = 'foo'
@@ -3422,65 +3539,85 @@ rb_str_concat(VALUE str1, VALUE str2)
int encidx;
if (RB_INTEGER_TYPE_P(str2)) {
- if (rb_num_to_uint(str2, &code) == 0) {
- }
- else if (FIXNUM_P(str2)) {
- rb_raise(rb_eRangeError, "%ld out of char range", FIX2LONG(str2));
- }
- else {
- rb_raise(rb_eRangeError, "bignum out of char range");
- }
+ if (rb_num_to_uint(str2, &code) == 0) {
+ }
+ else if (FIXNUM_P(str2)) {
+ rb_raise(rb_eRangeError, "%ld out of char range", FIX2LONG(str2));
+ }
+ else {
+ rb_raise(rb_eRangeError, "bignum out of char range");
+ }
}
else {
- return rb_str_append(str1, str2);
+ return rb_str_append(str1, str2);
}
- encidx = rb_enc_to_index(enc);
- if (encidx == ENCINDEX_ASCII || encidx == ENCINDEX_US_ASCII) {
- /* US-ASCII automatically extended to ASCII-8BIT */
- char buf[1];
- buf[0] = (char)code;
- if (code > 0xFF) {
- rb_raise(rb_eRangeError, "%u out of char range", code);
- }
- rb_str_cat(str1, buf, 1);
- if (encidx == ENCINDEX_US_ASCII && code > 127) {
- rb_enc_associate_index(str1, ENCINDEX_ASCII);
- ENC_CODERANGE_SET(str1, ENC_CODERANGE_VALID);
- }
+ encidx = rb_ascii8bit_appendable_encoding_index(enc, code);
+ if (encidx >= 0) {
+ char buf[1];
+ buf[0] = (char)code;
+ rb_str_cat(str1, buf, 1);
+ if (encidx != rb_enc_to_index(enc)) {
+ rb_enc_associate_index(str1, encidx);
+ ENC_CODERANGE_SET(str1, ENC_CODERANGE_VALID);
+ }
}
else {
- long pos = RSTRING_LEN(str1);
- int cr = ENC_CODERANGE(str1);
- int len;
- char *buf;
-
- switch (len = rb_enc_codelen(code, enc)) {
- case ONIGERR_INVALID_CODE_POINT_VALUE:
- rb_raise(rb_eRangeError, "invalid codepoint 0x%X in %s", code, rb_enc_name(enc));
- break;
- case ONIGERR_TOO_BIG_WIDE_CHAR_VALUE:
- case 0:
- rb_raise(rb_eRangeError, "%u out of char range", code);
- break;
- }
- buf = ALLOCA_N(char, len + 1);
- rb_enc_mbcput(code, buf, enc);
- if (rb_enc_precise_mbclen(buf, buf + len + 1, enc) != len) {
- rb_raise(rb_eRangeError, "invalid codepoint 0x%X in %s", code, rb_enc_name(enc));
- }
- rb_str_resize(str1, pos+len);
- memcpy(RSTRING_PTR(str1) + pos, buf, len);
- if (cr == ENC_CODERANGE_7BIT && code > 127)
- cr = ENC_CODERANGE_VALID;
- ENC_CODERANGE_SET(str1, cr);
+ long pos = RSTRING_LEN(str1);
+ int cr = ENC_CODERANGE(str1);
+ int len;
+ char *buf;
+
+ switch (len = rb_enc_codelen(code, enc)) {
+ case ONIGERR_INVALID_CODE_POINT_VALUE:
+ rb_raise(rb_eRangeError, "invalid codepoint 0x%X in %s", code, rb_enc_name(enc));
+ break;
+ case ONIGERR_TOO_BIG_WIDE_CHAR_VALUE:
+ case 0:
+ rb_raise(rb_eRangeError, "%u out of char range", code);
+ break;
+ }
+ buf = ALLOCA_N(char, len + 1);
+ rb_enc_mbcput(code, buf, enc);
+ if (rb_enc_precise_mbclen(buf, buf + len + 1, enc) != len) {
+ rb_raise(rb_eRangeError, "invalid codepoint 0x%X in %s", code, rb_enc_name(enc));
+ }
+ rb_str_resize(str1, pos+len);
+ memcpy(RSTRING_PTR(str1) + pos, buf, len);
+ if (cr == ENC_CODERANGE_7BIT && code > 127) {
+ cr = ENC_CODERANGE_VALID;
+ }
+ else if (cr == ENC_CODERANGE_BROKEN) {
+ cr = ENC_CODERANGE_UNKNOWN;
+ }
+ ENC_CODERANGE_SET(str1, cr);
}
return str1;
}
+int
+rb_ascii8bit_appendable_encoding_index(rb_encoding *enc, unsigned int code)
+{
+ int encidx = rb_enc_to_index(enc);
+
+ if (encidx == ENCINDEX_ASCII_8BIT || encidx == ENCINDEX_US_ASCII) {
+ /* US-ASCII automatically extended to ASCII-8BIT */
+ if (code > 0xFF) {
+ rb_raise(rb_eRangeError, "%u out of char range", code);
+ }
+ if (encidx == ENCINDEX_US_ASCII && code > 127) {
+ return ENCINDEX_ASCII_8BIT;
+ }
+ return encidx;
+ }
+ else {
+ return -1;
+ }
+}
+
/*
* call-seq:
- * string.prepend(*other_strings) -> string
+ * prepend(*other_strings) -> string
*
* Prepends each string in +other_strings+ to +self+ and returns +self+:
*
@@ -3497,16 +3634,16 @@ rb_str_prepend_multi(int argc, VALUE *argv, VALUE str)
str_modifiable(str);
if (argc == 1) {
- rb_str_update(str, 0L, 0L, argv[0]);
+ rb_str_update(str, 0L, 0L, argv[0]);
}
else if (argc > 1) {
- int i;
- VALUE arg_str = rb_str_tmp_new(0);
- rb_enc_copy(arg_str, str);
- for (i = 0; i < argc; i++) {
- rb_str_append(arg_str, argv[i]);
- }
- rb_str_update(str, 0L, 0L, arg_str);
+ int i;
+ VALUE arg_str = rb_str_tmp_new(0);
+ rb_enc_copy(arg_str, str);
+ for (i = 0; i < argc; i++) {
+ rb_str_append(arg_str, argv[i]);
+ }
+ rb_str_update(str, 0L, 0L, arg_str);
}
return str;
@@ -3515,11 +3652,12 @@ rb_str_prepend_multi(int argc, VALUE *argv, VALUE str)
st_index_t
rb_str_hash(VALUE str)
{
- int e = ENCODING_GET(str);
- if (e && rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
- e = 0;
+ st_index_t h = rb_memhash((const void *)RSTRING_PTR(str), RSTRING_LEN(str));
+ int e = RSTRING_LEN(str) ? ENCODING_GET(str) : 0;
+ if (e && !is_ascii_string(str)) {
+ h = rb_hash_end(rb_hash_uint32(h, (uint32_t)e));
}
- return rb_memhash((const void *)RSTRING_PTR(str), RSTRING_LEN(str)) ^ e;
+ return h;
}
int
@@ -3530,13 +3668,13 @@ rb_str_hash_cmp(VALUE str1, VALUE str2)
RSTRING_GETMEM(str1, ptr1, len1);
RSTRING_GETMEM(str2, ptr2, len2);
return (len1 != len2 ||
- !rb_str_comparable(str1, str2) ||
- memcmp(ptr1, ptr2, len1) != 0);
+ !rb_str_comparable(str1, str2) ||
+ memcmp(ptr1, ptr2, len1) != 0);
}
/*
* call-seq:
- * string.hash -> integer
+ * hash -> integer
*
* Returns the integer hash value for +self+.
* The value is based on the length, content and encoding of +self+.
@@ -3567,13 +3705,13 @@ rb_str_comparable(VALUE str1, VALUE str2)
rc1 = rb_enc_str_coderange(str1);
rc2 = rb_enc_str_coderange(str2);
if (rc1 == ENC_CODERANGE_7BIT) {
- if (rc2 == ENC_CODERANGE_7BIT) return TRUE;
- if (rb_enc_asciicompat(rb_enc_from_index(idx2)))
- return TRUE;
+ if (rc2 == ENC_CODERANGE_7BIT) return TRUE;
+ if (rb_enc_asciicompat(rb_enc_from_index(idx2)))
+ return TRUE;
}
if (rc2 == ENC_CODERANGE_7BIT) {
- if (rb_enc_asciicompat(rb_enc_from_index(idx1)))
- return TRUE;
+ if (rb_enc_asciicompat(rb_enc_from_index(idx1)))
+ return TRUE;
}
return FALSE;
}
@@ -3589,16 +3727,16 @@ rb_str_cmp(VALUE str1, VALUE str2)
RSTRING_GETMEM(str1, ptr1, len1);
RSTRING_GETMEM(str2, ptr2, len2);
if (ptr1 == ptr2 || (retval = memcmp(ptr1, ptr2, lesser(len1, len2))) == 0) {
- if (len1 == len2) {
- if (!rb_str_comparable(str1, str2)) {
- if (ENCODING_GET(str1) > ENCODING_GET(str2))
- return 1;
- return -1;
- }
- return 0;
- }
- if (len1 > len2) return 1;
- return -1;
+ if (len1 == len2) {
+ if (!rb_str_comparable(str1, str2)) {
+ if (ENCODING_GET(str1) > ENCODING_GET(str2))
+ return 1;
+ return -1;
+ }
+ return 0;
+ }
+ if (len1 > len2) return 1;
+ return -1;
}
if (retval > 0) return 1;
return -1;
@@ -3620,7 +3758,7 @@ rb_str_cmp(VALUE str1, VALUE str2)
* Returns +false+ if the two strings' encodings are not compatible:
* "\u{e4 f6 fc}".encode("ISO-8859-1") == ("\u{c4 d6 dc}") # => false
*
- * If +object+ is not an instance of \String but responds to +to_str+, then the
+ * If +object+ is not an instance of +String+ but responds to +to_str+, then the
* two strings are compared using <code>object.==</code>.
*/
@@ -3629,10 +3767,10 @@ rb_str_equal(VALUE str1, VALUE str2)
{
if (str1 == str2) return Qtrue;
if (!RB_TYPE_P(str2, T_STRING)) {
- if (!rb_respond_to(str2, idTo_str)) {
- return Qfalse;
- }
- return rb_equal(str2, str1);
+ if (!rb_respond_to(str2, idTo_str)) {
+ return Qfalse;
+ }
+ return rb_equal(str2, str1);
}
return rb_str_eql_internal(str1, str2);
}
@@ -3655,7 +3793,7 @@ rb_str_equal(VALUE str1, VALUE str2)
*
*/
-MJIT_FUNC_EXPORTED VALUE
+VALUE
rb_str_eql(VALUE str1, VALUE str2)
{
if (str1 == str2) return Qtrue;
@@ -3691,7 +3829,7 @@ rb_str_cmp_m(VALUE str1, VALUE str2)
int result;
VALUE s = rb_check_string_type(str2);
if (NIL_P(s)) {
- return rb_invcmp(str1, str2);
+ return rb_invcmp(str1, str2);
}
result = rb_str_cmp(str1, s);
return INT2FIX(result);
@@ -3702,13 +3840,13 @@ static VALUE str_casecmp_p(VALUE str1, VALUE str2);
/*
* call-seq:
- * casecmp(other_str) -> -1, 0, 1, or nil
+ * casecmp(other_string) -> -1, 0, 1, or nil
*
- * Compares +self+ and +other_string+, ignoring case, and returning:
+ * Compares <tt>self.downcase</tt> and <tt>other_string.downcase</tt>; returns:
*
- * - -1 if +other_string+ is larger.
+ * - -1 if <tt>other_string.downcase</tt> is larger.
* - 0 if the two are equal.
- * - 1 if +other_string+ is smaller.
+ * - 1 if <tt>other_string.downcase</tt> is smaller.
* - +nil+ if the two are incomparable.
*
* Examples:
@@ -3720,6 +3858,10 @@ static VALUE str_casecmp_p(VALUE str1, VALUE str2);
* 'foo'.casecmp('FOO') # => 0
* 'foo'.casecmp(1) # => nil
*
+ * See {Case Mapping}[rdoc-ref:case_mapping.rdoc].
+ *
+ * Related: String#casecmp?.
+ *
*/
static VALUE
@@ -3727,7 +3869,7 @@ rb_str_casecmp(VALUE str1, VALUE str2)
{
VALUE s = rb_check_string_type(str2);
if (NIL_P(s)) {
- return Qnil;
+ return Qnil;
}
return str_casecmp(str1, s);
}
@@ -3741,25 +3883,25 @@ str_casecmp(VALUE str1, VALUE str2)
enc = rb_enc_compatible(str1, str2);
if (!enc) {
- return Qnil;
+ return Qnil;
}
p1 = RSTRING_PTR(str1); p1end = RSTRING_END(str1);
p2 = RSTRING_PTR(str2); p2end = RSTRING_END(str2);
if (single_byte_optimizable(str1) && single_byte_optimizable(str2)) {
- while (p1 < p1end && p2 < p2end) {
- if (*p1 != *p2) {
+ while (p1 < p1end && p2 < p2end) {
+ if (*p1 != *p2) {
unsigned int c1 = TOLOWER(*p1 & 0xff);
unsigned int c2 = TOLOWER(*p2 & 0xff);
if (c1 != c2)
return INT2FIX(c1 < c2 ? -1 : 1);
- }
- p1++;
- p2++;
- }
+ }
+ p1++;
+ p2++;
+ }
}
else {
- while (p1 < p1end && p2 < p2end) {
+ while (p1 < p1end && p2 < p2end) {
int l1, c1 = rb_enc_ascget(p1, p1end, &l1, enc);
int l2, c2 = rb_enc_ascget(p2, p2end, &l2, enc);
@@ -3780,9 +3922,9 @@ str_casecmp(VALUE str1, VALUE str2)
if (l1 != l2)
return INT2FIX(l1 < l2 ? -1 : 1);
}
- p1 += l1;
- p2 += l2;
- }
+ p1 += l1;
+ p2 += l2;
+ }
}
if (RSTRING_LEN(str1) == RSTRING_LEN(str2)) return INT2FIX(0);
if (RSTRING_LEN(str1) > RSTRING_LEN(str2)) return INT2FIX(1);
@@ -3806,6 +3948,10 @@ str_casecmp(VALUE str1, VALUE str2)
*
* 'foo'.casecmp?(1) # => nil
*
+ * See {Case Mapping}[rdoc-ref:case_mapping.rdoc].
+ *
+ * Related: String#casecmp.
+ *
*/
static VALUE
@@ -3813,7 +3959,7 @@ rb_str_casecmp_p(VALUE str1, VALUE str2)
{
VALUE s = rb_check_string_type(str2);
if (NIL_P(s)) {
- return Qnil;
+ return Qnil;
}
return str_casecmp_p(str1, s);
}
@@ -3827,7 +3973,7 @@ str_casecmp_p(VALUE str1, VALUE str2)
enc = rb_enc_compatible(str1, str2);
if (!enc) {
- return Qnil;
+ return Qnil;
}
folded_str1 = rb_str_downcase(1, &fold_opt, str1);
@@ -3838,26 +3984,28 @@ str_casecmp_p(VALUE str1, VALUE str2)
static long
strseq_core(const char *str_ptr, const char *str_ptr_end, long str_len,
- const char *sub_ptr, long sub_len, long offset, rb_encoding *enc)
+ const char *sub_ptr, long sub_len, long offset, rb_encoding *enc)
{
const char *search_start = str_ptr;
long pos, search_len = str_len - offset;
for (;;) {
- const char *t;
- pos = rb_memsearch(sub_ptr, sub_len, search_start, search_len, enc);
- if (pos < 0) return pos;
- t = rb_enc_right_char_head(search_start, search_start+pos, str_ptr_end, enc);
- if (t == search_start + pos) break;
- search_len -= t - search_start;
- if (search_len <= 0) return -1;
- offset += t - search_start;
- search_start = t;
+ const char *t;
+ pos = rb_memsearch(sub_ptr, sub_len, search_start, search_len, enc);
+ if (pos < 0) return pos;
+ t = rb_enc_right_char_head(search_start, search_start+pos, str_ptr_end, enc);
+ if (t == search_start + pos) break;
+ search_len -= t - search_start;
+ if (search_len <= 0) return -1;
+ offset += t - search_start;
+ search_start = t;
}
return pos + offset;
}
+/* found index in byte */
#define rb_str_index(str, sub, offset) rb_strseq_index(str, sub, offset, 0)
+#define rb_str_byteindex(str, sub, offset) rb_strseq_index(str, sub, offset, 1)
static long
rb_strseq_index(VALUE str, VALUE sub, long offset, int in_byte)
@@ -3878,17 +4026,17 @@ rb_strseq_index(VALUE str, VALUE sub, long offset, int in_byte)
if (str_len < sub_len) return -1;
if (offset != 0) {
- long str_len_char, sub_len_char;
+ long str_len_char, sub_len_char;
int single_byte = single_byte_optimizable(str);
- str_len_char = (in_byte || single_byte) ? str_len : str_strlen(str, enc);
- sub_len_char = in_byte ? sub_len : str_strlen(sub, enc);
- if (offset < 0) {
- offset += str_len_char;
- if (offset < 0) return -1;
- }
- if (str_len_char - offset < sub_len_char) return -1;
- if (!in_byte) offset = str_offset(str_ptr, str_ptr_end, offset, enc, single_byte);
- str_ptr += offset;
+ str_len_char = (in_byte || single_byte) ? str_len : str_strlen(str, enc);
+ sub_len_char = in_byte ? sub_len : str_strlen(sub, enc);
+ if (offset < 0) {
+ offset += str_len_char;
+ if (offset < 0) return -1;
+ }
+ if (str_len_char - offset < sub_len_char) return -1;
+ if (!in_byte) offset = str_offset(str_ptr, str_ptr_end, offset, enc, single_byte);
+ str_ptr += offset;
}
if (sub_len == 0) return offset;
@@ -3902,123 +4050,185 @@ rb_strseq_index(VALUE str, VALUE sub, long offset, int in_byte)
* index(substring, offset = 0) -> integer or nil
* index(regexp, offset = 0) -> integer or nil
*
- * Returns the \Integer index of the first occurrence of the given +substring+,
+ * :include: doc/string/index.rdoc
+ *
+ */
+
+static VALUE
+rb_str_index_m(int argc, VALUE *argv, VALUE str)
+{
+ VALUE sub;
+ VALUE initpos;
+ rb_encoding *enc = STR_ENC_GET(str);
+ long pos;
+
+ if (rb_scan_args(argc, argv, "11", &sub, &initpos) == 2) {
+ long slen = str_strlen(str, enc); /* str's enc */
+ pos = NUM2LONG(initpos);
+ if (pos < 0 ? (pos += slen) < 0 : pos > slen) {
+ if (RB_TYPE_P(sub, T_REGEXP)) {
+ rb_backref_set(Qnil);
+ }
+ return Qnil;
+ }
+ }
+ else {
+ pos = 0;
+ }
+
+ if (RB_TYPE_P(sub, T_REGEXP)) {
+ pos = str_offset(RSTRING_PTR(str), RSTRING_END(str), pos,
+ enc, single_byte_optimizable(str));
+
+ if (rb_reg_search(sub, str, pos, 0) >= 0) {
+ VALUE match = rb_backref_get();
+ struct re_registers *regs = RMATCH_REGS(match);
+ pos = rb_str_sublen(str, BEG(0));
+ return LONG2NUM(pos);
+ }
+ }
+ else {
+ StringValue(sub);
+ pos = rb_str_index(str, sub, pos);
+ if (pos >= 0) {
+ pos = rb_str_sublen(str, pos);
+ return LONG2NUM(pos);
+ }
+ }
+ return Qnil;
+}
+
+/* Ensure that the given pos is a valid character boundary.
+ * Note that in this function, "character" means a code point
+ * (Unicode scalar value), not a grapheme cluster.
+ */
+static void
+str_ensure_byte_pos(VALUE str, long pos)
+{
+ const char *s = RSTRING_PTR(str);
+ const char *e = RSTRING_END(str);
+ const char *p = s + pos;
+ if (!at_char_boundary(s, p, e, rb_enc_get(str))) {
+ rb_raise(rb_eIndexError,
+ "offset %ld does not land on character boundary", pos);
+ }
+}
+
+/*
+ * call-seq:
+ * byteindex(substring, offset = 0) -> integer or nil
+ * byteindex(regexp, offset = 0) -> integer or nil
+ *
+ * Returns the Integer byte-based index of the first occurrence of the given +substring+,
* or +nil+ if none found:
*
- * 'foo'.index('f') # => 0
- * 'foo'.index('o') # => 1
- * 'foo'.index('oo') # => 1
- * 'foo'.index('ooo') # => nil
+ * 'foo'.byteindex('f') # => 0
+ * 'foo'.byteindex('o') # => 1
+ * 'foo'.byteindex('oo') # => 1
+ * 'foo'.byteindex('ooo') # => nil
*
- * Returns the \Integer index of the first match for the given \Regexp +regexp+,
+ * Returns the Integer byte-based index of the first match for the given Regexp +regexp+,
* or +nil+ if none found:
*
- * 'foo'.index(/f/) # => 0
- * 'foo'.index(/o/) # => 1
- * 'foo'.index(/oo/) # => 1
- * 'foo'.index(/ooo/) # => nil
+ * 'foo'.byteindex(/f/) # => 0
+ * 'foo'.byteindex(/o/) # => 1
+ * 'foo'.byteindex(/oo/) # => 1
+ * 'foo'.byteindex(/ooo/) # => nil
*
- * \Integer argument +offset+, if given, specifies the position in the
+ * Integer argument +offset+, if given, specifies the byte-based position in the
* string to begin the search:
*
- * 'foo'.index('o', 1) # => 1
- * 'foo'.index('o', 2) # => 2
- * 'foo'.index('o', 3) # => nil
+ * 'foo'.byteindex('o', 1) # => 1
+ * 'foo'.byteindex('o', 2) # => 2
+ * 'foo'.byteindex('o', 3) # => nil
*
* If +offset+ is negative, counts backward from the end of +self+:
*
- * 'foo'.index('o', -1) # => 2
- * 'foo'.index('o', -2) # => 1
- * 'foo'.index('o', -3) # => 1
- * 'foo'.index('o', -4) # => nil
+ * 'foo'.byteindex('o', -1) # => 2
+ * 'foo'.byteindex('o', -2) # => 1
+ * 'foo'.byteindex('o', -3) # => 1
+ * 'foo'.byteindex('o', -4) # => nil
*
- * Related: String#rindex.
+ * If +offset+ does not land on character (codepoint) boundary, +IndexError+ is
+ * raised.
+ *
+ * Related: String#index, String#byterindex.
*/
static VALUE
-rb_str_index_m(int argc, VALUE *argv, VALUE str)
+rb_str_byteindex_m(int argc, VALUE *argv, VALUE str)
{
VALUE sub;
VALUE initpos;
long pos;
if (rb_scan_args(argc, argv, "11", &sub, &initpos) == 2) {
- pos = NUM2LONG(initpos);
+ long slen = RSTRING_LEN(str);
+ pos = NUM2LONG(initpos);
+ if (pos < 0 ? (pos += slen) < 0 : pos > slen) {
+ if (RB_TYPE_P(sub, T_REGEXP)) {
+ rb_backref_set(Qnil);
+ }
+ return Qnil;
+ }
}
else {
- pos = 0;
- }
- if (pos < 0) {
- pos += str_strlen(str, NULL);
- if (pos < 0) {
- if (RB_TYPE_P(sub, T_REGEXP)) {
- rb_backref_set(Qnil);
- }
- return Qnil;
- }
+ pos = 0;
}
- if (RB_TYPE_P(sub, T_REGEXP)) {
- if (pos > str_strlen(str, NULL))
- return Qnil;
- pos = str_offset(RSTRING_PTR(str), RSTRING_END(str), pos,
- rb_enc_check(str, sub), single_byte_optimizable(str));
+ str_ensure_byte_pos(str, pos);
- if (rb_reg_search(sub, str, pos, 0) < 0) {
- return Qnil;
- }
- else {
+ if (RB_TYPE_P(sub, T_REGEXP)) {
+ if (rb_reg_search(sub, str, pos, 0) >= 0) {
VALUE match = rb_backref_get();
struct re_registers *regs = RMATCH_REGS(match);
- pos = rb_str_sublen(str, BEG(0));
+ pos = BEG(0);
return LONG2NUM(pos);
}
}
else {
StringValue(sub);
- pos = rb_str_index(str, sub, pos);
- pos = rb_str_sublen(str, pos);
+ pos = rb_str_byteindex(str, sub, pos);
+ if (pos >= 0) return LONG2NUM(pos);
}
-
- if (pos == -1) return Qnil;
- return LONG2NUM(pos);
+ return Qnil;
}
#ifdef HAVE_MEMRCHR
static long
-str_rindex(VALUE str, VALUE sub, const char *s, long pos, rb_encoding *enc)
+str_rindex(VALUE str, VALUE sub, const char *s, rb_encoding *enc)
{
char *hit, *adjusted;
int c;
long slen, searchlen;
char *sbeg, *e, *t;
- slen = RSTRING_LEN(sub);
- if (slen == 0) return pos;
sbeg = RSTRING_PTR(str);
+ slen = RSTRING_LEN(sub);
+ if (slen == 0) return s - sbeg;
e = RSTRING_END(str);
t = RSTRING_PTR(sub);
c = *t & 0xff;
searchlen = s - sbeg + 1;
do {
- hit = memrchr(sbeg, c, searchlen);
- if (!hit) break;
- adjusted = rb_enc_left_char_head(sbeg, hit, e, enc);
- if (hit != adjusted) {
- searchlen = adjusted - sbeg;
- continue;
- }
- if (memcmp(hit, t, slen) == 0)
- return rb_str_sublen(str, hit - sbeg);
- searchlen = adjusted - sbeg;
+ hit = memrchr(sbeg, c, searchlen);
+ if (!hit) break;
+ adjusted = rb_enc_left_char_head(sbeg, hit, e, enc);
+ if (hit != adjusted) {
+ searchlen = adjusted - sbeg;
+ continue;
+ }
+ if (memcmp(hit, t, slen) == 0)
+ return hit - sbeg;
+ searchlen = adjusted - sbeg;
} while (searchlen > 0);
return -1;
}
#else
static long
-str_rindex(VALUE str, VALUE sub, const char *s, long pos, rb_encoding *enc)
+str_rindex(VALUE str, VALUE sub, const char *s, rb_encoding *enc)
{
long slen;
char *sbeg, *e, *t;
@@ -4029,18 +4239,18 @@ str_rindex(VALUE str, VALUE sub, const char *s, long pos, rb_encoding *enc)
slen = RSTRING_LEN(sub);
while (s) {
- if (memcmp(s, t, slen) == 0) {
- return pos;
- }
- if (pos == 0) break;
- pos--;
- s = rb_enc_prev_char(sbeg, s, e, enc);
+ if (memcmp(s, t, slen) == 0) {
+ return s - sbeg;
+ }
+ if (s <= sbeg) break;
+ s = rb_enc_prev_char(sbeg, s, e, enc);
}
return -1;
}
#endif
+/* found index in byte */
static long
rb_str_rindex(VALUE str, VALUE sub, long pos)
{
@@ -4063,23 +4273,22 @@ rb_str_rindex(VALUE str, VALUE sub, long pos)
sbeg = RSTRING_PTR(str);
if (pos == 0) {
- if (memcmp(sbeg, RSTRING_PTR(sub), RSTRING_LEN(sub)) == 0)
- return 0;
- else
- return -1;
+ if (memcmp(sbeg, RSTRING_PTR(sub), RSTRING_LEN(sub)) == 0)
+ return 0;
+ else
+ return -1;
}
s = str_nth(sbeg, RSTRING_END(str), pos, enc, singlebyte);
- return str_rindex(str, sub, s, pos, enc);
+ return str_rindex(str, sub, s, enc);
}
-
/*
* call-seq:
* rindex(substring, offset = self.length) -> integer or nil
* rindex(regexp, offset = self.length) -> integer or nil
*
- * Returns the \Integer index of the _last_ occurrence of the given +substring+,
+ * Returns the Integer index of the _last_ occurrence of the given +substring+,
* or +nil+ if none found:
*
* 'foo'.rindex('f') # => 0
@@ -4087,7 +4296,7 @@ rb_str_rindex(VALUE str, VALUE sub, long pos)
* 'foo'.rindex('oo') # => 1
* 'foo'.rindex('ooo') # => nil
*
- * Returns the \Integer index of the _last_ match for the given \Regexp +regexp+,
+ * Returns the Integer index of the _last_ match for the given Regexp +regexp+,
* or +nil+ if none found:
*
* 'foo'.rindex(/f/) # => 0
@@ -4095,7 +4304,24 @@ rb_str_rindex(VALUE str, VALUE sub, long pos)
* 'foo'.rindex(/oo/) # => 1
* 'foo'.rindex(/ooo/) # => nil
*
- * \Integer argument +offset+, if given and non-negative, specifies the maximum starting position in the
+ * The _last_ match means starting at the possible last position, not
+ * the last of longest matches.
+ *
+ * 'foo'.rindex(/o+/) # => 2
+ * $~ #=> #<MatchData "o">
+ *
+ * To get the last longest match, needs to combine with negative
+ * lookbehind.
+ *
+ * 'foo'.rindex(/(?<!o)o+/) # => 1
+ * $~ #=> #<MatchData "oo">
+ *
+ * Or String#index with negative lookforward.
+ *
+ * 'foo'.index(/o+(?!.*o)/) # => 1
+ * $~ #=> #<MatchData "oo">
+ *
+ * Integer argument +offset+, if given and non-negative, specifies the maximum starting position in the
* string to _end_ the search:
*
* 'foo'.rindex('o', 0) # => nil
@@ -4103,7 +4329,7 @@ rb_str_rindex(VALUE str, VALUE sub, long pos)
* 'foo'.rindex('o', 2) # => 2
* 'foo'.rindex('o', 3) # => 2
*
- * If +offset+ is a negative \Integer, the maximum starting position in the
+ * If +offset+ is a negative Integer, the maximum starting position in the
* string to _end_ the search is the sum of the string's length and +offset+:
*
* 'foo'.rindex('o', -1) # => 2
@@ -4118,33 +4344,30 @@ static VALUE
rb_str_rindex_m(int argc, VALUE *argv, VALUE str)
{
VALUE sub;
- VALUE vpos;
+ VALUE initpos;
rb_encoding *enc = STR_ENC_GET(str);
long pos, len = str_strlen(str, enc); /* str's enc */
- if (rb_scan_args(argc, argv, "11", &sub, &vpos) == 2) {
- pos = NUM2LONG(vpos);
- if (pos < 0) {
- pos += len;
- if (pos < 0) {
- if (RB_TYPE_P(sub, T_REGEXP)) {
- rb_backref_set(Qnil);
- }
- return Qnil;
- }
- }
- if (pos > len) pos = len;
+ if (rb_scan_args(argc, argv, "11", &sub, &initpos) == 2) {
+ pos = NUM2LONG(initpos);
+ if (pos < 0 && (pos += len) < 0) {
+ if (RB_TYPE_P(sub, T_REGEXP)) {
+ rb_backref_set(Qnil);
+ }
+ return Qnil;
+ }
+ if (pos > len) pos = len;
}
else {
- pos = len;
+ pos = len;
}
if (RB_TYPE_P(sub, T_REGEXP)) {
- /* enc = rb_get_check(str, sub); */
- pos = str_offset(RSTRING_PTR(str), RSTRING_END(str), pos,
- enc, single_byte_optimizable(str));
+ /* enc = rb_enc_check(str, sub); */
+ pos = str_offset(RSTRING_PTR(str), RSTRING_END(str), pos,
+ enc, single_byte_optimizable(str));
- if (rb_reg_search(sub, str, pos, 1) >= 0) {
+ if (rb_reg_search(sub, str, pos, 1) >= 0) {
VALUE match = rb_backref_get();
struct re_registers *regs = RMATCH_REGS(match);
pos = rb_str_sublen(str, BEG(0));
@@ -4153,8 +4376,141 @@ rb_str_rindex_m(int argc, VALUE *argv, VALUE str)
}
else {
StringValue(sub);
- pos = rb_str_rindex(str, sub, pos);
- if (pos >= 0) return LONG2NUM(pos);
+ pos = rb_str_rindex(str, sub, pos);
+ if (pos >= 0) {
+ pos = rb_str_sublen(str, pos);
+ return LONG2NUM(pos);
+ }
+ }
+ return Qnil;
+}
+
+static long
+rb_str_byterindex(VALUE str, VALUE sub, long pos)
+{
+ long len, slen;
+ char *sbeg, *s;
+ rb_encoding *enc;
+
+ enc = rb_enc_check(str, sub);
+ if (is_broken_string(sub)) return -1;
+ len = RSTRING_LEN(str);
+ slen = RSTRING_LEN(sub);
+
+ /* substring longer than string */
+ if (len < slen) return -1;
+ if (len - pos < slen) pos = len - slen;
+ if (len == 0) return pos;
+
+ sbeg = RSTRING_PTR(str);
+
+ if (pos == 0) {
+ if (memcmp(sbeg, RSTRING_PTR(sub), RSTRING_LEN(sub)) == 0)
+ return 0;
+ else
+ return -1;
+ }
+
+ s = sbeg + pos;
+ return str_rindex(str, sub, s, enc);
+}
+
+
+/*
+ * call-seq:
+ * byterindex(substring, offset = self.bytesize) -> integer or nil
+ * byterindex(regexp, offset = self.bytesize) -> integer or nil
+ *
+ * Returns the Integer byte-based index of the _last_ occurrence of the given +substring+,
+ * or +nil+ if none found:
+ *
+ * 'foo'.byterindex('f') # => 0
+ * 'foo'.byterindex('o') # => 2
+ * 'foo'.byterindex('oo') # => 1
+ * 'foo'.byterindex('ooo') # => nil
+ *
+ * Returns the Integer byte-based index of the _last_ match for the given Regexp +regexp+,
+ * or +nil+ if none found:
+ *
+ * 'foo'.byterindex(/f/) # => 0
+ * 'foo'.byterindex(/o/) # => 2
+ * 'foo'.byterindex(/oo/) # => 1
+ * 'foo'.byterindex(/ooo/) # => nil
+ *
+ * The _last_ match means starting at the possible last position, not
+ * the last of longest matches.
+ *
+ * 'foo'.byterindex(/o+/) # => 2
+ * $~ #=> #<MatchData "o">
+ *
+ * To get the last longest match, needs to combine with negative
+ * lookbehind.
+ *
+ * 'foo'.byterindex(/(?<!o)o+/) # => 1
+ * $~ #=> #<MatchData "oo">
+ *
+ * Or String#byteindex with negative lookforward.
+ *
+ * 'foo'.byteindex(/o+(?!.*o)/) # => 1
+ * $~ #=> #<MatchData "oo">
+ *
+ * Integer argument +offset+, if given and non-negative, specifies the maximum starting byte-based position in the
+ * string to _end_ the search:
+ *
+ * 'foo'.byterindex('o', 0) # => nil
+ * 'foo'.byterindex('o', 1) # => 1
+ * 'foo'.byterindex('o', 2) # => 2
+ * 'foo'.byterindex('o', 3) # => 2
+ *
+ * If +offset+ is a negative Integer, the maximum starting position in the
+ * string to _end_ the search is the sum of the string's length and +offset+:
+ *
+ * 'foo'.byterindex('o', -1) # => 2
+ * 'foo'.byterindex('o', -2) # => 1
+ * 'foo'.byterindex('o', -3) # => nil
+ * 'foo'.byterindex('o', -4) # => nil
+ *
+ * If +offset+ does not land on character (codepoint) boundary, +IndexError+ is
+ * raised.
+ *
+ * Related: String#byteindex.
+ */
+
+static VALUE
+rb_str_byterindex_m(int argc, VALUE *argv, VALUE str)
+{
+ VALUE sub;
+ VALUE initpos;
+ long pos, len = RSTRING_LEN(str);
+
+ if (rb_scan_args(argc, argv, "11", &sub, &initpos) == 2) {
+ pos = NUM2LONG(initpos);
+ if (pos < 0 && (pos += len) < 0) {
+ if (RB_TYPE_P(sub, T_REGEXP)) {
+ rb_backref_set(Qnil);
+ }
+ return Qnil;
+ }
+ if (pos > len) pos = len;
+ }
+ else {
+ pos = len;
+ }
+
+ str_ensure_byte_pos(str, pos);
+
+ if (RB_TYPE_P(sub, T_REGEXP)) {
+ if (rb_reg_search(sub, str, pos, 1) >= 0) {
+ VALUE match = rb_backref_get();
+ struct re_registers *regs = RMATCH_REGS(match);
+ pos = BEG(0);
+ return LONG2NUM(pos);
+ }
+ }
+ else {
+ StringValue(sub);
+ pos = rb_str_byterindex(str, sub, pos);
+ if (pos >= 0) return LONG2NUM(pos);
}
return Qnil;
}
@@ -4164,21 +4520,20 @@ rb_str_rindex_m(int argc, VALUE *argv, VALUE str)
* string =~ regexp -> integer or nil
* string =~ object -> integer or nil
*
- * Returns the \Integer index of the first substring that matches
+ * Returns the Integer index of the first substring that matches
* the given +regexp+, or +nil+ if no match found:
*
* 'foo' =~ /f/ # => 0
* 'foo' =~ /o/ # => 1
* 'foo' =~ /x/ # => nil
*
- * Note: also updates
- * {Regexp-related global variables}[Regexp.html#class-Regexp-label-Special+global+variables].
+ * Note: also updates Regexp@Global+Variables.
*
- * If the given +object+ is not a \Regexp, returns the value
+ * If the given +object+ is not a Regexp, returns the value
* returned by <tt>object =~ self</tt>.
*
* Note that <tt>string =~ regexp</tt> is different from <tt>regexp =~ string</tt>
- * (see {Regexp#=~}[https://ruby-doc.org/core-2.7.1/Regexp.html#method-i-3D-7E]):
+ * (see Regexp#=~):
*
* number= nil
* "no. 9" =~ /(?<number>\d+)/
@@ -4193,13 +4548,13 @@ rb_str_match(VALUE x, VALUE y)
{
switch (OBJ_BUILTIN_TYPE(y)) {
case T_STRING:
- rb_raise(rb_eTypeError, "type mismatch: String given");
+ rb_raise(rb_eTypeError, "type mismatch: String given");
case T_REGEXP:
- return rb_reg_match(y, x);
+ return rb_reg_match(y, x);
default:
- return rb_funcall(y, idEqTilde, 1, x);
+ return rb_funcall(y, idEqTilde, 1, x);
}
}
@@ -4212,14 +4567,13 @@ static VALUE get_pat(VALUE);
* match(pattern, offset = 0) -> matchdata or nil
* match(pattern, offset = 0) {|matchdata| ... } -> object
*
- * Returns a \Matchdata object (or +nil+) based on +self+ and the given +pattern+.
+ * Returns a MatchData object (or +nil+) based on +self+ and the given +pattern+.
*
- * Note: also updates
- * {Regexp-related global variables}[Regexp.html#class-Regexp-label-Special+global+variables].
+ * Note: also updates Regexp@Global+Variables.
*
- * - Computes +regexp+ by converting +pattern+ (if not already a \Regexp).
+ * - Computes +regexp+ by converting +pattern+ (if not already a Regexp).
* regexp = Regexp.new(pattern)
- * - Computes +matchdata+, which will be either a \MatchData object or +nil+
+ * - Computes +matchdata+, which will be either a MatchData object or +nil+
* (see Regexp#match):
* matchdata = <tt>regexp.match(self)
*
@@ -4229,7 +4583,7 @@ static VALUE get_pat(VALUE);
* 'foo'.match('o') # => #<MatchData "o">
* 'foo'.match('x') # => nil
*
- * If \Integer argument +offset+ is given, the search begins at index +offset+:
+ * If Integer argument +offset+ is given, the search begins at index +offset+:
*
* 'foo'.match('f', 1) # => nil
* 'foo'.match('o', 1) # => #<MatchData "o">
@@ -4248,12 +4602,12 @@ rb_str_match_m(int argc, VALUE *argv, VALUE str)
{
VALUE re, result;
if (argc < 1)
- rb_check_arity(argc, 1, 2);
+ rb_check_arity(argc, 1, 2);
re = argv[0];
argv[0] = str;
result = rb_funcallv(get_pat(re), rb_intern("match"), argc, argv);
if (!NIL_P(result) && rb_block_given_p()) {
- return rb_yield(result);
+ return rb_yield(result);
}
return result;
}
@@ -4264,20 +4618,19 @@ rb_str_match_m(int argc, VALUE *argv, VALUE str)
*
* Returns +true+ or +false+ based on whether a match is found for +self+ and +pattern+.
*
- * Note: does not update
- * {Regexp-related global variables}[Regexp.html#class-Regexp-label-Special+global+variables].
+ * Note: does not update Regexp@Global+Variables.
*
- * Computes +regexp+ by converting +pattern+ (if not already a \Regexp).
+ * Computes +regexp+ by converting +pattern+ (if not already a Regexp).
* regexp = Regexp.new(pattern)
*
- * Returns +true+ if <tt>self+.match(regexp)</tt> returns a \Matchdata object,
+ * Returns +true+ if <tt>self+.match(regexp)</tt> returns a MatchData object,
* +false+ otherwise:
*
* 'foo'.match?(/o/) # => true
* 'foo'.match?('o') # => true
* 'foo'.match?(/x/) # => false
*
- * If \Integer argument +offset+ is given, the search begins at index +offset+:
+ * If Integer argument +offset+ is given, the search begins at index +offset+:
* 'foo'.match?('f', 1) # => false
* 'foo'.match?('o', 1) # => true
*
@@ -4305,21 +4658,21 @@ enc_succ_char(char *p, long len, rb_encoding *enc)
int l;
if (rb_enc_mbminlen(enc) > 1) {
- /* wchar, trivial case */
- int r = rb_enc_precise_mbclen(p, p + len, enc), c;
- if (!MBCLEN_CHARFOUND_P(r)) {
- return NEIGHBOR_NOT_CHAR;
- }
- c = rb_enc_mbc_to_codepoint(p, p + len, enc) + 1;
- l = rb_enc_code_to_mbclen(c, enc);
- if (!l) return NEIGHBOR_NOT_CHAR;
- if (l != len) return NEIGHBOR_WRAPPED;
- rb_enc_mbcput(c, p, enc);
- r = rb_enc_precise_mbclen(p, p + len, enc);
- if (!MBCLEN_CHARFOUND_P(r)) {
- return NEIGHBOR_NOT_CHAR;
- }
- return NEIGHBOR_FOUND;
+ /* wchar, trivial case */
+ int r = rb_enc_precise_mbclen(p, p + len, enc), c;
+ if (!MBCLEN_CHARFOUND_P(r)) {
+ return NEIGHBOR_NOT_CHAR;
+ }
+ c = rb_enc_mbc_to_codepoint(p, p + len, enc) + 1;
+ l = rb_enc_code_to_mbclen(c, enc);
+ if (!l) return NEIGHBOR_NOT_CHAR;
+ if (l != len) return NEIGHBOR_WRAPPED;
+ rb_enc_mbcput(c, p, enc);
+ r = rb_enc_precise_mbclen(p, p + len, enc);
+ if (!MBCLEN_CHARFOUND_P(r)) {
+ return NEIGHBOR_NOT_CHAR;
+ }
+ return NEIGHBOR_FOUND;
}
while (1) {
for (i = len-1; 0 <= i && (unsigned char)p[i] == 0xff; i--)
@@ -4356,23 +4709,23 @@ enc_pred_char(char *p, long len, rb_encoding *enc)
long i;
int l;
if (rb_enc_mbminlen(enc) > 1) {
- /* wchar, trivial case */
- int r = rb_enc_precise_mbclen(p, p + len, enc), c;
- if (!MBCLEN_CHARFOUND_P(r)) {
- return NEIGHBOR_NOT_CHAR;
- }
- c = rb_enc_mbc_to_codepoint(p, p + len, enc);
- if (!c) return NEIGHBOR_NOT_CHAR;
- --c;
- l = rb_enc_code_to_mbclen(c, enc);
- if (!l) return NEIGHBOR_NOT_CHAR;
- if (l != len) return NEIGHBOR_WRAPPED;
- rb_enc_mbcput(c, p, enc);
- r = rb_enc_precise_mbclen(p, p + len, enc);
- if (!MBCLEN_CHARFOUND_P(r)) {
- return NEIGHBOR_NOT_CHAR;
- }
- return NEIGHBOR_FOUND;
+ /* wchar, trivial case */
+ int r = rb_enc_precise_mbclen(p, p + len, enc), c;
+ if (!MBCLEN_CHARFOUND_P(r)) {
+ return NEIGHBOR_NOT_CHAR;
+ }
+ c = rb_enc_mbc_to_codepoint(p, p + len, enc);
+ if (!c) return NEIGHBOR_NOT_CHAR;
+ --c;
+ l = rb_enc_code_to_mbclen(c, enc);
+ if (!l) return NEIGHBOR_NOT_CHAR;
+ if (l != len) return NEIGHBOR_WRAPPED;
+ rb_enc_mbcput(c, p, enc);
+ r = rb_enc_precise_mbclen(p, p + len, enc);
+ if (!MBCLEN_CHARFOUND_P(r)) {
+ return NEIGHBOR_NOT_CHAR;
+ }
+ return NEIGHBOR_FOUND;
}
while (1) {
for (i = len-1; 0 <= i && (unsigned char)p[i] == 0; i--)
@@ -4435,12 +4788,12 @@ enc_succ_alnum_char(char *p, long len, rb_encoding *enc, char *carry)
MEMCPY(save, p, char, len);
for (try = 0; try <= max_gaps; ++try) {
- ret = enc_succ_char(p, len, enc);
- if (ret == NEIGHBOR_FOUND) {
- c = rb_enc_mbc_to_codepoint(p, p+len, enc);
- if (rb_enc_isctype(c, ctype, enc))
- return NEIGHBOR_FOUND;
- }
+ ret = enc_succ_char(p, len, enc);
+ if (ret == NEIGHBOR_FOUND) {
+ c = rb_enc_mbc_to_codepoint(p, p+len, enc);
+ if (rb_enc_isctype(c, ctype, enc))
+ return NEIGHBOR_FOUND;
+ }
}
MEMCPY(p, save, char, len);
range = 1;
@@ -4529,11 +4882,10 @@ static VALUE str_succ(VALUE str);
* s = '99zz99zz'
* s.succ # => "100aa00aa"
*
- * The successor to an empty \String is a new empty \String:
+ * The successor to an empty +String+ is a new empty +String+:
*
* ''.succ # => ""
*
- * String#next is an alias for String#succ.
*/
VALUE
@@ -4564,50 +4916,50 @@ str_succ(VALUE str)
s = e = sbeg + slen;
while ((s = rb_enc_prev_char(sbeg, s, e, enc)) != 0) {
- if (neighbor == NEIGHBOR_NOT_CHAR && last_alnum) {
- if (ISALPHA(*last_alnum) ? ISDIGIT(*s) :
- ISDIGIT(*last_alnum) ? ISALPHA(*s) : 0) {
- break;
- }
- }
- l = rb_enc_precise_mbclen(s, e, enc);
- if (!ONIGENC_MBCLEN_CHARFOUND_P(l)) continue;
- l = ONIGENC_MBCLEN_CHARFOUND_LEN(l);
+ if (neighbor == NEIGHBOR_NOT_CHAR && last_alnum) {
+ if (ISALPHA(*last_alnum) ? ISDIGIT(*s) :
+ ISDIGIT(*last_alnum) ? ISALPHA(*s) : 0) {
+ break;
+ }
+ }
+ l = rb_enc_precise_mbclen(s, e, enc);
+ if (!ONIGENC_MBCLEN_CHARFOUND_P(l)) continue;
+ l = ONIGENC_MBCLEN_CHARFOUND_LEN(l);
neighbor = enc_succ_alnum_char(s, l, enc, carry);
switch (neighbor) {
- case NEIGHBOR_NOT_CHAR:
- continue;
- case NEIGHBOR_FOUND:
- return str;
- case NEIGHBOR_WRAPPED:
- last_alnum = s;
- break;
- }
+ case NEIGHBOR_NOT_CHAR:
+ continue;
+ case NEIGHBOR_FOUND:
+ return str;
+ case NEIGHBOR_WRAPPED:
+ last_alnum = s;
+ break;
+ }
found_alnum = 1;
carry_pos = s - sbeg;
carry_len = l;
}
if (!found_alnum) { /* str contains no alnum */
- s = e;
- while ((s = rb_enc_prev_char(sbeg, s, e, enc)) != 0) {
+ s = e;
+ while ((s = rb_enc_prev_char(sbeg, s, e, enc)) != 0) {
enum neighbor_char neighbor;
- char tmp[ONIGENC_CODE_TO_MBC_MAXLEN];
- l = rb_enc_precise_mbclen(s, e, enc);
- if (!ONIGENC_MBCLEN_CHARFOUND_P(l)) continue;
- l = ONIGENC_MBCLEN_CHARFOUND_LEN(l);
- MEMCPY(tmp, s, char, l);
- neighbor = enc_succ_char(tmp, l, enc);
- switch (neighbor) {
- case NEIGHBOR_FOUND:
- MEMCPY(s, tmp, char, l);
+ char tmp[ONIGENC_CODE_TO_MBC_MAXLEN];
+ l = rb_enc_precise_mbclen(s, e, enc);
+ if (!ONIGENC_MBCLEN_CHARFOUND_P(l)) continue;
+ l = ONIGENC_MBCLEN_CHARFOUND_LEN(l);
+ MEMCPY(tmp, s, char, l);
+ neighbor = enc_succ_char(tmp, l, enc);
+ switch (neighbor) {
+ case NEIGHBOR_FOUND:
+ MEMCPY(s, tmp, char, l);
return str;
- break;
- case NEIGHBOR_WRAPPED:
- MEMCPY(s, tmp, char, l);
- break;
- case NEIGHBOR_NOT_CHAR:
- break;
- }
+ break;
+ case NEIGHBOR_WRAPPED:
+ MEMCPY(s, tmp, char, l);
+ break;
+ case NEIGHBOR_NOT_CHAR:
+ break;
+ }
if (rb_enc_precise_mbclen(s, s+l, enc) != l) {
/* wrapped to \0...\0. search next valid char. */
enc_succ_char(s, l, enc);
@@ -4617,8 +4969,8 @@ str_succ(VALUE str)
carry_len = l;
}
carry_pos = s - sbeg;
- }
- ENC_CODERANGE_SET(str, ENC_CODERANGE_UNKNOWN);
+ }
+ ENC_CODERANGE_SET(str, ENC_CODERANGE_UNKNOWN);
}
RESIZE_CAPA(str, slen + carry_len);
sbeg = RSTRING_PTR(str);
@@ -4638,8 +4990,6 @@ str_succ(VALUE str)
* succ! -> self
*
* Equivalent to String#succ, but modifies +self+ in place; returns +self+.
- *
- * String#next! is an alias for String#succ!.
*/
static VALUE
@@ -4654,8 +5004,8 @@ static int
all_digits_p(const char *s, long len)
{
while (len-- > 0) {
- if (!ISDIGIT(*s)) return 0;
- s++;
+ if (!ISDIGIT(*s)) return 0;
+ s++;
}
return 1;
}
@@ -4672,7 +5022,7 @@ str_upto_i(VALUE str, VALUE arg)
* upto(other_string, exclusive = false) {|string| ... } -> self
* upto(other_string, exclusive = false) -> new_enumerator
*
- * With a block given, calls the block with each \String value
+ * With a block given, calls the block with each +String+ value
* returned by successive calls to String#succ;
* the first value is +self+, the next is <tt>self.succ</tt>, and so on;
* the sequence terminates when value +other_string+ is reached;
@@ -4696,7 +5046,7 @@ str_upto_i(VALUE str, VALUE arg)
* '25'.upto('5') {|s| fail s }
* 'aa'.upto('a') {|s| fail s }
*
- * With no block given, returns a new \Enumerator:
+ * With no block given, returns a new Enumerator:
*
* 'a8'.upto('b6') # => #<Enumerator: "a8":upto("b6")>
*
@@ -4726,51 +5076,51 @@ rb_str_upto_each(VALUE beg, VALUE end, int excl, int (*each)(VALUE, VALUE), VALU
ascii = (is_ascii_string(beg) && is_ascii_string(end));
/* single character */
if (RSTRING_LEN(beg) == 1 && RSTRING_LEN(end) == 1 && ascii) {
- char c = RSTRING_PTR(beg)[0];
- char e = RSTRING_PTR(end)[0];
-
- if (c > e || (excl && c == e)) return beg;
- for (;;) {
- if ((*each)(rb_enc_str_new(&c, 1, enc), arg)) break;
- if (!excl && c == e) break;
- c++;
- if (excl && c == e) break;
- }
- return beg;
+ char c = RSTRING_PTR(beg)[0];
+ char e = RSTRING_PTR(end)[0];
+
+ if (c > e || (excl && c == e)) return beg;
+ for (;;) {
+ if ((*each)(rb_enc_str_new(&c, 1, enc), arg)) break;
+ if (!excl && c == e) break;
+ c++;
+ if (excl && c == e) break;
+ }
+ return beg;
}
/* both edges are all digits */
if (ascii && ISDIGIT(RSTRING_PTR(beg)[0]) && ISDIGIT(RSTRING_PTR(end)[0]) &&
- all_digits_p(RSTRING_PTR(beg), RSTRING_LEN(beg)) &&
- all_digits_p(RSTRING_PTR(end), RSTRING_LEN(end))) {
- VALUE b, e;
- int width;
-
- width = RSTRING_LENINT(beg);
- b = rb_str_to_inum(beg, 10, FALSE);
- e = rb_str_to_inum(end, 10, FALSE);
- if (FIXNUM_P(b) && FIXNUM_P(e)) {
- long bi = FIX2LONG(b);
- long ei = FIX2LONG(e);
- rb_encoding *usascii = rb_usascii_encoding();
-
- while (bi <= ei) {
- if (excl && bi == ei) break;
- if ((*each)(rb_enc_sprintf(usascii, "%.*ld", width, bi), arg)) break;
- bi++;
- }
- }
- else {
- ID op = excl ? '<' : idLE;
- VALUE args[2], fmt = rb_fstring_lit("%.*d");
-
- args[0] = INT2FIX(width);
- while (rb_funcall(b, op, 1, e)) {
- args[1] = b;
- if ((*each)(rb_str_format(numberof(args), args, fmt), arg)) break;
- b = rb_funcallv(b, succ, 0, 0);
- }
- }
- return beg;
+ all_digits_p(RSTRING_PTR(beg), RSTRING_LEN(beg)) &&
+ all_digits_p(RSTRING_PTR(end), RSTRING_LEN(end))) {
+ VALUE b, e;
+ int width;
+
+ width = RSTRING_LENINT(beg);
+ b = rb_str_to_inum(beg, 10, FALSE);
+ e = rb_str_to_inum(end, 10, FALSE);
+ if (FIXNUM_P(b) && FIXNUM_P(e)) {
+ long bi = FIX2LONG(b);
+ long ei = FIX2LONG(e);
+ rb_encoding *usascii = rb_usascii_encoding();
+
+ while (bi <= ei) {
+ if (excl && bi == ei) break;
+ if ((*each)(rb_enc_sprintf(usascii, "%.*ld", width, bi), arg)) break;
+ bi++;
+ }
+ }
+ else {
+ ID op = excl ? '<' : idLE;
+ VALUE args[2], fmt = rb_fstring_lit("%.*d");
+
+ args[0] = INT2FIX(width);
+ while (rb_funcall(b, op, 1, e)) {
+ args[1] = b;
+ if ((*each)(rb_str_format(numberof(args), args, fmt), arg)) break;
+ b = rb_funcallv(b, succ, 0, 0);
+ }
+ }
+ return beg;
}
/* normal case */
n = rb_str_cmp(beg, end);
@@ -4779,16 +5129,16 @@ rb_str_upto_each(VALUE beg, VALUE end, int excl, int (*each)(VALUE, VALUE), VALU
after_end = rb_funcallv(end, succ, 0, 0);
current = str_duplicate(rb_cString, beg);
while (!rb_str_equal(current, after_end)) {
- VALUE next = Qnil;
- if (excl || !rb_str_equal(current, end))
- next = rb_funcallv(current, succ, 0, 0);
- if ((*each)(current, arg)) break;
- if (NIL_P(next)) break;
- current = next;
- StringValue(current);
- if (excl && rb_str_equal(current, end)) break;
- if (RSTRING_LEN(current) > RSTRING_LEN(end) || RSTRING_LEN(current) == 0)
- break;
+ VALUE next = Qnil;
+ if (excl || !rb_str_equal(current, end))
+ next = rb_funcallv(current, succ, 0, 0);
+ if ((*each)(current, arg)) break;
+ if (NIL_P(next)) break;
+ current = next;
+ StringValue(current);
+ if (excl && rb_str_equal(current, end)) break;
+ if (RSTRING_LEN(current) > RSTRING_LEN(end) || RSTRING_LEN(current) == 0)
+ break;
}
return beg;
@@ -4803,36 +5153,36 @@ rb_str_upto_endless_each(VALUE beg, int (*each)(VALUE, VALUE), VALUE arg)
CONST_ID(succ, "succ");
/* both edges are all digits */
if (is_ascii_string(beg) && ISDIGIT(RSTRING_PTR(beg)[0]) &&
- all_digits_p(RSTRING_PTR(beg), RSTRING_LEN(beg))) {
- VALUE b, args[2], fmt = rb_fstring_lit("%.*d");
- int width = RSTRING_LENINT(beg);
- b = rb_str_to_inum(beg, 10, FALSE);
- if (FIXNUM_P(b)) {
- long bi = FIX2LONG(b);
- rb_encoding *usascii = rb_usascii_encoding();
-
- while (FIXABLE(bi)) {
- if ((*each)(rb_enc_sprintf(usascii, "%.*ld", width, bi), arg)) break;
- bi++;
- }
- b = LONG2NUM(bi);
- }
- args[0] = INT2FIX(width);
- while (1) {
- args[1] = b;
- if ((*each)(rb_str_format(numberof(args), args, fmt), arg)) break;
- b = rb_funcallv(b, succ, 0, 0);
- }
+ all_digits_p(RSTRING_PTR(beg), RSTRING_LEN(beg))) {
+ VALUE b, args[2], fmt = rb_fstring_lit("%.*d");
+ int width = RSTRING_LENINT(beg);
+ b = rb_str_to_inum(beg, 10, FALSE);
+ if (FIXNUM_P(b)) {
+ long bi = FIX2LONG(b);
+ rb_encoding *usascii = rb_usascii_encoding();
+
+ while (FIXABLE(bi)) {
+ if ((*each)(rb_enc_sprintf(usascii, "%.*ld", width, bi), arg)) break;
+ bi++;
+ }
+ b = LONG2NUM(bi);
+ }
+ args[0] = INT2FIX(width);
+ while (1) {
+ args[1] = b;
+ if ((*each)(rb_str_format(numberof(args), args, fmt), arg)) break;
+ b = rb_funcallv(b, succ, 0, 0);
+ }
}
/* normal case */
current = str_duplicate(rb_cString, beg);
while (1) {
- VALUE next = rb_funcallv(current, succ, 0, 0);
- if ((*each)(current, arg)) break;
- current = next;
- StringValue(current);
- if (RSTRING_LEN(current) == 0)
- break;
+ VALUE next = rb_funcallv(current, succ, 0, 0);
+ if ((*each)(current, arg)) break;
+ current = next;
+ StringValue(current);
+ if (RSTRING_LEN(current) == 0)
+ break;
}
return beg;
@@ -4857,32 +5207,32 @@ rb_str_include_range_p(VALUE beg, VALUE end, VALUE val, VALUE exclusive)
val = rb_check_string_type(val);
if (NIL_P(val)) return Qfalse;
if (rb_enc_asciicompat(STR_ENC_GET(beg)) &&
- rb_enc_asciicompat(STR_ENC_GET(end)) &&
- rb_enc_asciicompat(STR_ENC_GET(val))) {
- const char *bp = RSTRING_PTR(beg);
- const char *ep = RSTRING_PTR(end);
- const char *vp = RSTRING_PTR(val);
- if (RSTRING_LEN(beg) == 1 && RSTRING_LEN(end) == 1) {
- if (RSTRING_LEN(val) == 0 || RSTRING_LEN(val) > 1)
- return Qfalse;
- else {
- char b = *bp;
- char e = *ep;
- char v = *vp;
-
- if (ISASCII(b) && ISASCII(e) && ISASCII(v)) {
- if (b <= v && v < e) return Qtrue;
- return RBOOL(!RTEST(exclusive) && v == e);
- }
- }
- }
+ rb_enc_asciicompat(STR_ENC_GET(end)) &&
+ rb_enc_asciicompat(STR_ENC_GET(val))) {
+ const char *bp = RSTRING_PTR(beg);
+ const char *ep = RSTRING_PTR(end);
+ const char *vp = RSTRING_PTR(val);
+ if (RSTRING_LEN(beg) == 1 && RSTRING_LEN(end) == 1) {
+ if (RSTRING_LEN(val) == 0 || RSTRING_LEN(val) > 1)
+ return Qfalse;
+ else {
+ char b = *bp;
+ char e = *ep;
+ char v = *vp;
+
+ if (ISASCII(b) && ISASCII(e) && ISASCII(v)) {
+ if (b <= v && v < e) return Qtrue;
+ return RBOOL(!RTEST(exclusive) && v == e);
+ }
+ }
+ }
#if 0
- /* both edges are all digits */
- if (ISDIGIT(*bp) && ISDIGIT(*ep) &&
- all_digits_p(bp, RSTRING_LEN(beg)) &&
- all_digits_p(ep, RSTRING_LEN(end))) {
- /* TODO */
- }
+ /* both edges are all digits */
+ if (ISDIGIT(*bp) && ISDIGIT(*ep) &&
+ all_digits_p(bp, RSTRING_LEN(beg)) &&
+ all_digits_p(ep, RSTRING_LEN(end))) {
+ /* TODO */
+ }
#endif
}
rb_str_upto_each(beg, end, RTEST(exclusive), include_range_i, (VALUE)&val);
@@ -4896,7 +5246,7 @@ rb_str_subpat(VALUE str, VALUE re, VALUE backref)
if (rb_reg_search(re, str, 0, 0) >= 0) {
VALUE match = rb_backref_get();
int nth = rb_reg_backref_number(match, backref);
- return rb_reg_nth_match(nth, match);
+ return rb_reg_nth_match(nth, match);
}
return Qnil;
}
@@ -4907,28 +5257,28 @@ rb_str_aref(VALUE str, VALUE indx)
long idx;
if (FIXNUM_P(indx)) {
- idx = FIX2LONG(indx);
+ idx = FIX2LONG(indx);
}
else if (RB_TYPE_P(indx, T_REGEXP)) {
- return rb_str_subpat(str, indx, INT2FIX(0));
+ return rb_str_subpat(str, indx, INT2FIX(0));
}
else if (RB_TYPE_P(indx, T_STRING)) {
- if (rb_str_index(str, indx, 0) != -1)
+ if (rb_str_index(str, indx, 0) != -1)
return str_duplicate(rb_cString, indx);
- return Qnil;
+ return Qnil;
}
else {
- /* check if indx is Range */
- long beg, len = str_strlen(str, NULL);
- switch (rb_range_beg_len(indx, &beg, &len, len, 0)) {
- case Qfalse:
- break;
- case Qnil:
- return Qnil;
- default:
- return rb_str_substr(str, beg, len);
- }
- idx = NUM2LONG(indx);
+ /* check if indx is Range */
+ long beg, len = str_strlen(str, NULL);
+ switch (rb_range_beg_len(indx, &beg, &len, len, 0)) {
+ case Qfalse:
+ break;
+ case Qnil:
+ return Qnil;
+ default:
+ return rb_str_substr(str, beg, len);
+ }
+ idx = NUM2LONG(indx);
}
return str_substr(str, idx, 1, FALSE);
@@ -4944,100 +5294,23 @@ rb_str_aref(VALUE str, VALUE indx)
* string[substring] -> new_string or nil
*
* Returns the substring of +self+ specified by the arguments.
+ * See examples at {String Slices}[rdoc-ref:String@String+Slices].
*
- * When the single \Integer argument +index+ is given,
- * returns the 1-character substring found in +self+ at offset +index+:
- *
- * 'bar'[2] # => "r"
- *
- * Counts backward from the end of +self+ if +index+ is negative:
*
- * 'foo'[-3] # => "f"
- *
- * Returns +nil+ if +index+ is out of range:
- *
- * 'foo'[3] # => nil
- * 'foo'[-4] # => nil
- *
- * When the two \Integer arguments +start+ and +length+ are given,
- * returns the substring of the given +length+ found in +self+ at offset +start+:
- *
- * 'foo'[0, 2] # => "fo"
- * 'foo'[0, 0] # => ""
- *
- * Counts backward from the end of +self+ if +start+ is negative:
- *
- * 'foo'[-2, 2] # => "oo"
- *
- * Special case: returns a new empty \String if +start+ is equal to the length of +self+:
- *
- * 'foo'[3, 2] # => ""
- *
- * Returns +nil+ if +start+ is out of range:
- *
- * 'foo'[4, 2] # => nil
- * 'foo'[-4, 2] # => nil
- *
- * Returns the trailing substring of +self+ if +length+ is large:
- *
- * 'foo'[1, 50] # => "oo"
- *
- * Returns +nil+ if +length+ is negative:
- *
- * 'foo'[0, -1] # => nil
- *
- * When the single \Range argument +range+ is given,
- * derives +start+ and +length+ values from the given +range+,
- * and returns values as above:
- *
- * - <tt>'foo'[0..1]</tt> is equivalent to <tt>'foo'[0, 2]</tt>.
- * - <tt>'foo'[0...1]</tt> is equivalent to <tt>'foo'[0, 1]</tt>.
- *
- * When the \Regexp argument +regexp+ is given,
- * and the +capture+ argument is <tt>0</tt>,
- * returns the first matching substring found in +self+,
- * or +nil+ if none found:
- *
- * 'foo'[/o/] # => "o"
- * 'foo'[/x/] # => nil
- * s = 'hello there'
- * s[/[aeiou](.)\1/] # => "ell"
- * s[/[aeiou](.)\1/, 0] # => "ell"
- *
- * If argument +capture+ is given and not <tt>0</tt>,
- * it should be either an \Integer capture group index or a \String or \Symbol capture group name;
- * the method call returns only the specified capture
- * (see {Regexp Capturing}[Regexp.html#class-Regexp-label-Capturing]):
- *
- * s = 'hello there'
- * s[/[aeiou](.)\1/, 1] # => "l"
- * s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, "non_vowel"] # => "l"
- * s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, :vowel] # => "e"
- *
- * If an invalid capture group index is given, +nil+ is returned. If an invalid
- * capture group name is given, +IndexError+ is raised.
- *
- * When the single \String argument +substring+ is given,
- * returns the substring from +self+ if found, otherwise +nil+:
- *
- * 'foo'['oo'] # => "oo"
- * 'foo'['xx'] # => nil
- *
- * String#slice is an alias for String#[].
*/
static VALUE
rb_str_aref_m(int argc, VALUE *argv, VALUE str)
{
if (argc == 2) {
- if (RB_TYPE_P(argv[0], T_REGEXP)) {
- return rb_str_subpat(str, argv[0], argv[1]);
- }
- else {
- long beg = NUM2LONG(argv[0]);
- long len = NUM2LONG(argv[1]);
- return rb_str_substr(str, beg, len);
- }
+ if (RB_TYPE_P(argv[0], T_REGEXP)) {
+ return rb_str_subpat(str, argv[0], argv[1]);
+ }
+ else {
+ long beg = NUM2LONG(argv[0]);
+ long len = NUM2LONG(argv[1]);
+ return rb_str_substr(str, beg, len);
+ }
}
rb_check_arity(argc, 1, 2);
return rb_str_aref(str, argv[0]);
@@ -5053,13 +5326,12 @@ rb_str_drop_bytes(VALUE str, long len)
if (len > olen) len = olen;
nlen = olen - len;
if (str_embed_capa(str) >= nlen + TERM_LEN(str)) {
- char *oldptr = ptr;
- int fl = (int)(RBASIC(str)->flags & (STR_NOEMBED|STR_SHARED|STR_NOFREE));
- STR_SET_EMBED(str);
- STR_SET_EMBED_LEN(str, nlen);
+ char *oldptr = ptr;
+ int fl = (int)(RBASIC(str)->flags & (STR_NOEMBED|STR_SHARED|STR_NOFREE));
+ STR_SET_EMBED(str);
ptr = RSTRING(str)->as.embed.ary;
- memmove(ptr, oldptr + len, nlen);
- if (fl == STR_NOEMBED) xfree(oldptr);
+ memmove(ptr, oldptr + len, nlen);
+ if (fl == STR_NOEMBED) xfree(oldptr);
}
else {
if (!STR_SHARED_P(str)) {
@@ -5067,49 +5339,52 @@ rb_str_drop_bytes(VALUE str, long len)
rb_enc_cr_str_exact_copy(shared, str);
OBJ_FREEZE(shared);
}
- ptr = RSTRING(str)->as.heap.ptr += len;
- RSTRING(str)->as.heap.len = nlen;
+ ptr = RSTRING(str)->as.heap.ptr += len;
+ }
+ STR_SET_LEN(str, nlen);
+
+ if (!SHARABLE_MIDDLE_SUBSTRING) {
+ TERM_FILL(ptr + nlen, TERM_LEN(str));
}
- ptr[nlen] = 0;
ENC_CODERANGE_CLEAR(str);
return str;
}
static void
-rb_str_splice_0(VALUE str, long beg, long len, VALUE val)
+rb_str_update_1(VALUE str, long beg, long len, VALUE val, long vbeg, long vlen)
{
char *sptr;
- long slen, vlen = RSTRING_LEN(val);
+ long slen;
int cr;
if (beg == 0 && vlen == 0) {
- rb_str_drop_bytes(str, len);
- return;
+ rb_str_drop_bytes(str, len);
+ return;
}
str_modify_keep_cr(str);
RSTRING_GETMEM(str, sptr, slen);
if (len < vlen) {
- /* expand string */
- RESIZE_CAPA(str, slen + vlen - len);
- sptr = RSTRING_PTR(str);
+ /* expand string */
+ RESIZE_CAPA(str, slen + vlen - len);
+ sptr = RSTRING_PTR(str);
}
if (ENC_CODERANGE(str) == ENC_CODERANGE_7BIT)
- cr = rb_enc_str_coderange(val);
+ cr = rb_enc_str_coderange(val);
else
- cr = ENC_CODERANGE_UNKNOWN;
+ cr = ENC_CODERANGE_UNKNOWN;
if (vlen != len) {
- memmove(sptr + beg + vlen,
- sptr + beg + len,
- slen - (beg + len));
+ memmove(sptr + beg + vlen,
+ sptr + beg + len,
+ slen - (beg + len));
}
if (vlen < beg && len < 0) {
- MEMZERO(sptr + slen, char, -len);
+ MEMZERO(sptr + slen, char, -len);
}
if (vlen > 0) {
- memmove(sptr + beg, RSTRING_PTR(val), vlen);
+ memmove(sptr + beg, RSTRING_PTR(val) + vbeg, vlen);
}
slen += vlen - len;
STR_SET_LEN(str, slen);
@@ -5117,6 +5392,12 @@ rb_str_splice_0(VALUE str, long beg, long len, VALUE val)
ENC_CODERANGE_SET(str, cr);
}
+static inline void
+rb_str_update_0(VALUE str, long beg, long len, VALUE val)
+{
+ rb_str_update_1(str, beg, len, val, 0, RSTRING_LEN(val));
+}
+
void
rb_str_update(VALUE str, long beg, long len, VALUE val)
{
@@ -5133,17 +5414,17 @@ rb_str_update(VALUE str, long beg, long len, VALUE val)
slen = str_strlen(str, enc); /* rb_enc_check */
if ((slen < beg) || ((beg < 0) && (beg + slen < 0))) {
- rb_raise(rb_eIndexError, "index %ld out of string", beg);
+ rb_raise(rb_eIndexError, "index %ld out of string", beg);
}
if (beg < 0) {
- beg += slen;
+ beg += slen;
}
- assert(beg >= 0);
- assert(beg <= slen);
+ RUBY_ASSERT(beg >= 0);
+ RUBY_ASSERT(beg <= slen);
+
if (len > slen - beg) {
- len = slen - beg;
+ len = slen - beg;
}
- str_modify_keep_cr(str);
p = str_nth(RSTRING_PTR(str), RSTRING_END(str), beg, enc, singlebyte);
if (!p) p = RSTRING_END(str);
e = str_nth(p, RSTRING_END(str), len, enc, singlebyte);
@@ -5151,15 +5432,13 @@ rb_str_update(VALUE str, long beg, long len, VALUE val)
/* error check */
beg = p - RSTRING_PTR(str); /* physical position */
len = e - p; /* physical length */
- rb_str_splice_0(str, beg, len, val);
+ rb_str_update_0(str, beg, len, val);
rb_enc_associate(str, enc);
cr = ENC_CODERANGE_AND(ENC_CODERANGE(str), ENC_CODERANGE(val));
if (cr != ENC_CODERANGE_BROKEN)
- ENC_CODERANGE_SET(str, cr);
+ ENC_CODERANGE_SET(str, cr);
}
-#define rb_str_splice(str, beg, len, val) rb_str_update(str, beg, len, val)
-
static void
rb_str_subpat_set(VALUE str, VALUE re, VALUE backref, VALUE val)
{
@@ -5170,27 +5449,27 @@ rb_str_subpat_set(VALUE str, VALUE re, VALUE backref, VALUE val)
struct re_registers *regs;
if (rb_reg_search(re, str, 0, 0) < 0) {
- rb_raise(rb_eIndexError, "regexp not matched");
+ rb_raise(rb_eIndexError, "regexp not matched");
}
match = rb_backref_get();
nth = rb_reg_backref_number(match, backref);
regs = RMATCH_REGS(match);
if ((nth >= regs->num_regs) || ((nth < 0) && (-nth >= regs->num_regs))) {
- rb_raise(rb_eIndexError, "index %d out of regexp", nth);
+ rb_raise(rb_eIndexError, "index %d out of regexp", nth);
}
if (nth < 0) {
- nth += regs->num_regs;
+ nth += regs->num_regs;
}
start = BEG(nth);
if (start == -1) {
- rb_raise(rb_eIndexError, "regexp group %d not matched", nth);
+ rb_raise(rb_eIndexError, "regexp group %d not matched", nth);
}
end = END(nth);
len = end - start;
StringValue(val);
enc = rb_enc_check_str(str, val);
- rb_str_splice_0(str, start, len, val);
+ rb_str_update_0(str, start, len, val);
rb_enc_associate(str, enc);
}
@@ -5201,71 +5480,74 @@ rb_str_aset(VALUE str, VALUE indx, VALUE val)
switch (TYPE(indx)) {
case T_REGEXP:
- rb_str_subpat_set(str, indx, INT2FIX(0), val);
- return val;
+ rb_str_subpat_set(str, indx, INT2FIX(0), val);
+ return val;
case T_STRING:
- beg = rb_str_index(str, indx, 0);
- if (beg < 0) {
- rb_raise(rb_eIndexError, "string not matched");
- }
- beg = rb_str_sublen(str, beg);
- rb_str_splice(str, beg, str_strlen(indx, NULL), val);
- return val;
+ beg = rb_str_index(str, indx, 0);
+ if (beg < 0) {
+ rb_raise(rb_eIndexError, "string not matched");
+ }
+ beg = rb_str_sublen(str, beg);
+ rb_str_update(str, beg, str_strlen(indx, NULL), val);
+ return val;
default:
- /* check if indx is Range */
- {
- long beg, len;
- if (rb_range_beg_len(indx, &beg, &len, str_strlen(str, NULL), 2)) {
- rb_str_splice(str, beg, len, val);
- return val;
- }
- }
+ /* check if indx is Range */
+ {
+ long beg, len;
+ if (rb_range_beg_len(indx, &beg, &len, str_strlen(str, NULL), 2)) {
+ rb_str_update(str, beg, len, val);
+ return val;
+ }
+ }
/* FALLTHROUGH */
case T_FIXNUM:
- idx = NUM2LONG(indx);
- rb_str_splice(str, idx, 1, val);
+ idx = NUM2LONG(indx);
+ rb_str_update(str, idx, 1, val);
return val;
}
}
/*
* call-seq:
- * str[integer] = new_str
- * str[integer, integer] = new_str
- * str[range] = aString
- * str[regexp] = new_str
- * str[regexp, integer] = new_str
- * str[regexp, name] = new_str
- * str[other_str] = new_str
+ * string[index] = new_string
+ * string[start, length] = new_string
+ * string[range] = new_string
+ * string[regexp, capture = 0] = new_string
+ * string[substring] = new_string
+ *
+ * Replaces all, some, or none of the contents of +self+; returns +new_string+.
+ * See {String Slices}[rdoc-ref:String@String+Slices].
+ *
+ * A few examples:
+ *
+ * s = 'foo'
+ * s[2] = 'rtune' # => "rtune"
+ * s # => "fortune"
+ * s[1, 5] = 'init' # => "init"
+ * s # => "finite"
+ * s[3..4] = 'al' # => "al"
+ * s # => "finale"
+ * s[/e$/] = 'ly' # => "ly"
+ * s # => "finally"
+ * s['lly'] = 'ncial' # => "ncial"
+ * s # => "financial"
*
- * Element Assignment---Replaces some or all of the content of
- * <i>str</i>. The portion of the string affected is determined using
- * the same criteria as String#[]. If the replacement string is not
- * the same length as the text it is replacing, the string will be
- * adjusted accordingly. If the regular expression or string is used
- * as the index doesn't match a position in the string, IndexError is
- * raised. If the regular expression form is used, the optional
- * second Integer allows you to specify which portion of the match to
- * replace (effectively using the MatchData indexing rules. The forms
- * that take an Integer will raise an IndexError if the value is out
- * of range; the Range form will raise a RangeError, and the Regexp
- * and String will raise an IndexError on negative match.
*/
static VALUE
rb_str_aset_m(int argc, VALUE *argv, VALUE str)
{
if (argc == 3) {
- if (RB_TYPE_P(argv[0], T_REGEXP)) {
- rb_str_subpat_set(str, argv[0], argv[1], argv[2]);
- }
- else {
- rb_str_splice(str, NUM2LONG(argv[0]), NUM2LONG(argv[1]), argv[2]);
- }
- return argv[2];
+ if (RB_TYPE_P(argv[0], T_REGEXP)) {
+ rb_str_subpat_set(str, argv[0], argv[1], argv[2]);
+ }
+ else {
+ rb_str_update(str, NUM2LONG(argv[0]), NUM2LONG(argv[1]), argv[2]);
+ }
+ return argv[2];
}
rb_check_arity(argc, 2, 3);
return rb_str_aset(str, argv[0], argv[1]);
@@ -5277,11 +5559,11 @@ rb_str_aset_m(int argc, VALUE *argv, VALUE str)
*
* Inserts the given +other_string+ into +self+; returns +self+.
*
- * If the \Integer +index+ is positive, inserts +other_string+ at offset +index+:
+ * If the Integer +index+ is positive, inserts +other_string+ at offset +index+:
*
* 'foo'.insert(1, 'bar') # => "fbaroo"
*
- * If the \Integer +index+ is negative, counts backward from the end of +self+
+ * If the Integer +index+ is negative, counts backward from the end of +self+
* and inserts +other_string+ at offset <tt>index+1</tt>
* (that is, _after_ <tt>self[index]</tt>):
*
@@ -5295,28 +5577,26 @@ rb_str_insert(VALUE str, VALUE idx, VALUE str2)
long pos = NUM2LONG(idx);
if (pos == -1) {
- return rb_str_append(str, str2);
+ return rb_str_append(str, str2);
}
else if (pos < 0) {
- pos++;
+ pos++;
}
- rb_str_splice(str, pos, 0, str2);
+ rb_str_update(str, pos, 0, str2);
return str;
}
/*
* call-seq:
- * slice!(index) -> new_string or nil
- * slice!(start, length) -> new_string or nil
- * slice!(range) -> new_string or nil
- * slice!(regexp, capture = 0) -> new_string or nil
- * slice!(substring) -> new_string or nil
+ * slice!(index) -> new_string or nil
+ * slice!(start, length) -> new_string or nil
+ * slice!(range) -> new_string or nil
+ * slice!(regexp, capture = 0) -> new_string or nil
+ * slice!(substring) -> new_string or nil
*
- * Removes the substring of +self+ specified by the arguments;
- * returns the removed substring.
- *
- * See String#[] for details about the arguments that specify the substring.
+ * Removes and returns the substring of +self+ specified by the arguments.
+ * See {String Slices}[rdoc-ref:String@String+Slices].
*
* A few examples:
*
@@ -5341,50 +5621,50 @@ rb_str_slice_bang(int argc, VALUE *argv, VALUE str)
str_modify_keep_cr(str);
indx = argv[0];
if (RB_TYPE_P(indx, T_REGEXP)) {
- if (rb_reg_search(indx, str, 0, 0) < 0) return Qnil;
- VALUE match = rb_backref_get();
- struct re_registers *regs = RMATCH_REGS(match);
- int nth = 0;
- if (argc > 1 && (nth = rb_reg_backref_number(match, argv[1])) < 0) {
- if ((nth += regs->num_regs) <= 0) return Qnil;
- }
- else if (nth >= regs->num_regs) return Qnil;
- beg = BEG(nth);
- len = END(nth) - beg;
+ if (rb_reg_search(indx, str, 0, 0) < 0) return Qnil;
+ VALUE match = rb_backref_get();
+ struct re_registers *regs = RMATCH_REGS(match);
+ int nth = 0;
+ if (argc > 1 && (nth = rb_reg_backref_number(match, argv[1])) < 0) {
+ if ((nth += regs->num_regs) <= 0) return Qnil;
+ }
+ else if (nth >= regs->num_regs) return Qnil;
+ beg = BEG(nth);
+ len = END(nth) - beg;
goto subseq;
}
else if (argc == 2) {
- beg = NUM2LONG(indx);
- len = NUM2LONG(argv[1]);
+ beg = NUM2LONG(indx);
+ len = NUM2LONG(argv[1]);
goto num_index;
}
else if (FIXNUM_P(indx)) {
- beg = FIX2LONG(indx);
- if (!(p = rb_str_subpos(str, beg, &len))) return Qnil;
- if (!len) return Qnil;
- beg = p - RSTRING_PTR(str);
- goto subseq;
+ beg = FIX2LONG(indx);
+ if (!(p = rb_str_subpos(str, beg, &len))) return Qnil;
+ if (!len) return Qnil;
+ beg = p - RSTRING_PTR(str);
+ goto subseq;
}
else if (RB_TYPE_P(indx, T_STRING)) {
- beg = rb_str_index(str, indx, 0);
- if (beg == -1) return Qnil;
- len = RSTRING_LEN(indx);
+ beg = rb_str_index(str, indx, 0);
+ if (beg == -1) return Qnil;
+ len = RSTRING_LEN(indx);
result = str_duplicate(rb_cString, indx);
goto squash;
}
else {
- switch (rb_range_beg_len(indx, &beg, &len, str_strlen(str, NULL), 0)) {
- case Qnil:
- return Qnil;
- case Qfalse:
- beg = NUM2LONG(indx);
- if (!(p = rb_str_subpos(str, beg, &len))) return Qnil;
- if (!len) return Qnil;
- beg = p - RSTRING_PTR(str);
- goto subseq;
- default:
- goto num_index;
- }
+ switch (rb_range_beg_len(indx, &beg, &len, str_strlen(str, NULL), 0)) {
+ case Qnil:
+ return Qnil;
+ case Qfalse:
+ beg = NUM2LONG(indx);
+ if (!(p = rb_str_subpos(str, beg, &len))) return Qnil;
+ if (!len) return Qnil;
+ beg = p - RSTRING_PTR(str);
+ goto subseq;
+ default:
+ goto num_index;
+ }
}
num_index:
@@ -5397,21 +5677,21 @@ rb_str_slice_bang(int argc, VALUE *argv, VALUE str)
squash:
if (len > 0) {
- if (beg == 0) {
- rb_str_drop_bytes(str, len);
- }
- else {
- char *sptr = RSTRING_PTR(str);
- long slen = RSTRING_LEN(str);
- if (beg + len > slen) /* pathological check */
- len = slen - beg;
- memmove(sptr + beg,
- sptr + beg + len,
- slen - (beg + len));
- slen -= len;
- STR_SET_LEN(str, slen);
- TERM_FILL(&sptr[slen], TERM_LEN(str));
- }
+ if (beg == 0) {
+ rb_str_drop_bytes(str, len);
+ }
+ else {
+ char *sptr = RSTRING_PTR(str);
+ long slen = RSTRING_LEN(str);
+ if (beg + len > slen) /* pathological check */
+ len = slen - beg;
+ memmove(sptr + beg,
+ sptr + beg + len,
+ slen - (beg + len));
+ slen -= len;
+ STR_SET_LEN(str, slen);
+ TERM_FILL(&sptr[slen], TERM_LEN(str));
+ }
}
return result;
}
@@ -5423,17 +5703,17 @@ get_pat(VALUE pat)
switch (OBJ_BUILTIN_TYPE(pat)) {
case T_REGEXP:
- return pat;
+ return pat;
case T_STRING:
- break;
+ break;
default:
- val = rb_check_string_type(pat);
- if (NIL_P(val)) {
- Check_Type(pat, T_REGEXP);
- }
- pat = val;
+ val = rb_check_string_type(pat);
+ if (NIL_P(val)) {
+ Check_Type(pat, T_REGEXP);
+ }
+ pat = val;
}
return rb_reg_regcomp(pat);
@@ -5446,20 +5726,20 @@ get_pat_quoted(VALUE pat, int check)
switch (OBJ_BUILTIN_TYPE(pat)) {
case T_REGEXP:
- return pat;
+ return pat;
case T_STRING:
- break;
+ break;
default:
- val = rb_check_string_type(pat);
- if (NIL_P(val)) {
- Check_Type(pat, T_REGEXP);
- }
- pat = val;
+ val = rb_check_string_type(pat);
+ if (NIL_P(val)) {
+ Check_Type(pat, T_REGEXP);
+ }
+ pat = val;
}
if (check && is_broken_string(pat)) {
- rb_exc_raise(rb_reg_check_preprocess(pat));
+ rb_exc_raise(rb_reg_check_preprocess(pat));
}
return pat;
}
@@ -5468,20 +5748,20 @@ static long
rb_pat_search(VALUE pat, VALUE str, long pos, int set_backref_str)
{
if (BUILTIN_TYPE(pat) == T_STRING) {
- pos = rb_strseq_index(str, pat, pos, 1);
- if (set_backref_str) {
- if (pos >= 0) {
+ pos = rb_str_byteindex(str, pat, pos);
+ if (set_backref_str) {
+ if (pos >= 0) {
str = rb_str_new_frozen_String(str);
- rb_backref_set_string(str, pos, RSTRING_LEN(pat));
- }
- else {
- rb_backref_set(Qnil);
- }
- }
- return pos;
+ rb_backref_set_string(str, pos, RSTRING_LEN(pat));
+ }
+ else {
+ rb_backref_set(Qnil);
+ }
+ }
+ return pos;
}
else {
- return rb_reg_search0(pat, str, pos, 0, set_backref_str);
+ return rb_reg_search0(pat, str, pos, 0, set_backref_str);
}
}
@@ -5494,7 +5774,7 @@ rb_pat_search(VALUE pat, VALUE str, long pos, int set_backref_str)
* Returns +self+ with only the first occurrence
* (not all occurrences) of the given +pattern+ replaced.
*
- * See {Substitution Methods}[#class-String-label-Substitution+Methods].
+ * See {Substitution Methods}[rdoc-ref:String@Substitution+Methods].
*
* Related: String#sub, String#gsub, String#gsub!.
*
@@ -5511,14 +5791,14 @@ rb_str_sub_bang(int argc, VALUE *argv, VALUE str)
rb_check_arity(argc, min_arity, 2);
if (argc == 1) {
- iter = 1;
+ iter = 1;
}
else {
- repl = argv[1];
- hash = rb_check_hash_type(argv[1]);
- if (NIL_P(hash)) {
- StringValue(repl);
- }
+ repl = argv[1];
+ hash = rb_check_hash_type(argv[1]);
+ if (NIL_P(hash)) {
+ StringValue(repl);
+ }
}
pat = get_pat_quoted(argv[0], 1);
@@ -5526,29 +5806,29 @@ rb_str_sub_bang(int argc, VALUE *argv, VALUE str)
str_modifiable(str);
beg = rb_pat_search(pat, str, 0, 1);
if (beg >= 0) {
- rb_encoding *enc;
- int cr = ENC_CODERANGE(str);
- long beg0, end0;
- VALUE match, match0 = Qnil;
- struct re_registers *regs;
- char *p, *rp;
- long len, rlen;
-
- match = rb_backref_get();
- regs = RMATCH_REGS(match);
- if (RB_TYPE_P(pat, T_STRING)) {
- beg0 = beg;
- end0 = beg0 + RSTRING_LEN(pat);
- match0 = pat;
- }
- else {
- beg0 = BEG(0);
- end0 = END(0);
- if (iter) match0 = rb_reg_nth_match(0, match);
- }
-
- if (iter || !NIL_P(hash)) {
- p = RSTRING_PTR(str); len = RSTRING_LEN(str);
+ rb_encoding *enc;
+ int cr = ENC_CODERANGE(str);
+ long beg0, end0;
+ VALUE match, match0 = Qnil;
+ struct re_registers *regs;
+ char *p, *rp;
+ long len, rlen;
+
+ match = rb_backref_get();
+ regs = RMATCH_REGS(match);
+ if (RB_TYPE_P(pat, T_STRING)) {
+ beg0 = beg;
+ end0 = beg0 + RSTRING_LEN(pat);
+ match0 = pat;
+ }
+ else {
+ beg0 = BEG(0);
+ end0 = END(0);
+ if (iter) match0 = rb_reg_nth_match(0, match);
+ }
+
+ if (iter || !NIL_P(hash)) {
+ p = RSTRING_PTR(str); len = RSTRING_LEN(str);
if (iter) {
repl = rb_obj_as_string(rb_yield(match0));
@@ -5557,53 +5837,55 @@ rb_str_sub_bang(int argc, VALUE *argv, VALUE str)
repl = rb_hash_aref(hash, rb_str_subseq(str, beg0, end0 - beg0));
repl = rb_obj_as_string(repl);
}
- str_mod_check(str, p, len);
- rb_check_frozen(str);
- }
- else {
- repl = rb_reg_regsub(repl, str, regs, RB_TYPE_P(pat, T_STRING) ? Qnil : pat);
- }
+ str_mod_check(str, p, len);
+ rb_check_frozen(str);
+ }
+ else {
+ repl = rb_reg_regsub(repl, str, regs, RB_TYPE_P(pat, T_STRING) ? Qnil : pat);
+ }
enc = rb_enc_compatible(str, repl);
if (!enc) {
rb_encoding *str_enc = STR_ENC_GET(str);
- p = RSTRING_PTR(str); len = RSTRING_LEN(str);
- if (coderange_scan(p, beg0, str_enc) != ENC_CODERANGE_7BIT ||
- coderange_scan(p+end0, len-end0, str_enc) != ENC_CODERANGE_7BIT) {
+ p = RSTRING_PTR(str); len = RSTRING_LEN(str);
+ if (coderange_scan(p, beg0, str_enc) != ENC_CODERANGE_7BIT ||
+ coderange_scan(p+end0, len-end0, str_enc) != ENC_CODERANGE_7BIT) {
rb_raise(rb_eEncCompatError, "incompatible character encodings: %s and %s",
- rb_enc_name(str_enc),
- rb_enc_name(STR_ENC_GET(repl)));
+ rb_enc_name(str_enc),
+ rb_enc_name(STR_ENC_GET(repl)));
}
enc = STR_ENC_GET(repl);
}
- rb_str_modify(str);
- rb_enc_associate(str, enc);
- if (ENC_CODERANGE_UNKNOWN < cr && cr < ENC_CODERANGE_BROKEN) {
- int cr2 = ENC_CODERANGE(repl);
+ rb_str_modify(str);
+ rb_enc_associate(str, enc);
+ if (ENC_CODERANGE_UNKNOWN < cr && cr < ENC_CODERANGE_BROKEN) {
+ int cr2 = ENC_CODERANGE(repl);
if (cr2 == ENC_CODERANGE_BROKEN ||
(cr == ENC_CODERANGE_VALID && cr2 == ENC_CODERANGE_7BIT))
cr = ENC_CODERANGE_UNKNOWN;
else
cr = cr2;
- }
- plen = end0 - beg0;
+ }
+ plen = end0 - beg0;
rlen = RSTRING_LEN(repl);
- len = RSTRING_LEN(str);
- if (rlen > plen) {
- RESIZE_CAPA(str, len + rlen - plen);
- }
- p = RSTRING_PTR(str);
- if (rlen != plen) {
- memmove(p + beg0 + rlen, p + beg0 + plen, len - beg0 - plen);
- }
+ len = RSTRING_LEN(str);
+ if (rlen > plen) {
+ RESIZE_CAPA(str, len + rlen - plen);
+ }
+ p = RSTRING_PTR(str);
+ if (rlen != plen) {
+ memmove(p + beg0 + rlen, p + beg0 + plen, len - beg0 - plen);
+ }
rp = RSTRING_PTR(repl);
memmove(p + beg0, rp, rlen);
- len += rlen - plen;
- STR_SET_LEN(str, len);
- TERM_FILL(&RSTRING_PTR(str)[len], TERM_LEN(str));
- ENC_CODERANGE_SET(str, cr);
+ len += rlen - plen;
+ STR_SET_LEN(str, len);
+ TERM_FILL(&RSTRING_PTR(str)[len], TERM_LEN(str));
+ ENC_CODERANGE_SET(str, cr);
+
+ RB_GC_GUARD(match);
- return str;
+ return str;
}
return Qnil;
}
@@ -5617,7 +5899,7 @@ rb_str_sub_bang(int argc, VALUE *argv, VALUE str)
* Returns a copy of +self+ with only the first occurrence
* (not all occurrences) of the given +pattern+ replaced.
*
- * See {Substitution Methods}[#class-String-label-Substitution+Methods].
+ * See {Substitution Methods}[rdoc-ref:String@Substitution+Methods].
*
* Related: String#sub!, String#gsub, String#gsub!.
*
@@ -5634,8 +5916,7 @@ rb_str_sub(int argc, VALUE *argv, VALUE str)
static VALUE
str_gsub(int argc, VALUE *argv, VALUE str, int bang)
{
- VALUE pat, val = Qnil, repl, match, match0 = Qnil, dest, hash = Qnil;
- struct re_registers *regs;
+ VALUE pat, val = Qnil, repl, match0 = Qnil, dest, hash = Qnil;
long beg, beg0, end0;
long offset, blen, slen, len, last;
enum {STR, ITER, MAP} mode = STR;
@@ -5645,19 +5926,19 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang)
switch (argc) {
case 1:
- RETURN_ENUMERATOR(str, argc, argv);
- mode = ITER;
- break;
+ RETURN_ENUMERATOR(str, argc, argv);
+ mode = ITER;
+ break;
case 2:
- repl = argv[1];
- hash = rb_check_hash_type(argv[1]);
- if (NIL_P(hash)) {
- StringValue(repl);
- }
- else {
- mode = MAP;
- }
- break;
+ repl = argv[1];
+ hash = rb_check_hash_type(argv[1]);
+ if (NIL_P(hash)) {
+ StringValue(repl);
+ }
+ else {
+ mode = MAP;
+ }
+ break;
default:
rb_error_arity(argc, 1, 2);
}
@@ -5665,7 +5946,7 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang)
pat = get_pat_quoted(argv[0], 1);
beg = rb_pat_search(pat, str, 0, need_backref);
if (beg < 0) {
- if (bang) return Qnil; /* no match, no substitution */
+ if (bang) return Qnil; /* no match, no substitution */
return str_duplicate(rb_cString, str);
}
@@ -5680,20 +5961,20 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang)
ENC_CODERANGE_SET(dest, rb_enc_asciicompat(str_enc) ? ENC_CODERANGE_7BIT : ENC_CODERANGE_VALID);
do {
- match = rb_backref_get();
- regs = RMATCH_REGS(match);
- if (RB_TYPE_P(pat, T_STRING)) {
- beg0 = beg;
- end0 = beg0 + RSTRING_LEN(pat);
- match0 = pat;
- }
- else {
- beg0 = BEG(0);
- end0 = END(0);
- if (mode == ITER) match0 = rb_reg_nth_match(0, match);
- }
-
- if (mode) {
+ VALUE match = rb_backref_get();
+ struct re_registers *regs = RMATCH_REGS(match);
+ if (RB_TYPE_P(pat, T_STRING)) {
+ beg0 = beg;
+ end0 = beg0 + RSTRING_LEN(pat);
+ match0 = pat;
+ }
+ else {
+ beg0 = BEG(0);
+ end0 = END(0);
+ if (mode == ITER) match0 = rb_reg_nth_match(0, match);
+ }
+
+ if (mode) {
if (mode == ITER) {
val = rb_obj_as_string(rb_yield(match0));
}
@@ -5701,43 +5982,45 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang)
val = rb_hash_aref(hash, rb_str_subseq(str, beg0, end0 - beg0));
val = rb_obj_as_string(val);
}
- str_mod_check(str, sp, slen);
- if (val == dest) { /* paranoid check [ruby-dev:24827] */
- rb_raise(rb_eRuntimeError, "block should not cheat");
- }
- }
- else if (need_backref) {
- val = rb_reg_regsub(repl, str, regs, RB_TYPE_P(pat, T_STRING) ? Qnil : pat);
- if (need_backref < 0) {
- need_backref = val != repl;
- }
- }
- else {
- val = repl;
- }
-
- len = beg0 - offset; /* copy pre-match substr */
+ str_mod_check(str, sp, slen);
+ if (val == dest) { /* paranoid check [ruby-dev:24827] */
+ rb_raise(rb_eRuntimeError, "block should not cheat");
+ }
+ }
+ else if (need_backref) {
+ val = rb_reg_regsub(repl, str, regs, RB_TYPE_P(pat, T_STRING) ? Qnil : pat);
+ if (need_backref < 0) {
+ need_backref = val != repl;
+ }
+ }
+ else {
+ val = repl;
+ }
+
+ len = beg0 - offset; /* copy pre-match substr */
if (len) {
rb_enc_str_buf_cat(dest, cp, len, str_enc);
}
rb_str_buf_append(dest, val);
- last = offset;
- offset = end0;
- if (beg0 == end0) {
- /*
- * Always consume at least one character of the input string
- * in order to prevent infinite loops.
- */
- if (RSTRING_LEN(str) <= end0) break;
- len = rb_enc_fast_mbclen(RSTRING_PTR(str)+end0, RSTRING_END(str), str_enc);
+ last = offset;
+ offset = end0;
+ if (beg0 == end0) {
+ /*
+ * Always consume at least one character of the input string
+ * in order to prevent infinite loops.
+ */
+ if (RSTRING_LEN(str) <= end0) break;
+ len = rb_enc_fast_mbclen(RSTRING_PTR(str)+end0, RSTRING_END(str), str_enc);
rb_enc_str_buf_cat(dest, RSTRING_PTR(str)+end0, len, str_enc);
- offset = end0 + len;
- }
- cp = RSTRING_PTR(str) + offset;
- if (offset > RSTRING_LEN(str)) break;
- beg = rb_pat_search(pat, str, offset, need_backref);
+ offset = end0 + len;
+ }
+ cp = RSTRING_PTR(str) + offset;
+ if (offset > RSTRING_LEN(str)) break;
+ beg = rb_pat_search(pat, str, offset, need_backref);
+
+ RB_GC_GUARD(match);
} while (beg >= 0);
if (RSTRING_LEN(str) > offset) {
rb_enc_str_buf_cat(dest, cp, RSTRING_LEN(str) - offset, str_enc);
@@ -5747,7 +6030,7 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang)
str_shared_replace(str, dest);
}
else {
- str = dest;
+ str = dest;
}
return str;
@@ -5756,14 +6039,14 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang)
/*
* call-seq:
- * str.gsub!(pattern, replacement) -> self or nil
- * str.gsub!(pattern) {|match| ... } -> self or nil
- * str.gsub!(pattern) -> an_enumerator
+ * gsub!(pattern, replacement) -> self or nil
+ * gsub!(pattern) {|match| ... } -> self or nil
+ * gsub!(pattern) -> an_enumerator
*
* Performs the specified substring replacement(s) on +self+;
* returns +self+ if any replacement occurred, +nil+ otherwise.
*
- * See {Substitution Methods}[#class-String-label-Substitution+Methods].
+ * See {Substitution Methods}[rdoc-ref:String@Substitution+Methods].
*
* Returns an Enumerator if no +replacement+ and no block given.
*
@@ -5787,7 +6070,7 @@ rb_str_gsub_bang(int argc, VALUE *argv, VALUE str)
*
* Returns a copy of +self+ with all occurrences of the given +pattern+ replaced.
*
- * See {Substitution Methods}[#class-String-label-Substitution+Methods].
+ * See {Substitution Methods}[rdoc-ref:String@Substitution+Methods].
*
* Returns an Enumerator if no +replacement+ and no block given.
*
@@ -5804,13 +6087,13 @@ rb_str_gsub(int argc, VALUE *argv, VALUE str)
/*
* call-seq:
- * str.replace(other_str) -> str
+ * replace(other_string) -> self
*
- * Replaces the contents of <i>str</i> with the corresponding
- * values in <i>other_str</i>.
+ * Replaces the contents of +self+ with the contents of +other_string+:
+ *
+ * s = 'foo' # => "foo"
+ * s.replace('bar') # => "bar"
*
- * s = "hello" #=> "hello"
- * s.replace "world" #=> "world"
*/
VALUE
@@ -5826,12 +6109,13 @@ rb_str_replace(VALUE str, VALUE str2)
/*
* call-seq:
- * string.clear -> string
+ * clear -> self
+ *
+ * Removes the contents of +self+:
*
- * Makes string empty.
+ * s = 'foo' # => "foo"
+ * s.clear # => ""
*
- * a = "abcde"
- * a.clear #=> ""
*/
static VALUE
@@ -5839,23 +6123,24 @@ rb_str_clear(VALUE str)
{
str_discard(str);
STR_SET_EMBED(str);
- STR_SET_EMBED_LEN(str, 0);
+ STR_SET_LEN(str, 0);
RSTRING_PTR(str)[0] = 0;
if (rb_enc_asciicompat(STR_ENC_GET(str)))
- ENC_CODERANGE_SET(str, ENC_CODERANGE_7BIT);
+ ENC_CODERANGE_SET(str, ENC_CODERANGE_7BIT);
else
- ENC_CODERANGE_SET(str, ENC_CODERANGE_VALID);
+ ENC_CODERANGE_SET(str, ENC_CODERANGE_VALID);
return str;
}
/*
* call-seq:
- * string.chr -> string
+ * chr -> string
+ *
+ * Returns a string containing the first character of +self+:
*
- * Returns a one-character string at the beginning of the string.
+ * s = 'foo' # => "foo"
+ * s.chr # => "f"
*
- * a = "abcde"
- * a.chr #=> "a"
*/
static VALUE
@@ -5866,11 +6151,18 @@ rb_str_chr(VALUE str)
/*
* call-seq:
- * str.getbyte(index) -> 0 .. 255
+ * getbyte(index) -> integer or nil
*
- * returns the <i>index</i>th byte as an integer.
+ * Returns the byte at zero-based +index+ as an integer, or +nil+ if +index+ is out of range:
+ *
+ * s = 'abcde' # => "abcde"
+ * s.getbyte(0) # => 97
+ * s.getbyte(-1) # => 101
+ * s.getbyte(5) # => nil
+ *
+ * Related: String#setbyte.
*/
-static VALUE
+VALUE
rb_str_getbyte(VALUE str, VALUE index)
{
long pos = NUM2LONG(index);
@@ -5885,11 +6177,17 @@ rb_str_getbyte(VALUE str, VALUE index)
/*
* call-seq:
- * str.setbyte(index, integer) -> integer
+ * setbyte(index, integer) -> integer
+ *
+ * Sets the byte at zero-based +index+ to +integer+; returns +integer+:
+ *
+ * s = 'abcde' # => "abcde"
+ * s.setbyte(0, 98) # => 98
+ * s # => "bbcde"
*
- * modifies the <i>index</i>th byte as <i>integer</i>.
+ * Related: String#getbyte.
*/
-static VALUE
+VALUE
rb_str_setbyte(VALUE str, VALUE index, VALUE value)
{
long pos = NUM2LONG(index);
@@ -5908,34 +6206,34 @@ rb_str_setbyte(VALUE str, VALUE index, VALUE value)
char byte = (char)(NUM2INT(w) & 0xFF);
if (!str_independent(str))
- str_make_independent(str);
+ str_make_independent(str);
enc = STR_ENC_GET(str);
head = RSTRING_PTR(str);
ptr = &head[pos];
if (!STR_EMBED_P(str)) {
- cr = ENC_CODERANGE(str);
- switch (cr) {
- case ENC_CODERANGE_7BIT:
+ cr = ENC_CODERANGE(str);
+ switch (cr) {
+ case ENC_CODERANGE_7BIT:
left = ptr;
- *ptr = byte;
- if (ISASCII(byte)) goto end;
- nlen = rb_enc_precise_mbclen(left, head+len, enc);
- if (!MBCLEN_CHARFOUND_P(nlen))
- ENC_CODERANGE_SET(str, ENC_CODERANGE_BROKEN);
- else
- ENC_CODERANGE_SET(str, ENC_CODERANGE_VALID);
- goto end;
- case ENC_CODERANGE_VALID:
- left = rb_enc_left_char_head(head, ptr, head+len, enc);
- width = rb_enc_precise_mbclen(left, head+len, enc);
- *ptr = byte;
- nlen = rb_enc_precise_mbclen(left, head+len, enc);
- if (!MBCLEN_CHARFOUND_P(nlen))
- ENC_CODERANGE_SET(str, ENC_CODERANGE_BROKEN);
- else if (MBCLEN_CHARFOUND_LEN(nlen) != width || ISASCII(byte))
- ENC_CODERANGE_CLEAR(str);
- goto end;
- }
+ *ptr = byte;
+ if (ISASCII(byte)) goto end;
+ nlen = rb_enc_precise_mbclen(left, head+len, enc);
+ if (!MBCLEN_CHARFOUND_P(nlen))
+ ENC_CODERANGE_SET(str, ENC_CODERANGE_BROKEN);
+ else
+ ENC_CODERANGE_SET(str, ENC_CODERANGE_VALID);
+ goto end;
+ case ENC_CODERANGE_VALID:
+ left = rb_enc_left_char_head(head, ptr, head+len, enc);
+ width = rb_enc_precise_mbclen(left, head+len, enc);
+ *ptr = byte;
+ nlen = rb_enc_precise_mbclen(left, head+len, enc);
+ if (!MBCLEN_CHARFOUND_P(nlen))
+ ENC_CODERANGE_SET(str, ENC_CODERANGE_BROKEN);
+ else if (MBCLEN_CHARFOUND_LEN(nlen) != width || ISASCII(byte))
+ ENC_CODERANGE_CLEAR(str);
+ goto end;
+ }
}
ENC_CODERANGE_CLEAR(str);
*ptr = byte;
@@ -5947,124 +6245,242 @@ rb_str_setbyte(VALUE str, VALUE index, VALUE value)
static VALUE
str_byte_substr(VALUE str, long beg, long len, int empty)
{
- char *p, *s = RSTRING_PTR(str);
long n = RSTRING_LEN(str);
- VALUE str2;
if (beg > n || len < 0) return Qnil;
if (beg < 0) {
- beg += n;
- if (beg < 0) return Qnil;
+ beg += n;
+ if (beg < 0) return Qnil;
}
if (len > n - beg)
- len = n - beg;
+ len = n - beg;
if (len <= 0) {
- if (!empty) return Qnil;
- len = 0;
- p = 0;
+ if (!empty) return Qnil;
+ len = 0;
}
- else
- p = s + beg;
- if (!STR_EMBEDDABLE_P(len, TERM_LEN(str)) && SHARABLE_SUBSTRING_P(beg, len, n)) {
- str2 = rb_str_new_frozen(str);
- str2 = str_new_shared(rb_cString, str2);
- RSTRING(str2)->as.heap.ptr += beg;
- RSTRING(str2)->as.heap.len = len;
- }
- else {
- str2 = rb_str_new(p, len);
- }
+ VALUE str2 = str_subseq(str, beg, len);
- str_enc_copy(str2, str);
+ str_enc_copy_direct(str2, str);
if (RSTRING_LEN(str2) == 0) {
- if (!rb_enc_asciicompat(STR_ENC_GET(str)))
- ENC_CODERANGE_SET(str2, ENC_CODERANGE_VALID);
- else
- ENC_CODERANGE_SET(str2, ENC_CODERANGE_7BIT);
+ if (!rb_enc_asciicompat(STR_ENC_GET(str)))
+ ENC_CODERANGE_SET(str2, ENC_CODERANGE_VALID);
+ else
+ ENC_CODERANGE_SET(str2, ENC_CODERANGE_7BIT);
}
else {
- switch (ENC_CODERANGE(str)) {
- case ENC_CODERANGE_7BIT:
- ENC_CODERANGE_SET(str2, ENC_CODERANGE_7BIT);
- break;
- default:
- ENC_CODERANGE_SET(str2, ENC_CODERANGE_UNKNOWN);
- break;
- }
+ switch (ENC_CODERANGE(str)) {
+ case ENC_CODERANGE_7BIT:
+ ENC_CODERANGE_SET(str2, ENC_CODERANGE_7BIT);
+ break;
+ default:
+ ENC_CODERANGE_SET(str2, ENC_CODERANGE_UNKNOWN);
+ break;
+ }
}
return str2;
}
+VALUE
+rb_str_byte_substr(VALUE str, VALUE beg, VALUE len)
+{
+ return str_byte_substr(str, NUM2LONG(beg), NUM2LONG(len), TRUE);
+}
+
static VALUE
str_byte_aref(VALUE str, VALUE indx)
{
long idx;
if (FIXNUM_P(indx)) {
- idx = FIX2LONG(indx);
+ idx = FIX2LONG(indx);
}
else {
- /* check if indx is Range */
- long beg, len = RSTRING_LEN(str);
+ /* check if indx is Range */
+ long beg, len = RSTRING_LEN(str);
- switch (rb_range_beg_len(indx, &beg, &len, len, 0)) {
- case Qfalse:
- break;
- case Qnil:
- return Qnil;
- default:
- return str_byte_substr(str, beg, len, TRUE);
- }
+ switch (rb_range_beg_len(indx, &beg, &len, len, 0)) {
+ case Qfalse:
+ break;
+ case Qnil:
+ return Qnil;
+ default:
+ return str_byte_substr(str, beg, len, TRUE);
+ }
- idx = NUM2LONG(indx);
+ idx = NUM2LONG(indx);
}
return str_byte_substr(str, idx, 1, FALSE);
}
/*
* call-seq:
- * str.byteslice(integer) -> new_str or nil
- * str.byteslice(integer, integer) -> new_str or nil
- * str.byteslice(range) -> new_str or nil
+ * byteslice(index, length = 1) -> string or nil
+ * byteslice(range) -> string or nil
+ *
+ * Returns a substring of +self+, or +nil+ if the substring cannot be constructed.
+ *
+ * With integer arguments +index+ and +length+ given,
+ * returns the substring beginning at the given +index+
+ * of the given +length+ (if possible),
+ * or +nil+ if +length+ is negative or +index+ falls outside of +self+:
+ *
+ * s = '0123456789' # => "0123456789"
+ * s.byteslice(2) # => "2"
+ * s.byteslice(200) # => nil
+ * s.byteslice(4, 3) # => "456"
+ * s.byteslice(4, 30) # => "456789"
+ * s.byteslice(4, -1) # => nil
+ * s.byteslice(40, 2) # => nil
+ *
+ * In either case above, counts backwards from the end of +self+
+ * if +index+ is negative:
+ *
+ * s = '0123456789' # => "0123456789"
+ * s.byteslice(-4) # => "6"
+ * s.byteslice(-4, 3) # => "678"
+ *
+ * With Range argument +range+ given, returns
+ * <tt>byteslice(range.begin, range.size)</tt>:
*
- * Byte Reference---If passed a single Integer, returns a
- * substring of one byte at that position. If passed two Integer
- * objects, returns a substring starting at the offset given by the first, and
- * a length given by the second. If given a Range, a substring containing
- * bytes at offsets given by the range is returned. In all three cases, if
- * an offset is negative, it is counted from the end of <i>str</i>. Returns
- * <code>nil</code> if the initial offset falls outside the string, the length
- * is negative, or the beginning of the range is greater than the end.
- * The encoding of the resulted string keeps original encoding.
+ * s = '0123456789' # => "0123456789"
+ * s.byteslice(4..6) # => "456"
+ * s.byteslice(-6..-4) # => "456"
+ * s.byteslice(5..2) # => "" # range.size is zero.
+ * s.byteslice(40..42) # => nil
+ *
+ * In all cases, a returned string has the same encoding as +self+:
+ *
+ * s.encoding # => #<Encoding:UTF-8>
+ * s.byteslice(4).encoding # => #<Encoding:UTF-8>
*
- * "hello".byteslice(1) #=> "e"
- * "hello".byteslice(-1) #=> "o"
- * "hello".byteslice(1, 2) #=> "el"
- * "\x80\u3042".byteslice(1, 3) #=> "\u3042"
- * "\x03\u3042\xff".byteslice(1..3) #=> "\u3042"
*/
static VALUE
rb_str_byteslice(int argc, VALUE *argv, VALUE str)
{
if (argc == 2) {
- long beg = NUM2LONG(argv[0]);
- long end = NUM2LONG(argv[1]);
- return str_byte_substr(str, beg, end, TRUE);
+ long beg = NUM2LONG(argv[0]);
+ long len = NUM2LONG(argv[1]);
+ return str_byte_substr(str, beg, len, TRUE);
}
rb_check_arity(argc, 1, 2);
return str_byte_aref(str, argv[0]);
}
+static void
+str_check_beg_len(VALUE str, long *beg, long *len)
+{
+ long end, slen = RSTRING_LEN(str);
+
+ if (*len < 0) rb_raise(rb_eIndexError, "negative length %ld", *len);
+ if ((slen < *beg) || ((*beg < 0) && (*beg + slen < 0))) {
+ rb_raise(rb_eIndexError, "index %ld out of string", *beg);
+ }
+ if (*beg < 0) {
+ *beg += slen;
+ }
+ RUBY_ASSERT(*beg >= 0);
+ RUBY_ASSERT(*beg <= slen);
+
+ if (*len > slen - *beg) {
+ *len = slen - *beg;
+ }
+ end = *beg + *len;
+ str_ensure_byte_pos(str, *beg);
+ str_ensure_byte_pos(str, end);
+}
+
+/*
+ * call-seq:
+ * bytesplice(index, length, str) -> string
+ * bytesplice(index, length, str, str_index, str_length) -> string
+ * bytesplice(range, str) -> string
+ * bytesplice(range, str, str_range) -> string
+ *
+ * Replaces some or all of the content of +self+ with +str+, and returns +self+.
+ * The portion of the string affected is determined using
+ * the same criteria as String#byteslice, except that +length+ cannot be omitted.
+ * If the replacement string is not the same length as the text it is replacing,
+ * the string will be adjusted accordingly.
+ *
+ * If +str_index+ and +str_length+, or +str_range+ are given, the content of +self+ is replaced by str.byteslice(str_index, str_length) or str.byteslice(str_range); however the substring of +str+ is not allocated as a new string.
+ *
+ * The form that take an Integer will raise an IndexError if the value is out
+ * of range; the Range form will raise a RangeError.
+ * If the beginning or ending offset does not land on character (codepoint)
+ * boundary, an IndexError will be raised.
+ */
+
+static VALUE
+rb_str_bytesplice(int argc, VALUE *argv, VALUE str)
+{
+ long beg, len, vbeg, vlen;
+ VALUE val;
+ rb_encoding *enc;
+ int cr;
+
+ rb_check_arity(argc, 2, 5);
+ if (!(argc == 2 || argc == 3 || argc == 5)) {
+ rb_raise(rb_eArgError, "wrong number of arguments (given %d, expected 2, 3, or 5)", argc);
+ }
+ if (argc == 2 || (argc == 3 && !RB_INTEGER_TYPE_P(argv[0]))) {
+ if (!rb_range_beg_len(argv[0], &beg, &len, RSTRING_LEN(str), 2)) {
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected Range)",
+ rb_builtin_class_name(argv[0]));
+ }
+ val = argv[1];
+ StringValue(val);
+ if (argc == 2) {
+ /* bytesplice(range, str) */
+ vbeg = 0;
+ vlen = RSTRING_LEN(val);
+ }
+ else {
+ /* bytesplice(range, str, str_range) */
+ if (!rb_range_beg_len(argv[2], &vbeg, &vlen, RSTRING_LEN(val), 2)) {
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected Range)",
+ rb_builtin_class_name(argv[2]));
+ }
+ }
+ }
+ else {
+ beg = NUM2LONG(argv[0]);
+ len = NUM2LONG(argv[1]);
+ val = argv[2];
+ StringValue(val);
+ if (argc == 3) {
+ /* bytesplice(index, length, str) */
+ vbeg = 0;
+ vlen = RSTRING_LEN(val);
+ }
+ else {
+ /* bytesplice(index, length, str, str_index, str_length) */
+ vbeg = NUM2LONG(argv[3]);
+ vlen = NUM2LONG(argv[4]);
+ }
+ }
+ str_check_beg_len(str, &beg, &len);
+ str_check_beg_len(val, &vbeg, &vlen);
+ enc = rb_enc_check(str, val);
+ str_modify_keep_cr(str);
+ rb_str_update_1(str, beg, len, val, vbeg, vlen);
+ rb_enc_associate(str, enc);
+ cr = ENC_CODERANGE_AND(ENC_CODERANGE(str), ENC_CODERANGE(val));
+ if (cr != ENC_CODERANGE_BROKEN)
+ ENC_CODERANGE_SET(str, cr);
+ return str;
+}
+
/*
* call-seq:
- * str.reverse -> new_str
+ * reverse -> string
*
- * Returns a new string with the characters from <i>str</i> in reverse order.
+ * Returns a new string with the characters from +self+ in reverse order.
+ *
+ * 'stressed'.reverse # => "desserts"
*
- * "stressed".reverse #=> "desserts"
*/
static VALUE
@@ -6083,35 +6499,35 @@ rb_str_reverse(VALUE str)
cr = ENC_CODERANGE(str);
if (RSTRING_LEN(str) > 1) {
- if (single_byte_optimizable(str)) {
- while (s < e) {
- *--p = *s++;
- }
- }
- else if (cr == ENC_CODERANGE_VALID) {
- while (s < e) {
- int clen = rb_enc_fast_mbclen(s, e, enc);
-
- p -= clen;
- memcpy(p, s, clen);
- s += clen;
- }
- }
- else {
- cr = rb_enc_asciicompat(enc) ?
- ENC_CODERANGE_7BIT : ENC_CODERANGE_VALID;
- while (s < e) {
- int clen = rb_enc_mbclen(s, e, enc);
-
- if (clen > 1 || (*s & 0x80)) cr = ENC_CODERANGE_UNKNOWN;
- p -= clen;
- memcpy(p, s, clen);
- s += clen;
- }
- }
+ if (single_byte_optimizable(str)) {
+ while (s < e) {
+ *--p = *s++;
+ }
+ }
+ else if (cr == ENC_CODERANGE_VALID) {
+ while (s < e) {
+ int clen = rb_enc_fast_mbclen(s, e, enc);
+
+ p -= clen;
+ memcpy(p, s, clen);
+ s += clen;
+ }
+ }
+ else {
+ cr = rb_enc_asciicompat(enc) ?
+ ENC_CODERANGE_7BIT : ENC_CODERANGE_VALID;
+ while (s < e) {
+ int clen = rb_enc_mbclen(s, e, enc);
+
+ if (clen > 1 || (*s & 0x80)) cr = ENC_CODERANGE_UNKNOWN;
+ p -= clen;
+ memcpy(p, s, clen);
+ s += clen;
+ }
+ }
}
STR_SET_LEN(rev, RSTRING_LEN(str));
- str_enc_copy(rev, str);
+ str_enc_copy_direct(rev, str);
ENC_CODERANGE_SET(rev, cr);
return rev;
@@ -6120,33 +6536,38 @@ rb_str_reverse(VALUE str)
/*
* call-seq:
- * str.reverse! -> str
+ * reverse! -> self
+ *
+ * Returns +self+ with its characters reversed:
+ *
+ * s = 'stressed'
+ * s.reverse! # => "desserts"
+ * s # => "desserts"
*
- * Reverses <i>str</i> in place.
*/
static VALUE
rb_str_reverse_bang(VALUE str)
{
if (RSTRING_LEN(str) > 1) {
- if (single_byte_optimizable(str)) {
- char *s, *e, c;
-
- str_modify_keep_cr(str);
- s = RSTRING_PTR(str);
- e = RSTRING_END(str) - 1;
- while (s < e) {
- c = *s;
- *s++ = *e;
- *e-- = c;
- }
- }
- else {
- str_shared_replace(str, rb_str_reverse(str));
- }
+ if (single_byte_optimizable(str)) {
+ char *s, *e, c;
+
+ str_modify_keep_cr(str);
+ s = RSTRING_PTR(str);
+ e = RSTRING_END(str) - 1;
+ while (s < e) {
+ c = *s;
+ *s++ = *e;
+ *e-- = c;
+ }
+ }
+ else {
+ str_shared_replace(str, rb_str_reverse(str));
+ }
}
else {
- str_modify_keep_cr(str);
+ str_modify_keep_cr(str);
}
return str;
}
@@ -6154,17 +6575,18 @@ rb_str_reverse_bang(VALUE str)
/*
* call-seq:
- * str.include? other_str -> true or false
+ * include?(other_string) -> true or false
+ *
+ * Returns +true+ if +self+ contains +other_string+, +false+ otherwise:
*
- * Returns <code>true</code> if <i>str</i> contains the given string or
- * character.
+ * s = 'foo'
+ * s.include?('f') # => true
+ * s.include?('fo') # => true
+ * s.include?('food') # => false
*
- * "hello".include? "lo" #=> true
- * "hello".include? "ol" #=> false
- * "hello".include? ?h #=> true
*/
-static VALUE
+VALUE
rb_str_include(VALUE str, VALUE arg)
{
long i;
@@ -6178,23 +6600,34 @@ rb_str_include(VALUE str, VALUE arg)
/*
* call-seq:
- * str.to_i(base=10) -> integer
+ * to_i(base = 10) -> integer
*
- * Returns the result of interpreting leading characters in <i>str</i> as an
- * integer base <i>base</i> (between 2 and 36). Extraneous characters past the
- * end of a valid number are ignored. If there is not a valid number at the
- * start of <i>str</i>, <code>0</code> is returned. This method never raises an
- * exception when <i>base</i> is valid.
+ * Returns the result of interpreting leading characters in +self+
+ * as an integer in the given +base+ (which must be in (0, 2..36)):
+ *
+ * '123456'.to_i # => 123456
+ * '123def'.to_i(16) # => 1195503
+ *
+ * With +base+ zero, string +object+ may contain leading characters
+ * to specify the actual base:
+ *
+ * '123def'.to_i(0) # => 123
+ * '0123def'.to_i(0) # => 83
+ * '0b123def'.to_i(0) # => 1
+ * '0o123def'.to_i(0) # => 83
+ * '0d123def'.to_i(0) # => 123
+ * '0x123def'.to_i(0) # => 1195503
+ *
+ * Characters past a leading valid number (in the given +base+) are ignored:
+ *
+ * '12.345'.to_i # => 12
+ * '12345'.to_i(2) # => 1
+ *
+ * Returns zero if there is no leading valid number:
+ *
+ * 'abcdef'.to_i # => 0
+ * '2'.to_i(2) # => 0
*
- * "12345".to_i #=> 12345
- * "99 red balloons".to_i #=> 99
- * "0a".to_i #=> 0
- * "0a".to_i(16) #=> 10
- * "hello".to_i #=> 0
- * "1100101".to_i(2) #=> 101
- * "1100101".to_i(8) #=> 294977
- * "1100101".to_i(10) #=> 1100101
- * "1100101".to_i(16) #=> 17826049
*/
static VALUE
@@ -6203,7 +6636,7 @@ rb_str_to_i(int argc, VALUE *argv, VALUE str)
int base = 10;
if (rb_check_arity(argc, 0, 1) && (base = NUM2INT(argv[0])) < 0) {
- rb_raise(rb_eArgError, "invalid radix %d", base);
+ rb_raise(rb_eArgError, "invalid radix %d", base);
}
return rb_str_to_inum(str, base, FALSE);
}
@@ -6211,16 +6644,21 @@ rb_str_to_i(int argc, VALUE *argv, VALUE str)
/*
* call-seq:
- * str.to_f -> float
+ * to_f -> float
*
- * Returns the result of interpreting leading characters in <i>str</i> as a
- * floating point number. Extraneous characters past the end of a valid number
- * are ignored. If there is not a valid number at the start of <i>str</i>,
- * <code>0.0</code> is returned. This method never raises an exception.
+ * Returns the result of interpreting leading characters in +self+ as a Float:
+ *
+ * '3.14159'.to_f # => 3.14159
+ * '1.234e-2'.to_f # => 0.01234
+ *
+ * Characters past a leading valid number (in the given +base+) are ignored:
+ *
+ * '3.14 (pi to two places)'.to_f # => 3.14
+ *
+ * Returns zero if there is no leading valid number:
+ *
+ * 'abcdef'.to_f # => 0.0
*
- * "123.45e1".to_f #=> 1234.5
- * "45.67 degrees".to_f #=> 45.67
- * "thx1138".to_f #=> 0.0
*/
static VALUE
@@ -6232,19 +6670,17 @@ rb_str_to_f(VALUE str)
/*
* call-seq:
- * str.to_s -> str
- * str.to_str -> str
- *
- * Returns +self+.
+ * to_s -> self or string
*
- * If called on a subclass of String, converts the receiver to a String object.
+ * Returns +self+ if +self+ is a +String+,
+ * or +self+ converted to a +String+ if +self+ is a subclass of +String+.
*/
static VALUE
rb_str_to_s(VALUE str)
{
if (rb_obj_class(str) != rb_cString) {
- return str_duplicate(rb_cString, str);
+ return str_duplicate(rb_cString, str);
}
return str;
}
@@ -6273,23 +6709,23 @@ rb_str_buf_cat_escaped_char(VALUE result, unsigned int c, int unicode_p)
c &= 0xffffffff;
#endif
if (unicode_p) {
- if (c < 0x7F && ISPRINT(c)) {
- snprintf(buf, CHAR_ESC_LEN, "%c", c);
- }
- else if (c < 0x10000) {
- snprintf(buf, CHAR_ESC_LEN, "\\u%04X", c);
- }
- else {
- snprintf(buf, CHAR_ESC_LEN, "\\u{%X}", c);
- }
+ if (c < 0x7F && ISPRINT(c)) {
+ snprintf(buf, CHAR_ESC_LEN, "%c", c);
+ }
+ else if (c < 0x10000) {
+ snprintf(buf, CHAR_ESC_LEN, "\\u%04X", c);
+ }
+ else {
+ snprintf(buf, CHAR_ESC_LEN, "\\u{%X}", c);
+ }
}
else {
- if (c < 0x100) {
- snprintf(buf, CHAR_ESC_LEN, "\\x%02X", c);
- }
- else {
- snprintf(buf, CHAR_ESC_LEN, "\\x{%X}", c);
- }
+ if (c < 0x100) {
+ snprintf(buf, CHAR_ESC_LEN, "\\x%02X", c);
+ }
+ else {
+ snprintf(buf, CHAR_ESC_LEN, "\\x{%X}", c);
+ }
}
l = (int)strlen(buf); /* CHAR_ESC_LEN cannot exceed INT_MAX */
rb_str_buf_cat(result, buf, l);
@@ -6330,9 +6766,9 @@ rb_str_escape(VALUE str)
while (p < pend) {
unsigned int c;
const char *cc;
- int n = rb_enc_precise_mbclen(p, pend, enc);
+ int n = rb_enc_precise_mbclen(p, pend, enc);
if (!MBCLEN_CHARFOUND_P(n)) {
- if (p > prev) str_buf_cat(result, prev, p - prev);
+ if (p > prev) str_buf_cat(result, prev, p - prev);
n = rb_enc_mbminlen(enc);
if (pend < p + n)
n = (int)(pend - p);
@@ -6341,24 +6777,24 @@ rb_str_escape(VALUE str)
str_buf_cat(result, buf, strlen(buf));
prev = ++p;
}
- continue;
- }
+ continue;
+ }
n = MBCLEN_CHARFOUND_LEN(n);
- c = rb_enc_mbc_to_codepoint(p, pend, enc);
- p += n;
+ c = rb_enc_mbc_to_codepoint(p, pend, enc);
+ p += n;
cc = ruby_escaped_char(c);
- if (cc) {
- if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
+ if (cc) {
+ if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
str_buf_cat(result, cc, strlen(cc));
- prev = p;
- }
- else if (asciicompat && rb_enc_isascii(c, enc) && ISPRINT(c)) {
- }
- else {
- if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
- rb_str_buf_cat_escaped_char(result, c, unicode_p);
- prev = p;
- }
+ prev = p;
+ }
+ else if (asciicompat && rb_enc_isascii(c, enc) && ISPRINT(c)) {
+ }
+ else {
+ if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
+ rb_str_buf_cat_escaped_char(result, c, unicode_p);
+ prev = p;
+ }
}
if (p > prev) str_buf_cat(result, prev, p - prev);
ENCODING_CODERANGE_SET(result, rb_usascii_encindex(), ENC_CODERANGE_7BIT);
@@ -6367,22 +6803,23 @@ rb_str_escape(VALUE str)
}
/*
- * call-seq:
- * str.inspect -> string
+ * call-seq:
+ * inspect -> string
*
- * Returns a printable version of _str_, surrounded by quote marks,
- * with special characters escaped.
+ * Returns a printable version of +self+, enclosed in double-quotes,
+ * and with special characters escaped:
+ *
+ * s = "foo\tbar\tbaz\n"
+ * s.inspect
+ * # => "\"foo\\tbar\\tbaz\\n\""
*
- * str = "hello"
- * str[3] = "\b"
- * str.inspect #=> "\"hel\\bo\""
*/
VALUE
rb_str_inspect(VALUE str)
{
int encidx = ENCODING_GET(str);
- rb_encoding *enc = rb_enc_from_index(encidx), *actenc;
+ rb_encoding *enc = rb_enc_from_index(encidx);
const char *p, *pend, *prev;
char buf[CHAR_ESC_LEN + 1];
VALUE result = rb_str_buf_new(0);
@@ -6397,18 +6834,13 @@ rb_str_inspect(VALUE str)
p = RSTRING_PTR(str); pend = RSTRING_END(str);
prev = p;
- actenc = get_actual_encoding(encidx, str);
- if (actenc != enc) {
- enc = actenc;
- if (unicode_p) unicode_p = rb_enc_unicode_p(enc);
- }
while (p < pend) {
- unsigned int c, cc;
- int n;
+ unsigned int c, cc;
+ int n;
n = rb_enc_precise_mbclen(p, pend, enc);
if (!MBCLEN_CHARFOUND_P(n)) {
- if (p > prev) str_buf_cat(result, prev, p - prev);
+ if (p > prev) str_buf_cat(result, prev, p - prev);
n = rb_enc_mbminlen(enc);
if (pend < p + n)
n = (int)(pend - p);
@@ -6417,54 +6849,62 @@ rb_str_inspect(VALUE str)
str_buf_cat(result, buf, strlen(buf));
prev = ++p;
}
- continue;
- }
+ continue;
+ }
n = MBCLEN_CHARFOUND_LEN(n);
- c = rb_enc_mbc_to_codepoint(p, pend, enc);
- p += n;
- if ((asciicompat || unicode_p) &&
- (c == '"'|| c == '\\' ||
- (c == '#' &&
+ c = rb_enc_mbc_to_codepoint(p, pend, enc);
+ p += n;
+ if ((asciicompat || unicode_p) &&
+ (c == '"'|| c == '\\' ||
+ (c == '#' &&
p < pend &&
MBCLEN_CHARFOUND_P(rb_enc_precise_mbclen(p,pend,enc)) &&
(cc = rb_enc_codepoint(p,pend,enc),
(cc == '$' || cc == '@' || cc == '{'))))) {
- if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
- str_buf_cat2(result, "\\");
- if (asciicompat || enc == resenc) {
- prev = p - n;
- continue;
- }
- }
- switch (c) {
- case '\n': cc = 'n'; break;
- case '\r': cc = 'r'; break;
- case '\t': cc = 't'; break;
- case '\f': cc = 'f'; break;
- case '\013': cc = 'v'; break;
- case '\010': cc = 'b'; break;
- case '\007': cc = 'a'; break;
- case 033: cc = 'e'; break;
- default: cc = 0; break;
- }
- if (cc) {
- if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
- buf[0] = '\\';
- buf[1] = (char)cc;
- str_buf_cat(result, buf, 2);
- prev = p;
- continue;
- }
- if ((enc == resenc && rb_enc_isprint(c, enc)) ||
- (asciicompat && rb_enc_isascii(c, enc) && ISPRINT(c))) {
- continue;
- }
- else {
- if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
- rb_str_buf_cat_escaped_char(result, c, unicode_p);
- prev = p;
- continue;
- }
+ if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
+ str_buf_cat2(result, "\\");
+ if (asciicompat || enc == resenc) {
+ prev = p - n;
+ continue;
+ }
+ }
+ switch (c) {
+ case '\n': cc = 'n'; break;
+ case '\r': cc = 'r'; break;
+ case '\t': cc = 't'; break;
+ case '\f': cc = 'f'; break;
+ case '\013': cc = 'v'; break;
+ case '\010': cc = 'b'; break;
+ case '\007': cc = 'a'; break;
+ case 033: cc = 'e'; break;
+ default: cc = 0; break;
+ }
+ if (cc) {
+ if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
+ buf[0] = '\\';
+ buf[1] = (char)cc;
+ str_buf_cat(result, buf, 2);
+ prev = p;
+ continue;
+ }
+ /* The special casing of 0x85 (NEXT_LINE) here is because
+ * Oniguruma historically treats it as printable, but it
+ * doesn't match the print POSIX bracket class or character
+ * property in regexps.
+ *
+ * See Ruby Bug #16842 for details:
+ * https://bugs.ruby-lang.org/issues/16842
+ */
+ if ((enc == resenc && rb_enc_isprint(c, enc) && c != 0x85) ||
+ (asciicompat && rb_enc_isascii(c, enc) && ISPRINT(c))) {
+ continue;
+ }
+ else {
+ if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
+ rb_str_buf_cat_escaped_char(result, c, unicode_p);
+ prev = p;
+ continue;
+ }
}
if (p > prev) str_buf_cat(result, prev, p - prev);
str_buf_cat2(result, "\"");
@@ -6476,18 +6916,17 @@ rb_str_inspect(VALUE str)
/*
* call-seq:
- * str.dump -> new_str
+ * dump -> string
*
- * Returns a quoted version of the string with all non-printing characters
- * replaced by <code>\xHH</code> notation and all special characters escaped.
+ * Returns a printable version of +self+, enclosed in double-quotes,
+ * with special characters escaped, and with non-printing characters
+ * replaced by hexadecimal notation:
*
- * This method can be used for round-trip: if the resulting +new_str+ is
- * eval'ed, it will produce the original string.
+ * "hello \n ''".dump # => "\"hello \\n ''\""
+ * "\f\x00\xff\\\"".dump # => "\"\\f\\x00\\xFF\\\\\\\"\""
*
- * "hello \n ''".dump #=> "\"hello \\n ''\""
- * "\f\x00\xff\\\"".dump #=> "\"\\f\\x00\\xFF\\\\\\\"\""
+ * Related: String#undump (inverse of String#dump).
*
- * See also String#undump.
*/
VALUE
@@ -6504,55 +6943,55 @@ rb_str_dump(VALUE str)
len = 2; /* "" */
if (!rb_enc_asciicompat(enc)) {
- len += strlen(nonascii_suffix) - rb_strlen_lit("%s");
- len += strlen(enc->name);
+ len += strlen(nonascii_suffix) - rb_strlen_lit("%s");
+ len += strlen(enc->name);
}
p = RSTRING_PTR(str); pend = p + RSTRING_LEN(str);
while (p < pend) {
- int clen;
- unsigned char c = *p++;
-
- switch (c) {
- case '"': case '\\':
- case '\n': case '\r':
- case '\t': case '\f':
- case '\013': case '\010': case '\007': case '\033':
- clen = 2;
- break;
-
- case '#':
- clen = IS_EVSTR(p, pend) ? 2 : 1;
- break;
-
- default:
- if (ISPRINT(c)) {
- clen = 1;
- }
- else {
- if (u8 && c > 0x7F) { /* \u notation */
- int n = rb_enc_precise_mbclen(p-1, pend, enc);
- if (MBCLEN_CHARFOUND_P(n)) {
- unsigned int cc = rb_enc_mbc_to_codepoint(p-1, pend, enc);
- if (cc <= 0xFFFF)
- clen = 6; /* \uXXXX */
- else if (cc <= 0xFFFFF)
- clen = 9; /* \u{XXXXX} */
- else
- clen = 10; /* \u{XXXXXX} */
- p += MBCLEN_CHARFOUND_LEN(n)-1;
- break;
- }
- }
- clen = 4; /* \xNN */
- }
- break;
- }
-
- if (clen > LONG_MAX - len) {
- rb_raise(rb_eRuntimeError, "string size too big");
- }
- len += clen;
+ int clen;
+ unsigned char c = *p++;
+
+ switch (c) {
+ case '"': case '\\':
+ case '\n': case '\r':
+ case '\t': case '\f':
+ case '\013': case '\010': case '\007': case '\033':
+ clen = 2;
+ break;
+
+ case '#':
+ clen = IS_EVSTR(p, pend) ? 2 : 1;
+ break;
+
+ default:
+ if (ISPRINT(c)) {
+ clen = 1;
+ }
+ else {
+ if (u8 && c > 0x7F) { /* \u notation */
+ int n = rb_enc_precise_mbclen(p-1, pend, enc);
+ if (MBCLEN_CHARFOUND_P(n)) {
+ unsigned int cc = rb_enc_mbc_to_codepoint(p-1, pend, enc);
+ if (cc <= 0xFFFF)
+ clen = 6; /* \uXXXX */
+ else if (cc <= 0xFFFFF)
+ clen = 9; /* \u{XXXXX} */
+ else
+ clen = 10; /* \u{XXXXXX} */
+ p += MBCLEN_CHARFOUND_LEN(n)-1;
+ break;
+ }
+ }
+ clen = 4; /* \xNN */
+ }
+ break;
+ }
+
+ if (clen > LONG_MAX - len) {
+ rb_raise(rb_eRuntimeError, "string size too big");
+ }
+ len += clen;
}
result = rb_str_new(0, len);
@@ -6561,75 +7000,75 @@ rb_str_dump(VALUE str)
*q++ = '"';
while (p < pend) {
- unsigned char c = *p++;
-
- if (c == '"' || c == '\\') {
- *q++ = '\\';
- *q++ = c;
- }
- else if (c == '#') {
- if (IS_EVSTR(p, pend)) *q++ = '\\';
- *q++ = '#';
- }
- else if (c == '\n') {
- *q++ = '\\';
- *q++ = 'n';
- }
- else if (c == '\r') {
- *q++ = '\\';
- *q++ = 'r';
- }
- else if (c == '\t') {
- *q++ = '\\';
- *q++ = 't';
- }
- else if (c == '\f') {
- *q++ = '\\';
- *q++ = 'f';
- }
- else if (c == '\013') {
- *q++ = '\\';
- *q++ = 'v';
- }
- else if (c == '\010') {
- *q++ = '\\';
- *q++ = 'b';
- }
- else if (c == '\007') {
- *q++ = '\\';
- *q++ = 'a';
- }
- else if (c == '\033') {
- *q++ = '\\';
- *q++ = 'e';
- }
- else if (ISPRINT(c)) {
- *q++ = c;
- }
- else {
- *q++ = '\\';
- if (u8) {
- int n = rb_enc_precise_mbclen(p-1, pend, enc) - 1;
- if (MBCLEN_CHARFOUND_P(n)) {
- int cc = rb_enc_mbc_to_codepoint(p-1, pend, enc);
- p += n;
- if (cc <= 0xFFFF)
- snprintf(q, qend-q, "u%04X", cc); /* \uXXXX */
- else
- snprintf(q, qend-q, "u{%X}", cc); /* \u{XXXXX} or \u{XXXXXX} */
- q += strlen(q);
- continue;
- }
- }
- snprintf(q, qend-q, "x%02X", c);
- q += 3;
- }
+ unsigned char c = *p++;
+
+ if (c == '"' || c == '\\') {
+ *q++ = '\\';
+ *q++ = c;
+ }
+ else if (c == '#') {
+ if (IS_EVSTR(p, pend)) *q++ = '\\';
+ *q++ = '#';
+ }
+ else if (c == '\n') {
+ *q++ = '\\';
+ *q++ = 'n';
+ }
+ else if (c == '\r') {
+ *q++ = '\\';
+ *q++ = 'r';
+ }
+ else if (c == '\t') {
+ *q++ = '\\';
+ *q++ = 't';
+ }
+ else if (c == '\f') {
+ *q++ = '\\';
+ *q++ = 'f';
+ }
+ else if (c == '\013') {
+ *q++ = '\\';
+ *q++ = 'v';
+ }
+ else if (c == '\010') {
+ *q++ = '\\';
+ *q++ = 'b';
+ }
+ else if (c == '\007') {
+ *q++ = '\\';
+ *q++ = 'a';
+ }
+ else if (c == '\033') {
+ *q++ = '\\';
+ *q++ = 'e';
+ }
+ else if (ISPRINT(c)) {
+ *q++ = c;
+ }
+ else {
+ *q++ = '\\';
+ if (u8) {
+ int n = rb_enc_precise_mbclen(p-1, pend, enc) - 1;
+ if (MBCLEN_CHARFOUND_P(n)) {
+ int cc = rb_enc_mbc_to_codepoint(p-1, pend, enc);
+ p += n;
+ if (cc <= 0xFFFF)
+ snprintf(q, qend-q, "u%04X", cc); /* \uXXXX */
+ else
+ snprintf(q, qend-q, "u{%X}", cc); /* \u{XXXXX} or \u{XXXXXX} */
+ q += strlen(q);
+ continue;
+ }
+ }
+ snprintf(q, qend-q, "x%02X", c);
+ q += 3;
+ }
}
*q++ = '"';
*q = '\0';
if (!rb_enc_asciicompat(enc)) {
- snprintf(q, qend-q, nonascii_suffix, enc->name);
- encidx = rb_ascii8bit_encindex();
+ snprintf(q, qend-q, nonascii_suffix, enc->name);
+ encidx = rb_ascii8bit_encindex();
}
/* result from dump is ASCII */
rb_enc_associate_index(result, encidx);
@@ -6642,21 +7081,21 @@ unescape_ascii(unsigned int c)
{
switch (c) {
case 'n':
- return '\n';
+ return '\n';
case 'r':
- return '\r';
+ return '\r';
case 't':
- return '\t';
+ return '\t';
case 'f':
- return '\f';
+ return '\f';
case 'v':
- return '\13';
+ return '\13';
case 'b':
- return '\010';
+ return '\010';
case 'a':
- return '\007';
+ return '\007';
case 'e':
- return 033;
+ return 033;
}
UNREACHABLE_RETURN(-1);
}
@@ -6675,9 +7114,9 @@ undump_after_backslash(VALUE undumped, const char **ss, const char *s_end, rb_en
case '\\':
case '"':
case '#':
- rb_str_cat(undumped, s, 1); /* cat itself */
- s++;
- break;
+ rb_str_cat(undumped, s, 1); /* cat itself */
+ s++;
+ break;
case 'n':
case 'r':
case 't':
@@ -6688,81 +7127,81 @@ undump_after_backslash(VALUE undumped, const char **ss, const char *s_end, rb_en
case 'e':
*buf = unescape_ascii(*s);
rb_str_cat(undumped, (char *)buf, 1);
- s++;
- break;
+ s++;
+ break;
case 'u':
- if (*binary) {
- rb_raise(rb_eRuntimeError, "hex escape and Unicode escape are mixed");
- }
- *utf8 = true;
- if (++s >= s_end) {
- rb_raise(rb_eRuntimeError, "invalid Unicode escape");
- }
- if (enc_utf8 == NULL) enc_utf8 = rb_utf8_encoding();
- if (*penc != enc_utf8) {
- *penc = enc_utf8;
- rb_enc_associate(undumped, enc_utf8);
- }
- if (*s == '{') { /* handle \u{...} form */
- s++;
- for (;;) {
- if (s >= s_end) {
- rb_raise(rb_eRuntimeError, "unterminated Unicode escape");
- }
- if (*s == '}') {
- s++;
- break;
- }
- if (ISSPACE(*s)) {
- s++;
- continue;
- }
- c = scan_hex(s, s_end-s, &hexlen);
- if (hexlen == 0 || hexlen > 6) {
- rb_raise(rb_eRuntimeError, "invalid Unicode escape");
- }
- if (c > 0x10ffff) {
- rb_raise(rb_eRuntimeError, "invalid Unicode codepoint (too large)");
- }
- if (0xd800 <= c && c <= 0xdfff) {
- rb_raise(rb_eRuntimeError, "invalid Unicode codepoint");
- }
+ if (*binary) {
+ rb_raise(rb_eRuntimeError, "hex escape and Unicode escape are mixed");
+ }
+ *utf8 = true;
+ if (++s >= s_end) {
+ rb_raise(rb_eRuntimeError, "invalid Unicode escape");
+ }
+ if (enc_utf8 == NULL) enc_utf8 = rb_utf8_encoding();
+ if (*penc != enc_utf8) {
+ *penc = enc_utf8;
+ rb_enc_associate(undumped, enc_utf8);
+ }
+ if (*s == '{') { /* handle \u{...} form */
+ s++;
+ for (;;) {
+ if (s >= s_end) {
+ rb_raise(rb_eRuntimeError, "unterminated Unicode escape");
+ }
+ if (*s == '}') {
+ s++;
+ break;
+ }
+ if (ISSPACE(*s)) {
+ s++;
+ continue;
+ }
+ c = scan_hex(s, s_end-s, &hexlen);
+ if (hexlen == 0 || hexlen > 6) {
+ rb_raise(rb_eRuntimeError, "invalid Unicode escape");
+ }
+ if (c > 0x10ffff) {
+ rb_raise(rb_eRuntimeError, "invalid Unicode codepoint (too large)");
+ }
+ if (0xd800 <= c && c <= 0xdfff) {
+ rb_raise(rb_eRuntimeError, "invalid Unicode codepoint");
+ }
codelen = rb_enc_mbcput(c, (char *)buf, *penc);
rb_str_cat(undumped, (char *)buf, codelen);
- s += hexlen;
- }
- }
- else { /* handle \uXXXX form */
- c = scan_hex(s, 4, &hexlen);
- if (hexlen != 4) {
- rb_raise(rb_eRuntimeError, "invalid Unicode escape");
- }
- if (0xd800 <= c && c <= 0xdfff) {
- rb_raise(rb_eRuntimeError, "invalid Unicode codepoint");
- }
+ s += hexlen;
+ }
+ }
+ else { /* handle \uXXXX form */
+ c = scan_hex(s, 4, &hexlen);
+ if (hexlen != 4) {
+ rb_raise(rb_eRuntimeError, "invalid Unicode escape");
+ }
+ if (0xd800 <= c && c <= 0xdfff) {
+ rb_raise(rb_eRuntimeError, "invalid Unicode codepoint");
+ }
codelen = rb_enc_mbcput(c, (char *)buf, *penc);
rb_str_cat(undumped, (char *)buf, codelen);
- s += hexlen;
- }
- break;
+ s += hexlen;
+ }
+ break;
case 'x':
- if (*utf8) {
- rb_raise(rb_eRuntimeError, "hex escape and Unicode escape are mixed");
- }
- *binary = true;
- if (++s >= s_end) {
- rb_raise(rb_eRuntimeError, "invalid hex escape");
- }
- *buf = scan_hex(s, 2, &hexlen);
- if (hexlen != 2) {
- rb_raise(rb_eRuntimeError, "invalid hex escape");
- }
+ if (*utf8) {
+ rb_raise(rb_eRuntimeError, "hex escape and Unicode escape are mixed");
+ }
+ *binary = true;
+ if (++s >= s_end) {
+ rb_raise(rb_eRuntimeError, "invalid hex escape");
+ }
+ *buf = scan_hex(s, 2, &hexlen);
+ if (hexlen != 2) {
+ rb_raise(rb_eRuntimeError, "invalid hex escape");
+ }
rb_str_cat(undumped, (char *)buf, 1);
- s += hexlen;
- break;
+ s += hexlen;
+ break;
default:
- rb_str_cat(undumped, s-1, 2);
- s++;
+ rb_str_cat(undumped, s-1, 2);
+ s++;
}
*ss = s;
@@ -6772,12 +7211,17 @@ static VALUE rb_str_is_ascii_only_p(VALUE str);
/*
* call-seq:
- * str.undump -> new_str
+ * undump -> string
*
- * Returns an unescaped version of the string.
- * This does the inverse of String#dump.
+ * Returns an unescaped version of +self+:
+ *
+ * s_orig = "\f\x00\xff\\\"" # => "\f\u0000\xFF\\\""
+ * s_dumped = s_orig.dump # => "\"\\f\\x00\\xFF\\\\\\\"\""
+ * s_undumped = s_dumped.undump # => "\f\u0000\xFF\\\""
+ * s_undumped == s_orig # => true
+ *
+ * Related: String#dump (inverse of String#undump).
*
- * "\"hello \\n ''\"".undump #=> "hello \n ''"
*/
static VALUE
@@ -6793,10 +7237,10 @@ str_undump(VALUE str)
rb_must_asciicompat(str);
if (rb_str_is_ascii_only_p(str) == Qfalse) {
- rb_raise(rb_eRuntimeError, "non-ASCII character detected");
+ rb_raise(rb_eRuntimeError, "non-ASCII character detected");
}
if (!str_null_check(str, &w)) {
- rb_raise(rb_eRuntimeError, "string contains null byte");
+ rb_raise(rb_eRuntimeError, "string contains null byte");
}
if (RSTRING_LEN(str) < 2) goto invalid_format;
if (*s != '"') goto invalid_format;
@@ -6805,65 +7249,67 @@ str_undump(VALUE str)
s++;
for (;;) {
- if (s >= s_end) {
- rb_raise(rb_eRuntimeError, "unterminated dumped string");
- }
-
- if (*s == '"') {
- /* epilogue */
- s++;
- if (s == s_end) {
- /* ascii compatible dumped string */
- break;
- }
- else {
- static const char force_encoding_suffix[] = ".force_encoding(\""; /* "\")" */
- static const char dup_suffix[] = ".dup";
- const char *encname;
- int encidx;
- ptrdiff_t size;
-
- /* check separately for strings dumped by older versions */
- size = sizeof(dup_suffix) - 1;
- if (s_end - s > size && memcmp(s, dup_suffix, size) == 0) s += size;
-
- size = sizeof(force_encoding_suffix) - 1;
- if (s_end - s <= size) goto invalid_format;
- if (memcmp(s, force_encoding_suffix, size) != 0) goto invalid_format;
- s += size;
-
- if (utf8) {
- rb_raise(rb_eRuntimeError, "dumped string contained Unicode escape but used force_encoding");
- }
-
- encname = s;
- s = memchr(s, '"', s_end-s);
- size = s - encname;
- if (!s) goto invalid_format;
- if (s_end - s != 2) goto invalid_format;
- if (s[0] != '"' || s[1] != ')') goto invalid_format;
-
- encidx = rb_enc_find_index2(encname, (long)size);
- if (encidx < 0) {
- rb_raise(rb_eRuntimeError, "dumped string has unknown encoding name");
- }
- rb_enc_associate_index(undumped, encidx);
- }
- break;
- }
-
- if (*s == '\\') {
- s++;
- if (s >= s_end) {
- rb_raise(rb_eRuntimeError, "invalid escape");
- }
- undump_after_backslash(undumped, &s, s_end, &enc, &utf8, &binary);
- }
- else {
- rb_str_cat(undumped, s++, 1);
- }
+ if (s >= s_end) {
+ rb_raise(rb_eRuntimeError, "unterminated dumped string");
+ }
+
+ if (*s == '"') {
+ /* epilogue */
+ s++;
+ if (s == s_end) {
+ /* ascii compatible dumped string */
+ break;
+ }
+ else {
+ static const char force_encoding_suffix[] = ".force_encoding(\""; /* "\")" */
+ static const char dup_suffix[] = ".dup";
+ const char *encname;
+ int encidx;
+ ptrdiff_t size;
+
+ /* check separately for strings dumped by older versions */
+ size = sizeof(dup_suffix) - 1;
+ if (s_end - s > size && memcmp(s, dup_suffix, size) == 0) s += size;
+
+ size = sizeof(force_encoding_suffix) - 1;
+ if (s_end - s <= size) goto invalid_format;
+ if (memcmp(s, force_encoding_suffix, size) != 0) goto invalid_format;
+ s += size;
+
+ if (utf8) {
+ rb_raise(rb_eRuntimeError, "dumped string contained Unicode escape but used force_encoding");
+ }
+
+ encname = s;
+ s = memchr(s, '"', s_end-s);
+ size = s - encname;
+ if (!s) goto invalid_format;
+ if (s_end - s != 2) goto invalid_format;
+ if (s[0] != '"' || s[1] != ')') goto invalid_format;
+
+ encidx = rb_enc_find_index2(encname, (long)size);
+ if (encidx < 0) {
+ rb_raise(rb_eRuntimeError, "dumped string has unknown encoding name");
+ }
+ rb_enc_associate_index(undumped, encidx);
+ }
+ break;
+ }
+
+ if (*s == '\\') {
+ s++;
+ if (s >= s_end) {
+ rb_raise(rb_eRuntimeError, "invalid escape");
+ }
+ undump_after_backslash(undumped, &s, s_end, &enc, &utf8, &binary);
+ }
+ else {
+ rb_str_cat(undumped, s++, 1);
+ }
}
+ RB_GC_GUARD(str);
+
return undumped;
invalid_format:
rb_raise(rb_eRuntimeError, "invalid dumped string; not wrapped with '\"' nor '\"...\".force_encoding(\"...\")' form");
@@ -6873,8 +7319,8 @@ static void
rb_str_check_dummy_enc(rb_encoding *enc)
{
if (rb_enc_dummy_p(enc)) {
- rb_raise(rb_eEncCompatError, "incompatible encoding with this operation: %s",
- rb_enc_name(enc));
+ rb_raise(rb_eEncCompatError, "incompatible encoding with this operation: %s",
+ rb_enc_name(enc));
}
}
@@ -6894,35 +7340,35 @@ check_case_options(int argc, VALUE *argv, OnigCaseFoldType flags)
if (argc>2)
rb_raise(rb_eArgError, "too many options");
if (argv[0]==sym_turkic) {
- flags |= ONIGENC_CASE_FOLD_TURKISH_AZERI;
- if (argc==2) {
- if (argv[1]==sym_lithuanian)
- flags |= ONIGENC_CASE_FOLD_LITHUANIAN;
- else
- rb_raise(rb_eArgError, "invalid second option");
- }
+ flags |= ONIGENC_CASE_FOLD_TURKISH_AZERI;
+ if (argc==2) {
+ if (argv[1]==sym_lithuanian)
+ flags |= ONIGENC_CASE_FOLD_LITHUANIAN;
+ else
+ rb_raise(rb_eArgError, "invalid second option");
+ }
}
else if (argv[0]==sym_lithuanian) {
- flags |= ONIGENC_CASE_FOLD_LITHUANIAN;
- if (argc==2) {
- if (argv[1]==sym_turkic)
- flags |= ONIGENC_CASE_FOLD_TURKISH_AZERI;
- else
- rb_raise(rb_eArgError, "invalid second option");
- }
+ flags |= ONIGENC_CASE_FOLD_LITHUANIAN;
+ if (argc==2) {
+ if (argv[1]==sym_turkic)
+ flags |= ONIGENC_CASE_FOLD_TURKISH_AZERI;
+ else
+ rb_raise(rb_eArgError, "invalid second option");
+ }
}
else if (argc>1)
- rb_raise(rb_eArgError, "too many options");
+ rb_raise(rb_eArgError, "too many options");
else if (argv[0]==sym_ascii)
- flags |= ONIGENC_CASE_ASCII_ONLY;
+ flags |= ONIGENC_CASE_ASCII_ONLY;
else if (argv[0]==sym_fold) {
- if ((flags & (ONIGENC_CASE_UPCASE|ONIGENC_CASE_DOWNCASE)) == ONIGENC_CASE_DOWNCASE)
- flags ^= ONIGENC_CASE_FOLD|ONIGENC_CASE_DOWNCASE;
- else
- rb_raise(rb_eArgError, "option :fold only allowed for downcasing");
+ if ((flags & (ONIGENC_CASE_UPCASE|ONIGENC_CASE_DOWNCASE)) == ONIGENC_CASE_DOWNCASE)
+ flags ^= ONIGENC_CASE_FOLD|ONIGENC_CASE_DOWNCASE;
+ else
+ rb_raise(rb_eArgError, "option :fold only allowed for downcasing");
}
else
- rb_raise(rb_eArgError, "invalid option");
+ rb_raise(rb_eArgError, "invalid option");
return flags;
}
@@ -6962,7 +7408,8 @@ mapping_buffer_free(void *p)
static const rb_data_type_t mapping_buffer_type = {
"mapping_buffer",
- {0, mapping_buffer_free,}
+ {0, mapping_buffer_free,},
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
};
static VALUE
@@ -6986,54 +7433,56 @@ rb_str_casemap(VALUE source, OnigCaseFoldType *flags, rb_encoding *enc)
buffer_anchor = TypedData_Wrap_Struct(0, &mapping_buffer_type, 0);
pre_buffer = (mapping_buffer **)&DATA_PTR(buffer_anchor);
while (source_current < source_end) {
- /* increase multiplier using buffer count to converge quickly */
- size_t capa = (size_t)(source_end-source_current)*++buffer_count + CASE_MAPPING_ADDITIONAL_LENGTH;
- if (CASEMAP_DEBUG) {
- fprintf(stderr, "Buffer allocation, capa is %"PRIuSIZE"\n", capa); /* for tuning */
- }
+ /* increase multiplier using buffer count to converge quickly */
+ size_t capa = (size_t)(source_end-source_current)*++buffer_count + CASE_MAPPING_ADDITIONAL_LENGTH;
+ if (CASEMAP_DEBUG) {
+ fprintf(stderr, "Buffer allocation, capa is %"PRIuSIZE"\n", capa); /* for tuning */
+ }
current_buffer = xmalloc(offsetof(mapping_buffer, space) + capa);
*pre_buffer = current_buffer;
pre_buffer = &current_buffer->next;
- current_buffer->next = NULL;
- current_buffer->capa = capa;
- buffer_length_or_invalid = enc->case_map(flags,
- &source_current, source_end,
- current_buffer->space,
- current_buffer->space+current_buffer->capa,
- enc);
- if (buffer_length_or_invalid < 0) {
+ current_buffer->next = NULL;
+ current_buffer->capa = capa;
+ buffer_length_or_invalid = enc->case_map(flags,
+ &source_current, source_end,
+ current_buffer->space,
+ current_buffer->space+current_buffer->capa,
+ enc);
+ if (buffer_length_or_invalid < 0) {
current_buffer = DATA_PTR(buffer_anchor);
DATA_PTR(buffer_anchor) = 0;
mapping_buffer_free(current_buffer);
- rb_raise(rb_eArgError, "input string invalid");
- }
- target_length += current_buffer->used = buffer_length_or_invalid;
+ rb_raise(rb_eArgError, "input string invalid");
+ }
+ target_length += current_buffer->used = buffer_length_or_invalid;
}
if (CASEMAP_DEBUG) {
- fprintf(stderr, "Buffer count is %"PRIuSIZE"\n", buffer_count); /* for tuning */
+ fprintf(stderr, "Buffer count is %"PRIuSIZE"\n", buffer_count); /* for tuning */
}
if (buffer_count==1) {
target = rb_str_new((const char*)current_buffer->space, target_length);
}
else {
- char *target_current;
+ char *target_current;
target = rb_str_new(0, target_length);
- target_current = RSTRING_PTR(target);
+ target_current = RSTRING_PTR(target);
current_buffer = DATA_PTR(buffer_anchor);
- while (current_buffer) {
- memcpy(target_current, current_buffer->space, current_buffer->used);
- target_current += current_buffer->used;
- current_buffer = current_buffer->next;
- }
+ while (current_buffer) {
+ memcpy(target_current, current_buffer->space, current_buffer->used);
+ target_current += current_buffer->used;
+ current_buffer = current_buffer->next;
+ }
}
current_buffer = DATA_PTR(buffer_anchor);
DATA_PTR(buffer_anchor) = 0;
mapping_buffer_free(current_buffer);
+ RB_GC_GUARD(buffer_anchor);
+
/* TODO: check about string terminator character */
- str_enc_copy(target, source);
+ str_enc_copy_direct(target, source);
/*ENC_CODERANGE_SET(mapped, cr);*/
return target;
@@ -7066,10 +7515,10 @@ rb_str_ascii_casemap(VALUE source, VALUE target, OnigCaseFoldType *flags, rb_enc
if (length_or_invalid < 0)
rb_raise(rb_eArgError, "input string invalid");
if (CASEMAP_DEBUG && length_or_invalid != old_length) {
- fprintf(stderr, "problem with rb_str_ascii_casemap"
- "; old_length=%ld, new_length=%d\n", old_length, length_or_invalid);
- rb_raise(rb_eArgError, "internal problem with rb_str_ascii_casemap"
- "; old_length=%ld, new_length=%d\n", old_length, length_or_invalid);
+ fprintf(stderr, "problem with rb_str_ascii_casemap"
+ "; old_length=%ld, new_length=%d\n", old_length, length_or_invalid);
+ rb_raise(rb_eArgError, "internal problem with rb_str_ascii_casemap"
+ "; old_length=%ld, new_length=%d\n", old_length, length_or_invalid);
}
str_enc_copy(target, source);
@@ -7097,13 +7546,21 @@ upcase_single(VALUE str)
/*
* call-seq:
- * str.upcase! -> str or nil
- * str.upcase!([options]) -> str or nil
+ * upcase!(*options) -> self or nil
+ *
+ * Upcases the characters in +self+;
+ * returns +self+ if any changes were made, +nil+ otherwise:
*
- * Upcases the contents of <i>str</i>, returning <code>nil</code> if no changes
- * were made.
+ * s = 'Hello World!' # => "Hello World!"
+ * s.upcase! # => "HELLO WORLD!"
+ * s # => "HELLO WORLD!"
+ * s.upcase! # => nil
+ *
+ * The casing may be affected by the given +options+;
+ * see {Case Mapping}[rdoc-ref:case_mapping.rdoc].
+ *
+ * Related: String#upcase, String#downcase, String#downcase!.
*
- * See String#downcase for meaning of +options+ and use with different encodings.
*/
static VALUE
@@ -7122,7 +7579,7 @@ rb_str_upcase_bang(int argc, VALUE *argv, VALUE str)
else if (flags&ONIGENC_CASE_ASCII_ONLY)
rb_str_ascii_casemap(str, str, &flags, enc);
else
- str_shared_replace(str, rb_str_casemap(str, &flags, enc));
+ str_shared_replace(str, rb_str_casemap(str, &flags, enc));
if (ONIGENC_CASE_MODIFIED&flags) return str;
return Qnil;
@@ -7131,15 +7588,18 @@ rb_str_upcase_bang(int argc, VALUE *argv, VALUE str)
/*
* call-seq:
- * str.upcase -> new_str
- * str.upcase([options]) -> new_str
+ * upcase(*options) -> string
+ *
+ * Returns a string containing the upcased characters in +self+:
*
- * Returns a copy of <i>str</i> with all lowercase letters replaced with their
- * uppercase counterparts.
+ * s = 'Hello World!' # => "Hello World!"
+ * s.upcase # => "HELLO WORLD!"
*
- * See String#downcase for meaning of +options+ and use with different encodings.
+ * The casing may be affected by the given +options+;
+ * see {Case Mapping}[rdoc-ref:case_mapping.rdoc].
+ *
+ * Related: String#upcase!, String#downcase, String#downcase!.
*
- * "hEllO".upcase #=> "HELLO"
*/
static VALUE
@@ -7153,7 +7613,7 @@ rb_str_upcase(int argc, VALUE *argv, VALUE str)
enc = str_true_enc(str);
if (case_option_single_p(flags, enc, str)) {
ret = rb_str_new(RSTRING_PTR(str), RSTRING_LEN(str));
- str_enc_copy(ret, str);
+ str_enc_copy_direct(ret, str);
upcase_single(ret);
}
else if (flags&ONIGENC_CASE_ASCII_ONLY) {
@@ -7188,13 +7648,21 @@ downcase_single(VALUE str)
/*
* call-seq:
- * str.downcase! -> str or nil
- * str.downcase!([options]) -> str or nil
+ * downcase!(*options) -> self or nil
+ *
+ * Downcases the characters in +self+;
+ * returns +self+ if any changes were made, +nil+ otherwise:
+ *
+ * s = 'Hello World!' # => "Hello World!"
+ * s.downcase! # => "hello world!"
+ * s # => "hello world!"
+ * s.downcase! # => nil
*
- * Downcases the contents of <i>str</i>, returning <code>nil</code> if no
- * changes were made.
+ * The casing may be affected by the given +options+;
+ * see {Case Mapping}[rdoc-ref:case_mapping.rdoc].
+ *
+ * Related: String#downcase, String#upcase, String#upcase!.
*
- * See String#downcase for meaning of +options+ and use with different encodings.
*/
static VALUE
@@ -7213,7 +7681,7 @@ rb_str_downcase_bang(int argc, VALUE *argv, VALUE str)
else if (flags&ONIGENC_CASE_ASCII_ONLY)
rb_str_ascii_casemap(str, str, &flags, enc);
else
- str_shared_replace(str, rb_str_casemap(str, &flags, enc));
+ str_shared_replace(str, rb_str_casemap(str, &flags, enc));
if (ONIGENC_CASE_MODIFIED&flags) return str;
return Qnil;
@@ -7222,52 +7690,18 @@ rb_str_downcase_bang(int argc, VALUE *argv, VALUE str)
/*
* call-seq:
- * str.downcase -> new_str
- * str.downcase([options]) -> new_str
- *
- * Returns a copy of <i>str</i> with all uppercase letters replaced with their
- * lowercase counterparts. Which letters exactly are replaced, and by which
- * other letters, depends on the presence or absence of options, and on the
- * +encoding+ of the string.
+ * downcase(*options) -> string
*
- * The meaning of the +options+ is as follows:
+ * Returns a string containing the downcased characters in +self+:
*
- * No option ::
- * Full Unicode case mapping, suitable for most languages
- * (see :turkic and :lithuanian options below for exceptions).
- * Context-dependent case mapping as described in Table 3-14 of the
- * Unicode standard is currently not supported.
- * :ascii ::
- * Only the ASCII region, i.e. the characters ``A'' to ``Z'' and
- * ``a'' to ``z'', are affected.
- * This option cannot be combined with any other option.
- * :turkic ::
- * Full Unicode case mapping, adapted for Turkic languages
- * (Turkish, Azerbaijani, ...). This means that upper case I is mapped to
- * lower case dotless i, and so on.
- * :lithuanian ::
- * Currently, just full Unicode case mapping. In the future, full Unicode
- * case mapping adapted for Lithuanian (keeping the dot on the lower case
- * i even if there is an accent on top).
- * :fold ::
- * Only available on +downcase+ and +downcase!+. Unicode case <b>folding</b>,
- * which is more far-reaching than Unicode case mapping.
- * This option currently cannot be combined with any other option
- * (i.e. there is currently no variant for turkic languages).
+ * s = 'Hello World!' # => "Hello World!"
+ * s.downcase # => "hello world!"
*
- * Please note that several assumptions that are valid for ASCII-only case
- * conversions do not hold for more general case conversions. For example,
- * the length of the result may not be the same as the length of the input
- * (neither in characters nor in bytes), some roundtrip assumptions
- * (e.g. str.downcase == str.upcase.downcase) may not apply, and Unicode
- * normalization (i.e. String#unicode_normalize) is not necessarily maintained
- * by case mapping operations.
+ * The casing may be affected by the given +options+;
+ * see {Case Mapping}[rdoc-ref:case_mapping.rdoc].
*
- * Non-ASCII case mapping/folding is currently supported for UTF-8,
- * UTF-16BE/LE, UTF-32BE/LE, and ISO-8859-1~16 Strings/Symbols.
- * This support will be extended to other encodings.
+ * Related: String#downcase!, String#upcase, String#upcase!.
*
- * "hEllO".downcase #=> "hello"
*/
static VALUE
@@ -7281,7 +7715,7 @@ rb_str_downcase(int argc, VALUE *argv, VALUE str)
enc = str_true_enc(str);
if (case_option_single_p(flags, enc, str)) {
ret = rb_str_new(RSTRING_PTR(str), RSTRING_LEN(str));
- str_enc_copy(ret, str);
+ str_enc_copy_direct(ret, str);
downcase_single(ret);
}
else if (flags&ONIGENC_CASE_ASCII_ONLY) {
@@ -7298,20 +7732,22 @@ rb_str_downcase(int argc, VALUE *argv, VALUE str)
/*
* call-seq:
- * str.capitalize! -> str or nil
- * str.capitalize!([options]) -> str or nil
+ * capitalize!(*options) -> self or nil
+ *
+ * Upcases the first character in +self+;
+ * downcases the remaining characters;
+ * returns +self+ if any changes were made, +nil+ otherwise:
+ *
+ * s = 'hello World!' # => "hello World!"
+ * s.capitalize! # => "Hello world!"
+ * s # => "Hello world!"
+ * s.capitalize! # => nil
*
- * Modifies <i>str</i> by converting the first character to uppercase and the
- * remainder to lowercase. Returns <code>nil</code> if no changes are made.
- * There is an exception for modern Georgian (mkhedruli/MTAVRULI), where
- * the result is the same as for String#downcase, to avoid mixed case.
+ * The casing may be affected by the given +options+;
+ * see {Case Mapping}[rdoc-ref:case_mapping.rdoc].
*
- * See String#downcase for meaning of +options+ and use with different encodings.
+ * Related: String#capitalize.
*
- * a = "hello"
- * a.capitalize! #=> "Hello"
- * a #=> "Hello"
- * a.capitalize! #=> nil
*/
static VALUE
@@ -7327,7 +7763,7 @@ rb_str_capitalize_bang(int argc, VALUE *argv, VALUE str)
if (flags&ONIGENC_CASE_ASCII_ONLY)
rb_str_ascii_casemap(str, str, &flags, enc);
else
- str_shared_replace(str, rb_str_casemap(str, &flags, enc));
+ str_shared_replace(str, rb_str_casemap(str, &flags, enc));
if (ONIGENC_CASE_MODIFIED&flags) return str;
return Qnil;
@@ -7336,17 +7772,20 @@ rb_str_capitalize_bang(int argc, VALUE *argv, VALUE str)
/*
* call-seq:
- * str.capitalize -> new_str
- * str.capitalize([options]) -> new_str
+ * capitalize(*options) -> string
*
- * Returns a copy of <i>str</i> with the first character converted to uppercase
- * and the remainder to lowercase.
+ * Returns a string containing the characters in +self+;
+ * the first character is upcased;
+ * the remaining characters are downcased:
*
- * See String#downcase for meaning of +options+ and use with different encodings.
+ * s = 'hello World!' # => "hello World!"
+ * s.capitalize # => "Hello world!"
+ *
+ * The casing may be affected by the given +options+;
+ * see {Case Mapping}[rdoc-ref:case_mapping.rdoc].
+ *
+ * Related: String#capitalize!.
*
- * "hello".capitalize #=> "Hello"
- * "HELLO".capitalize #=> "Hello"
- * "123ABC".capitalize #=> "123abc"
*/
static VALUE
@@ -7372,14 +7811,22 @@ rb_str_capitalize(int argc, VALUE *argv, VALUE str)
/*
* call-seq:
- * str.swapcase! -> str or nil
- * str.swapcase!([options]) -> str or nil
+ * swapcase!(*options) -> self or nil
+ *
+ * Upcases each lowercase character in +self+;
+ * downcases uppercase character;
+ * returns +self+ if any changes were made, +nil+ otherwise:
*
- * Equivalent to String#swapcase, but modifies the receiver in place,
- * returning <i>str</i>, or <code>nil</code> if no changes were made.
+ * s = 'Hello World!' # => "Hello World!"
+ * s.swapcase! # => "hELLO wORLD!"
+ * s # => "hELLO wORLD!"
+ * ''.swapcase! # => nil
+ *
+ * The casing may be affected by the given +options+;
+ * see {Case Mapping}[rdoc-ref:case_mapping.rdoc].
+ *
+ * Related: String#swapcase.
*
- * See String#downcase for meaning of +options+ and use with
- * different encodings.
*/
static VALUE
@@ -7394,7 +7841,7 @@ rb_str_swapcase_bang(int argc, VALUE *argv, VALUE str)
if (flags&ONIGENC_CASE_ASCII_ONLY)
rb_str_ascii_casemap(str, str, &flags, enc);
else
- str_shared_replace(str, rb_str_casemap(str, &flags, enc));
+ str_shared_replace(str, rb_str_casemap(str, &flags, enc));
if (ONIGENC_CASE_MODIFIED&flags) return str;
return Qnil;
@@ -7403,16 +7850,20 @@ rb_str_swapcase_bang(int argc, VALUE *argv, VALUE str)
/*
* call-seq:
- * str.swapcase -> new_str
- * str.swapcase([options]) -> new_str
+ * swapcase(*options) -> string
+ *
+ * Returns a string containing the characters in +self+, with cases reversed;
+ * each uppercase character is downcased;
+ * each lowercase character is upcased:
*
- * Returns a copy of <i>str</i> with uppercase alphabetic characters converted
- * to lowercase and lowercase characters converted to uppercase.
+ * s = 'Hello World!' # => "Hello World!"
+ * s.swapcase # => "hELLO wORLD!"
*
- * See String#downcase for meaning of +options+ and use with different encodings.
+ * The casing may be affected by the given +options+;
+ * see {Case Mapping}[rdoc-ref:case_mapping.rdoc].
+ *
+ * Related: String#swapcase!.
*
- * "Hello".swapcase #=> "hELLO"
- * "cYbEr_PuNk11".swapcase #=> "CyBeR_pUnK11"
*/
static VALUE
@@ -7450,50 +7901,52 @@ trnext(struct tr *t, rb_encoding *enc)
for (;;) {
nextpart:
- if (!t->gen) {
- if (t->p == t->pend) return -1;
- if (rb_enc_ascget(t->p, t->pend, &n, enc) == '\\' && t->p + n < t->pend) {
- t->p += n;
- }
- t->now = rb_enc_codepoint_len(t->p, t->pend, &n, enc);
- t->p += n;
- if (rb_enc_ascget(t->p, t->pend, &n, enc) == '-' && t->p + n < t->pend) {
- t->p += n;
- if (t->p < t->pend) {
- unsigned int c = rb_enc_codepoint_len(t->p, t->pend, &n, enc);
- t->p += n;
- if (t->now > c) {
- if (t->now < 0x80 && c < 0x80) {
- rb_raise(rb_eArgError,
- "invalid range \"%c-%c\" in string transliteration",
- t->now, c);
- }
- else {
- rb_raise(rb_eArgError, "invalid range in string transliteration");
- }
- continue; /* not reached */
- }
- t->gen = 1;
- t->max = c;
- }
- }
- return t->now;
- }
- else {
- while (ONIGENC_CODE_TO_MBCLEN(enc, ++t->now) <= 0) {
- if (t->now == t->max) {
- t->gen = 0;
- goto nextpart;
- }
- }
- if (t->now < t->max) {
- return t->now;
- }
- else {
- t->gen = 0;
- return t->max;
- }
- }
+ if (!t->gen) {
+ if (t->p == t->pend) return -1;
+ if (rb_enc_ascget(t->p, t->pend, &n, enc) == '\\' && t->p + n < t->pend) {
+ t->p += n;
+ }
+ t->now = rb_enc_codepoint_len(t->p, t->pend, &n, enc);
+ t->p += n;
+ if (rb_enc_ascget(t->p, t->pend, &n, enc) == '-' && t->p + n < t->pend) {
+ t->p += n;
+ if (t->p < t->pend) {
+ unsigned int c = rb_enc_codepoint_len(t->p, t->pend, &n, enc);
+ t->p += n;
+ if (t->now > c) {
+ if (t->now < 0x80 && c < 0x80) {
+ rb_raise(rb_eArgError,
+ "invalid range \"%c-%c\" in string transliteration",
+ t->now, c);
+ }
+ else {
+ rb_raise(rb_eArgError, "invalid range in string transliteration");
+ }
+ continue; /* not reached */
+ }
+ else if (t->now < c) {
+ t->gen = 1;
+ t->max = c;
+ }
+ }
+ }
+ return t->now;
+ }
+ else {
+ while (ONIGENC_CODE_TO_MBCLEN(enc, ++t->now) <= 0) {
+ if (t->now == t->max) {
+ t->gen = 0;
+ goto nextpart;
+ }
+ }
+ if (t->now < t->max) {
+ return t->now;
+ }
+ else {
+ t->gen = 0;
+ return t->max;
+ }
+ }
}
}
@@ -7517,30 +7970,30 @@ tr_trans(VALUE str, VALUE src, VALUE repl, int sflag)
#define CHECK_IF_ASCII(c) \
(void)((cr == ENC_CODERANGE_7BIT && !rb_isascii(c)) ? \
- (cr = ENC_CODERANGE_VALID) : 0)
+ (cr = ENC_CODERANGE_VALID) : 0)
StringValue(src);
StringValue(repl);
if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return Qnil;
if (RSTRING_LEN(repl) == 0) {
- return rb_str_delete_bang(1, &src, str);
+ return rb_str_delete_bang(1, &src, str);
}
cr = ENC_CODERANGE(str);
e1 = rb_enc_check(str, src);
e2 = rb_enc_check(str, repl);
if (e1 == e2) {
- enc = e1;
+ enc = e1;
}
else {
- enc = rb_enc_check(src, repl);
+ enc = rb_enc_check(src, repl);
}
trsrc.p = RSTRING_PTR(src); trsrc.pend = trsrc.p + RSTRING_LEN(src);
if (RSTRING_LEN(src) > 1 &&
- rb_enc_ascget(trsrc.p, trsrc.pend, &l, enc) == '^' &&
- trsrc.p + l < trsrc.pend) {
- cflag = 1;
- trsrc.p += l;
+ rb_enc_ascget(trsrc.p, trsrc.pend, &l, enc) == '^' &&
+ trsrc.p + l < trsrc.pend) {
+ cflag = 1;
+ trsrc.p += l;
}
trrepl.p = RSTRING_PTR(repl);
trrepl.pend = trrepl.p + RSTRING_LEN(repl);
@@ -7549,198 +8002,213 @@ tr_trans(VALUE str, VALUE src, VALUE repl, int sflag)
trsrc.max = trrepl.max = 0;
if (cflag) {
- for (i=0; i<256; i++) {
- trans[i] = 1;
- }
- while ((c = trnext(&trsrc, enc)) != errc) {
- if (c < 256) {
- trans[c] = errc;
- }
- else {
- if (!hash) hash = rb_hash_new();
- rb_hash_aset(hash, UINT2NUM(c), Qtrue);
- }
- }
- while ((c = trnext(&trrepl, enc)) != errc)
- /* retrieve last replacer */;
- last = trrepl.now;
- for (i=0; i<256; i++) {
- if (trans[i] != errc) {
- trans[i] = last;
- }
- }
+ for (i=0; i<256; i++) {
+ trans[i] = 1;
+ }
+ while ((c = trnext(&trsrc, enc)) != errc) {
+ if (c < 256) {
+ trans[c] = errc;
+ }
+ else {
+ if (!hash) hash = rb_hash_new();
+ rb_hash_aset(hash, UINT2NUM(c), Qtrue);
+ }
+ }
+ while ((c = trnext(&trrepl, enc)) != errc)
+ /* retrieve last replacer */;
+ last = trrepl.now;
+ for (i=0; i<256; i++) {
+ if (trans[i] != errc) {
+ trans[i] = last;
+ }
+ }
}
else {
- unsigned int r;
-
- for (i=0; i<256; i++) {
- trans[i] = errc;
- }
- while ((c = trnext(&trsrc, enc)) != errc) {
- r = trnext(&trrepl, enc);
- if (r == errc) r = trrepl.now;
- if (c < 256) {
- trans[c] = r;
- if (rb_enc_codelen(r, enc) != 1) singlebyte = 0;
- }
- else {
- if (!hash) hash = rb_hash_new();
- rb_hash_aset(hash, UINT2NUM(c), UINT2NUM(r));
- }
- }
+ unsigned int r;
+
+ for (i=0; i<256; i++) {
+ trans[i] = errc;
+ }
+ while ((c = trnext(&trsrc, enc)) != errc) {
+ r = trnext(&trrepl, enc);
+ if (r == errc) r = trrepl.now;
+ if (c < 256) {
+ trans[c] = r;
+ if (rb_enc_codelen(r, enc) != 1) singlebyte = 0;
+ }
+ else {
+ if (!hash) hash = rb_hash_new();
+ rb_hash_aset(hash, UINT2NUM(c), UINT2NUM(r));
+ }
+ }
}
if (cr == ENC_CODERANGE_VALID && rb_enc_asciicompat(e1))
- cr = ENC_CODERANGE_7BIT;
+ cr = ENC_CODERANGE_7BIT;
str_modify_keep_cr(str);
s = (unsigned char *)RSTRING_PTR(str); send = (unsigned char *)RSTRING_END(str);
termlen = rb_enc_mbminlen(enc);
if (sflag) {
- int clen, tlen;
- long offset, max = RSTRING_LEN(str);
- unsigned int save = -1;
+ int clen, tlen;
+ long offset, max = RSTRING_LEN(str);
+ unsigned int save = -1;
unsigned char *buf = ALLOC_N(unsigned char, max + termlen), *t = buf;
- while (s < send) {
- int may_modify = 0;
-
- c0 = c = rb_enc_codepoint_len((char *)s, (char *)send, &clen, e1);
- tlen = enc == e1 ? clen : rb_enc_codelen(c, enc);
-
- s += clen;
- if (c < 256) {
- c = trans[c];
- }
- else if (hash) {
- VALUE tmp = rb_hash_lookup(hash, UINT2NUM(c));
- if (NIL_P(tmp)) {
- if (cflag) c = last;
- else c = errc;
- }
- else if (cflag) c = errc;
- else c = NUM2INT(tmp);
- }
- else {
- c = errc;
- }
- if (c != (unsigned int)-1) {
- if (save == c) {
- CHECK_IF_ASCII(c);
- continue;
- }
- save = c;
- tlen = rb_enc_codelen(c, enc);
- modify = 1;
- }
- else {
- save = -1;
- c = c0;
- if (enc != e1) may_modify = 1;
- }
- if ((offset = t - buf) + tlen > max) {
- size_t MAYBE_UNUSED(old) = max + termlen;
- max = offset + tlen + (send - s);
+ while (s < send) {
+ int may_modify = 0;
+
+ int r = rb_enc_precise_mbclen((char *)s, (char *)send, e1);
+ if (!MBCLEN_CHARFOUND_P(r)) {
+ xfree(buf);
+ rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(e1));
+ }
+ clen = MBCLEN_CHARFOUND_LEN(r);
+ c0 = c = rb_enc_mbc_to_codepoint((char *)s, (char *)send, e1);
+
+ tlen = enc == e1 ? clen : rb_enc_codelen(c, enc);
+
+ s += clen;
+ if (c < 256) {
+ c = trans[c];
+ }
+ else if (hash) {
+ VALUE tmp = rb_hash_lookup(hash, UINT2NUM(c));
+ if (NIL_P(tmp)) {
+ if (cflag) c = last;
+ else c = errc;
+ }
+ else if (cflag) c = errc;
+ else c = NUM2INT(tmp);
+ }
+ else {
+ c = errc;
+ }
+ if (c != (unsigned int)-1) {
+ if (save == c) {
+ CHECK_IF_ASCII(c);
+ continue;
+ }
+ save = c;
+ tlen = rb_enc_codelen(c, enc);
+ modify = 1;
+ }
+ else {
+ save = -1;
+ c = c0;
+ if (enc != e1) may_modify = 1;
+ }
+ if ((offset = t - buf) + tlen > max) {
+ size_t MAYBE_UNUSED(old) = max + termlen;
+ max = offset + tlen + (send - s);
SIZED_REALLOC_N(buf, unsigned char, max + termlen, old);
- t = buf + offset;
- }
- rb_enc_mbcput(c, t, enc);
- if (may_modify && memcmp(s, t, tlen) != 0) {
- modify = 1;
- }
- CHECK_IF_ASCII(c);
- t += tlen;
- }
- if (!STR_EMBED_P(str)) {
- ruby_sized_xfree(STR_HEAP_PTR(str), STR_HEAP_SIZE(str));
- }
+ t = buf + offset;
+ }
+ rb_enc_mbcput(c, t, enc);
+ if (may_modify && memcmp(s, t, tlen) != 0) {
+ modify = 1;
+ }
+ CHECK_IF_ASCII(c);
+ t += tlen;
+ }
+ if (!STR_EMBED_P(str)) {
+ ruby_sized_xfree(STR_HEAP_PTR(str), STR_HEAP_SIZE(str));
+ }
TERM_FILL((char *)t, termlen);
RSTRING(str)->as.heap.ptr = (char *)buf;
- RSTRING(str)->as.heap.len = t - buf;
- STR_SET_NOEMBED(str);
- RSTRING(str)->as.heap.aux.capa = max;
+ STR_SET_LEN(str, t - buf);
+ STR_SET_NOEMBED(str);
+ RSTRING(str)->as.heap.aux.capa = max;
}
else if (rb_enc_mbmaxlen(enc) == 1 || (singlebyte && !hash)) {
- while (s < send) {
- c = (unsigned char)*s;
- if (trans[c] != errc) {
- if (!cflag) {
- c = trans[c];
- *s = c;
- modify = 1;
- }
- else {
- *s = last;
- modify = 1;
- }
- }
- CHECK_IF_ASCII(c);
- s++;
- }
+ while (s < send) {
+ c = (unsigned char)*s;
+ if (trans[c] != errc) {
+ if (!cflag) {
+ c = trans[c];
+ *s = c;
+ modify = 1;
+ }
+ else {
+ *s = last;
+ modify = 1;
+ }
+ }
+ CHECK_IF_ASCII(c);
+ s++;
+ }
}
else {
- int clen, tlen;
- long offset, max = (long)((send - s) * 1.2);
+ int clen, tlen;
+ long offset, max = (long)((send - s) * 1.2);
unsigned char *buf = ALLOC_N(unsigned char, max + termlen), *t = buf;
- while (s < send) {
- int may_modify = 0;
- c0 = c = rb_enc_codepoint_len((char *)s, (char *)send, &clen, e1);
- tlen = enc == e1 ? clen : rb_enc_codelen(c, enc);
-
- if (c < 256) {
- c = trans[c];
- }
- else if (hash) {
- VALUE tmp = rb_hash_lookup(hash, UINT2NUM(c));
- if (NIL_P(tmp)) {
- if (cflag) c = last;
- else c = errc;
- }
- else if (cflag) c = errc;
- else c = NUM2INT(tmp);
- }
- else {
- c = cflag ? last : errc;
- }
- if (c != errc) {
- tlen = rb_enc_codelen(c, enc);
- modify = 1;
- }
- else {
- c = c0;
- if (enc != e1) may_modify = 1;
- }
- if ((offset = t - buf) + tlen > max) {
- size_t MAYBE_UNUSED(old) = max + termlen;
- max = offset + tlen + (long)((send - s) * 1.2);
+ while (s < send) {
+ int may_modify = 0;
+
+ int r = rb_enc_precise_mbclen((char *)s, (char *)send, e1);
+ if (!MBCLEN_CHARFOUND_P(r)) {
+ xfree(buf);
+ rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(e1));
+ }
+ clen = MBCLEN_CHARFOUND_LEN(r);
+ c0 = c = rb_enc_mbc_to_codepoint((char *)s, (char *)send, e1);
+
+ tlen = enc == e1 ? clen : rb_enc_codelen(c, enc);
+
+ if (c < 256) {
+ c = trans[c];
+ }
+ else if (hash) {
+ VALUE tmp = rb_hash_lookup(hash, UINT2NUM(c));
+ if (NIL_P(tmp)) {
+ if (cflag) c = last;
+ else c = errc;
+ }
+ else if (cflag) c = errc;
+ else c = NUM2INT(tmp);
+ }
+ else {
+ c = cflag ? last : errc;
+ }
+ if (c != errc) {
+ tlen = rb_enc_codelen(c, enc);
+ modify = 1;
+ }
+ else {
+ c = c0;
+ if (enc != e1) may_modify = 1;
+ }
+ if ((offset = t - buf) + tlen > max) {
+ size_t MAYBE_UNUSED(old) = max + termlen;
+ max = offset + tlen + (long)((send - s) * 1.2);
SIZED_REALLOC_N(buf, unsigned char, max + termlen, old);
- t = buf + offset;
- }
- if (s != t) {
- rb_enc_mbcput(c, t, enc);
- if (may_modify && memcmp(s, t, tlen) != 0) {
- modify = 1;
- }
- }
- CHECK_IF_ASCII(c);
- s += clen;
- t += tlen;
- }
- if (!STR_EMBED_P(str)) {
- ruby_sized_xfree(STR_HEAP_PTR(str), STR_HEAP_SIZE(str));
- }
+ t = buf + offset;
+ }
+ if (s != t) {
+ rb_enc_mbcput(c, t, enc);
+ if (may_modify && memcmp(s, t, tlen) != 0) {
+ modify = 1;
+ }
+ }
+ CHECK_IF_ASCII(c);
+ s += clen;
+ t += tlen;
+ }
+ if (!STR_EMBED_P(str)) {
+ ruby_sized_xfree(STR_HEAP_PTR(str), STR_HEAP_SIZE(str));
+ }
TERM_FILL((char *)t, termlen);
RSTRING(str)->as.heap.ptr = (char *)buf;
- RSTRING(str)->as.heap.len = t - buf;
- STR_SET_NOEMBED(str);
- RSTRING(str)->as.heap.aux.capa = max;
+ STR_SET_LEN(str, t - buf);
+ STR_SET_NOEMBED(str);
+ RSTRING(str)->as.heap.aux.capa = max;
}
if (modify) {
- if (cr != ENC_CODERANGE_BROKEN)
- ENC_CODERANGE_SET(str, cr);
- rb_enc_associate(str, enc);
- return str;
+ if (cr != ENC_CODERANGE_BROKEN)
+ ENC_CODERANGE_SET(str, cr);
+ rb_enc_associate(str, enc);
+ return str;
}
return Qnil;
}
@@ -7748,11 +8216,11 @@ tr_trans(VALUE str, VALUE src, VALUE repl, int sflag)
/*
* call-seq:
- * str.tr!(from_str, to_str) -> str or nil
+ * tr!(selector, replacements) -> self or nil
+ *
+ * Like String#tr, but modifies +self+ in place.
+ * Returns +self+ if any changes were made, +nil+ otherwise.
*
- * Translates <i>str</i> in place, using the same rules as
- * String#tr. Returns <i>str</i>, or <code>nil</code> if no changes
- * were made.
*/
static VALUE
@@ -7764,37 +8232,41 @@ rb_str_tr_bang(VALUE str, VALUE src, VALUE repl)
/*
* call-seq:
- * str.tr(from_str, to_str) => new_str
+ * tr(selector, replacements) -> new_string
*
- * Returns a copy of +str+ with the characters in +from_str+ replaced by the
- * corresponding characters in +to_str+. If +to_str+ is shorter than
- * +from_str+, it is padded with its last character in order to maintain the
- * correspondence.
+ * Returns a copy of +self+ with each character specified by string +selector+
+ * translated to the corresponding character in string +replacements+.
+ * The correspondence is _positional_:
*
- * "hello".tr('el', 'ip') #=> "hippo"
- * "hello".tr('aeiou', '*') #=> "h*ll*"
- * "hello".tr('aeiou', 'AA*') #=> "hAll*"
+ * - Each occurrence of the first character specified by +selector+
+ * is translated to the first character in +replacements+.
+ * - Each occurrence of the second character specified by +selector+
+ * is translated to the second character in +replacements+.
+ * - And so on.
*
- * Both strings may use the <code>c1-c2</code> notation to denote ranges of
- * characters, and +from_str+ may start with a <code>^</code>, which denotes
- * all characters except those listed.
+ * Example:
*
- * "hello".tr('a-y', 'b-z') #=> "ifmmp"
- * "hello".tr('^aeiou', '*') #=> "*e**o"
+ * 'hello'.tr('el', 'ip') #=> "hippo"
*
- * The backslash character <code>\\</code> can be used to escape
- * <code>^</code> or <code>-</code> and is otherwise ignored unless it
- * appears at the end of a range or the end of the +from_str+ or +to_str+:
+ * If +replacements+ is shorter than +selector+,
+ * it is implicitly padded with its own last character:
*
- * "hello^world".tr("\\^aeiou", "*") #=> "h*ll**w*rld"
- * "hello-world".tr("a\\-eo", "*") #=> "h*ll**w*rld"
+ * 'hello'.tr('aeiou', '-') # => "h-ll-"
+ * 'hello'.tr('aeiou', 'AA-') # => "hAll-"
*
- * "hello\r\nworld".tr("\r", "") #=> "hello\nworld"
- * "hello\r\nworld".tr("\\r", "") #=> "hello\r\nwold"
- * "hello\r\nworld".tr("\\\r", "") #=> "hello\nworld"
+ * Arguments +selector+ and +replacements+ must be valid character selectors
+ * (see {Character Selectors}[rdoc-ref:character_selectors.rdoc]),
+ * and may use any of its valid forms, including negation, ranges, and escaping:
+ *
+ * # Negation.
+ * 'hello'.tr('^aeiou', '-') # => "-e--o"
+ * # Ranges.
+ * 'ibm'.tr('b-z', 'a-z') # => "hal"
+ * # Escapes.
+ * 'hel^lo'.tr('\^aeiou', '-') # => "h-l-l-" # Escaped leading caret.
+ * 'i-b-m'.tr('b\-z', 'a-z') # => "ibabm" # Escaped embedded hyphen.
+ * 'foo\\bar'.tr('ab\\', 'XYZ') # => "fooZYXr" # Escaped backslash.
*
- * "X['\\b']".tr("X\\", "") #=> "['b']"
- * "X['\\b']".tr("X-\\]", "") #=> "'b'"
*/
static VALUE
@@ -7809,7 +8281,7 @@ rb_str_tr(VALUE str, VALUE src, VALUE repl)
#define TR_TABLE_SIZE (TR_TABLE_MAX+1)
static void
tr_setup_table(VALUE str, char stable[TR_TABLE_SIZE], int first,
- VALUE *tablep, VALUE *ctablep, rb_encoding *enc)
+ VALUE *tablep, VALUE *ctablep, rb_encoding *enc)
{
const unsigned int errc = -1;
char buf[TR_TABLE_MAX];
@@ -7822,51 +8294,51 @@ tr_setup_table(VALUE str, char stable[TR_TABLE_SIZE], int first,
tr.gen = tr.now = tr.max = 0;
if (RSTRING_LEN(str) > 1 && rb_enc_ascget(tr.p, tr.pend, &l, enc) == '^') {
- cflag = 1;
- tr.p += l;
+ cflag = 1;
+ tr.p += l;
}
if (first) {
- for (i=0; i<TR_TABLE_MAX; i++) {
- stable[i] = 1;
- }
- stable[TR_TABLE_MAX] = cflag;
+ for (i=0; i<TR_TABLE_MAX; i++) {
+ stable[i] = 1;
+ }
+ stable[TR_TABLE_MAX] = cflag;
}
else if (stable[TR_TABLE_MAX] && !cflag) {
- stable[TR_TABLE_MAX] = 0;
+ stable[TR_TABLE_MAX] = 0;
}
for (i=0; i<TR_TABLE_MAX; i++) {
- buf[i] = cflag;
+ buf[i] = cflag;
}
while ((c = trnext(&tr, enc)) != errc) {
- if (c < TR_TABLE_MAX) {
- buf[(unsigned char)c] = !cflag;
- }
- else {
- VALUE key = UINT2NUM(c);
-
- if (!table && (first || *tablep || stable[TR_TABLE_MAX])) {
- if (cflag) {
- ptable = *ctablep;
- table = ptable ? ptable : rb_hash_new();
- *ctablep = table;
- }
- else {
- table = rb_hash_new();
- ptable = *tablep;
- *tablep = table;
- }
- }
- if (table && (!ptable || (cflag ^ !NIL_P(rb_hash_aref(ptable, key))))) {
- rb_hash_aset(table, key, Qtrue);
- }
- }
+ if (c < TR_TABLE_MAX) {
+ buf[(unsigned char)c] = !cflag;
+ }
+ else {
+ VALUE key = UINT2NUM(c);
+
+ if (!table && (first || *tablep || stable[TR_TABLE_MAX])) {
+ if (cflag) {
+ ptable = *ctablep;
+ table = ptable ? ptable : rb_hash_new();
+ *ctablep = table;
+ }
+ else {
+ table = rb_hash_new();
+ ptable = *tablep;
+ *tablep = table;
+ }
+ }
+ if (table && (!ptable || (cflag ^ !NIL_P(rb_hash_aref(ptable, key))))) {
+ rb_hash_aset(table, key, Qtrue);
+ }
+ }
}
for (i=0; i<TR_TABLE_MAX; i++) {
- stable[i] = stable[i] && buf[i];
+ stable[i] = stable[i] && buf[i];
}
if (!table && !cflag) {
- *tablep = 0;
+ *tablep = 0;
}
}
@@ -7875,30 +8347,31 @@ static int
tr_find(unsigned int c, const char table[TR_TABLE_SIZE], VALUE del, VALUE nodel)
{
if (c < TR_TABLE_MAX) {
- return table[c] != 0;
+ return table[c] != 0;
}
else {
- VALUE v = UINT2NUM(c);
+ VALUE v = UINT2NUM(c);
- if (del) {
- if (!NIL_P(rb_hash_lookup(del, v)) &&
- (!nodel || NIL_P(rb_hash_lookup(nodel, v)))) {
- return TRUE;
- }
- }
- else if (nodel && !NIL_P(rb_hash_lookup(nodel, v))) {
- return FALSE;
- }
- return table[TR_TABLE_MAX] ? TRUE : FALSE;
+ if (del) {
+ if (!NIL_P(rb_hash_lookup(del, v)) &&
+ (!nodel || NIL_P(rb_hash_lookup(nodel, v)))) {
+ return TRUE;
+ }
+ }
+ else if (nodel && !NIL_P(rb_hash_lookup(nodel, v))) {
+ return FALSE;
+ }
+ return table[TR_TABLE_MAX] ? TRUE : FALSE;
}
}
/*
* call-seq:
- * str.delete!([other_str]+) -> str or nil
+ * delete!(*selectors) -> self or nil
+ *
+ * Like String#delete, but modifies +self+ in place.
+ * Returns +self+ if any changes were made, +nil+ otherwise.
*
- * Performs a <code>delete</code> operation in place, returning <i>str</i>, or
- * <code>nil</code> if <i>str</i> was not modified.
*/
static VALUE
@@ -7914,11 +8387,11 @@ rb_str_delete_bang(int argc, VALUE *argv, VALUE str)
if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return Qnil;
rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
for (i=0; i<argc; i++) {
- VALUE s = argv[i];
+ VALUE s = argv[i];
- StringValue(s);
- enc = rb_enc_check(str, s);
- tr_setup_table(s, squeez, i==0, &del, &nodel, enc);
+ StringValue(s);
+ enc = rb_enc_check(str, s);
+ tr_setup_table(s, squeez, i==0, &del, &nodel, enc);
}
str_modify_keep_cr(str);
@@ -7927,32 +8400,32 @@ rb_str_delete_bang(int argc, VALUE *argv, VALUE str)
send = RSTRING_END(str);
cr = ascompat ? ENC_CODERANGE_7BIT : ENC_CODERANGE_VALID;
while (s < send) {
- unsigned int c;
- int clen;
-
- if (ascompat && (c = *(unsigned char*)s) < 0x80) {
- if (squeez[c]) {
- modify = 1;
- }
- else {
- if (t != s) *t = c;
- t++;
- }
- s++;
- }
- else {
- c = rb_enc_codepoint_len(s, send, &clen, enc);
-
- if (tr_find(c, squeez, del, nodel)) {
- modify = 1;
- }
- else {
- if (t != s) rb_enc_mbcput(c, t, enc);
- t += clen;
- if (cr == ENC_CODERANGE_7BIT) cr = ENC_CODERANGE_VALID;
- }
- s += clen;
- }
+ unsigned int c;
+ int clen;
+
+ if (ascompat && (c = *(unsigned char*)s) < 0x80) {
+ if (squeez[c]) {
+ modify = 1;
+ }
+ else {
+ if (t != s) *t = c;
+ t++;
+ }
+ s++;
+ }
+ else {
+ c = rb_enc_codepoint_len(s, send, &clen, enc);
+
+ if (tr_find(c, squeez, del, nodel)) {
+ modify = 1;
+ }
+ else {
+ if (t != s) rb_enc_mbcput(c, t, enc);
+ t += clen;
+ if (cr == ENC_CODERANGE_7BIT) cr = ENC_CODERANGE_VALID;
+ }
+ s += clen;
+ }
}
TERM_FILL(t, TERM_LEN(str));
STR_SET_LEN(str, t - RSTRING_PTR(str));
@@ -7965,16 +8438,16 @@ rb_str_delete_bang(int argc, VALUE *argv, VALUE str)
/*
* call-seq:
- * str.delete([other_str]+) -> new_str
+ * delete(*selectors) -> new_string
*
- * Returns a copy of <i>str</i> with all characters in the intersection of its
- * arguments deleted. Uses the same rules for building the set of characters as
- * String#count.
+ * Returns a copy of +self+ with characters specified by +selectors+ removed
+ * (see {Multiple Character Selectors}[rdoc-ref:character_selectors.rdoc@Multiple+Character+Selectors]):
*
* "hello".delete "l","lo" #=> "heo"
* "hello".delete "lo" #=> "he"
* "hello".delete "aeiou", "^e" #=> "hell"
* "hello".delete "ej-m" #=> "ho"
+ *
*/
static VALUE
@@ -7988,10 +8461,10 @@ rb_str_delete(int argc, VALUE *argv, VALUE str)
/*
* call-seq:
- * str.squeeze!([other_str]*) -> str or nil
+ * squeeze!(*selectors) -> self or nil
*
- * Squeezes <i>str</i> in place, returning either <i>str</i>, or
- * <code>nil</code> if no changes were made.
+ * Like String#squeeze, but modifies +self+ in place.
+ * Returns +self+ if any changes were made, +nil+ otherwise.
*/
static VALUE
@@ -8006,18 +8479,18 @@ rb_str_squeeze_bang(int argc, VALUE *argv, VALUE str)
unsigned int save;
if (argc == 0) {
- enc = STR_ENC_GET(str);
+ enc = STR_ENC_GET(str);
}
else {
- for (i=0; i<argc; i++) {
- VALUE s = argv[i];
-
- StringValue(s);
- enc = rb_enc_check(str, s);
- if (singlebyte && !single_byte_optimizable(s))
- singlebyte = 0;
- tr_setup_table(s, squeez, i==0, &del, &nodel, enc);
- }
+ for (i=0; i<argc; i++) {
+ VALUE s = argv[i];
+
+ StringValue(s);
+ enc = rb_enc_check(str, s);
+ if (singlebyte && !single_byte_optimizable(s))
+ singlebyte = 0;
+ tr_setup_table(s, squeez, i==0, &del, &nodel, enc);
+ }
}
str_modify_keep_cr(str);
@@ -8030,39 +8503,39 @@ rb_str_squeeze_bang(int argc, VALUE *argv, VALUE str)
if (singlebyte) {
while (s < send) {
unsigned int c = *s++;
- if (c != save || (argc > 0 && !squeez[c])) {
- *t++ = save = c;
- }
- }
+ if (c != save || (argc > 0 && !squeez[c])) {
+ *t++ = save = c;
+ }
+ }
}
else {
- while (s < send) {
- unsigned int c;
- int clen;
+ while (s < send) {
+ unsigned int c;
+ int clen;
if (ascompat && (c = *s) < 0x80) {
- if (c != save || (argc > 0 && !squeez[c])) {
- *t++ = save = c;
- }
- s++;
- }
- else {
+ if (c != save || (argc > 0 && !squeez[c])) {
+ *t++ = save = c;
+ }
+ s++;
+ }
+ else {
c = rb_enc_codepoint_len((char *)s, (char *)send, &clen, enc);
- if (c != save || (argc > 0 && !tr_find(c, squeez, del, nodel))) {
- if (t != s) rb_enc_mbcput(c, t, enc);
- save = c;
- t += clen;
- }
- s += clen;
- }
- }
+ if (c != save || (argc > 0 && !tr_find(c, squeez, del, nodel))) {
+ if (t != s) rb_enc_mbcput(c, t, enc);
+ save = c;
+ t += clen;
+ }
+ s += clen;
+ }
+ }
}
TERM_FILL((char *)t, TERM_LEN(str));
if ((char *)t - RSTRING_PTR(str) != RSTRING_LEN(str)) {
STR_SET_LEN(str, (char *)t - RSTRING_PTR(str));
- modify = 1;
+ modify = 1;
}
if (modify) return str;
@@ -8072,17 +8545,19 @@ rb_str_squeeze_bang(int argc, VALUE *argv, VALUE str)
/*
* call-seq:
- * str.squeeze([other_str]*) -> new_str
+ * squeeze(*selectors) -> new_string
+ *
+ * Returns a copy of +self+ with characters specified by +selectors+ "squeezed"
+ * (see {Multiple Character Selectors}[rdoc-ref:character_selectors.rdoc@Multiple+Character+Selectors]):
*
- * Builds a set of characters from the <i>other_str</i> parameter(s)
- * using the procedure described for String#count. Returns a new
- * string where runs of the same character that occur in this set are
- * replaced by a single character. If no arguments are given, all
- * runs of identical characters are replaced by a single character.
+ * "Squeezed" means that each multiple-character run of a selected character
+ * is squeezed down to a single character;
+ * with no arguments given, squeezes all characters:
*
* "yellow moon".squeeze #=> "yelow mon"
* " now is the".squeeze(" ") #=> " now is the"
* "putters shoot balls".squeeze("m-z") #=> "puters shot balls"
+ *
*/
static VALUE
@@ -8096,10 +8571,12 @@ rb_str_squeeze(int argc, VALUE *argv, VALUE str)
/*
* call-seq:
- * str.tr_s!(from_str, to_str) -> str or nil
+ * tr_s!(selector, replacements) -> self or nil
*
- * Performs String#tr_s processing on <i>str</i> in place,
- * returning <i>str</i>, or <code>nil</code> if no changes were made.
+ * Like String#tr_s, but modifies +self+ in place.
+ * Returns +self+ if any changes were made, +nil+ otherwise.
+ *
+ * Related: String#squeeze!.
*/
static VALUE
@@ -8111,15 +8588,17 @@ rb_str_tr_s_bang(VALUE str, VALUE src, VALUE repl)
/*
* call-seq:
- * str.tr_s(from_str, to_str) -> new_str
+ * tr_s(selector, replacements) -> string
+ *
+ * Like String#tr, but also squeezes the modified portions of the translated string;
+ * returns a new string (translated and squeezed).
+ *
+ * 'hello'.tr_s('l', 'r') #=> "hero"
+ * 'hello'.tr_s('el', '-') #=> "h-o"
+ * 'hello'.tr_s('el', 'hx') #=> "hhxo"
*
- * Processes a copy of <i>str</i> as described under String#tr, then
- * removes duplicate characters in regions that were affected by the
- * translation.
+ * Related: String#squeeze.
*
- * "hello".tr_s('l', 'r') #=> "hero"
- * "hello".tr_s('el', '*') #=> "h*o"
- * "hello".tr_s('el', 'hx') #=> "hhxo"
*/
static VALUE
@@ -8133,15 +8612,11 @@ rb_str_tr_s(VALUE str, VALUE src, VALUE repl)
/*
* call-seq:
- * str.count([other_str]+) -> integer
+ * count(*selectors) -> integer
*
- * Each +other_str+ parameter defines a set of characters to count. The
- * intersection of these sets defines the characters to count in +str+. Any
- * +other_str+ that starts with a caret <code>^</code> is negated. The
- * sequence <code>c1-c2</code> means all characters between c1 and c2. The
- * backslash character <code>\\</code> can be used to escape <code>^</code> or
- * <code>-</code> and is otherwise ignored unless it appears at the end of a
- * sequence or the end of a +other_str+.
+ * Returns the total number of characters in +self+
+ * that are specified by the given +selectors+
+ * (see {Multiple Character Selectors}[rdoc-ref:character_selectors.rdoc@Multiple+Character+Selectors]):
*
* a = "hello world"
* a.count "lo" #=> 5
@@ -8175,30 +8650,30 @@ rb_str_count(int argc, VALUE *argv, VALUE str)
StringValue(tstr);
enc = rb_enc_check(str, tstr);
if (argc == 1) {
- const char *ptstr;
- if (RSTRING_LEN(tstr) == 1 && rb_enc_asciicompat(enc) &&
- (ptstr = RSTRING_PTR(tstr),
- ONIGENC_IS_ALLOWED_REVERSE_MATCH(enc, (const unsigned char *)ptstr, (const unsigned char *)ptstr+1)) &&
- !is_broken_string(str)) {
- int clen;
- unsigned char c = rb_enc_codepoint_len(ptstr, ptstr+1, &clen, enc);
-
- s = RSTRING_PTR(str);
- if (!s || RSTRING_LEN(str) == 0) return INT2FIX(0);
- send = RSTRING_END(str);
- while (s < send) {
- if (*(unsigned char*)s++ == c) n++;
- }
- return SIZET2NUM(n);
- }
+ const char *ptstr;
+ if (RSTRING_LEN(tstr) == 1 && rb_enc_asciicompat(enc) &&
+ (ptstr = RSTRING_PTR(tstr),
+ ONIGENC_IS_ALLOWED_REVERSE_MATCH(enc, (const unsigned char *)ptstr, (const unsigned char *)ptstr+1)) &&
+ !is_broken_string(str)) {
+ int clen;
+ unsigned char c = rb_enc_codepoint_len(ptstr, ptstr+1, &clen, enc);
+
+ s = RSTRING_PTR(str);
+ if (!s || RSTRING_LEN(str) == 0) return INT2FIX(0);
+ send = RSTRING_END(str);
+ while (s < send) {
+ if (*(unsigned char*)s++ == c) n++;
+ }
+ return SIZET2NUM(n);
+ }
}
tr_setup_table(tstr, table, TRUE, &del, &nodel, enc);
for (i=1; i<argc; i++) {
- tstr = argv[i];
- StringValue(tstr);
- enc = rb_enc_check(str, tstr);
- tr_setup_table(tstr, table, FALSE, &del, &nodel, enc);
+ tstr = argv[i];
+ StringValue(tstr);
+ enc = rb_enc_check(str, tstr);
+ tr_setup_table(tstr, table, FALSE, &del, &nodel, enc);
}
s = RSTRING_PTR(str);
@@ -8206,22 +8681,22 @@ rb_str_count(int argc, VALUE *argv, VALUE str)
send = RSTRING_END(str);
ascompat = rb_enc_asciicompat(enc);
while (s < send) {
- unsigned int c;
-
- if (ascompat && (c = *(unsigned char*)s) < 0x80) {
- if (table[c]) {
- n++;
- }
- s++;
- }
- else {
- int clen;
- c = rb_enc_codepoint_len(s, send, &clen, enc);
- if (tr_find(c, table, del, nodel)) {
- n++;
- }
- s += clen;
- }
+ unsigned int c;
+
+ if (ascompat && (c = *(unsigned char*)s) < 0x80) {
+ if (table[c]) {
+ n++;
+ }
+ s++;
+ }
+ else {
+ int clen;
+ c = rb_enc_codepoint_len(s, send, &clen, enc);
+ if (tr_find(c, table, del, nodel)) {
+ n++;
+ }
+ s += clen;
+ }
}
return SIZET2NUM(n);
@@ -8231,8 +8706,8 @@ static VALUE
rb_fs_check(VALUE val)
{
if (!NIL_P(val) && !RB_TYPE_P(val, T_STRING) && !RB_TYPE_P(val, T_REGEXP)) {
- val = rb_check_string_type(val);
- if (NIL_P(val)) return 0;
+ val = rb_check_string_type(val);
+ if (NIL_P(val)) return 0;
}
return val;
}
@@ -8262,27 +8737,27 @@ static long
split_string(VALUE result, VALUE str, long beg, long len, long empty_count)
{
if (empty_count >= 0 && len == 0) {
- return empty_count + 1;
+ return empty_count + 1;
}
if (empty_count > 0) {
- /* make different substrings */
- if (result) {
- do {
+ /* make different substrings */
+ if (result) {
+ do {
rb_ary_push(result, str_new_empty_String(str));
- } while (--empty_count > 0);
- }
- else {
- do {
+ } while (--empty_count > 0);
+ }
+ else {
+ do {
rb_yield(str_new_empty_String(str));
- } while (--empty_count > 0);
- }
+ } while (--empty_count > 0);
+ }
}
str = rb_str_subseq(str, beg, len);
if (result) {
- rb_ary_push(result, str);
+ rb_ary_push(result, str);
}
else {
- rb_yield(str);
+ rb_yield(str);
}
return empty_count;
}
@@ -8318,56 +8793,10 @@ literal_split_pattern(VALUE spat, split_type_t default_type)
/*
* call-seq:
- * str.split(pattern=nil, [limit]) -> an_array
- * str.split(pattern=nil, [limit]) {|sub| block } -> str
- *
- * Divides <i>str</i> into substrings based on a delimiter, returning an array
- * of these substrings.
- *
- * If <i>pattern</i> is a String, then its contents are used as
- * the delimiter when splitting <i>str</i>. If <i>pattern</i> is a single
- * space, <i>str</i> is split on whitespace, with leading and trailing
- * whitespace and runs of contiguous whitespace characters ignored.
- *
- * If <i>pattern</i> is a Regexp, <i>str</i> is divided where the
- * pattern matches. Whenever the pattern matches a zero-length string,
- * <i>str</i> is split into individual characters. If <i>pattern</i> contains
- * groups, the respective matches will be returned in the array as well.
- *
- * If <i>pattern</i> is <code>nil</code>, the value of <code>$;</code> is used.
- * If <code>$;</code> is <code>nil</code> (which is the default), <i>str</i> is
- * split on whitespace as if ' ' were specified.
- *
- * If the <i>limit</i> parameter is omitted, trailing null fields are
- * suppressed. If <i>limit</i> is a positive number, at most that number
- * of split substrings will be returned (captured groups will be returned
- * as well, but are not counted towards the limit).
- * If <i>limit</i> is <code>1</code>, the entire
- * string is returned as the only entry in an array. If negative, there is no
- * limit to the number of fields returned, and trailing null fields are not
- * suppressed.
- *
- * When the input +str+ is empty an empty Array is returned as the string is
- * considered to have no fields to split.
- *
- * " now's the time ".split #=> ["now's", "the", "time"]
- * " now's the time ".split(' ') #=> ["now's", "the", "time"]
- * " now's the time".split(/ /) #=> ["", "now's", "", "the", "time"]
- * "1, 2.34,56, 7".split(%r{,\s*}) #=> ["1", "2.34", "56", "7"]
- * "hello".split(//) #=> ["h", "e", "l", "l", "o"]
- * "hello".split(//, 3) #=> ["h", "e", "llo"]
- * "hi mom".split(%r{\s*}) #=> ["h", "i", "m", "o", "m"]
+ * split(field_sep = $;, limit = nil) -> array
+ * split(field_sep = $;, limit = nil) {|substring| ... } -> self
*
- * "mellow yellow".split("ello") #=> ["m", "w y", "w"]
- * "1,2,,3,4,,".split(',') #=> ["1", "2", "", "3", "4"]
- * "1,2,,3,4,,".split(',', 4) #=> ["1", "2", "", "3,4,,"]
- * "1,2,,3,4,,".split(',', -4) #=> ["1", "2", "", "3", "4", "", ""]
- *
- * "1:2:3".split(/(:)()()/, 2) #=> ["1", ":", "", "", "2:3"]
- *
- * "".split(',', -1) #=> []
- *
- * If a block is given, invoke the block with each split substring.
+ * :include: doc/string/split.rdoc
*
*/
@@ -8384,32 +8813,32 @@ rb_str_split_m(int argc, VALUE *argv, VALUE str)
result = rb_block_given_p() ? Qfalse : Qnil;
if (rb_scan_args(argc, argv, "02", &spat, &limit) == 2) {
- lim = NUM2INT(limit);
- if (lim <= 0) limit = Qnil;
- else if (lim == 1) {
- if (RSTRING_LEN(str) == 0)
+ lim = NUM2INT(limit);
+ if (lim <= 0) limit = Qnil;
+ else if (lim == 1) {
+ if (RSTRING_LEN(str) == 0)
return result ? rb_ary_new2(0) : str;
tmp = str_duplicate(rb_cString, str);
- if (!result) {
- rb_yield(tmp);
+ if (!result) {
+ rb_yield(tmp);
return str;
- }
- return rb_ary_new3(1, tmp);
- }
- i = 1;
+ }
+ return rb_ary_new3(1, tmp);
+ }
+ i = 1;
}
if (NIL_P(limit) && !lim) empty_count = 0;
enc = STR_ENC_GET(str);
split_type = SPLIT_TYPE_REGEXP;
if (!NIL_P(spat)) {
- spat = get_pat_quoted(spat, 0);
+ spat = get_pat_quoted(spat, 0);
}
else if (NIL_P(spat = rb_fs)) {
- split_type = SPLIT_TYPE_AWK;
+ split_type = SPLIT_TYPE_AWK;
}
else if (!(spat = rb_fs_check(spat))) {
- rb_raise(rb_eTypeError, "value of $; must be String or Regexp");
+ rb_raise(rb_eTypeError, "value of $; must be String or Regexp");
}
else {
rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "$; is set to non-nil value");
@@ -8427,106 +8856,108 @@ rb_str_split_m(int argc, VALUE *argv, VALUE str)
break;
case T_STRING:
- mustnot_broken(spat);
+ mustnot_broken(spat);
split_type = literal_split_pattern(spat, SPLIT_TYPE_STRING);
break;
default:
UNREACHABLE_RETURN(Qnil);
- }
+ }
}
#define SPLIT_STR(beg, len) (empty_count = split_string(result, str, beg, len, empty_count))
- if (result) result = rb_ary_new();
beg = 0;
char *ptr = RSTRING_PTR(str);
char *eptr = RSTRING_END(str);
if (split_type == SPLIT_TYPE_AWK) {
- char *bptr = ptr;
- int skip = 1;
- unsigned int c;
-
- end = beg;
- if (is_ascii_string(str)) {
- while (ptr < eptr) {
- c = (unsigned char)*ptr++;
- if (skip) {
- if (ascii_isspace(c)) {
- beg = ptr - bptr;
- }
- else {
- end = ptr - bptr;
- skip = 0;
- if (!NIL_P(limit) && lim <= i) break;
- }
- }
- else if (ascii_isspace(c)) {
- SPLIT_STR(beg, end-beg);
- skip = 1;
- beg = ptr - bptr;
- if (!NIL_P(limit)) ++i;
- }
- else {
- end = ptr - bptr;
- }
- }
- }
- else {
- while (ptr < eptr) {
- int n;
-
- c = rb_enc_codepoint_len(ptr, eptr, &n, enc);
- ptr += n;
- if (skip) {
- if (rb_isspace(c)) {
- beg = ptr - bptr;
- }
- else {
- end = ptr - bptr;
- skip = 0;
- if (!NIL_P(limit) && lim <= i) break;
- }
- }
- else if (rb_isspace(c)) {
- SPLIT_STR(beg, end-beg);
- skip = 1;
- beg = ptr - bptr;
- if (!NIL_P(limit)) ++i;
- }
- else {
- end = ptr - bptr;
- }
- }
- }
+ char *bptr = ptr;
+ int skip = 1;
+ unsigned int c;
+
+ if (result) result = rb_ary_new();
+ end = beg;
+ if (is_ascii_string(str)) {
+ while (ptr < eptr) {
+ c = (unsigned char)*ptr++;
+ if (skip) {
+ if (ascii_isspace(c)) {
+ beg = ptr - bptr;
+ }
+ else {
+ end = ptr - bptr;
+ skip = 0;
+ if (!NIL_P(limit) && lim <= i) break;
+ }
+ }
+ else if (ascii_isspace(c)) {
+ SPLIT_STR(beg, end-beg);
+ skip = 1;
+ beg = ptr - bptr;
+ if (!NIL_P(limit)) ++i;
+ }
+ else {
+ end = ptr - bptr;
+ }
+ }
+ }
+ else {
+ while (ptr < eptr) {
+ int n;
+
+ c = rb_enc_codepoint_len(ptr, eptr, &n, enc);
+ ptr += n;
+ if (skip) {
+ if (rb_isspace(c)) {
+ beg = ptr - bptr;
+ }
+ else {
+ end = ptr - bptr;
+ skip = 0;
+ if (!NIL_P(limit) && lim <= i) break;
+ }
+ }
+ else if (rb_isspace(c)) {
+ SPLIT_STR(beg, end-beg);
+ skip = 1;
+ beg = ptr - bptr;
+ if (!NIL_P(limit)) ++i;
+ }
+ else {
+ end = ptr - bptr;
+ }
+ }
+ }
}
else if (split_type == SPLIT_TYPE_STRING) {
- char *str_start = ptr;
- char *substr_start = ptr;
- char *sptr = RSTRING_PTR(spat);
- long slen = RSTRING_LEN(spat);
-
- mustnot_broken(str);
- enc = rb_enc_check(str, spat);
- while (ptr < eptr &&
- (end = rb_memsearch(sptr, slen, ptr, eptr - ptr, enc)) >= 0) {
- /* Check we are at the start of a char */
- char *t = rb_enc_right_char_head(ptr, ptr + end, eptr, enc);
- if (t != ptr + end) {
- ptr = t;
- continue;
- }
- SPLIT_STR(substr_start - str_start, (ptr+end) - substr_start);
- ptr += end + slen;
- substr_start = ptr;
- if (!NIL_P(limit) && lim <= ++i) break;
- }
- beg = ptr - str_start;
+ char *str_start = ptr;
+ char *substr_start = ptr;
+ char *sptr = RSTRING_PTR(spat);
+ long slen = RSTRING_LEN(spat);
+
+ if (result) result = rb_ary_new();
+ mustnot_broken(str);
+ enc = rb_enc_check(str, spat);
+ while (ptr < eptr &&
+ (end = rb_memsearch(sptr, slen, ptr, eptr - ptr, enc)) >= 0) {
+ /* Check we are at the start of a char */
+ char *t = rb_enc_right_char_head(ptr, ptr + end, eptr, enc);
+ if (t != ptr + end) {
+ ptr = t;
+ continue;
+ }
+ SPLIT_STR(substr_start - str_start, (ptr+end) - substr_start);
+ ptr += end + slen;
+ substr_start = ptr;
+ if (!NIL_P(limit) && lim <= ++i) break;
+ }
+ beg = ptr - str_start;
}
else if (split_type == SPLIT_TYPE_CHARS) {
char *str_start = ptr;
int n;
+ if (result) result = rb_ary_new_capa(RSTRING_LEN(str));
mustnot_broken(str);
enc = rb_enc_get(str);
while (ptr < eptr &&
@@ -8538,11 +8969,12 @@ rb_str_split_m(int argc, VALUE *argv, VALUE str)
beg = ptr - str_start;
}
else {
- long len = RSTRING_LEN(str);
- long start = beg;
- long idx;
- int last_null = 0;
- struct re_registers *regs;
+ if (result) result = rb_ary_new();
+ long len = RSTRING_LEN(str);
+ long start = beg;
+ long idx;
+ int last_null = 0;
+ struct re_registers *regs;
VALUE match = 0;
for (; rb_reg_search(spat, str, start, 0) >= 0;
@@ -8551,40 +8983,40 @@ rb_str_split_m(int argc, VALUE *argv, VALUE str)
if (!result) rb_match_busy(match);
regs = RMATCH_REGS(match);
end = BEG(0);
- if (start == end && BEG(0) == END(0)) {
- if (!ptr) {
- SPLIT_STR(0, 0);
- break;
- }
- else if (last_null == 1) {
+ if (start == end && BEG(0) == END(0)) {
+ if (!ptr) {
+ SPLIT_STR(0, 0);
+ break;
+ }
+ else if (last_null == 1) {
SPLIT_STR(beg, rb_enc_fast_mbclen(ptr+beg, eptr, enc));
- beg = start;
- }
- else {
+ beg = start;
+ }
+ else {
if (start == len)
start++;
else
start += rb_enc_fast_mbclen(ptr+start,eptr,enc);
- last_null = 1;
- continue;
- }
- }
- else {
- SPLIT_STR(beg, end-beg);
- beg = start = END(0);
- }
- last_null = 0;
-
- for (idx=1; idx < regs->num_regs; idx++) {
- if (BEG(idx) == -1) continue;
- SPLIT_STR(BEG(idx), END(idx)-BEG(idx));
- }
- if (!NIL_P(limit) && lim <= ++i) break;
- }
+ last_null = 1;
+ continue;
+ }
+ }
+ else {
+ SPLIT_STR(beg, end-beg);
+ beg = start = END(0);
+ }
+ last_null = 0;
+
+ for (idx=1; idx < regs->num_regs; idx++) {
+ if (BEG(idx) == -1) continue;
+ SPLIT_STR(BEG(idx), END(idx)-BEG(idx));
+ }
+ if (!NIL_P(limit) && lim <= ++i) break;
+ }
if (match) rb_match_unbusy(match);
}
if (RSTRING_LEN(str) > 0 && (!NIL_P(limit) || RSTRING_LEN(str) > beg || lim < 0)) {
- SPLIT_STR(beg, RSTRING_LEN(str)-beg);
+ SPLIT_STR(beg, RSTRING_LEN(str)-beg);
}
return result ? result : str;
@@ -8606,12 +9038,12 @@ static inline int
enumerator_element(VALUE ary, VALUE e)
{
if (ary) {
- rb_ary_push(ary, e);
- return 0;
+ rb_ary_push(ary, e);
+ return 0;
}
else {
- rb_yield(e);
- return 1;
+ rb_yield(e);
+ return 1;
}
}
@@ -8622,10 +9054,10 @@ chomp_newline(const char *p, const char *e, rb_encoding *enc)
{
const char *prev = rb_enc_prev_char(p, e, e, enc);
if (rb_enc_is_newline(prev, e, enc)) {
- e = prev;
- prev = rb_enc_prev_char(p, e, e, enc);
- if (prev && rb_enc_ascget(prev, e, NULL, enc) == '\r')
- e = prev;
+ e = prev;
+ prev = rb_enc_prev_char(p, e, e, enc);
+ if (prev && rb_enc_ascget(prev, e, NULL, enc) == '\r')
+ e = prev;
}
return e;
}
@@ -8635,9 +9067,9 @@ get_rs(void)
{
VALUE rs = rb_rs;
if (!NIL_P(rs) &&
- (!RB_TYPE_P(rs, T_STRING) ||
- RSTRING_LEN(rs) != 1 ||
- RSTRING_PTR(rs)[0] != '\n')) {
+ (!RB_TYPE_P(rs, T_STRING) ||
+ RSTRING_LEN(rs) != 1 ||
+ RSTRING_PTR(rs)[0] != '\n')) {
rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "$/ is set to non-default value");
}
return rs;
@@ -8655,23 +9087,23 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, VALUE ary)
int rsnewline = 0;
if (rb_scan_args(argc, argv, "01:", &rs, &opts) == 0)
- rs = rb_rs;
+ rs = rb_rs;
if (!NIL_P(opts)) {
- static ID keywords[1];
- if (!keywords[0]) {
- keywords[0] = rb_intern_const("chomp");
- }
- rb_get_kwargs(opts, keywords, 0, 1, &chomp);
- chomp = (chomp != Qundef && RTEST(chomp));
+ static ID keywords[1];
+ if (!keywords[0]) {
+ keywords[0] = rb_intern_const("chomp");
+ }
+ rb_get_kwargs(opts, keywords, 0, 1, &chomp);
+ chomp = (!UNDEF_P(chomp) && RTEST(chomp));
}
if (NIL_P(rs)) {
- if (!ENUM_ELEM(ary, str)) {
- return ary;
- }
- else {
- return orig;
- }
+ if (!ENUM_ELEM(ary, str)) {
+ return ary;
+ }
+ else {
+ return orig;
+ }
}
if (!RSTRING_LEN(str)) goto end;
@@ -8683,147 +9115,114 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, VALUE ary)
rslen = RSTRING_LEN(rs);
if (rs == rb_default_rs)
- enc = rb_enc_get(str);
+ enc = rb_enc_get(str);
else
- enc = rb_enc_check(str, rs);
+ enc = rb_enc_check(str, rs);
if (rslen == 0) {
- /* paragraph mode */
- int n;
- const char *eol = NULL;
- subend = subptr;
- while (subend < pend) {
- do {
- if (rb_enc_ascget(subend, pend, &n, enc) != '\r')
- n = 0;
- rslen = n + rb_enc_mbclen(subend + n, pend, enc);
- if (rb_enc_is_newline(subend + n, pend, enc)) {
- if (eol == subend) break;
- subend += rslen;
- if (subptr) eol = subend;
- }
- else {
- if (!subptr) subptr = subend;
- subend += rslen;
- }
- rslen = 0;
- } while (subend < pend);
- if (!subptr) break;
- line = rb_str_subseq(str, subptr - ptr,
- subend - subptr + (chomp ? 0 : rslen));
- if (ENUM_ELEM(ary, line)) {
- str_mod_check(str, ptr, len);
- }
- subptr = eol = NULL;
- }
- goto end;
+ /* paragraph mode */
+ int n;
+ const char *eol = NULL;
+ subend = subptr;
+ while (subend < pend) {
+ long chomp_rslen = 0;
+ do {
+ if (rb_enc_ascget(subend, pend, &n, enc) != '\r')
+ n = 0;
+ rslen = n + rb_enc_mbclen(subend + n, pend, enc);
+ if (rb_enc_is_newline(subend + n, pend, enc)) {
+ if (eol == subend) break;
+ subend += rslen;
+ if (subptr) {
+ eol = subend;
+ chomp_rslen = -rslen;
+ }
+ }
+ else {
+ if (!subptr) subptr = subend;
+ subend += rslen;
+ }
+ rslen = 0;
+ } while (subend < pend);
+ if (!subptr) break;
+ if (rslen == 0) chomp_rslen = 0;
+ line = rb_str_subseq(str, subptr - ptr,
+ subend - subptr + (chomp ? chomp_rslen : rslen));
+ if (ENUM_ELEM(ary, line)) {
+ str_mod_check(str, ptr, len);
+ }
+ subptr = eol = NULL;
+ }
+ goto end;
}
else {
- rsptr = RSTRING_PTR(rs);
- if (RSTRING_LEN(rs) == rb_enc_mbminlen(enc) &&
- rb_enc_is_newline(rsptr, rsptr + RSTRING_LEN(rs), enc)) {
- rsnewline = 1;
- }
+ rsptr = RSTRING_PTR(rs);
+ if (RSTRING_LEN(rs) == rb_enc_mbminlen(enc) &&
+ rb_enc_is_newline(rsptr, rsptr + RSTRING_LEN(rs), enc)) {
+ rsnewline = 1;
+ }
}
if ((rs == rb_default_rs) && !rb_enc_asciicompat(enc)) {
- rs = rb_str_new(rsptr, rslen);
- rs = rb_str_encode(rs, rb_enc_from_encoding(enc), 0, Qnil);
- rsptr = RSTRING_PTR(rs);
- rslen = RSTRING_LEN(rs);
+ rs = rb_str_new(rsptr, rslen);
+ rs = rb_str_encode(rs, rb_enc_from_encoding(enc), 0, Qnil);
+ rsptr = RSTRING_PTR(rs);
+ rslen = RSTRING_LEN(rs);
}
while (subptr < pend) {
- pos = rb_memsearch(rsptr, rslen, subptr, pend - subptr, enc);
- if (pos < 0) break;
- hit = subptr + pos;
- adjusted = rb_enc_right_char_head(subptr, hit, pend, enc);
- if (hit != adjusted) {
- subptr = adjusted;
- continue;
- }
- subend = hit += rslen;
- if (chomp) {
- if (rsnewline) {
- subend = chomp_newline(subptr, subend, enc);
- }
- else {
- subend -= rslen;
- }
- }
- line = rb_str_subseq(str, subptr - ptr, subend - subptr);
- if (ENUM_ELEM(ary, line)) {
- str_mod_check(str, ptr, len);
- }
- subptr = hit;
+ pos = rb_memsearch(rsptr, rslen, subptr, pend - subptr, enc);
+ if (pos < 0) break;
+ hit = subptr + pos;
+ adjusted = rb_enc_right_char_head(subptr, hit, pend, enc);
+ if (hit != adjusted) {
+ subptr = adjusted;
+ continue;
+ }
+ subend = hit += rslen;
+ if (chomp) {
+ if (rsnewline) {
+ subend = chomp_newline(subptr, subend, enc);
+ }
+ else {
+ subend -= rslen;
+ }
+ }
+ line = rb_str_subseq(str, subptr - ptr, subend - subptr);
+ if (ENUM_ELEM(ary, line)) {
+ str_mod_check(str, ptr, len);
+ }
+ subptr = hit;
}
if (subptr != pend) {
- if (chomp) {
- if (rsnewline) {
- pend = chomp_newline(subptr, pend, enc);
- }
- else if (pend - subptr >= rslen &&
- memcmp(pend - rslen, rsptr, rslen) == 0) {
- pend -= rslen;
- }
- }
- line = rb_str_subseq(str, subptr - ptr, pend - subptr);
- ENUM_ELEM(ary, line);
- RB_GC_GUARD(str);
+ if (chomp) {
+ if (rsnewline) {
+ pend = chomp_newline(subptr, pend, enc);
+ }
+ else if (pend - subptr >= rslen &&
+ memcmp(pend - rslen, rsptr, rslen) == 0) {
+ pend -= rslen;
+ }
+ }
+ line = rb_str_subseq(str, subptr - ptr, pend - subptr);
+ ENUM_ELEM(ary, line);
+ RB_GC_GUARD(str);
}
end:
if (ary)
- return ary;
+ return ary;
else
- return orig;
+ return orig;
}
/*
* call-seq:
- * str.each_line(separator=$/, chomp: false) {|substr| block } -> str
- * str.each_line(separator=$/, chomp: false) -> an_enumerator
- *
- * Splits <i>str</i> using the supplied parameter as the record
- * separator (<code>$/</code> by default), passing each substring in
- * turn to the supplied block. If a zero-length record separator is
- * supplied, the string is split into paragraphs delimited by
- * multiple successive newlines.
- *
- * If +chomp+ is +true+, +separator+ will be removed from the end of each
- * line.
- *
- * If no block is given, an enumerator is returned instead.
- *
- * "hello\nworld".each_line {|s| p s}
- * # prints:
- * # "hello\n"
- * # "world"
+ * each_line(line_sep = $/, chomp: false) {|substring| ... } -> self
+ * each_line(line_sep = $/, chomp: false) -> enumerator
*
- * "hello\nworld".each_line('l') {|s| p s}
- * # prints:
- * # "hel"
- * # "l"
- * # "o\nworl"
- * # "d"
- *
- * "hello\n\n\nworld".each_line('') {|s| p s}
- * # prints
- * # "hello\n\n"
- * # "world"
- *
- * "hello\nworld".each_line(chomp: true) {|s| p s}
- * # prints:
- * # "hello"
- * # "world"
- *
- * "hello\nworld".each_line('l', chomp: true) {|s| p s}
- * # prints:
- * # "he"
- * # ""
- * # "o\nwor"
- * # "d"
+ * :include: doc/string/each_line.rdoc
*
*/
@@ -8836,21 +9235,11 @@ rb_str_each_line(int argc, VALUE *argv, VALUE str)
/*
* call-seq:
- * str.lines(separator=$/, chomp: false) -> an_array
- *
- * Returns an array of lines in <i>str</i> split using the supplied
- * record separator (<code>$/</code> by default). This is a
- * shorthand for <code>str.each_line(separator, getline_args).to_a</code>.
+ * lines(Line_sep = $/, chomp: false) -> array_of_strings
*
- * If +chomp+ is +true+, +separator+ will be removed from the end of each
- * line.
+ * Forms substrings ("lines") of +self+ according to the given arguments
+ * (see String#each_line for details); returns the lines in an array.
*
- * "hello\nworld\n".lines #=> ["hello\n", "world\n"]
- * "hello world".lines(' ') #=> ["hello ", " ", "world"]
- * "hello\nworld\n".lines(chomp: true) #=> ["hello", "world"]
- *
- * If a block is given, which is a deprecated form, works the same as
- * <code>each_line</code>.
*/
static VALUE
@@ -8872,27 +9261,21 @@ rb_str_enumerate_bytes(VALUE str, VALUE ary)
long i;
for (i=0; i<RSTRING_LEN(str); i++) {
- ENUM_ELEM(ary, INT2FIX((unsigned char)RSTRING_PTR(str)[i]));
+ ENUM_ELEM(ary, INT2FIX((unsigned char)RSTRING_PTR(str)[i]));
}
if (ary)
- return ary;
+ return ary;
else
- return str;
+ return str;
}
/*
* call-seq:
- * str.each_byte {|integer| block } -> str
- * str.each_byte -> an_enumerator
- *
- * Passes each byte in <i>str</i> to the given block, or returns an
- * enumerator if no block is given.
+ * each_byte {|byte| ... } -> self
+ * each_byte -> enumerator
*
- * "hello".each_byte {|c| print c, ' ' }
+ * :include: doc/string/each_byte.rdoc
*
- * <em>produces:</em>
- *
- * 104 101 108 108 111
*/
static VALUE
@@ -8904,13 +9287,10 @@ rb_str_each_byte(VALUE str)
/*
* call-seq:
- * str.bytes -> an_array
+ * bytes -> array_of_bytes
*
- * Returns an array of bytes in <i>str</i>. This is a shorthand for
- * <code>str.each_byte.to_a</code>.
+ * :include: doc/string/bytes.rdoc
*
- * If a block is given, which is a deprecated form, works the same as
- * <code>each_byte</code>.
*/
static VALUE
@@ -8940,37 +9320,31 @@ rb_str_enumerate_chars(VALUE str, VALUE ary)
enc = rb_enc_get(str);
if (ENC_CODERANGE_CLEAN_P(ENC_CODERANGE(str))) {
- for (i = 0; i < len; i += n) {
- n = rb_enc_fast_mbclen(ptr + i, ptr + len, enc);
- ENUM_ELEM(ary, rb_str_subseq(str, i, n));
- }
+ for (i = 0; i < len; i += n) {
+ n = rb_enc_fast_mbclen(ptr + i, ptr + len, enc);
+ ENUM_ELEM(ary, rb_str_subseq(str, i, n));
+ }
}
else {
- for (i = 0; i < len; i += n) {
- n = rb_enc_mbclen(ptr + i, ptr + len, enc);
- ENUM_ELEM(ary, rb_str_subseq(str, i, n));
- }
+ for (i = 0; i < len; i += n) {
+ n = rb_enc_mbclen(ptr + i, ptr + len, enc);
+ ENUM_ELEM(ary, rb_str_subseq(str, i, n));
+ }
}
RB_GC_GUARD(str);
if (ary)
- return ary;
+ return ary;
else
- return orig;
+ return orig;
}
/*
* call-seq:
- * str.each_char {|cstr| block } -> str
- * str.each_char -> an_enumerator
+ * each_char {|c| ... } -> self
+ * each_char -> enumerator
*
- * Passes each character in <i>str</i> to the given block, or returns
- * an enumerator if no block is given.
+ * :include: doc/string/each_char.rdoc
*
- * "hello".each_char {|c| print c, ' ' }
- *
- * <em>produces:</em>
- *
- * h e l l o
*/
static VALUE
@@ -8982,13 +9356,10 @@ rb_str_each_char(VALUE str)
/*
* call-seq:
- * str.chars -> an_array
+ * chars -> array_of_characters
*
- * Returns an array of characters in <i>str</i>. This is a shorthand
- * for <code>str.each_char.to_a</code>.
+ * :include: doc/string/chars.rdoc
*
- * If a block is given, which is a deprecated form, works the same as
- * <code>each_char</code>.
*/
static VALUE
@@ -9008,7 +9379,7 @@ rb_str_enumerate_codepoints(VALUE str, VALUE ary)
rb_encoding *enc;
if (single_byte_optimizable(str))
- return rb_str_enumerate_bytes(str, ary);
+ return rb_str_enumerate_bytes(str, ary);
str = rb_str_new_frozen(str);
ptr = RSTRING_PTR(str);
@@ -9016,35 +9387,24 @@ rb_str_enumerate_codepoints(VALUE str, VALUE ary)
enc = STR_ENC_GET(str);
while (ptr < end) {
- c = rb_enc_codepoint_len(ptr, end, &n, enc);
- ENUM_ELEM(ary, UINT2NUM(c));
- ptr += n;
+ c = rb_enc_codepoint_len(ptr, end, &n, enc);
+ ENUM_ELEM(ary, UINT2NUM(c));
+ ptr += n;
}
RB_GC_GUARD(str);
if (ary)
- return ary;
+ return ary;
else
- return orig;
+ return orig;
}
/*
* call-seq:
- * str.each_codepoint {|integer| block } -> str
- * str.each_codepoint -> an_enumerator
- *
- * Passes the Integer ordinal of each character in <i>str</i>,
- * also known as a <i>codepoint</i> when applied to Unicode strings to the
- * given block. For encodings other than UTF-8/UTF-16(BE|LE)/UTF-32(BE|LE),
- * values are directly derived from the binary representation
- * of each character.
+ * each_codepoint {|integer| ... } -> self
+ * each_codepoint -> enumerator
*
- * If no block is given, an enumerator is returned instead.
+ * :include: doc/string/each_codepoint.rdoc
*
- * "hello\u0639".each_codepoint {|c| print c, ' ' }
- *
- * <em>produces:</em>
- *
- * 104 101 108 108 111 1593
*/
static VALUE
@@ -9056,14 +9416,10 @@ rb_str_each_codepoint(VALUE str)
/*
* call-seq:
- * str.codepoints -> an_array
+ * codepoints -> array_of_integers
*
- * Returns an array of the Integer ordinals of the
- * characters in <i>str</i>. This is a shorthand for
- * <code>str.each_codepoint.to_a</code>.
+ * :include: doc/string/codepoints.rdoc
*
- * If a block is given, which is a deprecated form, works the same as
- * <code>each_codepoint</code>.
*/
static VALUE
@@ -9077,74 +9433,93 @@ static regex_t *
get_reg_grapheme_cluster(rb_encoding *enc)
{
int encidx = rb_enc_to_index(enc);
- regex_t *reg_grapheme_cluster = NULL;
- static regex_t *reg_grapheme_cluster_utf8 = NULL;
- /* synchronize */
- if (encidx == rb_utf8_encindex() && reg_grapheme_cluster_utf8) {
- reg_grapheme_cluster = reg_grapheme_cluster_utf8;
- }
- if (!reg_grapheme_cluster) {
- const OnigUChar source_ascii[] = "\\X";
- OnigErrorInfo einfo;
- const OnigUChar *source = source_ascii;
- size_t source_len = sizeof(source_ascii) - 1;
- switch (encidx) {
+ const OnigUChar source_ascii[] = "\\X";
+ const OnigUChar *source = source_ascii;
+ size_t source_len = sizeof(source_ascii) - 1;
+
+ switch (encidx) {
#define CHARS_16BE(x) (OnigUChar)((x)>>8), (OnigUChar)(x)
#define CHARS_16LE(x) (OnigUChar)(x), (OnigUChar)((x)>>8)
#define CHARS_32BE(x) CHARS_16BE((x)>>16), CHARS_16BE(x)
#define CHARS_32LE(x) CHARS_16LE(x), CHARS_16LE((x)>>16)
#define CASE_UTF(e) \
- case ENCINDEX_UTF_##e: { \
- static const OnigUChar source_UTF_##e[] = {CHARS_##e('\\'), CHARS_##e('X')}; \
- source = source_UTF_##e; \
- source_len = sizeof(source_UTF_##e); \
- break; \
- }
- CASE_UTF(16BE); CASE_UTF(16LE); CASE_UTF(32BE); CASE_UTF(32LE);
+ case ENCINDEX_UTF_##e: { \
+ static const OnigUChar source_UTF_##e[] = {CHARS_##e('\\'), CHARS_##e('X')}; \
+ source = source_UTF_##e; \
+ source_len = sizeof(source_UTF_##e); \
+ break; \
+ }
+ CASE_UTF(16BE); CASE_UTF(16LE); CASE_UTF(32BE); CASE_UTF(32LE);
#undef CASE_UTF
#undef CHARS_16BE
#undef CHARS_16LE
#undef CHARS_32BE
#undef CHARS_32LE
- }
- int r = onig_new(&reg_grapheme_cluster, source, source + source_len,
- ONIG_OPTION_DEFAULT, enc, OnigDefaultSyntax, &einfo);
- if (r) {
- UChar message[ONIG_MAX_ERROR_MESSAGE_LEN];
- onig_error_code_to_str(message, r, &einfo);
- rb_fatal("cannot compile grapheme cluster regexp: %s", (char *)message);
- }
- if (encidx == rb_utf8_encindex()) {
- reg_grapheme_cluster_utf8 = reg_grapheme_cluster;
- }
}
+
+ regex_t *reg_grapheme_cluster;
+ OnigErrorInfo einfo;
+ int r = onig_new(&reg_grapheme_cluster, source, source + source_len,
+ ONIG_OPTION_DEFAULT, enc, OnigDefaultSyntax, &einfo);
+ if (r) {
+ UChar message[ONIG_MAX_ERROR_MESSAGE_LEN];
+ onig_error_code_to_str(message, r, &einfo);
+ rb_fatal("cannot compile grapheme cluster regexp: %s", (char *)message);
+ }
+
return reg_grapheme_cluster;
}
+static regex_t *
+get_cached_reg_grapheme_cluster(rb_encoding *enc)
+{
+ int encidx = rb_enc_to_index(enc);
+ static regex_t *reg_grapheme_cluster_utf8 = NULL;
+
+ if (encidx == rb_utf8_encindex()) {
+ if (!reg_grapheme_cluster_utf8) {
+ reg_grapheme_cluster_utf8 = get_reg_grapheme_cluster(enc);
+ }
+
+ return reg_grapheme_cluster_utf8;
+ }
+
+ return NULL;
+}
+
static VALUE
rb_str_each_grapheme_cluster_size(VALUE str, VALUE args, VALUE eobj)
{
size_t grapheme_cluster_count = 0;
- regex_t *reg_grapheme_cluster = NULL;
- rb_encoding *enc = rb_enc_from_index(ENCODING_GET(str));
+ rb_encoding *enc = get_encoding(str);
const char *ptr, *end;
if (!rb_enc_unicode_p(enc)) {
- return rb_str_length(str);
+ return rb_str_length(str);
+ }
+
+ bool cached_reg_grapheme_cluster = true;
+ regex_t *reg_grapheme_cluster = get_cached_reg_grapheme_cluster(enc);
+ if (!reg_grapheme_cluster) {
+ reg_grapheme_cluster = get_reg_grapheme_cluster(enc);
+ cached_reg_grapheme_cluster = false;
}
- reg_grapheme_cluster = get_reg_grapheme_cluster(enc);
ptr = RSTRING_PTR(str);
end = RSTRING_END(str);
while (ptr < end) {
- OnigPosition len = onig_match(reg_grapheme_cluster,
- (const OnigUChar *)ptr, (const OnigUChar *)end,
- (const OnigUChar *)ptr, NULL, 0);
- if (len <= 0) break;
- grapheme_cluster_count++;
- ptr += len;
+ OnigPosition len = onig_match(reg_grapheme_cluster,
+ (const OnigUChar *)ptr, (const OnigUChar *)end,
+ (const OnigUChar *)ptr, NULL, 0);
+ if (len <= 0) break;
+ grapheme_cluster_count++;
+ ptr += len;
+ }
+
+ if (!cached_reg_grapheme_cluster) {
+ onig_free(reg_grapheme_cluster);
}
return SIZET2NUM(grapheme_cluster_count);
@@ -9154,46 +9529,51 @@ static VALUE
rb_str_enumerate_grapheme_clusters(VALUE str, VALUE ary)
{
VALUE orig = str;
- regex_t *reg_grapheme_cluster = NULL;
- rb_encoding *enc = rb_enc_from_index(ENCODING_GET(str));
+ rb_encoding *enc = get_encoding(str);
const char *ptr0, *ptr, *end;
if (!rb_enc_unicode_p(enc)) {
- return rb_str_enumerate_chars(str, ary);
+ return rb_str_enumerate_chars(str, ary);
}
if (!ary) str = rb_str_new_frozen(str);
- reg_grapheme_cluster = get_reg_grapheme_cluster(enc);
+
+ bool cached_reg_grapheme_cluster = true;
+ regex_t *reg_grapheme_cluster = get_cached_reg_grapheme_cluster(enc);
+ if (!reg_grapheme_cluster) {
+ reg_grapheme_cluster = get_reg_grapheme_cluster(enc);
+ cached_reg_grapheme_cluster = false;
+ }
+
ptr0 = ptr = RSTRING_PTR(str);
end = RSTRING_END(str);
while (ptr < end) {
- OnigPosition len = onig_match(reg_grapheme_cluster,
- (const OnigUChar *)ptr, (const OnigUChar *)end,
- (const OnigUChar *)ptr, NULL, 0);
- if (len <= 0) break;
+ OnigPosition len = onig_match(reg_grapheme_cluster,
+ (const OnigUChar *)ptr, (const OnigUChar *)end,
+ (const OnigUChar *)ptr, NULL, 0);
+ if (len <= 0) break;
ENUM_ELEM(ary, rb_str_subseq(str, ptr-ptr0, len));
- ptr += len;
+ ptr += len;
}
+
+ if (!cached_reg_grapheme_cluster) {
+ onig_free(reg_grapheme_cluster);
+ }
+
RB_GC_GUARD(str);
if (ary)
- return ary;
+ return ary;
else
- return orig;
+ return orig;
}
/*
* call-seq:
- * str.each_grapheme_cluster {|cstr| block } -> str
- * str.each_grapheme_cluster -> an_enumerator
+ * each_grapheme_cluster {|gc| ... } -> self
+ * each_grapheme_cluster -> enumerator
*
- * Passes each grapheme cluster in <i>str</i> to the given block, or returns
- * an enumerator if no block is given.
- * Unlike String#each_char, this enumerates by grapheme clusters defined by
- * Unicode Standard Annex #29 http://unicode.org/reports/tr29/
- *
- * "a\u0300".each_char.to_a.size #=> 2
- * "a\u0300".each_grapheme_cluster.to_a.size #=> 1
+ * :include: doc/string/each_grapheme_cluster.rdoc
*
*/
@@ -9206,13 +9586,10 @@ rb_str_each_grapheme_cluster(VALUE str)
/*
* call-seq:
- * str.grapheme_clusters -> an_array
+ * grapheme_clusters -> array_of_grapheme_clusters
*
- * Returns an array of grapheme clusters in <i>str</i>. This is a shorthand
- * for <code>str.each_grapheme_cluster.to_a</code>.
+ * :include: doc/string/grapheme_clusters.rdoc
*
- * If a block is given, which is a deprecated form, works the same as
- * <code>each_grapheme_cluster</code>.
*/
static VALUE
@@ -9234,19 +9611,20 @@ chopped_length(VALUE str)
p = rb_enc_prev_char(beg, end, end, enc);
if (!p) return 0;
if (p > beg && rb_enc_ascget(p, end, 0, enc) == '\n') {
- p2 = rb_enc_prev_char(beg, p, end, enc);
- if (p2 && rb_enc_ascget(p2, end, 0, enc) == '\r') p = p2;
+ p2 = rb_enc_prev_char(beg, p, end, enc);
+ if (p2 && rb_enc_ascget(p2, end, 0, enc) == '\r') p = p2;
}
return p - beg;
}
/*
* call-seq:
- * str.chop! -> str or nil
+ * chop! -> self or nil
+ *
+ * Like String#chop, but modifies +self+ in place;
+ * returns +nil+ if +self+ is empty, +self+ otherwise.
*
- * Processes <i>str</i> as for String#chop, returning <i>str</i>, or
- * <code>nil</code> if <i>str</i> is the empty string. See also
- * String#chomp!.
+ * Related: String#chomp!.
*/
static VALUE
@@ -9254,14 +9632,14 @@ rb_str_chop_bang(VALUE str)
{
str_modify_keep_cr(str);
if (RSTRING_LEN(str) > 0) {
- long len;
- len = chopped_length(str);
- STR_SET_LEN(str, len);
- TERM_FILL(&RSTRING_PTR(str)[len], TERM_LEN(str));
- if (ENC_CODERANGE(str) != ENC_CODERANGE_7BIT) {
- ENC_CODERANGE_CLEAR(str);
- }
- return str;
+ long len;
+ len = chopped_length(str);
+ STR_SET_LEN(str, len);
+ TERM_FILL(&RSTRING_PTR(str)[len], TERM_LEN(str));
+ if (ENC_CODERANGE(str) != ENC_CODERANGE_7BIT) {
+ ENC_CODERANGE_CLEAR(str);
+ }
+ return str;
}
return Qnil;
}
@@ -9269,20 +9647,10 @@ rb_str_chop_bang(VALUE str)
/*
* call-seq:
- * str.chop -> new_str
+ * chop -> new_string
*
- * Returns a new String with the last character removed. If the
- * string ends with <code>\r\n</code>, both characters are
- * removed. Applying <code>chop</code> to an empty string returns an
- * empty string. String#chomp is often a safer alternative, as it
- * leaves the string unchanged if it doesn't end in a record
- * separator.
+ * :include: doc/string/chop.rdoc
*
- * "string\r\n".chop #=> "string"
- * "string\n\r".chop #=> "string\n"
- * "string\n".chop #=> "string"
- * "string".chop #=> "strin"
- * "x".chop.chop #=> ""
*/
static VALUE
@@ -9342,55 +9710,55 @@ chompped_length(VALUE str, VALUE rs)
enc = rb_enc_get(str);
RSTRING_GETMEM(rs, rsptr, rslen);
if (rslen == 0) {
- if (rb_enc_mbminlen(enc) > 1) {
- while (e > p) {
- pp = rb_enc_left_char_head(p, e-rb_enc_mbminlen(enc), e, enc);
- if (!rb_enc_is_newline(pp, e, enc)) break;
- e = pp;
- pp -= rb_enc_mbminlen(enc);
- if (pp >= p) {
- pp = rb_enc_left_char_head(p, pp, e, enc);
- if (rb_enc_ascget(pp, e, 0, enc) == '\r') {
- e = pp;
- }
- }
- }
- }
- else {
- while (e > p && *(e-1) == '\n') {
- --e;
- if (e > p && *(e-1) == '\r')
- --e;
- }
- }
- return e - p;
+ if (rb_enc_mbminlen(enc) > 1) {
+ while (e > p) {
+ pp = rb_enc_left_char_head(p, e-rb_enc_mbminlen(enc), e, enc);
+ if (!rb_enc_is_newline(pp, e, enc)) break;
+ e = pp;
+ pp -= rb_enc_mbminlen(enc);
+ if (pp >= p) {
+ pp = rb_enc_left_char_head(p, pp, e, enc);
+ if (rb_enc_ascget(pp, e, 0, enc) == '\r') {
+ e = pp;
+ }
+ }
+ }
+ }
+ else {
+ while (e > p && *(e-1) == '\n') {
+ --e;
+ if (e > p && *(e-1) == '\r')
+ --e;
+ }
+ }
+ return e - p;
}
if (rslen > len) return len;
enc = rb_enc_get(rs);
newline = rsptr[rslen-1];
if (rslen == rb_enc_mbminlen(enc)) {
- if (rslen == 1) {
- if (newline == '\n')
+ if (rslen == 1) {
+ if (newline == '\n')
return smart_chomp(str, e, p);
- }
- else {
- if (rb_enc_is_newline(rsptr, rsptr+rslen, enc))
+ }
+ else {
+ if (rb_enc_is_newline(rsptr, rsptr+rslen, enc))
return smart_chomp(str, e, p);
- }
+ }
}
enc = rb_enc_check(str, rs);
if (is_broken_string(rs)) {
- return len;
+ return len;
}
pp = e - rslen;
if (p[len-1] == newline &&
- (rslen <= 1 ||
- memcmp(rsptr, pp, rslen) == 0)) {
- if (rb_enc_left_char_head(p, pp, e, enc) == pp)
- return len - rslen;
- RB_GC_GUARD(rs);
+ (rslen <= 1 ||
+ memcmp(rsptr, pp, rslen) == 0)) {
+ if (at_char_boundary(p, pp, e, enc))
+ return len - rslen;
+ RB_GC_GUARD(rs);
}
return len;
}
@@ -9398,19 +9766,19 @@ chompped_length(VALUE str, VALUE rs)
/*!
* Returns the separator for arguments of rb_str_chomp.
*
- * @return returns rb_ps ($/) as default, the default value of rb_ps ($/) is "\n".
+ * @return returns rb_rs ($/) as default, the default value of rb_rs ($/) is "\n".
*/
static VALUE
chomp_rs(int argc, const VALUE *argv)
{
rb_check_arity(argc, 0, 1);
if (argc > 0) {
- VALUE rs = argv[0];
- if (!NIL_P(rs)) StringValue(rs);
- return rs;
+ VALUE rs = argv[0];
+ if (!NIL_P(rs)) StringValue(rs);
+ return rs;
}
else {
- return rb_rs;
+ return rb_rs;
}
}
@@ -9424,18 +9792,18 @@ rb_str_chomp_string(VALUE str, VALUE rs)
STR_SET_LEN(str, len);
TERM_FILL(&RSTRING_PTR(str)[len], TERM_LEN(str));
if (ENC_CODERANGE(str) != ENC_CODERANGE_7BIT) {
- ENC_CODERANGE_CLEAR(str);
+ ENC_CODERANGE_CLEAR(str);
}
return str;
}
/*
* call-seq:
- * str.chomp!(separator=$/) -> str or nil
+ * chomp!(line_sep = $/) -> self or nil
+ *
+ * Like String#chomp, but modifies +self+ in place;
+ * returns +nil+ if no modification made, +self+ otherwise.
*
- * Modifies <i>str</i> in place as described for String#chomp,
- * returning <i>str</i>, or <code>nil</code> if no modifications were
- * made.
*/
static VALUE
@@ -9443,7 +9811,7 @@ rb_str_chomp_bang(int argc, VALUE *argv, VALUE str)
{
VALUE rs;
str_modifiable(str);
- if (RSTRING_LEN(str) == 0) return Qnil;
+ if (RSTRING_LEN(str) == 0 && argc < 2) return Qnil;
rs = chomp_rs(argc, argv);
if (NIL_P(rs)) return Qnil;
return rb_str_chomp_string(str, rs);
@@ -9452,24 +9820,10 @@ rb_str_chomp_bang(int argc, VALUE *argv, VALUE str)
/*
* call-seq:
- * str.chomp(separator=$/) -> new_str
+ * chomp(line_sep = $/) -> new_string
*
- * Returns a new String with the given record separator removed
- * from the end of <i>str</i> (if present). If <code>$/</code> has not been
- * changed from the default Ruby record separator, then <code>chomp</code> also
- * removes carriage return characters (that is, it will remove <code>\n</code>,
- * <code>\r</code>, and <code>\r\n</code>). If <code>$/</code> is an empty string,
- * it will remove all trailing newlines from the string.
+ * :include: doc/string/chomp.rdoc
*
- * "hello".chomp #=> "hello"
- * "hello\n".chomp #=> "hello"
- * "hello\r\n".chomp #=> "hello"
- * "hello\n\r".chomp #=> "hello\n"
- * "hello\r".chomp #=> "hello"
- * "hello \n there".chomp #=> "hello \n there"
- * "hello".chomp("llo") #=> "he"
- * "hello\r\n\r\n".chomp('') #=> "hello"
- * "hello\r\n\r\r\n".chomp('') #=> "hello\r\n\r"
*/
static VALUE
@@ -9492,30 +9846,25 @@ lstrip_offset(VALUE str, const char *s, const char *e, rb_encoding *enc)
while (s < e && (*s == '\0' || ascii_isspace(*s))) s++;
}
else {
- while (s < e) {
- int n;
- unsigned int cc = rb_enc_codepoint_len(s, e, &n, enc);
+ while (s < e) {
+ int n;
+ unsigned int cc = rb_enc_codepoint_len(s, e, &n, enc);
if (cc && !rb_isspace(cc)) break;
- s += n;
- }
+ s += n;
+ }
}
return s - start;
}
/*
* call-seq:
- * str.lstrip! -> self or nil
- *
- * Removes leading whitespace from the receiver.
- * Returns the altered receiver, or +nil+ if no change was made.
- * See also String#rstrip! and String#strip!.
+ * lstrip! -> self or nil
*
- * Refer to String#strip for the definition of whitespace.
+ * Like String#lstrip, except that any modifications are made in +self+;
+ * returns +self+ if any modification are made, +nil+ otherwise.
*
- * " hello ".lstrip! #=> "hello "
- * "hello ".lstrip! #=> nil
- * "hello".lstrip! #=> nil
+ * Related: String#rstrip!, String#strip!.
*/
static VALUE
@@ -9530,12 +9879,12 @@ rb_str_lstrip_bang(VALUE str)
RSTRING_GETMEM(str, start, olen);
loffset = lstrip_offset(str, start, start+olen, enc);
if (loffset > 0) {
- long len = olen-loffset;
- s = start + loffset;
- memmove(start, s, len);
- STR_SET_LEN(str, len);
- TERM_FILL(start+len, rb_enc_mbminlen(enc));
- return str;
+ long len = olen-loffset;
+ s = start + loffset;
+ memmove(start, s, len);
+ STR_SET_LEN(str, len);
+ TERM_FILL(start+len, rb_enc_mbminlen(enc));
+ return str;
}
return Qnil;
}
@@ -9543,15 +9892,17 @@ rb_str_lstrip_bang(VALUE str)
/*
* call-seq:
- * str.lstrip -> new_str
+ * lstrip -> new_string
*
- * Returns a copy of the receiver with leading whitespace removed.
- * See also String#rstrip and String#strip.
+ * Returns a copy of +self+ with leading whitespace removed;
+ * see {Whitespace in Strings}[rdoc-ref:String@Whitespace+in+Strings]:
*
- * Refer to String#strip for the definition of whitespace.
+ * whitespace = "\x00\t\n\v\f\r "
+ * s = whitespace + 'abc' + whitespace
+ * s # => "\u0000\t\n\v\f\r abc\u0000\t\n\v\f\r "
+ * s.lstrip # => "abc\u0000\t\n\v\f\r "
*
- * " hello ".lstrip #=> "hello "
- * "hello".lstrip #=> "hello"
+ * Related: String#rstrip, String#strip.
*/
static VALUE
@@ -9571,39 +9922,37 @@ rstrip_offset(VALUE str, const char *s, const char *e, rb_encoding *enc)
const char *t;
rb_str_check_dummy_enc(enc);
+ if (rb_enc_str_coderange(str) == ENC_CODERANGE_BROKEN) {
+ rb_raise(rb_eEncCompatError, "invalid byte sequence in %s", rb_enc_name(enc));
+ }
if (!s || s >= e) return 0;
t = e;
/* remove trailing spaces or '\0's */
if (single_byte_optimizable(str)) {
- unsigned char c;
- while (s < t && ((c = *(t-1)) == '\0' || ascii_isspace(c))) t--;
+ unsigned char c;
+ while (s < t && ((c = *(t-1)) == '\0' || ascii_isspace(c))) t--;
}
else {
- char *tp;
+ char *tp;
while ((tp = rb_enc_prev_char(s, t, e, enc)) != NULL) {
- unsigned int c = rb_enc_codepoint(tp, e, enc);
- if (c && !rb_isspace(c)) break;
- t = tp;
- }
+ unsigned int c = rb_enc_codepoint(tp, e, enc);
+ if (c && !rb_isspace(c)) break;
+ t = tp;
+ }
}
return e - t;
}
/*
* call-seq:
- * str.rstrip! -> self or nil
- *
- * Removes trailing whitespace from the receiver.
- * Returns the altered receiver, or +nil+ if no change was made.
- * See also String#lstrip! and String#strip!.
+ * rstrip! -> self or nil
*
- * Refer to String#strip for the definition of whitespace.
+ * Like String#rstrip, except that any modifications are made in +self+;
+ * returns +self+ if any modification are made, +nil+ otherwise.
*
- * " hello ".rstrip! #=> " hello"
- * " hello".rstrip! #=> nil
- * "hello".rstrip! #=> nil
+ * Related: String#lstrip!, String#strip!.
*/
static VALUE
@@ -9618,11 +9967,11 @@ rb_str_rstrip_bang(VALUE str)
RSTRING_GETMEM(str, start, olen);
roffset = rstrip_offset(str, start, start+olen, enc);
if (roffset > 0) {
- long len = olen - roffset;
+ long len = olen - roffset;
- STR_SET_LEN(str, len);
- TERM_FILL(start+len, rb_enc_mbminlen(enc));
- return str;
+ STR_SET_LEN(str, len);
+ TERM_FILL(start+len, rb_enc_mbminlen(enc));
+ return str;
}
return Qnil;
}
@@ -9630,15 +9979,17 @@ rb_str_rstrip_bang(VALUE str)
/*
* call-seq:
- * str.rstrip -> new_str
+ * rstrip -> new_string
*
- * Returns a copy of the receiver with trailing whitespace removed.
- * See also String#lstrip and String#strip.
+ * Returns a copy of the receiver with trailing whitespace removed;
+ * see {Whitespace in Strings}[rdoc-ref:String@Whitespace+in+Strings]:
*
- * Refer to String#strip for the definition of whitespace.
+ * whitespace = "\x00\t\n\v\f\r "
+ * s = whitespace + 'abc' + whitespace
+ * s # => "\u0000\t\n\v\f\r abc\u0000\t\n\v\f\r "
+ * s.rstrip # => "\u0000\t\n\v\f\r abc"
*
- * " hello ".rstrip #=> " hello"
- * "hello".rstrip #=> "hello"
+ * Related: String#lstrip, String#strip.
*/
static VALUE
@@ -9659,15 +10010,12 @@ rb_str_rstrip(VALUE str)
/*
* call-seq:
- * str.strip! -> self or nil
+ * strip! -> self or nil
*
- * Removes leading and trailing whitespace from the receiver.
- * Returns the altered receiver, or +nil+ if there was no change.
+ * Like String#strip, except that any modifications are made in +self+;
+ * returns +self+ if any modification are made, +nil+ otherwise.
*
- * Refer to String#strip for the definition of whitespace.
- *
- * " hello ".strip! #=> "hello"
- * "hello".strip! #=> nil
+ * Related: String#lstrip!, String#strip!.
*/
static VALUE
@@ -9684,14 +10032,14 @@ rb_str_strip_bang(VALUE str)
roffset = rstrip_offset(str, start+loffset, start+olen, enc);
if (loffset > 0 || roffset > 0) {
- long len = olen-roffset;
- if (loffset > 0) {
- len -= loffset;
- memmove(start, start + loffset, len);
- }
- STR_SET_LEN(str, len);
- TERM_FILL(start+len, rb_enc_mbminlen(enc));
- return str;
+ long len = olen-roffset;
+ if (loffset > 0) {
+ len -= loffset;
+ memmove(start, start + loffset, len);
+ }
+ STR_SET_LEN(str, len);
+ TERM_FILL(start+len, rb_enc_mbminlen(enc));
+ return str;
}
return Qnil;
}
@@ -9699,17 +10047,17 @@ rb_str_strip_bang(VALUE str)
/*
* call-seq:
- * str.strip -> new_str
+ * strip -> new_string
*
- * Returns a copy of the receiver with leading and trailing whitespace removed.
+ * Returns a copy of the receiver with leading and trailing whitespace removed;
+ * see {Whitespace in Strings}[rdoc-ref:String@Whitespace+in+Strings]:
*
- * Whitespace is defined as any of the following characters:
- * null, horizontal tab, line feed, vertical tab, form feed, carriage return, space.
+ * whitespace = "\x00\t\n\v\f\r "
+ * s = whitespace + 'abc' + whitespace
+ * s # => "\u0000\t\n\v\f\r abc\u0000\t\n\v\f\r "
+ * s.strip # => "abc"
*
- * " hello ".strip #=> "hello"
- * "\tgoodbye\r\n".strip #=> "goodbye"
- * "\x00\t\n\v\f\r ".strip #=> ""
- * "hello".strip #=> "hello"
+ * Related: String#lstrip, String#rstrip.
*/
static VALUE
@@ -9730,83 +10078,97 @@ rb_str_strip(VALUE str)
static VALUE
scan_once(VALUE str, VALUE pat, long *start, int set_backref_str)
{
- VALUE result, match;
- struct re_registers *regs;
- int i;
+ VALUE result = Qnil;
long end, pos = rb_pat_search(pat, str, *start, set_backref_str);
if (pos >= 0) {
- if (BUILTIN_TYPE(pat) == T_STRING) {
- regs = NULL;
- end = pos + RSTRING_LEN(pat);
- }
- else {
- match = rb_backref_get();
- regs = RMATCH_REGS(match);
- pos = BEG(0);
- end = END(0);
- }
- if (pos == end) {
- rb_encoding *enc = STR_ENC_GET(str);
- /*
- * Always consume at least one character of the input string
- */
- if (RSTRING_LEN(str) > end)
- *start = end + rb_enc_fast_mbclen(RSTRING_PTR(str) + end,
- RSTRING_END(str), enc);
- else
- *start = end + 1;
- }
- else {
- *start = end;
- }
- if (!regs || regs->num_regs == 1) {
- result = rb_str_subseq(str, pos, end - pos);
- return result;
- }
- result = rb_ary_new2(regs->num_regs);
- for (i=1; i < regs->num_regs; i++) {
- VALUE s = Qnil;
- if (BEG(i) >= 0) {
- s = rb_str_subseq(str, BEG(i), END(i)-BEG(i));
- }
- rb_ary_push(result, s);
- }
-
- return result;
+ VALUE match;
+ struct re_registers *regs;
+ if (BUILTIN_TYPE(pat) == T_STRING) {
+ regs = NULL;
+ end = pos + RSTRING_LEN(pat);
+ }
+ else {
+ match = rb_backref_get();
+ regs = RMATCH_REGS(match);
+ pos = BEG(0);
+ end = END(0);
+ }
+
+ if (pos == end) {
+ rb_encoding *enc = STR_ENC_GET(str);
+ /*
+ * Always consume at least one character of the input string
+ */
+ if (RSTRING_LEN(str) > end)
+ *start = end + rb_enc_fast_mbclen(RSTRING_PTR(str) + end,
+ RSTRING_END(str), enc);
+ else
+ *start = end + 1;
+ }
+ else {
+ *start = end;
+ }
+
+ if (!regs || regs->num_regs == 1) {
+ result = rb_str_subseq(str, pos, end - pos);
+ return result;
+ }
+ else {
+ result = rb_ary_new2(regs->num_regs);
+ for (int i = 1; i < regs->num_regs; i++) {
+ VALUE s = Qnil;
+ if (BEG(i) >= 0) {
+ s = rb_str_subseq(str, BEG(i), END(i)-BEG(i));
+ }
+
+ rb_ary_push(result, s);
+ }
+ }
+
+ RB_GC_GUARD(match);
}
- return Qnil;
+
+ return result;
}
/*
* call-seq:
- * str.scan(pattern) -> array
- * str.scan(pattern) {|match, ...| block } -> str
+ * scan(string_or_regexp) -> array
+ * scan(string_or_regexp) {|matches| ... } -> self
+ *
+ * Matches a pattern against +self+; the pattern is:
+ *
+ * - +string_or_regexp+ itself, if it is a Regexp.
+ * - <tt>Regexp.quote(string_or_regexp)</tt>, if +string_or_regexp+ is a string.
*
- * Both forms iterate through <i>str</i>, matching the pattern (which may be a
- * Regexp or a String). For each match, a result is
- * generated and either added to the result array or passed to the block. If
- * the pattern contains no groups, each individual result consists of the
- * matched string, <code>$&</code>. If the pattern contains groups, each
- * individual result is itself an array containing one entry per group.
+ * Iterates through +self+, generating a collection of matching results:
*
- * a = "cruel world"
- * a.scan(/\w+/) #=> ["cruel", "world"]
- * a.scan(/.../) #=> ["cru", "el ", "wor"]
- * a.scan(/(...)/) #=> [["cru"], ["el "], ["wor"]]
- * a.scan(/(..)(..)/) #=> [["cr", "ue"], ["l ", "wo"]]
+ * - If the pattern contains no groups, each result is the
+ * matched string, <code>$&</code>.
+ * - If the pattern contains groups, each result is an array
+ * containing one entry per group.
*
- * And the block form:
+ * With no block given, returns an array of the results:
*
- * a.scan(/\w+/) {|w| print "<<#{w}>> " }
- * print "\n"
- * a.scan(/(.)(.)/) {|x,y| print y, x }
- * print "\n"
+ * s = 'cruel world'
+ * s.scan(/\w+/) # => ["cruel", "world"]
+ * s.scan(/.../) # => ["cru", "el ", "wor"]
+ * s.scan(/(...)/) # => [["cru"], ["el "], ["wor"]]
+ * s.scan(/(..)(..)/) # => [["cr", "ue"], ["l ", "wo"]]
*
- * <em>produces:</em>
+ * With a block given, calls the block with each result; returns +self+:
+ *
+ * s.scan(/\w+/) {|w| print "<<#{w}>> " }
+ * print "\n"
+ * s.scan(/(.)(.)/) {|x,y| print y, x }
+ * print "\n"
+ *
+ * Output:
*
* <<cruel>> <<world>>
* rceu lowlr
+ *
*/
static VALUE
@@ -9820,23 +10182,23 @@ rb_str_scan(VALUE str, VALUE pat)
pat = get_pat_quoted(pat, 1);
mustnot_broken(str);
if (!rb_block_given_p()) {
- VALUE ary = rb_ary_new();
+ VALUE ary = rb_ary_new();
- while (!NIL_P(result = scan_once(str, pat, &start, 0))) {
- last = prev;
- prev = start;
- rb_ary_push(ary, result);
- }
- if (last >= 0) rb_pat_search(pat, str, last, 1);
- else rb_backref_set(Qnil);
- return ary;
+ while (!NIL_P(result = scan_once(str, pat, &start, 0))) {
+ last = prev;
+ prev = start;
+ rb_ary_push(ary, result);
+ }
+ if (last >= 0) rb_pat_search(pat, str, last, 1);
+ else rb_backref_set(Qnil);
+ return ary;
}
while (!NIL_P(result = scan_once(str, pat, &start, 1))) {
- last = prev;
- prev = start;
- rb_yield(result);
- str_mod_check(str, p, len);
+ last = prev;
+ prev = start;
+ rb_yield(result);
+ str_mod_check(str, p, len);
}
if (last >= 0) rb_pat_search(pat, str, last, 1);
return str;
@@ -9845,16 +10207,20 @@ rb_str_scan(VALUE str, VALUE pat)
/*
* call-seq:
- * str.hex -> integer
+ * hex -> integer
*
- * Treats leading characters from <i>str</i> as a string of hexadecimal digits
+ * Interprets the leading substring of +self+ as a string of hexadecimal digits
* (with an optional sign and an optional <code>0x</code>) and returns the
- * corresponding number. Zero is returned on error.
+ * corresponding number;
+ * returns zero if there is no such leading substring:
+ *
+ * '0x0a'.hex # => 10
+ * '-1234'.hex # => -4660
+ * '0'.hex # => 0
+ * 'non-numeric'.hex # => 0
+ *
+ * Related: String#oct.
*
- * "0x0a".hex #=> 10
- * "-1234".hex #=> -4660
- * "0".hex #=> 0
- * "wombat".hex #=> 0
*/
static VALUE
@@ -9866,19 +10232,22 @@ rb_str_hex(VALUE str)
/*
* call-seq:
- * str.oct -> integer
+ * oct -> integer
*
- * Treats leading characters of <i>str</i> as a string of octal digits (with an
- * optional sign) and returns the corresponding number. Returns 0 if the
- * conversion fails.
+ * Interprets the leading substring of +self+ as a string of octal digits
+ * (with an optional sign) and returns the corresponding number;
+ * returns zero if there is no such leading substring:
*
- * "123".oct #=> 83
- * "-377".oct #=> -255
- * "bad".oct #=> 0
- * "0377bad".oct #=> 255
+ * '123'.oct # => 83
+ * '-377'.oct # => -255
+ * '0377non-numeric'.oct # => 255
+ * 'non-numeric'.oct # => 0
+ *
+ * If +self+ starts with <tt>0</tt>, radix indicators are honored;
+ * see Kernel#Integer.
+ *
+ * Related: String#hex.
*
- * If +str+ starts with <code>0</code>, radix indicators are honored.
- * See Kernel#Integer.
*/
static VALUE
@@ -9892,41 +10261,18 @@ rb_str_oct(VALUE str)
# include "ruby/atomic.h"
static struct {
- rb_atomic_t initialized;
rb_nativethread_lock_t lock;
-} crypt_mutex;
-
-static void
-crypt_mutex_destroy(void)
-{
- RUBY_ASSERT_ALWAYS(crypt_mutex.initialized == 1);
- rb_nativethread_lock_destroy(&crypt_mutex.lock);
- crypt_mutex.initialized = 0;
-}
+} crypt_mutex = {PTHREAD_MUTEX_INITIALIZER};
static void
crypt_mutex_initialize(void)
{
- rb_atomic_t i;
- while ((i = RUBY_ATOMIC_CAS(crypt_mutex.initialized, 0, 2)) == 2);
- switch (i) {
- case 0:
- rb_nativethread_lock_initialize(&crypt_mutex.lock);
- atexit(crypt_mutex_destroy);
- RUBY_ASSERT(crypt_mutex.initialized == 2);
- RUBY_ATOMIC_CAS(crypt_mutex.initialized, 2, 1);
- break;
- case 1:
- break;
- default:
- rb_bug("crypt_mutex.initialized: %d->%d", i, crypt_mutex.initialized);
- }
}
#endif
/*
* call-seq:
- * str.crypt(salt_str) -> new_str
+ * crypt(salt_str) -> new_string
*
* Returns the string generated by calling <code>crypt(3)</code>
* standard library function with <code>str</code> and
@@ -10008,15 +10354,15 @@ rb_str_crypt(VALUE str, VALUE salt)
s = StringValueCStr(str);
saltp = RSTRING_PTR(salt);
if (RSTRING_LEN(salt) < 2 || !saltp[0] || !saltp[1]) {
- rb_raise(rb_eArgError, "salt too short (need >=2 bytes)");
+ rb_raise(rb_eArgError, "salt too short (need >=2 bytes)");
}
#ifdef BROKEN_CRYPT
if (!ISASCII((unsigned char)saltp[0]) || !ISASCII((unsigned char)saltp[1])) {
- salt_8bit_clean[0] = saltp[0] & 0x7f;
- salt_8bit_clean[1] = saltp[1] & 0x7f;
- salt_8bit_clean[2] = '\0';
- saltp = salt_8bit_clean;
+ salt_8bit_clean[0] = saltp[0] & 0x7f;
+ salt_8bit_clean[1] = saltp[1] & 0x7f;
+ salt_8bit_clean[2] = '\0';
+ saltp = salt_8bit_clean;
}
#endif
#ifdef HAVE_CRYPT_R
@@ -10031,9 +10377,9 @@ rb_str_crypt(VALUE str, VALUE salt)
res = crypt(s, saltp);
#endif
if (!res) {
- int err = errno;
- CRYPT_END();
- rb_syserr_fail(err, "crypt");
+ int err = errno;
+ CRYPT_END();
+ rb_syserr_fail(err, "crypt");
}
result = rb_str_new_cstr(res);
CRYPT_END();
@@ -10043,11 +10389,10 @@ rb_str_crypt(VALUE str, VALUE salt)
/*
* call-seq:
- * str.ord -> integer
+ * ord -> integer
*
- * Returns the Integer ordinal of a one-character string.
+ * :include: doc/string/ord.rdoc
*
- * "a".ord #=> 97
*/
static VALUE
@@ -10060,13 +10405,10 @@ rb_str_ord(VALUE s)
}
/*
* call-seq:
- * str.sum(n=16) -> integer
+ * sum(n = 16) -> integer
+ *
+ * :include: doc/string/sum.rdoc
*
- * Returns a basic <em>n</em>-bit checksum of the characters in <i>str</i>,
- * where <em>n</em> is the optional Integer parameter, defaulting
- * to 16. The result is simply the sum of the binary value of each byte in
- * <i>str</i> modulo <code>2**n - 1</code>. This is not a particularly good
- * checksum.
*/
static VALUE
@@ -10141,15 +10483,15 @@ rb_str_justify(int argc, VALUE *argv, VALUE str, char jflag)
termlen = rb_enc_mbminlen(enc);
width = NUM2LONG(w);
if (argc == 2) {
- StringValue(pad);
- enc = rb_enc_check(str, pad);
- f = RSTRING_PTR(pad);
- flen = RSTRING_LEN(pad);
- fclen = str_strlen(pad, enc); /* rb_enc_check */
- singlebyte = single_byte_optimizable(pad);
- if (flen == 0 || fclen == 0) {
- rb_raise(rb_eArgError, "zero width padding");
- }
+ StringValue(pad);
+ enc = rb_enc_check(str, pad);
+ f = RSTRING_PTR(pad);
+ flen = RSTRING_LEN(pad);
+ fclen = str_strlen(pad, enc); /* rb_enc_check */
+ singlebyte = single_byte_optimizable(pad);
+ if (flen == 0 || fclen == 0) {
+ rb_raise(rb_eArgError, "zero width padding");
+ }
}
len = str_strlen(str, enc); /* rb_enc_check */
if (width < 0 || len >= width) return str_duplicate(rb_cString, str);
@@ -10176,14 +10518,14 @@ rb_str_justify(int argc, VALUE *argv, VALUE str, char jflag)
}
else {
while (llen >= fclen) {
- memcpy(p,f,flen);
- p += flen;
- llen -= fclen;
- }
+ memcpy(p,f,flen);
+ p += flen;
+ llen -= fclen;
+ }
if (llen > 0) {
memcpy(p, f, llen2);
p += llen2;
- }
+ }
}
memcpy(p, RSTRING_PTR(str), size);
p += size;
@@ -10193,22 +10535,22 @@ rb_str_justify(int argc, VALUE *argv, VALUE str, char jflag)
}
else {
while (rlen >= fclen) {
- memcpy(p,f,flen);
- p += flen;
- rlen -= fclen;
- }
+ memcpy(p,f,flen);
+ p += flen;
+ rlen -= fclen;
+ }
if (rlen > 0) {
memcpy(p, f, rlen2);
p += rlen2;
- }
+ }
}
TERM_FILL(p, termlen);
STR_SET_LEN(res, p-RSTRING_PTR(res));
rb_enc_associate(res, enc);
if (argc == 2)
- cr = ENC_CODERANGE_AND(cr, ENC_CODERANGE(pad));
+ cr = ENC_CODERANGE_AND(cr, ENC_CODERANGE(pad));
if (cr != ENC_CODERANGE_BROKEN)
- ENC_CODERANGE_SET(res, cr);
+ ENC_CODERANGE_SET(res, cr);
RB_GC_GUARD(pad);
return res;
@@ -10217,15 +10559,12 @@ rb_str_justify(int argc, VALUE *argv, VALUE str, char jflag)
/*
* call-seq:
- * str.ljust(integer, padstr=' ') -> new_str
+ * ljust(size, pad_string = ' ') -> new_string
*
- * If <i>integer</i> is greater than the length of <i>str</i>, returns a new
- * String of length <i>integer</i> with <i>str</i> left justified
- * and padded with <i>padstr</i>; otherwise, returns <i>str</i>.
+ * :include: doc/string/ljust.rdoc
+ *
+ * Related: String#rjust, String#center.
*
- * "hello".ljust(4) #=> "hello"
- * "hello".ljust(20) #=> "hello "
- * "hello".ljust(20, '1234') #=> "hello123412341234123"
*/
static VALUE
@@ -10234,18 +10573,14 @@ rb_str_ljust(int argc, VALUE *argv, VALUE str)
return rb_str_justify(argc, argv, str, 'l');
}
-
/*
* call-seq:
- * str.rjust(integer, padstr=' ') -> new_str
+ * rjust(size, pad_string = ' ') -> new_string
+ *
+ * :include: doc/string/rjust.rdoc
*
- * If <i>integer</i> is greater than the length of <i>str</i>, returns a new
- * String of length <i>integer</i> with <i>str</i> right justified
- * and padded with <i>padstr</i>; otherwise, returns <i>str</i>.
+ * Related: String#ljust, String#center.
*
- * "hello".rjust(4) #=> "hello"
- * "hello".rjust(20) #=> " hello"
- * "hello".rjust(20, '1234') #=> "123412341234123hello"
*/
static VALUE
@@ -10257,15 +10592,12 @@ rb_str_rjust(int argc, VALUE *argv, VALUE str)
/*
* call-seq:
- * str.center(width, padstr=' ') -> new_str
+ * center(size, pad_string = ' ') -> new_string
*
- * Centers +str+ in +width+. If +width+ is greater than the length of +str+,
- * returns a new String of length +width+ with +str+ centered and padded with
- * +padstr+; otherwise, returns +str+.
+ * :include: doc/string/center.rdoc
+ *
+ * Related: String#ljust, String#rjust.
*
- * "hello".center(4) #=> "hello"
- * "hello".center(20) #=> " hello "
- * "hello".center(20, '123') #=> "1231231hello12312312"
*/
static VALUE
@@ -10276,17 +10608,10 @@ rb_str_center(int argc, VALUE *argv, VALUE str)
/*
* call-seq:
- * str.partition(sep) -> [head, sep, tail]
- * str.partition(regexp) -> [head, match, tail]
+ * partition(string_or_regexp) -> [head, match, tail]
*
- * Searches <i>sep</i> or pattern (<i>regexp</i>) in the string
- * and returns the part before it, the match, and the part
- * after it.
- * If it is not found, returns two empty strings and <i>str</i>.
+ * :include: doc/string/partition.rdoc
*
- * "hello".partition("l") #=> ["he", "l", "lo"]
- * "hello".partition("x") #=> ["hello", "", ""]
- * "hello".partition(/.l/) #=> ["h", "el", "lo"]
*/
static VALUE
@@ -10296,23 +10621,23 @@ rb_str_partition(VALUE str, VALUE sep)
sep = get_pat_quoted(sep, 0);
if (RB_TYPE_P(sep, T_REGEXP)) {
- if (rb_reg_search(sep, str, 0, 0) < 0) {
+ if (rb_reg_search(sep, str, 0, 0) < 0) {
goto failed;
- }
- VALUE match = rb_backref_get();
- struct re_registers *regs = RMATCH_REGS(match);
+ }
+ VALUE match = rb_backref_get();
+ struct re_registers *regs = RMATCH_REGS(match);
pos = BEG(0);
- sep = rb_str_subseq(str, pos, END(0) - pos);
+ sep = rb_str_subseq(str, pos, END(0) - pos);
}
else {
- pos = rb_str_index(str, sep, 0);
- if (pos < 0) goto failed;
+ pos = rb_str_index(str, sep, 0);
+ if (pos < 0) goto failed;
}
return rb_ary_new3(3, rb_str_subseq(str, 0, pos),
- sep,
- rb_str_subseq(str, pos+RSTRING_LEN(sep),
- RSTRING_LEN(str)-pos-RSTRING_LEN(sep)));
+ sep,
+ rb_str_subseq(str, pos+RSTRING_LEN(sep),
+ RSTRING_LEN(str)-pos-RSTRING_LEN(sep)));
failed:
return rb_ary_new3(3, str_duplicate(rb_cString, str), str_new_empty_String(str), str_new_empty_String(str));
@@ -10320,17 +10645,10 @@ rb_str_partition(VALUE str, VALUE sep)
/*
* call-seq:
- * str.rpartition(sep) -> [head, sep, tail]
- * str.rpartition(regexp) -> [head, match, tail]
+ * rpartition(sep) -> [head, match, tail]
*
- * Searches <i>sep</i> or pattern (<i>regexp</i>) in the string from the end
- * of the string, and returns the part before it, the match, and the part
- * after it.
- * If it is not found, returns two empty strings and <i>str</i>.
+ * :include: doc/string/rpartition.rdoc
*
- * "hello".rpartition("l") #=> ["hel", "l", "o"]
- * "hello".rpartition("x") #=> ["", "", "hello"]
- * "hello".rpartition(/.l/) #=> ["he", "ll", "o"]
*/
static VALUE
@@ -10344,41 +10662,33 @@ rb_str_rpartition(VALUE str, VALUE sep)
goto failed;
}
VALUE match = rb_backref_get();
- struct re_registers *regs = RMATCH_REGS(match);
+ struct re_registers *regs = RMATCH_REGS(match);
pos = BEG(0);
sep = rb_str_subseq(str, pos, END(0) - pos);
}
else {
- pos = rb_str_sublen(str, pos);
- pos = rb_str_rindex(str, sep, pos);
+ pos = rb_str_sublen(str, pos);
+ pos = rb_str_rindex(str, sep, pos);
if (pos < 0) {
goto failed;
}
- pos = rb_str_offset(str, pos);
}
return rb_ary_new3(3, rb_str_subseq(str, 0, pos),
- sep,
- rb_str_subseq(str, pos+RSTRING_LEN(sep),
- RSTRING_LEN(str)-pos-RSTRING_LEN(sep)));
+ sep,
+ rb_str_subseq(str, pos+RSTRING_LEN(sep),
+ RSTRING_LEN(str)-pos-RSTRING_LEN(sep)));
failed:
return rb_ary_new3(3, str_new_empty_String(str), str_new_empty_String(str), str_duplicate(rb_cString, str));
}
/*
* call-seq:
- * str.start_with?([prefixes]+) -> true or false
- *
- * Returns true if +str+ starts with one of the +prefixes+ given.
- * Each of the +prefixes+ should be a String or a Regexp.
+ * start_with?(*string_or_regexp) -> true or false
*
- * "hello".start_with?("hell") #=> true
- * "hello".start_with?(/H/i) #=> true
+ * :include: doc/string/start_with_p.rdoc
*
- * # returns true if one of the prefixes matches.
- * "hello".start_with?("heaven", "hell") #=> true
- * "hello".start_with?("heaven", "paradise") #=> false
*/
static VALUE
@@ -10387,56 +10697,62 @@ rb_str_start_with(int argc, VALUE *argv, VALUE str)
int i;
for (i=0; i<argc; i++) {
- VALUE tmp = argv[i];
- if (RB_TYPE_P(tmp, T_REGEXP)) {
- if (rb_reg_start_with_p(tmp, str))
- return Qtrue;
- }
- else {
- StringValue(tmp);
- rb_enc_check(str, tmp);
- if (RSTRING_LEN(str) < RSTRING_LEN(tmp)) continue;
- if (memcmp(RSTRING_PTR(str), RSTRING_PTR(tmp), RSTRING_LEN(tmp)) == 0)
- return Qtrue;
- }
+ VALUE tmp = argv[i];
+ if (RB_TYPE_P(tmp, T_REGEXP)) {
+ if (rb_reg_start_with_p(tmp, str))
+ return Qtrue;
+ }
+ else {
+ const char *p, *s, *e;
+ long slen, tlen;
+ rb_encoding *enc;
+
+ StringValue(tmp);
+ enc = rb_enc_check(str, tmp);
+ if ((tlen = RSTRING_LEN(tmp)) == 0) return Qtrue;
+ if ((slen = RSTRING_LEN(str)) < tlen) continue;
+ p = RSTRING_PTR(str);
+ e = p + slen;
+ s = p + tlen;
+ if (!at_char_right_boundary(p, s, e, enc))
+ continue;
+ if (memcmp(p, RSTRING_PTR(tmp), tlen) == 0)
+ return Qtrue;
+ }
}
return Qfalse;
}
/*
* call-seq:
- * str.end_with?([suffixes]+) -> true or false
- *
- * Returns true if +str+ ends with one of the +suffixes+ given.
+ * end_with?(*strings) -> true or false
*
- * "hello".end_with?("ello") #=> true
+ * :include: doc/string/end_with_p.rdoc
*
- * # returns true if one of the +suffixes+ matches.
- * "hello".end_with?("heaven", "ello") #=> true
- * "hello".end_with?("heaven", "paradise") #=> false
*/
static VALUE
rb_str_end_with(int argc, VALUE *argv, VALUE str)
{
int i;
- char *p, *s, *e;
- rb_encoding *enc;
for (i=0; i<argc; i++) {
- VALUE tmp = argv[i];
- long slen, tlen;
- StringValue(tmp);
- enc = rb_enc_check(str, tmp);
- if ((tlen = RSTRING_LEN(tmp)) == 0) return Qtrue;
- if ((slen = RSTRING_LEN(str)) < tlen) continue;
- p = RSTRING_PTR(str);
+ VALUE tmp = argv[i];
+ const char *p, *s, *e;
+ long slen, tlen;
+ rb_encoding *enc;
+
+ StringValue(tmp);
+ enc = rb_enc_check(str, tmp);
+ if ((tlen = RSTRING_LEN(tmp)) == 0) return Qtrue;
+ if ((slen = RSTRING_LEN(str)) < tlen) continue;
+ p = RSTRING_PTR(str);
e = p + slen;
- s = e - tlen;
- if (rb_enc_left_char_head(p, s, e, enc) != s)
- continue;
- if (memcmp(s, RSTRING_PTR(tmp), RSTRING_LEN(tmp)) == 0)
- return Qtrue;
+ s = e - tlen;
+ if (!at_char_boundary(p, s, e, enc))
+ continue;
+ if (memcmp(s, RSTRING_PTR(tmp), tlen) == 0)
+ return Qtrue;
}
return Qfalse;
}
@@ -10453,12 +10769,17 @@ rb_str_end_with(int argc, VALUE *argv, VALUE str)
static long
deleted_prefix_length(VALUE str, VALUE prefix)
{
- char *strptr, *prefixptr;
+ const char *strptr, *prefixptr;
long olen, prefixlen;
+ rb_encoding *enc = rb_enc_get(str);
StringValue(prefix);
- if (is_broken_string(prefix)) return 0;
- rb_enc_check(str, prefix);
+
+ if (!is_broken_string(prefix) ||
+ !rb_enc_asciicompat(enc) ||
+ !rb_enc_asciicompat(rb_enc_get(prefix))) {
+ enc = rb_enc_check(str, prefix);
+ }
/* return 0 if not start with prefix */
prefixlen = RSTRING_LEN(prefix);
@@ -10468,19 +10789,30 @@ deleted_prefix_length(VALUE str, VALUE prefix)
strptr = RSTRING_PTR(str);
prefixptr = RSTRING_PTR(prefix);
if (memcmp(strptr, prefixptr, prefixlen) != 0) return 0;
+ if (is_broken_string(prefix)) {
+ if (!is_broken_string(str)) {
+ /* prefix in a valid string cannot be broken */
+ return 0;
+ }
+ const char *strend = strptr + olen;
+ const char *after_prefix = strptr + prefixlen;
+ if (!at_char_right_boundary(strptr, after_prefix, strend, enc)) {
+ /* prefix does not end at char-boundary */
+ return 0;
+ }
+ }
+ /* prefix part in `str` also should be valid. */
return prefixlen;
}
/*
* call-seq:
- * str.delete_prefix!(prefix) -> self or nil
+ * delete_prefix!(prefix) -> self or nil
*
- * Deletes leading <code>prefix</code> from <i>str</i>, returning
- * <code>nil</code> if no change was made.
+ * Like String#delete_prefix, except that +self+ is modified in place.
+ * Returns +self+ if the prefix is removed, +nil+ otherwise.
*
- * "hello".delete_prefix!("hel") #=> "lo"
- * "hello".delete_prefix!("llo") #=> nil
*/
static VALUE
@@ -10497,12 +10829,10 @@ rb_str_delete_prefix_bang(VALUE str, VALUE prefix)
/*
* call-seq:
- * str.delete_prefix(prefix) -> new_str
+ * delete_prefix(prefix) -> new_string
*
- * Returns a copy of <i>str</i> with leading <code>prefix</code> deleted.
+ * :include: doc/string/delete_prefix.rdoc
*
- * "hello".delete_prefix("hel") #=> "lo"
- * "hello".delete_prefix("llo") #=> "hello"
*/
static VALUE
@@ -10528,7 +10858,7 @@ rb_str_delete_prefix(VALUE str, VALUE prefix)
static long
deleted_suffix_length(VALUE str, VALUE suffix)
{
- char *strptr, *suffixptr, *s;
+ const char *strptr, *suffixptr;
long olen, suffixlen;
rb_encoding *enc;
@@ -10543,22 +10873,21 @@ deleted_suffix_length(VALUE str, VALUE suffix)
if (olen < suffixlen) return 0;
strptr = RSTRING_PTR(str);
suffixptr = RSTRING_PTR(suffix);
- s = strptr + olen - suffixlen;
- if (memcmp(s, suffixptr, suffixlen) != 0) return 0;
- if (rb_enc_left_char_head(strptr, s, strptr + olen, enc) != s) return 0;
+ const char *strend = strptr + olen;
+ const char *before_suffix = strend - suffixlen;
+ if (memcmp(before_suffix, suffixptr, suffixlen) != 0) return 0;
+ if (!at_char_boundary(strptr, before_suffix, strend, enc)) return 0;
return suffixlen;
}
/*
* call-seq:
- * str.delete_suffix!(suffix) -> self or nil
+ * delete_suffix!(suffix) -> self or nil
*
- * Deletes trailing <code>suffix</code> from <i>str</i>, returning
- * <code>nil</code> if no change was made.
+ * Like String#delete_suffix, except that +self+ is modified in place.
+ * Returns +self+ if the suffix is removed, +nil+ otherwise.
*
- * "hello".delete_suffix!("llo") #=> "he"
- * "hello".delete_suffix!("hel") #=> nil
*/
static VALUE
@@ -10576,19 +10905,17 @@ rb_str_delete_suffix_bang(VALUE str, VALUE suffix)
STR_SET_LEN(str, len);
TERM_FILL(&RSTRING_PTR(str)[len], TERM_LEN(str));
if (ENC_CODERANGE(str) != ENC_CODERANGE_7BIT) {
- ENC_CODERANGE_CLEAR(str);
+ ENC_CODERANGE_CLEAR(str);
}
return str;
}
/*
* call-seq:
- * str.delete_suffix(suffix) -> new_str
+ * delete_suffix(suffix) -> new_string
*
- * Returns a copy of <i>str</i> with trailing <code>suffix</code> deleted.
+ * :include: doc/string/delete_suffix.rdoc
*
- * "hello".delete_suffix("llo") #=> "he"
- * "hello".delete_suffix("hel") #=> "hello"
*/
static VALUE
@@ -10606,7 +10933,7 @@ void
rb_str_setter(VALUE val, ID id, VALUE *var)
{
if (!NIL_P(val) && !RB_TYPE_P(val, T_STRING)) {
- rb_raise(rb_eTypeError, "value of %"PRIsVALUE" must be String", rb_id2str(id));
+ rb_raise(rb_eTypeError, "value of %"PRIsVALUE" must be String", rb_id2str(id));
}
*var = val;
}
@@ -10616,12 +10943,12 @@ rb_fs_setter(VALUE val, ID id, VALUE *var)
{
val = rb_fs_check(val);
if (!val) {
- rb_raise(rb_eTypeError,
- "value of %"PRIsVALUE" must be String or Regexp",
- rb_id2str(id));
+ rb_raise(rb_eTypeError,
+ "value of %"PRIsVALUE" must be String or Regexp",
+ rb_id2str(id));
}
if (!NIL_P(val)) {
- rb_warn_deprecated("`$;'", NULL);
+ rb_warn_deprecated("'$;'", NULL);
}
*var = val;
}
@@ -10629,51 +10956,87 @@ rb_fs_setter(VALUE val, ID id, VALUE *var)
/*
* call-seq:
- * str.force_encoding(encoding) -> str
+ * force_encoding(encoding) -> self
+ *
+ * :include: doc/string/force_encoding.rdoc
*
- * Changes the encoding to +encoding+ and returns self.
*/
static VALUE
rb_str_force_encoding(VALUE str, VALUE enc)
{
str_modifiable(str);
- rb_enc_associate(str, rb_to_encoding(enc));
+
+ rb_encoding *encoding = rb_to_encoding(enc);
+ int idx = rb_enc_to_index(encoding);
+
+ // If the encoding is unchanged, we do nothing.
+ if (ENCODING_GET(str) == idx) {
+ return str;
+ }
+
+ rb_enc_associate_index(str, idx);
+
+ // If the coderange was 7bit and the new encoding is ASCII-compatible
+ // we can keep the coderange.
+ if (ENC_CODERANGE(str) == ENC_CODERANGE_7BIT && encoding && rb_enc_asciicompat(encoding)) {
+ return str;
+ }
+
ENC_CODERANGE_CLEAR(str);
return str;
}
/*
* call-seq:
- * str.b -> str
+ * b -> string
+ *
+ * :include: doc/string/b.rdoc
*
- * Returns a copied string whose encoding is ASCII-8BIT.
*/
static VALUE
rb_str_b(VALUE str)
{
VALUE str2;
- if (FL_TEST(str, STR_NOEMBED)) {
- str2 = str_alloc_heap(rb_cString);
+ if (STR_EMBED_P(str)) {
+ str2 = str_alloc_embed(rb_cString, RSTRING_LEN(str) + TERM_LEN(str));
}
else {
- str2 = str_alloc_embed(rb_cString, RSTRING_EMBED_LEN(str) + TERM_LEN(str));
+ str2 = str_alloc_heap(rb_cString);
}
str_replace_shared_without_enc(str2, str);
- ENC_CODERANGE_CLEAR(str2);
+
+ if (rb_enc_asciicompat(STR_ENC_GET(str))) {
+ // BINARY strings can never be broken; they're either 7-bit ASCII or VALID.
+ // If we know the receiver's code range then we know the result's code range.
+ int cr = ENC_CODERANGE(str);
+ switch (cr) {
+ case ENC_CODERANGE_7BIT:
+ ENC_CODERANGE_SET(str2, ENC_CODERANGE_7BIT);
+ break;
+ case ENC_CODERANGE_BROKEN:
+ case ENC_CODERANGE_VALID:
+ ENC_CODERANGE_SET(str2, ENC_CODERANGE_VALID);
+ break;
+ default:
+ ENC_CODERANGE_CLEAR(str2);
+ break;
+ }
+ }
+
return str2;
}
/*
* call-seq:
- * str.valid_encoding? -> true or false
+ * valid_encoding? -> true or false
*
- * Returns true for a string which is encoded correctly.
+ * Returns +true+ if +self+ is encoded correctly, +false+ otherwise:
*
- * "\xc2\xa1".force_encoding("UTF-8").valid_encoding? #=> true
- * "\xc2".force_encoding("UTF-8").valid_encoding? #=> false
- * "\x80".force_encoding("UTF-8").valid_encoding? #=> false
+ * "\xc2\xa1".force_encoding("UTF-8").valid_encoding? # => true
+ * "\xc2".force_encoding("UTF-8").valid_encoding? # => false
+ * "\x80".force_encoding("UTF-8").valid_encoding? # => false
*/
static VALUE
@@ -10686,12 +11049,14 @@ rb_str_valid_encoding_p(VALUE str)
/*
* call-seq:
- * str.ascii_only? -> true or false
+ * ascii_only? -> true or false
+ *
+ * Returns +true+ if +self+ contains only ASCII characters,
+ * +false+ otherwise:
*
- * Returns true for a string which has only ASCII characters.
+ * 'abc'.ascii_only? # => true
+ * "abc\u{6666}".ascii_only? # => false
*
- * "abc".force_encoding("UTF-8").ascii_only? #=> true
- * "abc\u{6666}".force_encoding("UTF-8").ascii_only? #=> false
*/
static VALUE
@@ -10714,27 +11079,27 @@ rb_str_ellipsize(VALUE str, long len)
if (len < 0) rb_raise(rb_eIndexError, "negative length %ld", len);
if (len * rb_enc_mbminlen(enc) >= blen ||
- (e = rb_enc_nth(p, e, len, enc)) - p == blen) {
- ret = str;
+ (e = rb_enc_nth(p, e, len, enc)) - p == blen) {
+ ret = str;
}
else if (len <= ellipsislen ||
- !(e = rb_enc_step_back(p, e, e, len = ellipsislen, enc))) {
- if (rb_enc_asciicompat(enc)) {
+ !(e = rb_enc_step_back(p, e, e, len = ellipsislen, enc))) {
+ if (rb_enc_asciicompat(enc)) {
ret = rb_str_new(ellipsis, len);
- rb_enc_associate(ret, enc);
- }
- else {
- estr = rb_usascii_str_new(ellipsis, len);
- ret = rb_str_encode(estr, rb_enc_from_encoding(enc), 0, Qnil);
- }
+ rb_enc_associate(ret, enc);
+ }
+ else {
+ estr = rb_usascii_str_new(ellipsis, len);
+ ret = rb_str_encode(estr, rb_enc_from_encoding(enc), 0, Qnil);
+ }
}
else if (ret = rb_str_subseq(str, 0, e - p), rb_enc_asciicompat(enc)) {
- rb_str_cat(ret, ellipsis, ellipsislen);
+ rb_str_cat(ret, ellipsis, ellipsislen);
}
else {
- estr = rb_str_encode(rb_usascii_str_new(ellipsis, ellipsislen),
- rb_enc_from_encoding(enc), 0, Qnil);
- rb_str_append(ret, estr);
+ estr = rb_str_encode(rb_usascii_str_new(ellipsis, ellipsislen),
+ rb_enc_from_encoding(enc), 0, Qnil);
+ rb_str_append(ret, estr);
}
return ret;
}
@@ -10746,14 +11111,14 @@ str_compat_and_valid(VALUE str, rb_encoding *enc)
str = StringValue(str);
cr = rb_enc_str_coderange(str);
if (cr == ENC_CODERANGE_BROKEN) {
- rb_raise(rb_eArgError, "replacement must be valid byte sequence '%+"PRIsVALUE"'", str);
+ rb_raise(rb_eArgError, "replacement must be valid byte sequence '%+"PRIsVALUE"'", str);
}
else {
- rb_encoding *e = STR_ENC_GET(str);
- if (cr == ENC_CODERANGE_7BIT ? rb_enc_mbminlen(enc) != 1 : enc != e) {
- rb_raise(rb_eEncCompatError, "incompatible character encodings: %s and %s",
- rb_enc_name(enc), rb_enc_name(e));
- }
+ rb_encoding *e = STR_ENC_GET(str);
+ if (cr == ENC_CODERANGE_7BIT ? rb_enc_mbminlen(enc) != 1 : enc != e) {
+ rb_raise(rb_eEncCompatError, "incompatible character encodings: %s and %s",
+ rb_enc_name(enc), rb_enc_name(e));
+ }
}
return str;
}
@@ -10772,9 +11137,9 @@ rb_enc_str_scrub(rb_encoding *enc, VALUE str, VALUE repl)
{
int cr = ENC_CODERANGE_UNKNOWN;
if (enc == STR_ENC_GET(str)) {
- /* cached coderange makes sense only when enc equals the
- * actual encoding of str */
- cr = ENC_CODERANGE(str);
+ /* cached coderange makes sense only when enc equals the
+ * actual encoding of str */
+ cr = ENC_CODERANGE(str);
}
return enc_str_scrub(enc, str, repl, cr);
}
@@ -10789,26 +11154,26 @@ enc_str_scrub(rb_encoding *enc, VALUE str, VALUE repl, int cr)
long slen;
if (rb_block_given_p()) {
- if (!NIL_P(repl))
- rb_raise(rb_eArgError, "both of block and replacement given");
- replen = 0;
+ if (!NIL_P(repl))
+ rb_raise(rb_eArgError, "both of block and replacement given");
+ replen = 0;
}
if (ENC_CODERANGE_CLEAN_P(cr))
- return Qnil;
+ return Qnil;
if (!NIL_P(repl)) {
- repl = str_compat_and_valid(repl, enc);
+ repl = str_compat_and_valid(repl, enc);
}
if (rb_enc_dummy_p(enc)) {
- return Qnil;
+ return Qnil;
}
encidx = rb_enc_to_index(enc);
#define DEFAULT_REPLACE_CHAR(str) do { \
- static const char replace[sizeof(str)-1] = str; \
- rep = replace; replen = (int)sizeof(replace); \
+ static const char replace[sizeof(str)-1] = str; \
+ rep = replace; replen = (int)sizeof(replace); \
} while (0)
slen = RSTRING_LEN(str);
@@ -10818,204 +11183,204 @@ enc_str_scrub(rb_encoding *enc, VALUE str, VALUE repl, int cr)
sp = p;
if (rb_enc_asciicompat(enc)) {
- int rep7bit_p;
- if (!replen) {
- rep = NULL;
- rep7bit_p = FALSE;
- }
- else if (!NIL_P(repl)) {
- rep = RSTRING_PTR(repl);
- replen = RSTRING_LEN(repl);
- rep7bit_p = (ENC_CODERANGE(repl) == ENC_CODERANGE_7BIT);
- }
- else if (encidx == rb_utf8_encindex()) {
- DEFAULT_REPLACE_CHAR("\xEF\xBF\xBD");
- rep7bit_p = FALSE;
- }
- else {
- DEFAULT_REPLACE_CHAR("?");
- rep7bit_p = TRUE;
- }
- cr = ENC_CODERANGE_7BIT;
-
- p = search_nonascii(p, e);
- if (!p) {
- p = e;
- }
- while (p < e) {
- int ret = rb_enc_precise_mbclen(p, e, enc);
- if (MBCLEN_NEEDMORE_P(ret)) {
- break;
- }
- else if (MBCLEN_CHARFOUND_P(ret)) {
- cr = ENC_CODERANGE_VALID;
- p += MBCLEN_CHARFOUND_LEN(ret);
- }
- else if (MBCLEN_INVALID_P(ret)) {
- /*
- * p1~p: valid ascii/multibyte chars
- * p ~e: invalid bytes + unknown bytes
- */
- long clen = rb_enc_mbmaxlen(enc);
- if (NIL_P(buf)) buf = rb_str_buf_new(RSTRING_LEN(str));
- if (p > p1) {
- rb_str_buf_cat(buf, p1, p - p1);
- }
-
- if (e - p < clen) clen = e - p;
- if (clen <= 2) {
- clen = 1;
- }
- else {
- const char *q = p;
- clen--;
- for (; clen > 1; clen--) {
- ret = rb_enc_precise_mbclen(q, q + clen, enc);
- if (MBCLEN_NEEDMORE_P(ret)) break;
- if (MBCLEN_INVALID_P(ret)) continue;
- UNREACHABLE;
- }
- }
- if (rep) {
- rb_str_buf_cat(buf, rep, replen);
- if (!rep7bit_p) cr = ENC_CODERANGE_VALID;
- }
- else {
- repl = rb_yield(rb_enc_str_new(p, clen, enc));
+ int rep7bit_p;
+ if (!replen) {
+ rep = NULL;
+ rep7bit_p = FALSE;
+ }
+ else if (!NIL_P(repl)) {
+ rep = RSTRING_PTR(repl);
+ replen = RSTRING_LEN(repl);
+ rep7bit_p = (ENC_CODERANGE(repl) == ENC_CODERANGE_7BIT);
+ }
+ else if (encidx == rb_utf8_encindex()) {
+ DEFAULT_REPLACE_CHAR("\xEF\xBF\xBD");
+ rep7bit_p = FALSE;
+ }
+ else {
+ DEFAULT_REPLACE_CHAR("?");
+ rep7bit_p = TRUE;
+ }
+ cr = ENC_CODERANGE_7BIT;
+
+ p = search_nonascii(p, e);
+ if (!p) {
+ p = e;
+ }
+ while (p < e) {
+ int ret = rb_enc_precise_mbclen(p, e, enc);
+ if (MBCLEN_NEEDMORE_P(ret)) {
+ break;
+ }
+ else if (MBCLEN_CHARFOUND_P(ret)) {
+ cr = ENC_CODERANGE_VALID;
+ p += MBCLEN_CHARFOUND_LEN(ret);
+ }
+ else if (MBCLEN_INVALID_P(ret)) {
+ /*
+ * p1~p: valid ascii/multibyte chars
+ * p ~e: invalid bytes + unknown bytes
+ */
+ long clen = rb_enc_mbmaxlen(enc);
+ if (NIL_P(buf)) buf = rb_str_buf_new(RSTRING_LEN(str));
+ if (p > p1) {
+ rb_str_buf_cat(buf, p1, p - p1);
+ }
+
+ if (e - p < clen) clen = e - p;
+ if (clen <= 2) {
+ clen = 1;
+ }
+ else {
+ const char *q = p;
+ clen--;
+ for (; clen > 1; clen--) {
+ ret = rb_enc_precise_mbclen(q, q + clen, enc);
+ if (MBCLEN_NEEDMORE_P(ret)) break;
+ if (MBCLEN_INVALID_P(ret)) continue;
+ UNREACHABLE;
+ }
+ }
+ if (rep) {
+ rb_str_buf_cat(buf, rep, replen);
+ if (!rep7bit_p) cr = ENC_CODERANGE_VALID;
+ }
+ else {
+ repl = rb_yield(rb_enc_str_new(p, clen, enc));
str_mod_check(str, sp, slen);
- repl = str_compat_and_valid(repl, enc);
- rb_str_buf_cat(buf, RSTRING_PTR(repl), RSTRING_LEN(repl));
- if (ENC_CODERANGE(repl) == ENC_CODERANGE_VALID)
- cr = ENC_CODERANGE_VALID;
- }
- p += clen;
- p1 = p;
- p = search_nonascii(p, e);
- if (!p) {
- p = e;
- break;
- }
- }
- else {
- UNREACHABLE;
- }
- }
- if (NIL_P(buf)) {
- if (p == e) {
- ENC_CODERANGE_SET(str, cr);
- return Qnil;
- }
- buf = rb_str_buf_new(RSTRING_LEN(str));
- }
- if (p1 < p) {
- rb_str_buf_cat(buf, p1, p - p1);
- }
- if (p < e) {
- if (rep) {
- rb_str_buf_cat(buf, rep, replen);
- if (!rep7bit_p) cr = ENC_CODERANGE_VALID;
- }
- else {
- repl = rb_yield(rb_enc_str_new(p, e-p, enc));
+ repl = str_compat_and_valid(repl, enc);
+ rb_str_buf_cat(buf, RSTRING_PTR(repl), RSTRING_LEN(repl));
+ if (ENC_CODERANGE(repl) == ENC_CODERANGE_VALID)
+ cr = ENC_CODERANGE_VALID;
+ }
+ p += clen;
+ p1 = p;
+ p = search_nonascii(p, e);
+ if (!p) {
+ p = e;
+ break;
+ }
+ }
+ else {
+ UNREACHABLE;
+ }
+ }
+ if (NIL_P(buf)) {
+ if (p == e) {
+ ENC_CODERANGE_SET(str, cr);
+ return Qnil;
+ }
+ buf = rb_str_buf_new(RSTRING_LEN(str));
+ }
+ if (p1 < p) {
+ rb_str_buf_cat(buf, p1, p - p1);
+ }
+ if (p < e) {
+ if (rep) {
+ rb_str_buf_cat(buf, rep, replen);
+ if (!rep7bit_p) cr = ENC_CODERANGE_VALID;
+ }
+ else {
+ repl = rb_yield(rb_enc_str_new(p, e-p, enc));
str_mod_check(str, sp, slen);
- repl = str_compat_and_valid(repl, enc);
- rb_str_buf_cat(buf, RSTRING_PTR(repl), RSTRING_LEN(repl));
- if (ENC_CODERANGE(repl) == ENC_CODERANGE_VALID)
- cr = ENC_CODERANGE_VALID;
- }
- }
+ repl = str_compat_and_valid(repl, enc);
+ rb_str_buf_cat(buf, RSTRING_PTR(repl), RSTRING_LEN(repl));
+ if (ENC_CODERANGE(repl) == ENC_CODERANGE_VALID)
+ cr = ENC_CODERANGE_VALID;
+ }
+ }
}
else {
- /* ASCII incompatible */
- long mbminlen = rb_enc_mbminlen(enc);
- if (!replen) {
- rep = NULL;
- }
- else if (!NIL_P(repl)) {
- rep = RSTRING_PTR(repl);
- replen = RSTRING_LEN(repl);
- }
- else if (encidx == ENCINDEX_UTF_16BE) {
- DEFAULT_REPLACE_CHAR("\xFF\xFD");
- }
- else if (encidx == ENCINDEX_UTF_16LE) {
- DEFAULT_REPLACE_CHAR("\xFD\xFF");
- }
- else if (encidx == ENCINDEX_UTF_32BE) {
- DEFAULT_REPLACE_CHAR("\x00\x00\xFF\xFD");
- }
- else if (encidx == ENCINDEX_UTF_32LE) {
- DEFAULT_REPLACE_CHAR("\xFD\xFF\x00\x00");
- }
- else {
- DEFAULT_REPLACE_CHAR("?");
- }
-
- while (p < e) {
- int ret = rb_enc_precise_mbclen(p, e, enc);
- if (MBCLEN_NEEDMORE_P(ret)) {
- break;
- }
- else if (MBCLEN_CHARFOUND_P(ret)) {
- p += MBCLEN_CHARFOUND_LEN(ret);
- }
- else if (MBCLEN_INVALID_P(ret)) {
- const char *q = p;
- long clen = rb_enc_mbmaxlen(enc);
- if (NIL_P(buf)) buf = rb_str_buf_new(RSTRING_LEN(str));
- if (p > p1) rb_str_buf_cat(buf, p1, p - p1);
-
- if (e - p < clen) clen = e - p;
- if (clen <= mbminlen * 2) {
- clen = mbminlen;
- }
- else {
- clen -= mbminlen;
- for (; clen > mbminlen; clen-=mbminlen) {
- ret = rb_enc_precise_mbclen(q, q + clen, enc);
- if (MBCLEN_NEEDMORE_P(ret)) break;
- if (MBCLEN_INVALID_P(ret)) continue;
- UNREACHABLE;
- }
- }
- if (rep) {
- rb_str_buf_cat(buf, rep, replen);
- }
- else {
- repl = rb_yield(rb_enc_str_new(p, clen, enc));
+ /* ASCII incompatible */
+ long mbminlen = rb_enc_mbminlen(enc);
+ if (!replen) {
+ rep = NULL;
+ }
+ else if (!NIL_P(repl)) {
+ rep = RSTRING_PTR(repl);
+ replen = RSTRING_LEN(repl);
+ }
+ else if (encidx == ENCINDEX_UTF_16BE) {
+ DEFAULT_REPLACE_CHAR("\xFF\xFD");
+ }
+ else if (encidx == ENCINDEX_UTF_16LE) {
+ DEFAULT_REPLACE_CHAR("\xFD\xFF");
+ }
+ else if (encidx == ENCINDEX_UTF_32BE) {
+ DEFAULT_REPLACE_CHAR("\x00\x00\xFF\xFD");
+ }
+ else if (encidx == ENCINDEX_UTF_32LE) {
+ DEFAULT_REPLACE_CHAR("\xFD\xFF\x00\x00");
+ }
+ else {
+ DEFAULT_REPLACE_CHAR("?");
+ }
+
+ while (p < e) {
+ int ret = rb_enc_precise_mbclen(p, e, enc);
+ if (MBCLEN_NEEDMORE_P(ret)) {
+ break;
+ }
+ else if (MBCLEN_CHARFOUND_P(ret)) {
+ p += MBCLEN_CHARFOUND_LEN(ret);
+ }
+ else if (MBCLEN_INVALID_P(ret)) {
+ const char *q = p;
+ long clen = rb_enc_mbmaxlen(enc);
+ if (NIL_P(buf)) buf = rb_str_buf_new(RSTRING_LEN(str));
+ if (p > p1) rb_str_buf_cat(buf, p1, p - p1);
+
+ if (e - p < clen) clen = e - p;
+ if (clen <= mbminlen * 2) {
+ clen = mbminlen;
+ }
+ else {
+ clen -= mbminlen;
+ for (; clen > mbminlen; clen-=mbminlen) {
+ ret = rb_enc_precise_mbclen(q, q + clen, enc);
+ if (MBCLEN_NEEDMORE_P(ret)) break;
+ if (MBCLEN_INVALID_P(ret)) continue;
+ UNREACHABLE;
+ }
+ }
+ if (rep) {
+ rb_str_buf_cat(buf, rep, replen);
+ }
+ else {
+ repl = rb_yield(rb_enc_str_new(p, clen, enc));
str_mod_check(str, sp, slen);
- repl = str_compat_and_valid(repl, enc);
- rb_str_buf_cat(buf, RSTRING_PTR(repl), RSTRING_LEN(repl));
- }
- p += clen;
- p1 = p;
- }
- else {
- UNREACHABLE;
- }
- }
- if (NIL_P(buf)) {
- if (p == e) {
- ENC_CODERANGE_SET(str, ENC_CODERANGE_VALID);
- return Qnil;
- }
- buf = rb_str_buf_new(RSTRING_LEN(str));
- }
- if (p1 < p) {
- rb_str_buf_cat(buf, p1, p - p1);
- }
- if (p < e) {
- if (rep) {
- rb_str_buf_cat(buf, rep, replen);
- }
- else {
- repl = rb_yield(rb_enc_str_new(p, e-p, enc));
+ repl = str_compat_and_valid(repl, enc);
+ rb_str_buf_cat(buf, RSTRING_PTR(repl), RSTRING_LEN(repl));
+ }
+ p += clen;
+ p1 = p;
+ }
+ else {
+ UNREACHABLE;
+ }
+ }
+ if (NIL_P(buf)) {
+ if (p == e) {
+ ENC_CODERANGE_SET(str, ENC_CODERANGE_VALID);
+ return Qnil;
+ }
+ buf = rb_str_buf_new(RSTRING_LEN(str));
+ }
+ if (p1 < p) {
+ rb_str_buf_cat(buf, p1, p - p1);
+ }
+ if (p < e) {
+ if (rep) {
+ rb_str_buf_cat(buf, rep, replen);
+ }
+ else {
+ repl = rb_yield(rb_enc_str_new(p, e-p, enc));
str_mod_check(str, sp, slen);
- repl = str_compat_and_valid(repl, enc);
- rb_str_buf_cat(buf, RSTRING_PTR(repl), RSTRING_LEN(repl));
- }
- }
- cr = ENC_CODERANGE_VALID;
+ repl = str_compat_and_valid(repl, enc);
+ rb_str_buf_cat(buf, RSTRING_PTR(repl), RSTRING_LEN(repl));
+ }
+ }
+ cr = ENC_CODERANGE_VALID;
}
ENCODING_CODERANGE_SET(buf, rb_enc_to_index(enc), cr);
return buf;
@@ -11023,17 +11388,11 @@ enc_str_scrub(rb_encoding *enc, VALUE str, VALUE repl, int cr)
/*
* call-seq:
- * str.scrub -> new_str
- * str.scrub(repl) -> new_str
- * str.scrub{|bytes|} -> new_str
+ * scrub(replacement_string = default_replacement) -> new_string
+ * scrub{|bytes| ... } -> new_string
*
- * If the string is invalid byte sequence then replace invalid bytes with given replacement
- * character, else returns self.
- * If block is given, replace invalid bytes with returned value of the block.
+ * :include: doc/string/scrub.rdoc
*
- * "abc\u3042\x81".scrub #=> "abc\u3042\uFFFD"
- * "abc\u3042\x81".scrub("*") #=> "abc\u3042*"
- * "abc\u3042\xE3\x80".scrub{|bytes| '<'+bytes.unpack1('H*')+'>' } #=> "abc\u3042<e380>"
*/
static VALUE
str_scrub(int argc, VALUE *argv, VALUE str)
@@ -11045,17 +11404,12 @@ str_scrub(int argc, VALUE *argv, VALUE str)
/*
* call-seq:
- * str.scrub! -> str
- * str.scrub!(repl) -> str
- * str.scrub!{|bytes|} -> str
+ * scrub! -> self
+ * scrub!(replacement_string = default_replacement) -> self
+ * scrub!{|bytes| ... } -> self
*
- * If the string is invalid byte sequence then replace invalid bytes with given replacement
- * character, else returns self.
- * If block is given, replace invalid bytes with returned value of the block.
+ * Like String#scrub, except that any replacements are made in +self+.
*
- * "abc\u3042\x81".scrub! #=> "abc\u3042\uFFFD"
- * "abc\u3042\x81".scrub!("*") #=> "abc\u3042*"
- * "abc\u3042\xE3\x80".scrub!{|bytes| '<'+bytes.unpack1('H*')+'>' } #=> "abc\u3042<e380>"
*/
static VALUE
str_scrub_bang(int argc, VALUE *argv, VALUE str)
@@ -11077,8 +11431,8 @@ unicode_normalize_common(int argc, VALUE *argv, VALUE str, ID id)
VALUE argv2[2];
if (!UnicodeNormalizeRequired) {
- rb_require("unicode_normalize/normalize.rb");
- UnicodeNormalizeRequired = 1;
+ rb_require("unicode_normalize/normalize.rb");
+ UnicodeNormalizeRequired = 1;
}
argv2[0] = str;
if (rb_check_arity(argc, 0, 1)) argv2[1] = argv[0];
@@ -11087,25 +11441,36 @@ unicode_normalize_common(int argc, VALUE *argv, VALUE str, ID id)
/*
* call-seq:
- * str.unicode_normalize(form=:nfc)
+ * unicode_normalize(form = :nfc) -> string
+ *
+ * Returns a copy of +self+ with
+ * {Unicode normalization}[https://unicode.org/reports/tr15] applied.
+ *
+ * Argument +form+ must be one of the following symbols
+ * (see {Unicode normalization forms}[https://unicode.org/reports/tr15/#Norm_Forms]):
+ *
+ * - +:nfc+: Canonical decomposition, followed by canonical composition.
+ * - +:nfd+: Canonical decomposition.
+ * - +:nfkc+: Compatibility decomposition, followed by canonical composition.
+ * - +:nfkd+: Compatibility decomposition.
*
- * Unicode Normalization---Returns a normalized form of +str+,
- * using Unicode normalizations NFC, NFD, NFKC, or NFKD.
- * The normalization form used is determined by +form+, which can
- * be any of the four values +:nfc+, +:nfd+, +:nfkc+, or +:nfkd+.
- * The default is +:nfc+.
+ * The encoding of +self+ must be one of:
+ *
+ * - Encoding::UTF_8
+ * - Encoding::UTF_16BE
+ * - Encoding::UTF_16LE
+ * - Encoding::UTF_32BE
+ * - Encoding::UTF_32LE
+ * - Encoding::GB18030
+ * - Encoding::UCS_2BE
+ * - Encoding::UCS_4BE
+ *
+ * Examples:
*
- * If the string is not in a Unicode Encoding, then an Exception is raised.
- * In this context, 'Unicode Encoding' means any of UTF-8, UTF-16BE/LE,
- * and UTF-32BE/LE, as well as GB18030, UCS_2BE, and UCS_4BE.
- * Anything other than UTF-8 is implemented by converting to UTF-8,
- * which makes it slower than UTF-8.
+ * "a\u0300".unicode_normalize # => "a"
+ * "\u00E0".unicode_normalize(:nfd) # => "a "
*
- * "a\u0300".unicode_normalize #=> "\u00E0"
- * "a\u0300".unicode_normalize(:nfc) #=> "\u00E0"
- * "\u00E0".unicode_normalize(:nfd) #=> "a\u0300"
- * "\xE0".force_encoding('ISO-8859-1').unicode_normalize(:nfd)
- * #=> Encoding::CompatibilityError raised
+ * Related: String#unicode_normalize!, String#unicode_normalized?.
*/
static VALUE
rb_str_unicode_normalize(int argc, VALUE *argv, VALUE str)
@@ -11115,10 +11480,13 @@ rb_str_unicode_normalize(int argc, VALUE *argv, VALUE str)
/*
* call-seq:
- * str.unicode_normalize!(form=:nfc)
+ * unicode_normalize!(form = :nfc) -> self
+ *
+ * Like String#unicode_normalize, except that the normalization
+ * is performed on +self+.
+ *
+ * Related String#unicode_normalized?.
*
- * Destructive version of String#unicode_normalize, doing Unicode
- * normalization in place.
*/
static VALUE
rb_str_unicode_normalize_bang(int argc, VALUE *argv, VALUE str)
@@ -11127,21 +11495,27 @@ rb_str_unicode_normalize_bang(int argc, VALUE *argv, VALUE str)
}
/* call-seq:
- * str.unicode_normalized?(form=:nfc)
+ * unicode_normalized?(form = :nfc) -> true or false
+ *
+ * Returns +true+ if +self+ is in the given +form+ of Unicode normalization,
+ * +false+ otherwise.
+ * The +form+ must be one of +:nfc+, +:nfd+, +:nfkc+, or +:nfkd+.
+ *
+ * Examples:
+ *
+ * "a\u0300".unicode_normalized? # => false
+ * "a\u0300".unicode_normalized?(:nfd) # => true
+ * "\u00E0".unicode_normalized? # => true
+ * "\u00E0".unicode_normalized?(:nfd) # => false
*
- * Checks whether +str+ is in Unicode normalization form +form+,
- * which can be any of the four values +:nfc+, +:nfd+, +:nfkc+, or +:nfkd+.
- * The default is +:nfc+.
*
- * If the string is not in a Unicode Encoding, then an Exception is raised.
- * For details, see String#unicode_normalize.
+ * Raises an exception if +self+ is not in a Unicode encoding:
+ *
+ * s = "\xE0".force_encoding('ISO-8859-1')
+ * s.unicode_normalized? # Raises Encoding::CompatibilityError.
+ *
+ * Related: String#unicode_normalize, String#unicode_normalize!.
*
- * "a\u0300".unicode_normalized? #=> false
- * "a\u0300".unicode_normalized?(:nfd) #=> true
- * "\u00E0".unicode_normalized? #=> true
- * "\u00E0".unicode_normalized?(:nfd) #=> false
- * "\xE0".force_encoding('ISO-8859-1').unicode_normalized?
- * #=> Encoding::CompatibilityError raised
*/
static VALUE
rb_str_unicode_normalized_p(int argc, VALUE *argv, VALUE str)
@@ -11152,15 +11526,18 @@ rb_str_unicode_normalized_p(int argc, VALUE *argv, VALUE str)
/**********************************************************************
* Document-class: Symbol
*
- * Symbol objects represent named identifiers inside the Ruby interpreter. They
- * are generated using the <code>:name</code> and
- * <code>:"string"</code> literals syntax, and by the various
- * <code>to_sym</code> methods. The same Symbol object will be
- * created for a given name or string for the duration of a program's
- * execution, regardless of the context or meaning of that name. Thus
- * if <code>Fred</code> is a constant in one context, a method in
- * another, and a class in a third, the Symbol <code>:Fred</code>
- * will be the same object in all three contexts.
+ * A +Symbol+ object represents a named identifier inside the Ruby interpreter.
+ *
+ * You can create a +Symbol+ object explicitly with:
+ *
+ * - A {symbol literal}[rdoc-ref:syntax/literals.rdoc@Symbol+Literals].
+ *
+ * The same +Symbol+ object will be
+ * created for a given name or string for the duration of a program's
+ * execution, regardless of the context or meaning of that name. Thus
+ * if <code>Fred</code> is a constant in one context, a method in
+ * another, and a class in a third, the +Symbol+ <code>:Fred</code>
+ * will be the same object in all three contexts.
*
* module One
* class Fred
@@ -11202,19 +11579,77 @@ rb_str_unicode_normalized_p(int argc, VALUE *argv, VALUE str)
* local_variables
* # => [:seven]
*
- * Symbol objects are different from String objects in that
- * Symbol objects represent identifiers, while String objects
- * represent text or data.
+ * A +Symbol+ object differs from a String object in that
+ * a +Symbol+ object represents an identifier, while a String object
+ * represents text or data.
+ *
+ * == What's Here
+ *
+ * First, what's elsewhere. \Class +Symbol+:
+ *
+ * - Inherits from {class Object}[rdoc-ref:Object@What-27s+Here].
+ * - Includes {module Comparable}[rdoc-ref:Comparable@What-27s+Here].
+ *
+ * Here, class +Symbol+ provides methods that are useful for:
+ *
+ * - {Querying}[rdoc-ref:Symbol@Methods+for+Querying]
+ * - {Comparing}[rdoc-ref:Symbol@Methods+for+Comparing]
+ * - {Converting}[rdoc-ref:Symbol@Methods+for+Converting]
+ *
+ * === Methods for Querying
+ *
+ * - ::all_symbols: Returns an array of the symbols currently in Ruby's symbol table.
+ * - #=~: Returns the index of the first substring in symbol that matches a
+ * given Regexp or other object; returns +nil+ if no match is found.
+ * - #[], #slice : Returns a substring of symbol
+ * determined by a given index, start/length, or range, or string.
+ * - #empty?: Returns +true+ if +self.length+ is zero; +false+ otherwise.
+ * - #encoding: Returns the Encoding object that represents the encoding
+ * of symbol.
+ * - #end_with?: Returns +true+ if symbol ends with
+ * any of the given strings.
+ * - #match: Returns a MatchData object if symbol
+ * matches a given Regexp; +nil+ otherwise.
+ * - #match?: Returns +true+ if symbol
+ * matches a given Regexp; +false+ otherwise.
+ * - #length, #size: Returns the number of characters in symbol.
+ * - #start_with?: Returns +true+ if symbol starts with
+ * any of the given strings.
+ *
+ * === Methods for Comparing
+ *
+ * - #<=>: Returns -1, 0, or 1 as a given symbol is smaller than, equal to,
+ * or larger than symbol.
+ * - #==, #===: Returns +true+ if a given symbol has the same content and
+ * encoding.
+ * - #casecmp: Ignoring case, returns -1, 0, or 1 as a given
+ * symbol is smaller than, equal to, or larger than symbol.
+ * - #casecmp?: Returns +true+ if symbol is equal to a given symbol
+ * after Unicode case folding; +false+ otherwise.
+ *
+ * === Methods for Converting
+ *
+ * - #capitalize: Returns symbol with the first character upcased
+ * and all other characters downcased.
+ * - #downcase: Returns symbol with all characters downcased.
+ * - #inspect: Returns the string representation of +self+ as a symbol literal.
+ * - #name: Returns the frozen string corresponding to symbol.
+ * - #succ, #next: Returns the symbol that is the successor to symbol.
+ * - #swapcase: Returns symbol with all upcase characters downcased
+ * and all downcase characters upcased.
+ * - #to_proc: Returns a Proc object which responds to the method named by symbol.
+ * - #to_s, #id2name: Returns the string corresponding to +self+.
+ * - #to_sym, #intern: Returns +self+.
+ * - #upcase: Returns symbol with all characters upcased.
*
*/
/*
* call-seq:
- * sym == obj -> true or false
+ * symbol == object -> true or false
*
- * Equality---If <i>sym</i> and <i>obj</i> are exactly the same
- * symbol, returns <code>true</code>.
+ * Returns +true+ if +object+ is the same object as +self+, +false+ otherwise.
*/
#define sym_equal rb_obj_equal
@@ -11223,14 +11658,14 @@ static int
sym_printable(const char *s, const char *send, rb_encoding *enc)
{
while (s < send) {
- int n;
- int c = rb_enc_precise_mbclen(s, send, enc);
+ int n;
+ int c = rb_enc_precise_mbclen(s, send, enc);
- if (!MBCLEN_CHARFOUND_P(c)) return FALSE;
- n = MBCLEN_CHARFOUND_LEN(c);
- c = rb_enc_mbc_to_codepoint(s, send, enc);
- if (!rb_enc_isprint(c, enc)) return FALSE;
- s += n;
+ if (!MBCLEN_CHARFOUND_P(c)) return FALSE;
+ n = MBCLEN_CHARFOUND_LEN(c);
+ c = rb_enc_mbc_to_codepoint(s, send, enc);
+ if (!rb_enc_isprint(c, enc)) return FALSE;
+ s += n;
}
return TRUE;
}
@@ -11248,8 +11683,8 @@ rb_str_symname_p(VALUE sym)
ptr = RSTRING_PTR(sym);
len = RSTRING_LEN(sym);
if ((resenc != enc && !rb_str_is_ascii_only_p(sym)) || len != (long)strlen(ptr) ||
- !rb_enc_symname2_p(ptr, len, enc) || !sym_printable(ptr, ptr + len, enc)) {
- return FALSE;
+ !rb_enc_symname2_p(ptr, len, enc) || !sym_printable(ptr, ptr + len, enc)) {
+ return FALSE;
}
return TRUE;
}
@@ -11269,29 +11704,32 @@ rb_str_quote_unprintable(VALUE str)
ptr = RSTRING_PTR(str);
len = RSTRING_LEN(str);
if ((resenc != enc && !rb_str_is_ascii_only_p(str)) ||
- !sym_printable(ptr, ptr + len, enc)) {
- return rb_str_escape(str);
+ !sym_printable(ptr, ptr + len, enc)) {
+ return rb_str_escape(str);
}
return str;
}
-MJIT_FUNC_EXPORTED VALUE
+VALUE
rb_id_quote_unprintable(ID id)
{
VALUE str = rb_id2str(id);
if (!rb_str_symname_p(str)) {
- return rb_str_escape(str);
+ return rb_str_escape(str);
}
return str;
}
/*
* call-seq:
- * sym.inspect -> string
+ * inspect -> string
+ *
+ * Returns a string representation of +self+ (including the leading colon):
*
- * Returns the representation of <i>sym</i> as a symbol literal.
+ * :foo.inspect # => ":foo"
+ *
+ * Related: Symbol#to_s, Symbol#name.
*
- * :fred.inspect #=> ":fred"
*/
static VALUE
@@ -11303,116 +11741,57 @@ sym_inspect(VALUE sym)
char *dest;
if (!rb_str_symname_p(str)) {
- str = rb_str_inspect(str);
- len = RSTRING_LEN(str);
- rb_str_resize(str, len + 1);
- dest = RSTRING_PTR(str);
- memmove(dest + 1, dest, len);
+ str = rb_str_inspect(str);
+ len = RSTRING_LEN(str);
+ rb_str_resize(str, len + 1);
+ dest = RSTRING_PTR(str);
+ memmove(dest + 1, dest, len);
}
else {
- rb_encoding *enc = STR_ENC_GET(str);
- RSTRING_GETMEM(str, ptr, len);
- str = rb_enc_str_new(0, len + 1, enc);
- dest = RSTRING_PTR(str);
- memcpy(dest + 1, ptr, len);
+ rb_encoding *enc = STR_ENC_GET(str);
+ VALUE orig_str = str;
+
+ len = RSTRING_LEN(orig_str);
+ str = rb_enc_str_new(0, len + 1, enc);
+
+ // Get data pointer after allocation
+ ptr = RSTRING_PTR(orig_str);
+ dest = RSTRING_PTR(str);
+ memcpy(dest + 1, ptr, len);
+
+ RB_GC_GUARD(orig_str);
}
dest[0] = ':';
return str;
}
-#if 0 /* for RDoc */
-/*
- * call-seq:
- * sym.name -> string
- *
- * Returns the name or string corresponding to <i>sym</i>. Unlike #to_s, the
- * returned string is frozen.
- *
- * :fred.name #=> "fred"
- * :fred.name.frozen? #=> true
- * :fred.to_s #=> "fred"
- * :fred.to_s.frozen? #=> false
- */
-VALUE
-rb_sym2str(VALUE sym)
-{
-
-}
-#endif
-
-
-/*
- * call-seq:
- * sym.id2name -> string
- * sym.to_s -> string
- *
- * Returns the name or string corresponding to <i>sym</i>.
- *
- * :fred.id2name #=> "fred"
- * :ginger.to_s #=> "ginger"
- *
- * Note that this string is not frozen (unlike the symbol itself).
- * To get a frozen string, use #name.
- */
-
-
VALUE
rb_sym_to_s(VALUE sym)
{
return str_new_shared(rb_cString, rb_sym2str(sym));
}
-
-/*
- * call-seq:
- * sym.to_sym -> sym
- * sym.intern -> sym
- *
- * In general, <code>to_sym</code> returns the Symbol corresponding
- * to an object. As <i>sym</i> is already a symbol, <code>self</code> is returned
- * in this case.
- */
-
-static VALUE
-sym_to_sym(VALUE sym)
-{
- return sym;
-}
-
-MJIT_FUNC_EXPORTED VALUE
+VALUE
rb_sym_proc_call(ID mid, int argc, const VALUE *argv, int kw_splat, VALUE passed_proc)
{
VALUE obj;
if (argc < 1) {
- rb_raise(rb_eArgError, "no receiver given");
+ rb_raise(rb_eArgError, "no receiver given");
}
obj = argv[0];
return rb_funcall_with_block_kw(obj, mid, argc - 1, argv + 1, passed_proc, kw_splat);
}
-#if 0
/*
- * call-seq:
- * sym.to_proc
+ * call-seq:
+ * succ
*
- * Returns a _Proc_ object which responds to the given method by _sym_.
+ * Equivalent to <tt>self.to_s.succ.to_sym</tt>:
*
- * (1..3).collect(&:to_s) #=> ["1", "2", "3"]
- */
-
-VALUE
-rb_sym_to_proc(VALUE sym)
-{
-}
-#endif
-
-/*
- * call-seq:
+ * :foo.succ # => :fop
*
- * sym.succ
- *
- * Same as <code>sym.to_s.succ.intern</code>.
+ * Related: String#succ.
*/
static VALUE
@@ -11422,91 +11801,74 @@ sym_succ(VALUE sym)
}
/*
- * call-seq:
+ * call-seq:
+ * symbol <=> object -> -1, 0, +1, or nil
+ *
+ * If +object+ is a symbol,
+ * returns the equivalent of <tt>symbol.to_s <=> object.to_s</tt>:
*
- * symbol <=> other_symbol -> -1, 0, +1, or nil
+ * :bar <=> :foo # => -1
+ * :foo <=> :foo # => 0
+ * :foo <=> :bar # => 1
*
- * Compares +symbol+ with +other_symbol+ after calling #to_s on each of the
- * symbols. Returns -1, 0, +1, or +nil+ depending on whether +symbol+ is
- * less than, equal to, or greater than +other_symbol+.
+ * Otherwise, returns +nil+:
*
- * +nil+ is returned if the two values are incomparable.
+ * :foo <=> 'bar' # => nil
*
- * See String#<=> for more information.
+ * Related: String#<=>.
*/
static VALUE
sym_cmp(VALUE sym, VALUE other)
{
if (!SYMBOL_P(other)) {
- return Qnil;
+ return Qnil;
}
return rb_str_cmp_m(rb_sym2str(sym), rb_sym2str(other));
}
/*
- * call-seq:
- * sym.casecmp(other_symbol) -> -1, 0, +1, or nil
- *
- * Case-insensitive version of Symbol#<=>.
- * Currently, case-insensitivity only works on characters A-Z/a-z,
- * not all of Unicode. This is different from Symbol#casecmp?.
- *
- * :aBcDeF.casecmp(:abcde) #=> 1
- * :aBcDeF.casecmp(:abcdef) #=> 0
- * :aBcDeF.casecmp(:abcdefg) #=> -1
- * :abcdef.casecmp(:ABCDEF) #=> 0
+ * call-seq:
+ * casecmp(object) -> -1, 0, 1, or nil
*
- * +nil+ is returned if the two symbols have incompatible encodings,
- * or if +other_symbol+ is not a symbol.
+ * :include: doc/symbol/casecmp.rdoc
*
- * :foo.casecmp(2) #=> nil
- * "\u{e4 f6 fc}".encode("ISO-8859-1").to_sym.casecmp(:"\u{c4 d6 dc}") #=> nil
*/
static VALUE
sym_casecmp(VALUE sym, VALUE other)
{
if (!SYMBOL_P(other)) {
- return Qnil;
+ return Qnil;
}
return str_casecmp(rb_sym2str(sym), rb_sym2str(other));
}
/*
- * call-seq:
- * sym.casecmp?(other_symbol) -> true, false, or nil
- *
- * Returns +true+ if +sym+ and +other_symbol+ are equal after
- * Unicode case folding, +false+ if they are not equal.
- *
- * :aBcDeF.casecmp?(:abcde) #=> false
- * :aBcDeF.casecmp?(:abcdef) #=> true
- * :aBcDeF.casecmp?(:abcdefg) #=> false
- * :abcdef.casecmp?(:ABCDEF) #=> true
- * :"\u{e4 f6 fc}".casecmp?(:"\u{c4 d6 dc}") #=> true
+ * call-seq:
+ * casecmp?(object) -> true, false, or nil
*
- * +nil+ is returned if the two symbols have incompatible encodings,
- * or if +other_symbol+ is not a symbol.
+ * :include: doc/symbol/casecmp_p.rdoc
*
- * :foo.casecmp?(2) #=> nil
- * "\u{e4 f6 fc}".encode("ISO-8859-1").to_sym.casecmp?(:"\u{c4 d6 dc}") #=> nil
*/
static VALUE
sym_casecmp_p(VALUE sym, VALUE other)
{
if (!SYMBOL_P(other)) {
- return Qnil;
+ return Qnil;
}
return str_casecmp_p(rb_sym2str(sym), rb_sym2str(other));
}
/*
- * call-seq:
- * sym =~ obj -> integer or nil
+ * call-seq:
+ * symbol =~ object -> integer or nil
+ *
+ * Equivalent to <tt>symbol.to_s =~ object</tt>,
+ * including possible updates to global variables;
+ * see String#=~.
*
- * Returns <code>sym.to_s =~ obj</code>.
*/
static VALUE
@@ -11516,11 +11878,14 @@ sym_match(VALUE sym, VALUE other)
}
/*
- * call-seq:
- * sym.match(pattern) -> matchdata or nil
- * sym.match(pattern, pos) -> matchdata or nil
+ * call-seq:
+ * match(pattern, offset = 0) -> matchdata or nil
+ * match(pattern, offset = 0) {|matchdata| } -> object
+ *
+ * Equivalent to <tt>self.to_s.match</tt>,
+ * including possible updates to global variables;
+ * see String#match.
*
- * Returns <code>sym.to_s.match</code>.
*/
static VALUE
@@ -11530,11 +11895,12 @@ sym_match_m(int argc, VALUE *argv, VALUE sym)
}
/*
- * call-seq:
- * sym.match?(pattern) -> true or false
- * sym.match?(pattern, pos) -> true or false
+ * call-seq:
+ * match?(pattern, offset) -> true or false
+ *
+ * Equivalent to <tt>sym.to_s.match?</tt>;
+ * see String#match.
*
- * Returns <code>sym.to_s.match?</code>.
*/
static VALUE
@@ -11544,13 +11910,15 @@ sym_match_m_p(int argc, VALUE *argv, VALUE sym)
}
/*
- * call-seq:
- * sym[idx] -> char
- * sym[b, n] -> string
- * sym.slice(idx) -> char
- * sym.slice(b, n) -> string
+ * call-seq:
+ * symbol[index] -> string or nil
+ * symbol[start, length] -> string or nil
+ * symbol[range] -> string or nil
+ * symbol[regexp, capture = 0] -> string or nil
+ * symbol[substring] -> string or nil
+ *
+ * Equivalent to <tt>symbol.to_s[]</tt>; see String#[].
*
- * Returns <code>sym.to_s[]</code>.
*/
static VALUE
@@ -11560,11 +11928,10 @@ sym_aref(int argc, VALUE *argv, VALUE sym)
}
/*
- * call-seq:
- * sym.length -> integer
- * sym.size -> integer
+ * call-seq:
+ * length -> integer
*
- * Same as <code>sym.to_s.length</code>.
+ * Equivalent to <tt>self.to_s.length</tt>; see String#length.
*/
static VALUE
@@ -11574,10 +11941,11 @@ sym_length(VALUE sym)
}
/*
- * call-seq:
- * sym.empty? -> true or false
+ * call-seq:
+ * empty? -> true or false
+ *
+ * Returns +true+ if +self+ is <tt>:''</tt>, +false+ otherwise.
*
- * Returns whether _sym_ is :"" or not.
*/
static VALUE
@@ -11587,11 +11955,13 @@ sym_empty(VALUE sym)
}
/*
- * call-seq:
- * sym.upcase -> symbol
- * sym.upcase([options]) -> symbol
+ * call-seq:
+ * upcase(*options) -> symbol
+ *
+ * Equivalent to <tt>sym.to_s.upcase.to_sym</tt>.
+ *
+ * See String#upcase.
*
- * Same as <code>sym.to_s.upcase.intern</code>.
*/
static VALUE
@@ -11601,11 +11971,15 @@ sym_upcase(int argc, VALUE *argv, VALUE sym)
}
/*
- * call-seq:
- * sym.downcase -> symbol
- * sym.downcase([options]) -> symbol
+ * call-seq:
+ * downcase(*options) -> symbol
+ *
+ * Equivalent to <tt>sym.to_s.downcase.to_sym</tt>.
+ *
+ * See String#downcase.
+ *
+ * Related: Symbol#upcase.
*
- * Same as <code>sym.to_s.downcase.intern</code>.
*/
static VALUE
@@ -11615,11 +11989,13 @@ sym_downcase(int argc, VALUE *argv, VALUE sym)
}
/*
- * call-seq:
- * sym.capitalize -> symbol
- * sym.capitalize([options]) -> symbol
+ * call-seq:
+ * capitalize(*options) -> symbol
+ *
+ * Equivalent to <tt>sym.to_s.capitalize.to_sym</tt>.
+ *
+ * See String#capitalize.
*
- * Same as <code>sym.to_s.capitalize.intern</code>.
*/
static VALUE
@@ -11629,11 +12005,13 @@ sym_capitalize(int argc, VALUE *argv, VALUE sym)
}
/*
- * call-seq:
- * sym.swapcase -> symbol
- * sym.swapcase([options]) -> symbol
+ * call-seq:
+ * swapcase(*options) -> symbol
+ *
+ * Equivalent to <tt>sym.to_s.swapcase.to_sym</tt>.
+ *
+ * See String#swapcase.
*
- * Same as <code>sym.to_s.swapcase.intern</code>.
*/
static VALUE
@@ -11644,17 +12022,10 @@ sym_swapcase(int argc, VALUE *argv, VALUE sym)
/*
* call-seq:
- * sym.start_with?([prefixes]+) -> true or false
- *
- * Returns true if +sym+ starts with one of the +prefixes+ given.
- * Each of the +prefixes+ should be a String or a Regexp.
+ * start_with?(*string_or_regexp) -> true or false
*
- * :hello.start_with?("hell") #=> true
- * :hello.start_with?(/H/i) #=> true
+ * Equivalent to <tt>self.to_s.start_with?</tt>; see String#start_with?.
*
- * # returns true if one of the prefixes matches.
- * :hello.start_with?("heaven", "hell") #=> true
- * :hello.start_with?("heaven", "paradise") #=> false
*/
static VALUE
@@ -11665,15 +12036,11 @@ sym_start_with(int argc, VALUE *argv, VALUE sym)
/*
* call-seq:
- * sym.end_with?([suffixes]+) -> true or false
+ * end_with?(*strings) -> true or false
*
- * Returns true if +sym+ ends with one of the +suffixes+ given.
*
- * :hello.end_with?("ello") #=> true
+ * Equivalent to <tt>self.to_s.end_with?</tt>; see String#end_with?.
*
- * # returns true if one of the +suffixes+ matches.
- * :hello.end_with?("heaven", "ello") #=> true
- * :hello.end_with?("heaven", "paradise") #=> false
*/
static VALUE
@@ -11683,10 +12050,11 @@ sym_end_with(int argc, VALUE *argv, VALUE sym)
}
/*
- * call-seq:
- * sym.encoding -> encoding
+ * call-seq:
+ * encoding -> encoding
+ *
+ * Equivalent to <tt>self.to_s.encoding</tt>; see String#encoding.
*
- * Returns the Encoding object that represents the encoding of _sym_.
*/
static VALUE
@@ -11699,12 +12067,12 @@ static VALUE
string_for_symbol(VALUE name)
{
if (!RB_TYPE_P(name, T_STRING)) {
- VALUE tmp = rb_check_string_type(name);
- if (NIL_P(tmp)) {
- rb_raise(rb_eTypeError, "%+"PRIsVALUE" is not a symbol",
- name);
- }
- name = tmp;
+ VALUE tmp = rb_check_string_type(name);
+ if (NIL_P(tmp)) {
+ rb_raise(rb_eTypeError, "%+"PRIsVALUE" is not a symbol",
+ name);
+ }
+ name = tmp;
}
return name;
}
@@ -11713,7 +12081,7 @@ ID
rb_to_id(VALUE name)
{
if (SYMBOL_P(name)) {
- return SYM2ID(name);
+ return SYM2ID(name);
}
name = string_for_symbol(name);
return rb_intern_str(name);
@@ -11723,7 +12091,7 @@ VALUE
rb_to_symbol(VALUE name)
{
if (SYMBOL_P(name)) {
- return name;
+ return name;
}
name = string_for_symbol(name);
return rb_str_intern(name);
@@ -11731,18 +12099,13 @@ rb_to_symbol(VALUE name)
/*
* call-seq:
- * Symbol.all_symbols => array
+ * Symbol.all_symbols -> array_of_symbols
+ *
+ * Returns an array of all symbols currently in Ruby's symbol table:
*
- * Returns an array of all the symbols currently in Ruby's symbol
- * table.
+ * Symbol.all_symbols.size # => 9334
+ * Symbol.all_symbols.take(3) # => [:!, :"\"", :"#"]
*
- * Symbol.all_symbols.size #=> 903
- * Symbol.all_symbols[1,20] #=> [:floor, :ARGV, :Binding, :symlink,
- * :chown, :EOFError, :$;, :String,
- * :LOCK_SH, :"setuid?", :$<,
- * :default_proc, :compact, :extend,
- * :Tms, :getwd, :$=, :ThreadGroup,
- * :wait2, :$>]
*/
static VALUE
@@ -11773,7 +12136,7 @@ rb_interned_str_cstr(const char *ptr)
VALUE
rb_enc_interned_str(const char *ptr, long len, rb_encoding *enc)
{
- if (UNLIKELY(rb_enc_autoload_p(enc))) {
+ if (enc != NULL && UNLIKELY(rb_enc_autoload_p(enc))) {
rb_enc_autoload(enc);
}
@@ -11787,398 +12150,15 @@ rb_enc_interned_str_cstr(const char *ptr, rb_encoding *enc)
return rb_enc_interned_str(ptr, strlen(ptr), enc);
}
-/*
- * A \String object has an arbitrary sequence of bytes,
- * typically representing text or binary data.
- * A \String object may be created using String::new or as literals.
- *
- * String objects differ from Symbol objects in that Symbol objects are
- * designed to be used as identifiers, instead of text or data.
- *
- * Some \String methods modify +self+.
- * Typically, a method whose name ends with <tt>!</tt> modifies +self+
- * and returns +self+;
- * often a similarly named method (without the <tt>!</tt>)
- * returns a new string.
- *
- * In general, if there exist both bang and non-bang version of method,
- * the bang! mutates and the non-bang! does not.
- * However, a method without a bang can also mutate, such as String#replace.
- *
- * == Substitution Methods
- *
- * These methods perform substitutions:
- *
- * - String#sub: One substitution (or none); returns a new string.
- * - String#sub!: One substitution (or none); returns +self+.
- * - String#gsub: Zero or more substitutions; returns a new string.
- * - String#gsub!: Zero or more substitutions; returns +self+.
- *
- * Each of these methods takes:
- *
- * - A first argument, +pattern+ (string or regexp),
- * that specifies the substring(s) to be replaced.
- *
- * - Either of these:
- *
- * - A second argument, +replacement+ (string or hash),
- * that determines the replacing string.
- * - A block that will determine the replacing string.
- *
- * The examples in this section mostly use methods String#sub and String#gsub;
- * the principles illustrated apply to all four substitution methods.
- *
- * <b>Argument +pattern+</b>
- *
- * Argument +pattern+ is commonly a regular expression:
- *
- * s = 'hello'
- * s.sub(/[aeiou]/, '*') # => "h*llo"
- * s.gsub(/[aeiou]/, '*') # => "h*ll*"
- * s.gsub(/[aeiou]/, '') # => "hll"
- * s.sub(/ell/, 'al') # => "halo"
- * s.gsub(/xyzzy/, '*') # => "hello"
- * 'THX1138'.gsub(/\d+/, '00') # => "THX00"
- *
- * When +pattern+ is a string, all its characters are treated
- * as ordinary characters (not as regexp special characters):
- *
- * 'THX1138'.gsub('\d+', '00') # => "THX1138"
- *
- * <b>\String +replacement+</b>
- *
- * If +replacement+ is a string, that string will determine
- * the replacing string that is to be substituted for the matched text.
- *
- * Each of the examples above uses a simple string as the replacing string.
- *
- * \String +replacement+ may contain back-references to the pattern's captures:
- *
- * - <tt>\n</tt> (_n_ a non-negative integer) refers to <tt>$n</tt>.
- * - <tt>\k<name></tt> refers to the named capture +name+.
- *
- * See rdoc-ref:regexp.rdoc for details.
- *
- * Note that within the string +replacement+, a character combination
- * such as <tt>$&</tt> is treated as ordinary text, and not as
- * a special match variable.
- * However, you may refer to some special match variables using these
- * combinations:
- *
- * - <tt>\&</tt> and <tt>\0</tt> correspond to <tt>$&</tt>,
- * which contains the complete matched text.
- * - <tt>\'</tt> corresponds to <tt>$'</tt>,
- * which contains string after match.
- * - <tt>\`</tt> corresponds to <tt>$`</tt>,
- * which contains string before match.
- * - <tt>\+</tt> corresponds to <tt>$+</tt>,
- * which contains last capture group.
- *
- * See rdoc-ref:regexp.rdoc for details.
- *
- * Note that <tt>\\\\</tt> is interpreted as an escape, i.e., a single backslash.
- *
- * Note also that a string literal consumes backslashes.
- * See rdoc-ref:syntax/literals.rdoc for details about string literals.
- *
- * A back-reference is typically preceded by an additional backslash.
- * For example, if you want to write a back-reference <tt>\&</tt> in
- * +replacement+ with a double-quoted string literal, you need to write
- * <tt>"..\\\\&.."</tt>.
- *
- * If you want to write a non-back-reference string <tt>\&</tt> in
- * +replacement+, you need first to escape the backslash to prevent
- * this method from interpreting it as a back-reference, and then you
- * need to escape the backslashes again to prevent a string literal from
- * consuming them: <tt>"..\\\\\\\\&.."</tt>.
- *
- * You may want to use the block form to avoid a lot of backslashes.
- *
- * <b>\Hash +replacement+</b>
- *
- * If argument +replacement+ is a hash, and +pattern+ matches one of its keys,
- * the replacing string is the value for that key:
- *
- * h = {'foo' => 'bar', 'baz' => 'bat'}
- * 'food'.sub('foo', h) # => "bard"
- *
- * Note that a symbol key does not match:
- *
- * h = {foo: 'bar', baz: 'bat'}
- * 'food'.sub('foo', h) # => "d"
- *
- * <b>Block</b>
- *
- * In the block form, the current match string is passed to the block;
- * the block's return value becomes the replacing string:
- *
- * s = '@'
- * '1234'.gsub(/\d/) {|match| s.succ! } # => "ABCD"
- *
- * Special match variables such as <tt>$1</tt>, <tt>$2</tt>, <tt>$`</tt>,
- * <tt>$&</tt>, and <tt>$'</tt> are set appropriately.
- *
- *
- * == What's Here
- *
- * First, what's elsewhere. \Class \String:
- *
- * - Inherits from {class Object}[Object.html#class-Object-label-What-27s+Here].
- * - Includes {module Comparable}[Comparable.html#module-Comparable-label-What-27s+Here].
- *
- * Here, class \String provides methods that are useful for:
- *
- * - {Creating a String}[#class-String-label-Methods+for+Creating+a+String]
- * - {Frozen/Unfrozen Strings}[#class-String-label-Methods+for+a+Frozen-2FUnfrozen+String]
- * - {Querying}[#class-String-label-Methods+for+Querying]
- * - {Comparing}[#class-String-label-Methods+for+Comparing]
- * - {Modifying a String}[#class-String-label-Methods+for+Modifying+a+String]
- * - {Converting to New String}[#class-String-label-Methods+for+Converting+to+New+String]
- * - {Converting to Non-String}[#class-String-label-Methods+for+Converting+to+Non--5CString]
- * - {Iterating}[#class-String-label-Methods+for+Iterating]
- *
- * === Methods for Creating a \String
- *
- * - ::new:: Returns a new string.
- * - ::try_convert:: Returns a new string created from a given object.
- *
- * === Methods for a Frozen/Unfrozen String
- *
- * - {#+string}[#method-i-2B-40]:: Returns a string that is not frozen:
- * +self+, if not frozen; +self.dup+ otherwise.
- * - {#-string}[#method-i-2D-40]:: Returns a string that is frozen:
- * +self+, if already frozen; +self.freeze+ otherwise.
- * - #freeze:: Freezes +self+, if not already frozen; returns +self+.
- *
- * === Methods for Querying
- *
- * _Counts_
- *
- * - #length, #size:: Returns the count of characters (not bytes).
- * - #empty?:: Returns +true+ if +self.length+ is zero; +false+ otherwise.
- * - #bytesize:: Returns the count of bytes.
- * - #count:: Returns the count of substrings matching given strings.
- *
- * _Substrings_
- *
- * - {#=~}[#method-i-3D~]:: Returns the index of the first substring that matches a given Regexp or other object;
- * returns +nil+ if no match is found.
- * - #index:: Returns the index of the _first_ occurrence of a given substring;
- * returns +nil+ if none found.
- * - #rindex:: Returns the index of the _last_ occurrence of a given substring;
- * returns +nil+ if none found.
- * - #include?:: Returns +true+ if the string contains a given substring; +false+ otherwise.
- * - #match:: Returns a MatchData object if the string matches a given Regexp; +nil+ otherwise.
- * - #match?:: Returns +true+ if the string matches a given Regexp; +false+ otherwise.
- * - #start_with?:: Returns +true+ if the string begins with any of the given substrings.
- * - #end_with?:: Returns +true+ if the string ends with any of the given substrings.
- *
- * _Encodings_
- *
- * - #encoding:: Returns the Encoding object that represents the encoding of the string.
- * - #unicode_normalized?:: Returns +true+ if the string is in Unicode normalized form; +false+ otherwise.
- * - #valid_encoding?:: Returns +true+ if the string contains only characters that are valid
- * for its encoding.
- * - #ascii_only?:: Returns +true+ if the string has only ASCII characters; +false+ otherwise.
- *
- * _Other_
- *
- * - #sum:: Returns a basic checksum for the string: the sum of each byte.
- * - #hash:: Returns the integer hash code.
- *
- * === Methods for Comparing
- *
- * - {#==, #===}[#method-i-3D-3D]:: Returns +true+ if a given other string has the same content as +self+.
- * - #eql?:: Returns +true+ if the content is the same as the given other string.
- * - {#<=>}[#method-i-3C-3D-3E]:: Returns -1, 0, or 1 as a given other string is smaller than, equal to, or larger than +self+.
- * - #casecmp:: Ignoring case, returns -1, 0, or 1 as a given
- * other string is smaller than, equal to, or larger than +self+.
- * - #casecmp?:: Returns +true+ if the string is equal to a given string after Unicode case folding;
- * +false+ otherwise.
- *
- * === Methods for Modifying a \String
- *
- * Each of these methods modifies +self+.
- *
- * _Insertion_
- *
- * - #insert:: Returns +self+ with a given string inserted at a given offset.
- * - #<<:: Returns +self+ concatenated with a given string or integer.
- *
- * _Substitution_
- *
- * - #sub!:: Replaces the first substring that matches a given pattern with a given replacement string;
- * returns +self+ if any changes, +nil+ otherwise.
- * - #gsub!:: Replaces each substring that matches a given pattern with a given replacement string;
- * returns +self+ if any changes, +nil+ otherwise.
- * - #succ!, #next!:: Returns +self+ modified to become its own successor.
- * - #replace:: Returns +self+ with its entire content replaced by a given string.
- * - #reverse!:: Returns +self+ with its characters in reverse order.
- * - #setbyte:: Sets the byte at a given integer offset to a given value; returns the argument.
- * - #tr!:: Replaces specified characters in +self+ with specified replacement characters;
- * returns +self+ if any changes, +nil+ otherwise.
- * - #tr_s!:: Replaces specified characters in +self+ with specified replacement characters,
- * removing duplicates from the substrings that were modified;
- * returns +self+ if any changes, +nil+ otherwise.
- *
- * _Casing_
- *
- * - #capitalize!:: Upcases the initial character and downcases all others;
- * returns +self+ if any changes, +nil+ otherwise.
- * - #downcase!:: Downcases all characters; returns +self+ if any changes, +nil+ otherwise.
- * - #upcase!:: Upcases all characters; returns +self+ if any changes, +nil+ otherwise.
- * - #swapcase!:: Upcases each downcase character and downcases each upcase character;
- * returns +self+ if any changes, +nil+ otherwise.
- *
- * _Encoding_
- *
- * - #encode!:: Returns +self+ with all characters transcoded from one given encoding into another.
- * - #unicode_normalize!:: Unicode-normalizes +self+; returns +self+.
- * - #scrub!:: Replaces each invalid byte with a given character; returns +self+.
- * - #force_encoding:: Changes the encoding to a given encoding; returns +self+.
- *
- * _Deletion_
- *
- * - #clear:: Removes all content, so that +self+ is empty; returns +self+.
- * - #slice!, #[]=:: Removes a substring determined by a given index, start/length, range, regexp, or substring.
- * - #squeeze!:: Removes contiguous duplicate characters; returns +self+.
- * - #delete!:: Removes characters as determined by the intersection of substring arguments.
- * - #lstrip!:: Removes leading whitespace; returns +self+ if any changes, +nil+ otherwise.
- * - #rstrip!:: Removes trailing whitespace; returns +self+ if any changes, +nil+ otherwise.
- * - #strip!:: Removes leading and trailing whitespace; returns +self+ if any changes, +nil+ otherwise.
- * - #chomp!:: Removes trailing record separator, if found; returns +self+ if any changes, +nil+ otherwise.
- * - #chop!:: Removes trailing whitespace if found, otherwise removes the last character;
- * returns +self+ if any changes, +nil+ otherwise.
- *
- * === Methods for Converting to New \String
- *
- * Each of these methods returns a new \String based on +self+,
- * often just a modified copy of +self+.
- *
- * _Extension_
- *
- * - #*:: Returns the concatenation of multiple copies of +self+,
- * - #+:: Returns the concatenation of +self+ and a given other string.
- * - #center:: Returns a copy of +self+ centered between pad substring.
- * - #concat:: Returns the concatenation of +self+ with given other strings.
- * - #prepend:: Returns the concatenation of a given other string with +self+.
- * - #ljust:: Returns a copy of +self+ of a given length, right-padded with a given other string.
- * - #rjust:: Returns a copy of +self+ of a given length, left-padded with a given other string.
- *
- * _Encoding_
- *
- * - #b:: Returns a copy of +self+ with ASCII-8BIT encoding.
- * - #scrub:: Returns a copy of +self+ with each invalid byte replaced with a given character.
- * - #unicode_normalize:: Returns a copy of +self+ with each character Unicode-normalized.
- * - #encode:: Returns a copy of +self+ with all characters transcoded from one given encoding into another.
- *
- * _Substitution_
- *
- * - #dump:: Returns a copy of +self with all non-printing characters replaced by \xHH notation
- * and all special characters escaped.
- * - #undump:: Returns a copy of +self with all <tt>\xNN</tt> notation replace by <tt>\uNNNN</tt> notation
- * and all escaped characters unescaped.
- * - #sub:: Returns a copy of +self+ with the first substring matching a given pattern
- * replaced with a given replacement string;.
- * - #gsub:: Returns a copy of +self+ with each substring that matches a given pattern
- * replaced with a given replacement string.
- * - #succ, #next:: Returns the string that is the successor to +self+.
- * - #reverse:: Returns a copy of +self+ with its characters in reverse order.
- * - #tr:: Returns a copy of +self+ with specified characters replaced with specified replacement characters.
- * - #tr_s:: Returns a copy of +self+ with specified characters replaced with specified replacement characters,
- * removing duplicates from the substrings that were modified.
- * - #%:: Returns the string resulting from formatting a given object into +self+
- *
- * _Casing_
- *
- * - #capitalize:: Returns a copy of +self+ with the first character upcased
- * and all other characters downcased.
- * - #downcase:: Returns a copy of +self+ with all characters downcased.
- * - #upcase:: Returns a copy of +self+ with all characters upcased.
- * - #swapcase:: Returns a copy of +self+ with all upcase characters downcased
- * and all downcase characters upcased.
- *
- * _Deletion_
- *
- * - #delete:: Returns a copy of +self+ with characters removed
- * - #delete_prefix:: Returns a copy of +self+ with a given prefix removed.
- * - #delete_suffix:: Returns a copy of +self+ with a given suffix removed.
- * - #lstrip:: Returns a copy of +self+ with leading whitespace removed.
- * - #rstrip:: Returns a copy of +self+ with trailing whitespace removed.
- * - #strip:: Returns a copy of +self+ with leading and trailing whitespace removed.
- * - #chomp:: Returns a copy of +self+ with a trailing record separator removed, if found.
- * - #chop:: Returns a copy of +self+ with trailing whitespace or the last character removed.
- * - #squeeze:: Returns a copy of +self+ with contiguous duplicate characters removed.
- * - #[], #slice:: Returns a substring determined by a given index, start/length, or range, or string.
- * - #byteslice:: Returns a substring determined by a given index, start/length, or range.
- * - #chr:: Returns the first character.
- *
- * _Duplication_
- *
- * - #to_s, $to_str:: If +self+ is a subclass of \String, returns +self+ copied into a \String;
- * otherwise, returns +self+.
- *
- * === Methods for Converting to Non-\String
- *
- * Each of these methods converts the contents of +self+ to a non-\String.
- *
- * <em>Characters, Bytes, and Clusters</em>
- *
- * - #bytes:: Returns an array of the bytes in +self+.
- * - #chars:: Returns an array of the characters in +self+.
- * - #codepoints:: Returns an array of the integer ordinals in +self+.
- * - #getbyte:: Returns an integer byte as determined by a given index.
- * - #grapheme_clusters:: Returns an array of the grapheme clusters in +self+.
- *
- * _Splitting_
- *
- * - #lines:: Returns an array of the lines in +self+, as determined by a given record separator.
- * - #partition:: Returns a 3-element array determined by the first substring that matches
- * a given substring or regexp,
- * - #rpartition:: Returns a 3-element array determined by the last substring that matches
- * a given substring or regexp,
- * - #split:: Returns an array of substrings determined by a given delimiter -- regexp or string --
- * or, if a block given, passes those substrings to the block.
- *
- * _Matching_
- *
- * - #scan:: Returns an array of substrings matching a given regexp or string, or,
- * if a block given, passes each matching substring to the block.
- * - #unpack:: Returns an array of substrings extracted from +self+ according to a given format.
- * - #unpack1:: Returns the first substring extracted from +self+ according to a given format.
- *
- * _Numerics_
- *
- * - #hex:: Returns the integer value of the leading characters, interpreted as hexadecimal digits.
- * - #oct:: Returns the integer value of the leading characters, interpreted as octal digits.
- * - #ord:: Returns the integer ordinal of the first character in +self+.
- * - #to_i:: Returns the integer value of leading characters, interpreted as an integer.
- * - #to_f:: Returns the floating-point value of leading characters, interpreted as a floating-point number.
- *
- * <em>Strings and Symbols</em>
- *
- * - #inspect:: Returns copy of +self+, enclosed in double-quotes, with special characters escaped.
- * - #to_sym, #intern:: Returns the symbol corresponding to +self+.
- *
- * === Methods for Iterating
- *
- * - #each_byte:: Calls the given block with each successive byte in +self+.
- * - #each_char:: Calls the given block with each successive character in +self+.
- * - #each_codepoint:: Calls the given block with each successive integer codepoint in +self+.
- * - #each_grapheme_cluster:: Calls the given block with each successive grapheme cluster in +self+.
- * - #each_line:: Calls the given block with each successive line in +self+,
- * as determined by a given record separator.
- * - #upto:: Calls the given block with each string value returned by successive calls to #succ.
- */
-
void
Init_String(void)
{
rb_cString = rb_define_class("String", rb_cObject);
- assert(rb_vm_fstring_table());
+ RUBY_ASSERT(rb_vm_fstring_table());
st_foreach(rb_vm_fstring_table(), fstring_set_class_i, rb_cString);
rb_include_module(rb_cString, rb_mComparable);
rb_define_alloc_func(rb_cString, empty_str_alloc);
+ rb_define_singleton_method(rb_cString, "new", rb_str_s_new, -1);
rb_define_singleton_method(rb_cString, "try_convert", rb_str_s_try_convert, 1);
rb_define_method(rb_cString, "initialize", rb_str_init, -1);
rb_define_method(rb_cString, "initialize_copy", rb_str_replace, 1);
@@ -12208,18 +12188,23 @@ Init_String(void)
rb_define_method(rb_cString, "next!", rb_str_succ_bang, 0);
rb_define_method(rb_cString, "upto", rb_str_upto, -1);
rb_define_method(rb_cString, "index", rb_str_index_m, -1);
+ rb_define_method(rb_cString, "byteindex", rb_str_byteindex_m, -1);
rb_define_method(rb_cString, "rindex", rb_str_rindex_m, -1);
+ rb_define_method(rb_cString, "byterindex", rb_str_byterindex_m, -1);
rb_define_method(rb_cString, "replace", rb_str_replace, 1);
rb_define_method(rb_cString, "clear", rb_str_clear, 0);
rb_define_method(rb_cString, "chr", rb_str_chr, 0);
rb_define_method(rb_cString, "getbyte", rb_str_getbyte, 1);
rb_define_method(rb_cString, "setbyte", rb_str_setbyte, 2);
rb_define_method(rb_cString, "byteslice", rb_str_byteslice, -1);
+ rb_define_method(rb_cString, "bytesplice", rb_str_bytesplice, -1);
rb_define_method(rb_cString, "scrub", str_scrub, -1);
rb_define_method(rb_cString, "scrub!", str_scrub_bang, -1);
rb_define_method(rb_cString, "freeze", rb_str_freeze, 0);
rb_define_method(rb_cString, "+@", str_uplus, 0);
rb_define_method(rb_cString, "-@", str_uminus, 0);
+ rb_define_method(rb_cString, "dup", rb_str_dup_m, 0);
+ rb_define_alias(rb_cString, "dedup", "-@");
rb_define_method(rb_cString, "to_i", rb_str_to_i, -1);
rb_define_method(rb_cString, "to_f", rb_str_to_f, 0);
@@ -12346,12 +12331,8 @@ Init_String(void)
rb_define_method(rb_cSymbol, "==", sym_equal, 1);
rb_define_method(rb_cSymbol, "===", sym_equal, 1);
rb_define_method(rb_cSymbol, "inspect", sym_inspect, 0);
- rb_define_method(rb_cSymbol, "to_s", rb_sym_to_s, 0);
- rb_define_method(rb_cSymbol, "id2name", rb_sym_to_s, 0);
- rb_define_method(rb_cSymbol, "name", rb_sym2str, 0);
- rb_define_method(rb_cSymbol, "intern", sym_to_sym, 0);
- rb_define_method(rb_cSymbol, "to_sym", sym_to_sym, 0);
- rb_define_method(rb_cSymbol, "to_proc", rb_sym_to_proc, 0);
+ rb_define_method(rb_cSymbol, "name", rb_sym2str, 0); /* in symbol.c */
+ rb_define_method(rb_cSymbol, "to_proc", rb_sym_to_proc, 0); /* in proc.c */
rb_define_method(rb_cSymbol, "succ", sym_succ, 0);
rb_define_method(rb_cSymbol, "next", sym_succ, 0);