Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
9280 lines (8345 sloc) 231.207 kb
/**********************************************************************
string.c -
$Author$
created at: Mon Aug 9 17:12:58 JST 1993
Copyright (C) 1993-2007 Yukihiro Matsumoto
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
Copyright (C) 2000 Information-technology Promotion Agency, Japan
**********************************************************************/
#include "internal.h"
#include "ruby/re.h"
#include "probes.h"
#include "gc.h"
#include <assert.h>
#define BEG(no) (regs->beg[(no)])
#define END(no) (regs->end[(no)])
#include <math.h>
#include <ctype.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#define STRING_ENUMERATORS_WANTARRAY 0 /* next major */
#undef rb_str_new
#undef rb_usascii_str_new
#undef rb_utf8_str_new
#undef rb_enc_str_new
#undef rb_str_new_cstr
#undef rb_tainted_str_new_cstr
#undef rb_usascii_str_new_cstr
#undef rb_utf8_str_new_cstr
#undef rb_enc_str_new_cstr
#undef rb_external_str_new_cstr
#undef rb_locale_str_new_cstr
#undef rb_str_dup_frozen
#undef rb_str_buf_new_cstr
#undef rb_str_buf_cat
#undef rb_str_buf_cat2
#undef rb_str_cat2
#undef rb_str_cat_cstr
static VALUE rb_str_clear(VALUE str);
VALUE rb_cString;
VALUE rb_cSymbol;
/* FLAGS of RString
*
* 1: RSTRING_NOEMBED
* 2: STR_SHARED (== ELTS_SHARED)
* 2-6: RSTRING_EMBED_LEN (5 bits == 32)
* 7: STR_TMPLOCK
* 8-9: ENC_CODERANGE (2 bits)
* 10-16: ENCODING (7 bits == 128)
* 17: RSTRING_FSTR
* 18: STR_NOFREE
* 19: STR_FAKESTR
*/
#define RUBY_MAX_CHAR_LEN 16
#define STR_TMPLOCK FL_USER7
#define STR_NOFREE FL_USER18
#define STR_FAKESTR FL_USER19
#define STR_SET_NOEMBED(str) do {\
FL_SET((str), STR_NOEMBED);\
STR_SET_EMBED_LEN((str), 0);\
} while (0)
#define STR_SET_EMBED(str) FL_UNSET((str), (STR_NOEMBED|STR_NOFREE))
#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)
#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);\
}\
} 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)
#define TERM_LEN(str) rb_enc_mbminlen(rb_enc_get(str))
#define TERM_FILL(ptr, termlen) do {\
char *const term_fill_ptr = (ptr);\
const int term_fill_len = (termlen);\
*term_fill_ptr = '\0';\
if (UNLIKELY(term_fill_len > 1))\
memset(term_fill_ptr, 0, term_fill_len);\
} while (0)
#define RESIZE_CAPA(str,capacity) do {\
const int termlen = TERM_LEN(str);\
RESIZE_CAPA_TERM(str,capacity,termlen);\
} while (0)
#define RESIZE_CAPA_TERM(str,capacity,termlen) do {\
if (STR_EMBED_P(str)) {\
if ((capacity) > RSTRING_EMBED_LEN_MAX) {\
char *const tmp = ALLOC_N(char, (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;\
STR_SET_NOEMBED(str);\
RSTRING(str)->as.heap.aux.capa = (capacity);\
}\
}\
else {\
assert(!FL_TEST((str), STR_SHARED)); \
REALLOC_N(RSTRING(str)->as.heap.ptr, char, (capacity)+termlen);\
RSTRING(str)->as.heap.aux.capa = (capacity);\
}\
} while (0)
#define STR_SET_SHARED(str, shared_str) do { \
if (!FL_TEST(str, STR_FAKESTR)) { \
RB_OBJ_WRITE((str), &RSTRING(str)->as.heap.aux.shared, (shared_str)); \
FL_SET((str), STR_SHARED); \
} \
} while (0)
#define STR_HEAP_PTR(str) (RSTRING(str)->as.heap.ptr)
#define STR_HEAP_SIZE(str) (RSTRING(str)->as.heap.aux.capa + TERM_LEN(str))
#define STR_ENC_GET(str) get_encoding(str)
#if !defined SHARABLE_MIDDLE_SUBSTRING
# define SHARABLE_MIDDLE_SUBSTRING 0
#endif
#if !SHARABLE_MIDDLE_SUBSTRING
#define SHARABLE_SUBSTRING_P(beg, len, end) ((beg) + (len) == (end))
#else
#define SHARABLE_SUBSTRING_P(beg, len, end) 1
#endif
static 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);
}
static void
mustnot_broken(VALUE str)
{
if (is_broken_string(str)) {
rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(STR_ENC_GET(str)));
}
}
static void
mustnot_wchar(VALUE str)
{
rb_encoding *enc = STR_ENC_GET(str);
if (rb_enc_mbminlen(enc) > 1) {
rb_raise(rb_eArgError, "wide char encoding: %s", rb_enc_name(enc));
}
}
static int fstring_cmp(VALUE a, VALUE b);
/* in case we restart MVM development, this needs to be per-VM */
static st_table* frozen_strings;
static inline st_table*
rb_vm_fstring_table(void)
{
return frozen_strings;
}
static const struct st_hash_type fstring_hash_type = {
fstring_cmp,
rb_str_hash,
};
static int
fstr_update_callback(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
{
VALUE *fstr = (VALUE *)arg;
VALUE str = (VALUE)*key;
if (existing) {
/* because of lazy sweep, str may be unmarked already and swept
* at next time */
if (rb_objspace_garbage_object_p(str)) {
*fstr = Qundef;
return ST_DELETE;
}
*fstr = str;
return ST_STOP;
}
else {
if (STR_SHARED_P(str)) { /* str should not be shared */
str = rb_enc_str_new(RSTRING_PTR(str), RSTRING_LEN(str), STR_ENC_GET(str));
OBJ_FREEZE(str);
}
else {
str = rb_str_new_frozen(str);
}
RBASIC(str)->flags |= RSTRING_FSTR;
*key = *value = *fstr = str;
return ST_CONTINUE;
}
}
VALUE
rb_fstring(VALUE str)
{
VALUE ret;
Check_Type(str, T_STRING);
if (FL_TEST(str, RSTRING_FSTR))
return str;
do {
ret = str;
st_update(rb_vm_fstring_table(), (st_data_t)str,
fstr_update_callback, (st_data_t)&ret);
} while (ret == Qundef);
return ret;
}
static VALUE
setup_fake_str(struct RString *fake_str, const char *name, long len, int encidx)
{
fake_str->basic.flags = T_STRING|RSTRING_NOEMBED|STR_NOFREE|STR_FAKESTR;
/* SHARED to be allocated by the callback */
ENCODING_SET_INLINED((VALUE)fake_str, encidx);
RBASIC_SET_CLASS_RAW((VALUE)fake_str, rb_cString);
fake_str->as.heap.len = len;
fake_str->as.heap.ptr = (char *)name;
fake_str->as.heap.aux.capa = len;
return (VALUE)fake_str;
}
VALUE
rb_setup_fake_str(struct RString *fake_str, const char *name, long len, rb_encoding *enc)
{
return setup_fake_str(fake_str, name, len, rb_enc_to_index(enc));
}
VALUE
rb_fstring_new(const char *ptr, long len)
{
struct RString fake_str;
return rb_fstring(setup_fake_str(&fake_str, ptr, len, ENCINDEX_US_ASCII));
}
static int
fstring_set_class_i(st_data_t key, st_data_t val, st_data_t arg)
{
RBASIC_SET_CLASS((VALUE)key, (VALUE)arg);
return ST_CONTINUE;
}
static int
fstring_cmp(VALUE a, VALUE b)
{
int cmp = rb_str_hash_cmp(a, b);
if (cmp != 0) {
return cmp;
}
return ENCODING_GET(b) - ENCODING_GET(a);
}
static inline int
single_byte_optimizable(VALUE str)
{
rb_encoding *enc;
/* Conservative. It may be ENC_CODERANGE_UNKNOWN. */
if (ENC_CODERANGE(str) == ENC_CODERANGE_7BIT)
return 1;
enc = STR_ENC_GET(str);
if (rb_enc_mbmaxlen(enc) == 1)
return 1;
/* Conservative. Possibly single byte.
* "\xa1" in Shift_JIS for example. */
return 0;
}
VALUE rb_fs;
static inline const char *
search_nonascii(const char *p, const char *e)
{
#if SIZEOF_VOIDP == 8
# define NONASCII_MASK 0x8080808080808080ULL
#elif SIZEOF_VOIDP == 4
# define NONASCII_MASK 0x80808080UL
#endif
#ifdef NONASCII_MASK
if ((int)SIZEOF_VOIDP * 2 < e - p) {
const uintptr_t *s, *t;
const uintptr_t lowbits = SIZEOF_VOIDP - 1;
s = (const uintptr_t*)(~lowbits & ((uintptr_t)p + lowbits));
while (p < (const char *)s) {
if (!ISASCII(*p))
return p;
p++;
}
t = (const uintptr_t*)(~lowbits & (uintptr_t)e);
while (s < t) {
if (*s & NONASCII_MASK) {
t = s;
break;
}
s++;
}
p = (const char *)t;
}
#endif
while (p < e) {
if (!ISASCII(*p))
return p;
p++;
}
return NULL;
}
static int
coderange_scan(const char *p, long len, rb_encoding *enc)
{
const char *e = p + len;
if (rb_enc_to_index(enc) == rb_ascii8bit_encindex()) {
/* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
p = search_nonascii(p, e);
return p ? ENC_CODERANGE_VALID : ENC_CODERANGE_7BIT;
}
if (rb_enc_asciicompat(enc)) {
p = search_nonascii(p, e);
if (!p) return ENC_CODERANGE_7BIT;
for (;;) {
int ret = rb_enc_precise_mbclen(p, e, enc);
if (!MBCLEN_CHARFOUND_P(ret)) return ENC_CODERANGE_BROKEN;
p += MBCLEN_CHARFOUND_LEN(ret);
if (p == e) break;
p = search_nonascii(p, e);
if (!p) break;
}
}
else {
while (p < e) {
int ret = rb_enc_precise_mbclen(p, e, enc);
if (!MBCLEN_CHARFOUND_P(ret)) return ENC_CODERANGE_BROKEN;
p += MBCLEN_CHARFOUND_LEN(ret);
}
}
return ENC_CODERANGE_VALID;
}
long
rb_str_coderange_scan_restartable(const char *s, const char *e, rb_encoding *enc, int *cr)
{
const char *p = s;
if (*cr == ENC_CODERANGE_BROKEN)
return e - s;
if (rb_enc_to_index(enc) == rb_ascii8bit_encindex()) {
/* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
if (*cr == ENC_CODERANGE_VALID) return e - s;
p = search_nonascii(p, e);
*cr = p ? ENC_CODERANGE_VALID : ENC_CODERANGE_7BIT;
return e - s;
}
else if (rb_enc_asciicompat(enc)) {
p = search_nonascii(p, e);
if (!p) {
if (*cr != ENC_CODERANGE_VALID) *cr = ENC_CODERANGE_7BIT;
return e - s;
}
for (;;) {
int ret = rb_enc_precise_mbclen(p, e, enc);
if (!MBCLEN_CHARFOUND_P(ret)) {
*cr = MBCLEN_INVALID_P(ret) ? ENC_CODERANGE_BROKEN: ENC_CODERANGE_UNKNOWN;
return p - s;
}
p += MBCLEN_CHARFOUND_LEN(ret);
if (p == e) break;
p = search_nonascii(p, e);
if (!p) break;
}
}
else {
while (p < e) {
int ret = rb_enc_precise_mbclen(p, e, enc);
if (!MBCLEN_CHARFOUND_P(ret)) {
*cr = MBCLEN_INVALID_P(ret) ? ENC_CODERANGE_BROKEN: ENC_CODERANGE_UNKNOWN;
return p - s;
}
p += MBCLEN_CHARFOUND_LEN(ret);
}
}
*cr = ENC_CODERANGE_VALID;
return e - s;
}
static inline void
str_enc_copy(VALUE str1, VALUE str2)
{
rb_enc_set_index(str1, ENCODING_GET(str2));
}
static void
rb_enc_cr_str_copy_for_substr(VALUE dest, VALUE src)
{
/* this function is designed for copying encoding and coderange
* from src to new string "dest" which is made from the part of src.
*/
str_enc_copy(dest, src);
if (RSTRING_LEN(dest) == 0) {
if (!rb_enc_asciicompat(STR_ENC_GET(src)))
ENC_CODERANGE_SET(dest, ENC_CODERANGE_VALID);
else
ENC_CODERANGE_SET(dest, ENC_CODERANGE_7BIT);
return;
}
switch (ENC_CODERANGE(src)) {
case ENC_CODERANGE_7BIT:
ENC_CODERANGE_SET(dest, ENC_CODERANGE_7BIT);
break;
case ENC_CODERANGE_VALID:
if (!rb_enc_asciicompat(STR_ENC_GET(src)) ||
search_nonascii(RSTRING_PTR(dest), RSTRING_END(dest)))
ENC_CODERANGE_SET(dest, ENC_CODERANGE_VALID);
else
ENC_CODERANGE_SET(dest, ENC_CODERANGE_7BIT);
break;
default:
break;
}
}
static void
rb_enc_cr_str_exact_copy(VALUE dest, VALUE src)
{
str_enc_copy(dest, src);
ENC_CODERANGE_SET(dest, ENC_CODERANGE(src));
}
int
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);
if (rb_enc_mbminlen(enc) > 1 && rb_enc_dummy_p(enc)) {
cr = ENC_CODERANGE_BROKEN;
}
else {
cr = coderange_scan(RSTRING_PTR(str), RSTRING_LEN(str),
get_actual_encoding(encidx, str));
}
ENC_CODERANGE_SET(str, cr);
}
return cr;
}
int
rb_enc_str_asciionly_p(VALUE str)
{
rb_encoding *enc = STR_ENC_GET(str);
if (!rb_enc_asciicompat(enc))
return FALSE;
else if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT)
return TRUE;
return FALSE;
}
static inline void
str_mod_check(VALUE s, const char *p, long len)
{
if (RSTRING_PTR(s) != p || RSTRING_LEN(s) != len){
rb_raise(rb_eRuntimeError, "string modified");
}
}
size_t
rb_str_capacity(VALUE str)
{
if (STR_EMBED_P(str)) {
return RSTRING_EMBED_LEN_MAX;
}
else if (FL_TEST(str, STR_SHARED|STR_NOFREE)) {
return RSTRING(str)->as.heap.len;
}
else {
return RSTRING(str)->as.heap.aux.capa;
}
}
static inline void
must_not_null(const char *ptr)
{
if (!ptr) {
rb_raise(rb_eArgError, "NULL pointer given");
}
}
static inline VALUE
str_alloc(VALUE klass)
{
NEWOBJ_OF(str, struct RString, klass, T_STRING | (RGENGC_WB_PROTECTED_STRING ? FL_WB_PROTECTED : 0));
return (VALUE)str;
}
static inline VALUE
empty_str_alloc(VALUE klass)
{
if (RUBY_DTRACE_STRING_CREATE_ENABLED()) {
RUBY_DTRACE_STRING_CREATE(0, rb_sourcefile(), rb_sourceline());
}
return str_alloc(klass);
}
static VALUE
str_new0(VALUE klass, const char *ptr, long len, int termlen)
{
VALUE str;
if (len < 0) {
rb_raise(rb_eArgError, "negative string size (or size too big)");
}
if (RUBY_DTRACE_STRING_CREATE_ENABLED()) {
RUBY_DTRACE_STRING_CREATE(len, rb_sourcefile(), rb_sourceline());
}
str = str_alloc(klass);
if (len > RSTRING_EMBED_LEN_MAX) {
RSTRING(str)->as.heap.aux.capa = len;
RSTRING(str)->as.heap.ptr = ALLOC_N(char, len + termlen);
STR_SET_NOEMBED(str);
}
else if (len == 0) {
ENC_CODERANGE_SET(str, ENC_CODERANGE_7BIT);
}
if (ptr) {
memcpy(RSTRING_PTR(str), ptr, len);
}
STR_SET_LEN(str, len);
TERM_FILL(RSTRING_PTR(str) + len, termlen);
return str;
}
static VALUE
str_new(VALUE klass, const char *ptr, long len)
{
return str_new0(klass, ptr, len, 1);
}
VALUE
rb_str_new(const char *ptr, long len)
{
return str_new(rb_cString, ptr, len);
}
VALUE
rb_usascii_str_new(const char *ptr, long len)
{
VALUE str = rb_str_new(ptr, len);
ENCODING_CODERANGE_SET(str, rb_usascii_encindex(), ENC_CODERANGE_7BIT);
return str;
}
VALUE
rb_utf8_str_new(const char *ptr, long len)
{
VALUE str = str_new(rb_cString, ptr, len);
rb_enc_associate_index(str, rb_utf8_encindex());
return str;
}
VALUE
rb_enc_str_new(const char *ptr, long len, rb_encoding *enc)
{
VALUE str;
if (!enc) return rb_str_new(ptr, len);
str = str_new0(rb_cString, ptr, len, rb_enc_mbminlen(enc));
rb_enc_associate(str, enc);
return str;
}
VALUE
rb_str_new_cstr(const char *ptr)
{
must_not_null(ptr);
return rb_str_new(ptr, strlen(ptr));
}
VALUE
rb_usascii_str_new_cstr(const char *ptr)
{
VALUE str = rb_str_new_cstr(ptr);
ENCODING_CODERANGE_SET(str, rb_usascii_encindex(), ENC_CODERANGE_7BIT);
return str;
}
VALUE
rb_utf8_str_new_cstr(const char *ptr)
{
VALUE str = rb_str_new_cstr(ptr);
rb_enc_associate_index(str, rb_utf8_encindex());
return str;
}
VALUE
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");
}
return rb_enc_str_new(ptr, strlen(ptr), enc);
}
static VALUE
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)");
}
if (!ptr) {
str = str_new(klass, ptr, len);
}
else {
if (RUBY_DTRACE_STRING_CREATE_ENABLED()) {
RUBY_DTRACE_STRING_CREATE(len, rb_sourcefile(), rb_sourceline());
}
str = str_alloc(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;
}
rb_enc_associate_index(str, encindex);
return str;
}
VALUE
rb_str_new_static(const char *ptr, long len)
{
return str_new_static(rb_cString, ptr, len, 0);
}
VALUE
rb_usascii_str_new_static(const char *ptr, long len)
{
return str_new_static(rb_cString, ptr, len, ENCINDEX_US_ASCII);
}
VALUE
rb_utf8_str_new_static(const char *ptr, long len)
{
return str_new_static(rb_cString, ptr, len, ENCINDEX_UTF_8);
}
VALUE
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)
{
VALUE str = rb_str_new(ptr, len);
OBJ_TAINT(str);
return str;
}
VALUE
rb_tainted_str_new_cstr(const char *ptr)
{
VALUE str = rb_str_new_cstr(ptr);
OBJ_TAINT(str);
return str;
}
VALUE
rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags, VALUE ecopts)
{
rb_econv_t *ec;
rb_econv_result_t ret;
long len, olen;
VALUE econv_wrapper;
VALUE newstr;
const unsigned char *start, *sp;
unsigned char *dest, *dp;
size_t converted_output = 0;
if (!to) return str;
if (!from) from = rb_enc_get(str);
if (from == to) return str;
if ((rb_enc_asciicompat(to) && is_ascii_string(str)) ||
to == rb_ascii8bit_encoding()) {
if (STR_ENC_GET(str) != to) {
str = rb_str_dup(str);
rb_enc_associate(str, to);
}
return str;
}
len = RSTRING_LEN(str);
newstr = rb_str_new(0, len);
OBJ_INFECT(newstr, str);
olen = len;
econv_wrapper = rb_obj_alloc(rb_cEncodingConverter);
RBASIC_CLEAR_CLASS(econv_wrapper);
ec = rb_econv_open_opts(from->name, to->name, ecflags, ecopts);
if (!ec) return str;
DATA_PTR(econv_wrapper) = ec;
sp = (unsigned char*)RSTRING_PTR(str);
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);
}
DATA_PTR(econv_wrapper) = 0;
rb_econv_close(ec);
rb_gc_force_recycle(econv_wrapper);
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;
default:
/* some error, return original */
return str;
}
}
VALUE
rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
{
return rb_str_conv_enc_opts(str, from, to, 0, Qnil);
}
VALUE
rb_external_str_new_with_enc(const char *ptr, long len, rb_encoding *eenc)
{
VALUE str;
str = rb_tainted_str_new(ptr, len);
return rb_external_str_with_enc(str, eenc);
}
VALUE
rb_external_str_with_enc(VALUE str, rb_encoding *eenc)
{
if (eenc == rb_usascii_encoding() &&
rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
rb_enc_associate(str, rb_ascii8bit_encoding());
return str;
}
rb_enc_associate(str, eenc);
return rb_str_conv_enc(str, eenc, rb_default_internal_encoding());
}
VALUE
rb_external_str_new(const char *ptr, long len)
{
return rb_external_str_new_with_enc(ptr, len, rb_default_external_encoding());
}
VALUE
rb_external_str_new_cstr(const char *ptr)
{
return rb_external_str_new_with_enc(ptr, strlen(ptr), rb_default_external_encoding());
}
VALUE
rb_locale_str_new(const char *ptr, long len)
{
return rb_external_str_new_with_enc(ptr, len, rb_locale_encoding());
}
VALUE
rb_locale_str_new_cstr(const char *ptr)
{
return rb_external_str_new_with_enc(ptr, strlen(ptr), rb_locale_encoding());
}
VALUE
rb_filesystem_str_new(const char *ptr, long len)
{
return rb_external_str_new_with_enc(ptr, len, rb_filesystem_encoding());
}
VALUE
rb_filesystem_str_new_cstr(const char *ptr)
{
return rb_external_str_new_with_enc(ptr, strlen(ptr), rb_filesystem_encoding());
}
VALUE
rb_str_export(VALUE str)
{
return rb_str_conv_enc(str, STR_ENC_GET(str), rb_default_external_encoding());
}
VALUE
rb_str_export_locale(VALUE str)
{
return rb_str_conv_enc(str, STR_ENC_GET(str), rb_locale_encoding());
}
VALUE
rb_str_export_to_enc(VALUE str, rb_encoding *enc)
{
return rb_str_conv_enc(str, STR_ENC_GET(str), enc);
}
static VALUE
str_replace_shared_without_enc(VALUE str2, VALUE str)
{
if (RSTRING_LEN(str) <= RSTRING_EMBED_LEN_MAX) {
STR_SET_EMBED(str2);
memcpy(RSTRING_PTR(str2), RSTRING_PTR(str), RSTRING_LEN(str)+1);
STR_SET_EMBED_LEN(str2, RSTRING_LEN(str));
}
else {
str = rb_str_new_frozen(str);
FL_SET(str2, STR_NOEMBED);
RSTRING(str2)->as.heap.len = RSTRING_LEN(str);
RSTRING(str2)->as.heap.ptr = RSTRING_PTR(str);
STR_SET_SHARED(str2, str);
}
return str2;
}
static VALUE
str_replace_shared(VALUE str2, VALUE str)
{
str_replace_shared_without_enc(str2, str);
rb_enc_cr_str_exact_copy(str2, str);
return str2;
}
static VALUE
str_new_shared(VALUE klass, VALUE str)
{
return str_replace_shared(str_alloc(klass), str);
}
VALUE
rb_str_new_shared(VALUE str)
{
VALUE str2 = str_new_shared(rb_obj_class(str), str);
OBJ_INFECT(str2, str);
return str2;
}
VALUE
rb_str_new_frozen(VALUE orig)
{
VALUE klass, str;
if (OBJ_FROZEN(orig)) return orig;
klass = rb_obj_class(orig);
if (STR_EMBED_P(orig)) {
str = str_new(klass, RSTRING_PTR(orig), RSTRING_LEN(orig));
}
else {
if (FL_TEST(orig, STR_SHARED)) {
VALUE shared = RSTRING(orig)->as.heap.aux.shared;
long ofs = RSTRING_PTR(orig) - RSTRING_PTR(shared);
long rest = RSTRING_LEN(shared) - ofs - RSTRING_LEN(orig);
assert(OBJ_FROZEN(shared));
if ((ofs > 0) || (rest > 0) ||
(klass != RBASIC(shared)->klass) ||
((RBASIC(shared)->flags ^ RBASIC(orig)->flags) & FL_TAINT) ||
ENCODING_GET(shared) != ENCODING_GET(orig)) {
str = str_new_shared(klass, shared);
RSTRING(str)->as.heap.ptr += ofs;
RSTRING(str)->as.heap.len -= ofs + rest;
}
else {
return shared;
}
}
else {
str = str_alloc(klass);
STR_SET_NOEMBED(str);
RSTRING(str)->as.heap.len = 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;
RBASIC(orig)->flags &= ~STR_NOFREE;
STR_SET_SHARED(orig, str);
}
}
rb_enc_cr_str_exact_copy(str, orig);
OBJ_INFECT(str, orig);
OBJ_FREEZE(str);
return str;
}
VALUE
rb_str_new_with_class(VALUE obj, const char *ptr, long len)
{
return str_new(rb_obj_class(obj), ptr, len);
}
static VALUE
str_new_empty(VALUE str)
{
VALUE v = rb_str_new_with_class(str, 0, 0);
rb_enc_copy(v, str);
OBJ_INFECT(v, str);
return v;
}
#define STR_BUF_MIN_SIZE 128
VALUE
rb_str_buf_new(long capa)
{
VALUE str = str_alloc(rb_cString);
if (capa < STR_BUF_MIN_SIZE) {
capa = STR_BUF_MIN_SIZE;
}
FL_SET(str, STR_NOEMBED);
RSTRING(str)->as.heap.aux.capa = capa;
RSTRING(str)->as.heap.ptr = ALLOC_N(char, capa+1);
RSTRING(str)->as.heap.ptr[0] = '\0';
return str;
}
VALUE
rb_str_buf_new_cstr(const char *ptr)
{
VALUE str;
long len = strlen(ptr);
str = rb_str_buf_new(len);
rb_str_buf_cat(str, ptr, len);
return str;
}
VALUE
rb_str_tmp_new(long len)
{
return str_new(0, 0, len);
}
void *
rb_alloc_tmp_buffer(volatile VALUE *store, long len)
{
VALUE s = rb_str_tmp_new(len);
*store = s;
return RSTRING_PTR(s);
}
void
rb_free_tmp_buffer(volatile VALUE *store)
{
VALUE s = *store;
*store = 0;
if (s) rb_str_clear(s);
}
void
rb_str_free(VALUE str)
{
if (FL_TEST(str, RSTRING_FSTR)) {
st_data_t fstr = (st_data_t)str;
st_delete(rb_vm_fstring_table(), &fstr, NULL);
}
if (!STR_EMBED_P(str) && !FL_TEST(str, STR_SHARED|STR_NOFREE)) {
ruby_sized_xfree(STR_HEAP_PTR(str), STR_HEAP_SIZE(str));
}
}
RUBY_FUNC_EXPORTED size_t
rb_str_memsize(VALUE str)
{
if (FL_TEST(str, STR_NOEMBED|STR_SHARED|STR_NOFREE) == STR_NOEMBED) {
return STR_HEAP_SIZE(str);
}
else {
return 0;
}
}
VALUE
rb_str_to_str(VALUE str)
{
return rb_convert_type(str, T_STRING, "String", "to_str");
}
static inline void str_discard(VALUE str);
void
rb_str_shared_replace(VALUE str, VALUE str2)
{
rb_encoding *enc;
int cr;
if (str == str2) return;
enc = STR_ENC_GET(str2);
cr = ENC_CODERANGE(str2);
str_discard(str);
OBJ_INFECT(str, str2);
if (RSTRING_LEN(str2) <= RSTRING_EMBED_LEN_MAX) {
STR_SET_EMBED(str);
memcpy(RSTRING_PTR(str), RSTRING_PTR(str2), RSTRING_LEN(str2)+1);
STR_SET_EMBED_LEN(str, RSTRING_LEN(str2));
rb_enc_associate(str, enc);
ENC_CODERANGE_SET(str, cr);
}
else {
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);
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);
}
}
static ID id_to_s;
VALUE
rb_obj_as_string(VALUE obj)
{
VALUE str;
if (RB_TYPE_P(obj, T_STRING)) {
return obj;
}
str = rb_funcall(obj, id_to_s, 0);
if (!RB_TYPE_P(str, T_STRING))
return rb_any_to_s(obj);
if (OBJ_TAINTED(obj)) OBJ_TAINT(str);
return str;
}
static VALUE
str_replace(VALUE str, VALUE str2)
{
long len;
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);
}
else {
str_replace_shared(str, str2);
}
OBJ_INFECT(str, str2);
rb_enc_cr_str_exact_copy(str, str2);
return str;
}
static VALUE
str_duplicate(VALUE klass, VALUE str)
{
VALUE dup = str_alloc(klass);
str_replace(dup, str);
return dup;
}
VALUE
rb_str_dup(VALUE str)
{
return str_duplicate(rb_obj_class(str), str);
}
VALUE
rb_str_resurrect(VALUE str)
{
if (RUBY_DTRACE_STRING_CREATE_ENABLED()) {
RUBY_DTRACE_STRING_CREATE(RSTRING_LEN(str),
rb_sourcefile(), rb_sourceline());
}
return str_duplicate(rb_cString, str);
}
/*
* call-seq:
* String.new(str="") -> new_str
*
* Returns a new string object containing a copy of <i>str</i>.
*/
static VALUE
rb_str_init(int argc, VALUE *argv, VALUE str)
{
VALUE orig;
if (argc > 0 && rb_scan_args(argc, argv, "01", &orig) == 1)
rb_str_replace(str, orig);
return str;
}
#ifdef NONASCII_MASK
#define is_utf8_lead_byte(c) (((c)&0xC0) != 0x80)
/*
* UTF-8 leading bytes have either 0xxxxxxx or 11xxxxxx
* bit representation. (see http://en.wikipedia.org/wiki/UTF-8)
* Therefore, the following pseudocode can detect UTF-8 leading bytes.
*
* if (!(byte & 0x80))
* byte |= 0x40; // turn on bit6
* return ((byte>>6) & 1); // bit6 represent whether this byte is leading or not.
*
* This function calculates whether a byte is leading or not for all bytes
* in the argument word by concurrently using the above logic, and then
* adds up the number of leading bytes in the word.
*/
static inline uintptr_t
count_utf8_lead_bytes_with_word(const uintptr_t *s)
{
uintptr_t d = *s;
/* Transform so that bit0 indicates whether we have a UTF-8 leading byte or not. */
d |= ~(d>>1);
d >>= 6;
d &= NONASCII_MASK >> 7;
/* Gather all bytes. */
d += (d>>8);
d += (d>>16);
#if SIZEOF_VOIDP == 8
d += (d>>32);
#endif
return (d&0xF);
}
#endif
static inline long
enc_strlen(const char *p, const char *e, rb_encoding *enc, int cr)
{
long c;
const char *q;
if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) {
return (e - p + rb_enc_mbminlen(enc) - 1) / 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;
}
#endif
else if (rb_enc_asciicompat(enc)) {
c = 0;
if (cr == ENC_CODERANGE_7BIT || cr == ENC_CODERANGE_VALID) {
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;
}
for (c=0; p<e; c++) {
p += rb_enc_mbclen(p, e, enc);
}
return c;
}
long
rb_enc_strlen(const char *p, const char *e, rb_encoding *enc)
{
return enc_strlen(p, e, enc, ENC_CODERANGE_UNKNOWN);
}
/* To get strlen with cr
* Note that given cr is not used.
*/
long
rb_enc_strlen_cr(const char *p, const char *e, rb_encoding *enc, int *cr)
{
long c;
const char *q;
int ret;
*cr = 0;
if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) {
return (e - p + rb_enc_mbminlen(enc) - 1) / 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;
}
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;
if (p + rb_enc_mbminlen(enc) <= e)
p += rb_enc_mbminlen(enc);
else
p = e;
}
}
if (!*cr) *cr = ENC_CODERANGE_7BIT;
return c;
}
/* enc must be str's enc or rb_enc_check(str, str2) */
static long
str_strlen(VALUE str, rb_encoding *enc)
{
const char *p, *e;
int cr;
if (single_byte_optimizable(str)) return RSTRING_LEN(str);
if (!enc) enc = STR_ENC_GET(str);
p = RSTRING_PTR(str);
e = RSTRING_END(str);
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;
}
else {
return enc_strlen(p, e, enc, cr);
}
}
long
rb_str_strlen(VALUE str)
{
return str_strlen(str, NULL);
}
/*
* call-seq:
* str.length -> integer
* str.size -> integer
*
* Returns the character length of <i>str</i>.
*/
VALUE
rb_str_length(VALUE str)
{
return LONG2NUM(str_strlen(str, NULL));
}
/*
* call-seq:
* str.bytesize -> integer
*
* Returns the length of +str+ in bytes.
*
* "\x80\u3042".bytesize #=> 4
* "hello".bytesize #=> 5
*/
static VALUE
rb_str_bytesize(VALUE str)
{
return LONG2NUM(RSTRING_LEN(str));
}
/*
* call-seq:
* str.empty? -> true or false
*
* Returns <code>true</code> if <i>str</i> has a length of zero.
*
* "hello".empty? #=> false
* " ".empty? #=> false
* "".empty? #=> true
*/
static VALUE
rb_str_empty(VALUE str)
{
if (RSTRING_LEN(str) == 0)
return Qtrue;
return Qfalse;
}
/*
* call-seq:
* str + other_str -> new_str
*
* Concatenation---Returns a new <code>String</code> containing
* <i>other_str</i> concatenated to <i>str</i>.
*
* "Hello from " + self.to_s #=> "Hello from main"
*/
VALUE
rb_str_plus(VALUE str1, VALUE str2)
{
VALUE str3;
rb_encoding *enc;
char *ptr1, *ptr2, *ptr3;
long len1, len2;
StringValue(str2);
enc = rb_enc_check(str1, str2);
RSTRING_GETMEM(str1, ptr1, len1);
RSTRING_GETMEM(str2, ptr2, len2);
str3 = rb_str_new(0, len1+len2);
ptr3 = RSTRING_PTR(str3);
memcpy(ptr3, ptr1, len1);
memcpy(ptr3+len1, ptr2, len2);
TERM_FILL(&ptr3[len1+len2], rb_enc_mbminlen(enc));
if (OBJ_TAINTED(str1) || OBJ_TAINTED(str2))
OBJ_TAINT(str3);
ENCODING_CODERANGE_SET(str3, rb_enc_to_index(enc),
ENC_CODERANGE_AND(ENC_CODERANGE(str1), ENC_CODERANGE(str2)));
RB_GC_GUARD(str1);
RB_GC_GUARD(str2);
return str3;
}
/*
* call-seq:
* str * integer -> new_str
*
* Copy --- Returns a new String containing +integer+ copies of the receiver.
* +integer+ must be greater than or equal to 0.
*
* "Ho! " * 3 #=> "Ho! Ho! Ho! "
* "Ho! " * 0 #=> ""
*/
VALUE
rb_str_times(VALUE str, VALUE times)
{
VALUE str2;
long n, len;
char *ptr2;
int termlen;
len = NUM2LONG(times);
if (len < 0) {
rb_raise(rb_eArgError, "negative argument");
}
if (len && LONG_MAX/len < RSTRING_LEN(str)) {
rb_raise(rb_eArgError, "argument too big");
}
len *= RSTRING_LEN(str);
termlen = TERM_LEN(str);
str2 = rb_str_new_with_class(str, 0, (len + termlen - 1));
ptr2 = RSTRING_PTR(str2);
if (len) {
n = RSTRING_LEN(str);
memcpy(ptr2, RSTRING_PTR(str), n);
while (n <= len/2) {
memcpy(ptr2 + n, ptr2, n);
n *= 2;
}
memcpy(ptr2 + n, ptr2, len-n);
}
STR_SET_LEN(str2, len);
TERM_FILL(&ptr2[len], termlen);
OBJ_INFECT(str2, str);
rb_enc_cr_str_copy_for_substr(str2, str);
return str2;
}
/*
* call-seq:
* str % arg -> new_str
*
* Format---Uses <i>str</i> as a format specification, and returns the result
* of applying it to <i>arg</i>. If the format specification contains more than
* one substitution, then <i>arg</i> must be an <code>Array</code> or <code>Hash</code>
* containing the values to be substituted. See <code>Kernel::sprintf</code> for
* details of the format string.
*
* "%05d" % 123 #=> "00123"
* "%-5s: %08x" % [ "ID", self.object_id ] #=> "ID : 200e14d6"
* "foo = %{foo}" % { :foo => 'bar' } #=> "foo = bar"
*/
static VALUE
rb_str_format_m(VALUE str, VALUE arg)
{
VALUE tmp = rb_check_array_type(arg);
if (!NIL_P(tmp)) {
VALUE rv = rb_str_format(RARRAY_LENINT(tmp), RARRAY_CONST_PTR(tmp), str);
RB_GC_GUARD(tmp);
return rv;
}
return rb_str_format(1, &arg, str);
}
static inline void
str_modifiable(VALUE str)
{
if (FL_TEST(str, STR_TMPLOCK)) {
rb_raise(rb_eRuntimeError, "can't modify string; temporarily locked");
}
rb_check_frozen(str);
}
static inline int
str_independent(VALUE str)
{
str_modifiable(str);
if (STR_EMBED_P(str) || !FL_TEST(str, STR_SHARED|STR_NOFREE)) {
return 1;
}
else {
return 0;
}
}
static void
str_make_independent_expand(VALUE str, long expand)
{
char *ptr;
long len = RSTRING_LEN(str);
const int termlen = TERM_LEN(str);
long capa = len + expand;
if (len > capa) len = capa;
if (capa + termlen - 1 <= RSTRING_EMBED_LEN_MAX && !STR_EMBED_P(str)) {
ptr = RSTRING(str)->as.heap.ptr;
STR_SET_EMBED(str);
memcpy(RSTRING(str)->as.ary, ptr, len);
TERM_FILL(RSTRING(str)->as.ary + len, termlen);
STR_SET_EMBED_LEN(str, len);
return;
}
ptr = ALLOC_N(char, capa + termlen);
if (RSTRING_PTR(str)) {
memcpy(ptr, RSTRING_PTR(str), len);
}
STR_SET_NOEMBED(str);
FL_UNSET(str, STR_SHARED|STR_NOFREE);
TERM_FILL(ptr + len, termlen);
RSTRING(str)->as.heap.ptr = ptr;
RSTRING(str)->as.heap.len = len;
RSTRING(str)->as.heap.aux.capa = capa;
}
#define str_make_independent(str) str_make_independent_expand((str), 0L)
void
rb_str_modify(VALUE str)
{
if (!str_independent(str))
str_make_independent(str);
ENC_CODERANGE_CLEAR(str);
}
void
rb_str_modify_expand(VALUE str, long expand)
{
if (expand < 0) {
rb_raise(rb_eArgError, "negative expanding string size");
}
if (!str_independent(str)) {
str_make_independent_expand(str, expand);
}
else if (expand > 0) {
long len = RSTRING_LEN(str);
long capa = len + expand;
int termlen = TERM_LEN(str);
if (!STR_EMBED_P(str)) {
REALLOC_N(RSTRING(str)->as.heap.ptr, char, capa + termlen);
RSTRING(str)->as.heap.aux.capa = capa;
}
else if (capa + termlen > RSTRING_EMBED_LEN_MAX + 1) {
str_make_independent_expand(str, expand);
}
}
ENC_CODERANGE_CLEAR(str);
}
/* As rb_str_modify(), but don't clear coderange */
static void
str_modify_keep_cr(VALUE str)
{
if (!str_independent(str))
str_make_independent(str);
if (ENC_CODERANGE(str) == ENC_CODERANGE_BROKEN)
/* Force re-scan later */
ENC_CODERANGE_CLEAR(str);
}
static inline void
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;
}
}
void
rb_must_asciicompat(VALUE str)
{
rb_encoding *enc = rb_enc_get(str);
if (!rb_enc_asciicompat(enc)) {
rb_raise(rb_eEncCompatError, "ASCII incompatible encoding: %s", rb_enc_name(enc));
}
}
VALUE
rb_string_value(volatile VALUE *ptr)
{
VALUE s = *ptr;
if (!RB_TYPE_P(s, T_STRING)) {
s = rb_str_to_str(s);
*ptr = s;
}
return s;
}
char *
rb_string_value_ptr(volatile VALUE *ptr)
{
VALUE str = rb_string_value(ptr);
return RSTRING_PTR(str);
}
static int
zero_filled(const char *s, int n)
{
for (; n > 0; --n) {
if (*s++) return 0;
}
return 1;
}
static const char *
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;
}
return 0;
}
static char *
str_fill_term(VALUE str, char *s, long len, int oldtermlen, int termlen)
{
long capa = rb_str_capacity(str) + 1;
if (capa < len + termlen) {
rb_str_modify_expand(str, termlen);
}
else if (!str_independent(str)) {
if (zero_filled(s + len, termlen)) return s;
str_make_independent(str);
}
s = RSTRING_PTR(str);
TERM_FILL(s + len, termlen);
return s;
}
char *
rb_string_value_cstr(volatile VALUE *ptr)
{
VALUE str = rb_string_value(ptr);
char *s = RSTRING_PTR(str);
long len = RSTRING_LEN(str);
rb_encoding *enc = rb_enc_get(str);
const int minlen = rb_enc_mbminlen(enc);
if (minlen > 1) {
if (str_null_char(s, len, minlen, enc)) {
rb_raise(rb_eArgError, "string contains null char");
}
return str_fill_term(str, s, len, minlen, minlen);
}
if (!s || memchr(s, 0, len)) {
rb_raise(rb_eArgError, "string contains null byte");
}
if (s[len]) {
rb_str_modify(str);
s = RSTRING_PTR(str);
s[RSTRING_LEN(str)] = 0;
}
return s;
}
void
rb_str_fill_terminator(VALUE str, const int newminlen)
{
char *s = RSTRING_PTR(str);
long len = RSTRING_LEN(str);
rb_encoding *enc = rb_enc_get(str);
str_fill_term(str, s, len, rb_enc_mbminlen(enc), newminlen);
}
VALUE
rb_check_string_type(VALUE str)
{
str = rb_check_convert_type(str, T_STRING, "String", "to_str");
return str;
}
/*
* call-seq:
* String.try_convert(obj) -> string or nil
*
* Try to convert <i>obj</i> into a String, using to_str method.
* Returns converted string or nil if <i>obj</i> cannot be converted
* for any reason.
*
* String.try_convert("str") #=> "str"
* String.try_convert(/re/) #=> nil
*/
static VALUE
rb_str_s_try_convert(VALUE dummy, VALUE str)
{
return rb_check_string_type(str);
}
static char*
str_nth_len(const char *p, const char *e, long *nthp, rb_encoding *enc)
{
long nth = *nthp;
if (rb_enc_mbmaxlen(enc) == 1) {
p += nth;
}
else if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) {
p += nth * rb_enc_mbmaxlen(enc);
}
else if (rb_enc_asciicompat(enc)) {
const char *p2, *e2;
int n;
while (p < e && 0 < nth) {
e2 = p + nth;
if (e < e2) {
*nthp = nth;
return (char *)e;
}
if (ISASCII(*p)) {
p2 = search_nonascii(p, e2);
if (!p2) {
nth -= e2 - p;
*nthp = nth;
return (char *)e2;
}
nth -= p2 - p;
p = p2;
}
n = rb_enc_mbclen(p, e, enc);
p += n;
nth--;
}
*nthp = nth;
if (nth != 0) {
return (char *)e;
}
return (char *)p;
}
else {
while (p < e && nth--) {
p += rb_enc_mbclen(p, e, enc);
}
}
if (p > e) p = e;
*nthp = nth;
return (char*)p;
}
char*
rb_enc_nth(const char *p, const char *e, long nth, rb_encoding *enc)
{
return str_nth_len(p, e, &nth, enc);
}
static char*
str_nth(const char *p, const char *e, long nth, rb_encoding *enc, int singlebyte)
{
if (singlebyte)
p += nth;
else {
p = str_nth_len(p, e, &nth, enc);
}
if (!p) return 0;
if (p > e) p = e;
return (char *)p;
}
/* char offset to byte offset */
static long
str_offset(const char *p, const char *e, long nth, rb_encoding *enc, int singlebyte)
{
const char *pp = str_nth(p, e, nth, enc, singlebyte);
if (!pp) return e - p;
return pp - p;
}
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));
}
#ifdef NONASCII_MASK
static char *
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;
}
while (p < e) {
if (is_utf8_lead_byte(*p)) {
if (nth == 0) break;
nth--;
}
p++;
}
*nthp = nth;
return (char *)p;
}
static long
str_utf8_offset(const char *p, const char *e, long nth)
{
const char *pp = str_utf8_nth(p, e, &nth);
return pp - p;
}
#endif
/* byte offset to char offset */
long
rb_str_sublen(VALUE str, long pos)
{
if (single_byte_optimizable(str) || pos < 0)
return pos;
else {
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)
{
VALUE str2;
if (RSTRING_EMBED_LEN_MAX < len && SHARABLE_SUBSTRING_P(beg, len, RSTRING_LEN(str))) {
long olen;
str2 = rb_str_new_shared(rb_str_new_frozen(str));
RSTRING(str2)->as.heap.ptr += beg;
olen = RSTRING(str2)->as.heap.len;
if (olen > len) RSTRING(str2)->as.heap.len = len;
}
else {
str2 = rb_str_new_with_class(str, RSTRING_PTR(str)+beg, len);
RB_GC_GUARD(str);
}
rb_enc_cr_str_copy_for_substr(str2, str);
OBJ_INFECT(str2, str);
return str2;
}
char *
rb_str_subpos(VALUE str, long beg, long *lenp)
{
long len = *lenp;
long slen = -1L;
long blen = RSTRING_LEN(str);
rb_encoding *enc = STR_ENC_GET(str);
char *p, *s = RSTRING_PTR(str), *e = s + blen;
if (len < 0) return 0;
if (!blen) {
len = 0;
}
if (single_byte_optimizable(str)) {
if (beg > blen) return 0;
if (beg < 0) {
beg += blen;
if (beg < 0) return 0;
}
if (beg + len > blen)
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;
}
}
else if (beg > 0 && beg > RSTRING_LEN(str)) {
return 0;
}
if (len == 0) {
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 &&
enc == rb_utf8_encoding()) {
p = str_utf8_nth(s, e, &beg);
if (beg > 0) return 0;
len = str_utf8_offset(p, e, len);
}
#endif
else if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) {
int char_sz = rb_enc_mbmaxlen(enc);
p = s + beg * char_sz;
if (p > e) {
return 0;
}
else if (len * char_sz > e - p)
len = e - p;
else
len *= char_sz;
}
else if ((p = str_nth_len(s, e, &beg, enc)) == e) {
if (beg > 0) return 0;
len = 0;
}
else {
len = str_offset(p, e, len, enc, 0);
}
end:
*lenp = len;
RB_GC_GUARD(str);
return p;
}
VALUE
rb_str_substr(VALUE str, long beg, long len)
{
VALUE str2;
char *p = rb_str_subpos(str, beg, &len);
if (!p) return Qnil;
if (len > RSTRING_EMBED_LEN_MAX && 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_obj_class(str2), str2);
RSTRING(str2)->as.heap.ptr += ofs;
RSTRING(str2)->as.heap.len = len;
}
else {
str2 = rb_str_new_with_class(str, p, len);
OBJ_INFECT(str2, str);
RB_GC_GUARD(str);
}
rb_enc_cr_str_copy_for_substr(str2, str);
return str2;
}
VALUE
rb_str_freeze(VALUE str)
{
if (OBJ_FROZEN(str)) return str;
rb_str_resize(str, RSTRING_LEN(str));
return rb_obj_freeze(str);
}
RUBY_ALIAS_FUNCTION(rb_str_dup_frozen(VALUE str), rb_str_new_frozen, (str))
#define rb_str_dup_frozen rb_str_new_frozen
VALUE
rb_str_locktmp(VALUE str)
{
if (FL_TEST(str, STR_TMPLOCK)) {
rb_raise(rb_eRuntimeError, "temporal locking already locked string");
}
FL_SET(str, STR_TMPLOCK);
return str;
}
VALUE
rb_str_unlocktmp(VALUE str)
{
if (!FL_TEST(str, STR_TMPLOCK)) {
rb_raise(rb_eRuntimeError, "temporal unlocking already unlocked string");
}
FL_UNSET(str, STR_TMPLOCK);
return str;
}
VALUE
rb_str_locktmp_ensure(VALUE str, VALUE (*func)(VALUE), VALUE arg)
{
rb_str_locktmp(str);
return rb_ensure(func, arg, rb_str_unlocktmp, str);
}
void
rb_str_set_len(VALUE str, long len)
{
long capa;
const int termlen = TERM_LEN(str);
str_modifiable(str);
if (STR_SHARED_P(str)) {
rb_raise(rb_eRuntimeError, "can't set length of shared string");
}
if (len + termlen - 1 > (capa = (long)rb_str_capacity(str))) {
rb_bug("probable buffer overflow: %ld for %ld", len, capa);
}
STR_SET_LEN(str, len);
TERM_FILL(&RSTRING_PTR(str)[len], termlen);
}
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)");
}
independent = str_independent(str);
ENC_CODERANGE_CLEAR(str);
slen = RSTRING_LEN(str);
{
long capa;
const int termlen = TERM_LEN(str);
if (STR_EMBED_P(str)) {
if (len == slen) return str;
if (len + termlen <= RSTRING_EMBED_LEN_MAX + 1) {
STR_SET_EMBED_LEN(str, len);
TERM_FILL(RSTRING(str)->as.ary + len, termlen);
return str;
}
str_make_independent_expand(str, len - slen);
}
else if (len + termlen <= RSTRING_EMBED_LEN_MAX + 1) {
char *ptr = STR_HEAP_PTR(str);
STR_SET_EMBED(str);
if (slen > len) slen = len;
if (slen > 0) MEMCPY(RSTRING(str)->as.ary, ptr, char, slen);
TERM_FILL(RSTRING(str)->as.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, len - slen);
}
else if ((capa = RSTRING(str)->as.heap.aux.capa) < len ||
(capa - len) > (len < 1024 ? len : 1024)) {
REALLOC_N(RSTRING(str)->as.heap.ptr, char, len + termlen);
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 */
}
return str;
}
static VALUE
str_buf_cat(VALUE str, const char *ptr, long len)
{
long capa, total, olen, off = -1;
char *sptr;
const int termlen = TERM_LEN(str);
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 = RSTRING_EMBED_LEN_MAX;
sptr = RSTRING(str)->as.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;
}
if (olen >= LONG_MAX - len) {
rb_raise(rb_eArgError, "string sizes too big");
}
total = olen + len;
if (capa <= total) {
while (total > capa) {
if (capa > LONG_MAX / 2) {
capa = (total + 4095) / 4096 * 4096;
break;
}
capa = 2 * capa;
}
RESIZE_CAPA_TERM(str, capa, termlen);
sptr = RSTRING_PTR(str);
}
if (off != -1) {
ptr = sptr + off;
}
memcpy(sptr + olen, ptr, len);
STR_SET_LEN(str, total);
TERM_FILL(sptr + total, termlen); /* sentinel */
return str;
}
#define str_buf_cat2(str, ptr) str_buf_cat((str), (ptr), strlen(ptr))
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)");
}
return str_buf_cat(str, ptr, len);
}
VALUE
rb_str_cat_cstr(VALUE str, const char *ptr)
{
must_not_null(ptr);
return rb_str_buf_cat(str, ptr, strlen(ptr));
}
RUBY_ALIAS_FUNCTION(rb_str_buf_cat(VALUE str, const char *ptr, long len), rb_str_cat, (str, ptr, len))
RUBY_ALIAS_FUNCTION(rb_str_buf_cat2(VALUE str, const char *ptr), rb_str_cat_cstr, (str, ptr))
RUBY_ALIAS_FUNCTION(rb_str_cat2(VALUE str, const char *ptr), rb_str_cat_cstr, (str, ptr))
static VALUE
rb_enc_cr_str_buf_cat(VALUE str, const char *ptr, long len,
int ptr_encindex, int ptr_cr, int *ptr_cr_ret)
{
int str_encindex = ENCODING_GET(str);
int res_encindex;
int str_cr, res_cr;
rb_encoding *str_enc, *ptr_enc;
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) {
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);
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);
return str;
}
goto incompatible;
}
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);
}
}
}
if (ptr_cr_ret)
*ptr_cr_ret = ptr_cr;
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);
incompatible:
rb_raise(rb_eEncCompatError, "incompatible character encodings: %s and %s",
rb_enc_name(str_enc), rb_enc_name(ptr_enc));
}
if (str_cr == ENC_CODERANGE_UNKNOWN) {
res_encindex = str_encindex;
res_cr = ENC_CODERANGE_UNKNOWN;
}
else if (str_cr == ENC_CODERANGE_7BIT) {
if (ptr_cr == ENC_CODERANGE_7BIT) {
res_encindex = str_encindex;
res_cr = ENC_CODERANGE_7BIT;
}
else {
res_encindex = ptr_encindex;
res_cr = ptr_cr;
}
}
else if (str_cr == ENC_CODERANGE_VALID) {
res_encindex = str_encindex;
if (ptr_cr == ENC_CODERANGE_7BIT || ptr_cr == ENC_CODERANGE_VALID)
res_cr = str_cr;
else
res_cr = ptr_cr;
}
else { /* str_cr == ENC_CODERANGE_BROKEN */
res_encindex = str_encindex;
res_cr = str_cr;
if (0 < len) res_cr = ENC_CODERANGE_UNKNOWN;
}
if (len < 0) {
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);
return str;
}
VALUE
rb_enc_str_buf_cat(VALUE str, const char *ptr, long len, rb_encoding *ptr_enc)
{
return rb_enc_cr_str_buf_cat(str, ptr, len,
rb_enc_to_index(ptr_enc), ENC_CODERANGE_UNKNOWN, NULL);
}
VALUE
rb_str_buf_cat_ascii(VALUE str, const char *ptr)
{
/* ptr must reference NUL terminated ASCII string. */
int encindex = ENCODING_GET(str);
rb_encoding *enc = rb_enc_from_index(encindex);
if (rb_enc_asciicompat(enc)) {
return rb_enc_cr_str_buf_cat(str, ptr, strlen(ptr),
encindex, ENC_CODERANGE_7BIT, 0);
}
else {
char *buf = ALLOCA_N(char, rb_enc_mbmaxlen(enc));
while (*ptr) {
unsigned int c = (unsigned char)*ptr;
int len = rb_enc_codelen(c, enc);
rb_enc_mbcput(c, buf, enc);
rb_enc_cr_str_buf_cat(str, buf, len,
encindex, ENC_CODERANGE_VALID, 0);
ptr++;
}
return str;
}
}
VALUE
rb_str_buf_append(VALUE str, VALUE str2)
{
int str2_cr;
str2_cr = ENC_CODERANGE(str2);
rb_enc_cr_str_buf_cat(str, RSTRING_PTR(str2), RSTRING_LEN(str2),
ENCODING_GET(str2), str2_cr, &str2_cr);
OBJ_INFECT(str, str2);
ENC_CODERANGE_SET(str2, str2_cr);
return str;
}
VALUE
rb_str_append(VALUE str, VALUE str2)
{
StringValue(str2);
return rb_str_buf_append(str, str2);
}
/*
* call-seq:
* str << integer -> str
* str.concat(integer) -> str
* str << obj -> str
* str.concat(obj) -> str
*
* Append---Concatenates the given object to <i>str</i>. If the object is a
* <code>Integer</code>, it is considered as a codepoint, and is converted
* to a character before concatenation.
*
* a = "hello "
* a << "world" #=> "hello world"
* a.concat(33) #=> "hello world!"
*/
VALUE
rb_str_concat(VALUE str1, VALUE str2)
{
unsigned int code;
rb_encoding *enc = STR_ENC_GET(str1);
if (FIXNUM_P(str2) || RB_TYPE_P(str2, T_BIGNUM)) {
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);
}
if (enc == rb_usascii_encoding()) {
/* 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 (code > 127) {
rb_enc_associate(str1, rb_ascii8bit_encoding());
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);
}
return str1;
}
/*
* call-seq:
* str.prepend(other_str) -> str
*
* Prepend---Prepend the given string to <i>str</i>.
*
* a = "world"
* a.prepend("hello ") #=> "hello world"
* a #=> "hello world"
*/
static VALUE
rb_str_prepend(VALUE str, VALUE str2)
{
StringValue(str2);
StringValue(str);
rb_str_update(str, 0L, 0L, str2);
return 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;
}
return rb_memhash((const void *)RSTRING_PTR(str), RSTRING_LEN(str)) ^ e;
}
int
rb_str_hash_cmp(VALUE str1, VALUE str2)
{
long len;
if (!rb_str_comparable(str1, str2)) return 1;
if (RSTRING_LEN(str1) == (len = RSTRING_LEN(str2)) &&
memcmp(RSTRING_PTR(str1), RSTRING_PTR(str2), len) == 0) {
return 0;
}
return 1;
}
/*
* call-seq:
* str.hash -> fixnum
*
* Return a hash based on the string's length, content and encoding.
*
* See also Object#hash.
*/
static VALUE
rb_str_hash_m(VALUE str)
{
st_index_t hval = rb_str_hash(str);
return INT2FIX(hval);
}
#define lesser(a,b) (((a)>(b))?(b):(a))
int
rb_str_comparable(VALUE str1, VALUE str2)
{
int idx1, idx2;
int rc1, rc2;
if (RSTRING_LEN(str1) == 0) return TRUE;
if (RSTRING_LEN(str2) == 0) return TRUE;
idx1 = ENCODING_GET(str1);
idx2 = ENCODING_GET(str2);
if (idx1 == idx2) return TRUE;
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) {
if (rb_enc_asciicompat(rb_enc_from_index(idx1)))
return TRUE;
}
return FALSE;
}
int
rb_str_cmp(VALUE str1, VALUE str2)
{
long len1, len2;
const char *ptr1, *ptr2;
int retval;
if (str1 == str2) return 0;
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 (retval > 0) return 1;
return -1;
}
/* expect tail call optimization */
static VALUE
str_eql(const VALUE str1, const VALUE str2)
{
const long len = RSTRING_LEN(str1);
const char *ptr1, *ptr2;
if (len != RSTRING_LEN(str2)) return Qfalse;
if (!rb_str_comparable(str1, str2)) return Qfalse;
if ((ptr1 = RSTRING_PTR(str1)) == (ptr2 = RSTRING_PTR(str2)))
return Qtrue;
if (memcmp(ptr1, ptr2, len) == 0)
return Qtrue;
return Qfalse;
}
/*
* call-seq:
* str == obj -> true or false
* str === obj -> true or false
*
* === Equality
*
* Returns whether +str+ == +obj+, similar to Object#==.
*
* If +obj+ is not an instance of String but responds to +to_str+, then the
* two strings are compared using case equality Object#===.
*
* Otherwise, returns similarly to String#eql?, comparing length and content.
*/
VALUE
rb_str_equal(VALUE str1, VALUE str2)
{
if (str1 == str2) return Qtrue;
if (!RB_TYPE_P(str2, T_STRING)) {
if (!rb_respond_to(str2, rb_intern("to_str"))) {
return Qfalse;
}
return rb_equal(str2, str1);
}
return str_eql(str1, str2);
}
/*
* call-seq:
* str.eql?(other) -> true or false
*
* Two strings are equal if they have the same length and content.
*/
static VALUE
rb_str_eql(VALUE str1, VALUE str2)
{
if (str1 == str2) return Qtrue;
if (!RB_TYPE_P(str2, T_STRING)) return Qfalse;
return str_eql(str1, str2);
}
/*
* call-seq:
* string <=> other_string -> -1, 0, +1 or nil
*
*
* Comparison---Returns -1, 0, +1 or nil depending on whether +string+ is less
* than, equal to, or greater than +other_string+.
*
* +nil+ is returned if the two values are incomparable.
*
* If the strings are of different lengths, and the strings are equal when
* compared up to the shortest length, then the longer string is considered
* greater than the shorter one.
*
* <code><=></code> is the basis for the methods <code><</code>,
* <code><=</code>, <code>></code>, <code>>=</code>, and
* <code>between?</code>, included from module Comparable. The method
* String#== does not use Comparable#==.
*
* "abcdef" <=> "abcde" #=> 1
* "abcdef" <=> "abcdef" #=> 0
* "abcdef" <=> "abcdefg" #=> -1
* "abcdef" <=> "ABCDEF" #=> 1
* "abcdef" <=> 1 #=> nil
*/
static VALUE
rb_str_cmp_m(VALUE str1, VALUE str2)
{
int result;
if (!RB_TYPE_P(str2, T_STRING)) {
VALUE tmp = rb_check_funcall(str2, rb_intern("to_str"), 0, 0);
if (RB_TYPE_P(tmp, T_STRING)) {
result = rb_str_cmp(str1, tmp);
}
else {
return rb_invcmp(str1, str2);
}
}
else {
result = rb_str_cmp(str1, str2);
}
return INT2FIX(result);
}
/*
* call-seq:
* str.casecmp(other_str) -> -1, 0, +1 or nil
*
* Case-insensitive version of <code>String#<=></code>.
*
* "abcdef".casecmp("abcde") #=> 1
* "aBcDeF".casecmp("abcdef") #=> 0
* "abcdef".casecmp("abcdefg") #=> -1
* "abcdef".casecmp("ABCDEF") #=> 0
*/
static VALUE
rb_str_casecmp(VALUE str1, VALUE str2)
{
long len;
rb_encoding *enc;
char *p1, *p1end, *p2, *p2end;
StringValue(str2);
enc = rb_enc_compatible(str1, str2);
if (!enc) {
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) {
unsigned int c1 = TOUPPER(*p1 & 0xff);
unsigned int c2 = TOUPPER(*p2 & 0xff);
if (c1 != c2)
return INT2FIX(c1 < c2 ? -1 : 1);
}
p1++;
p2++;
}
}
else {
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);
if (0 <= c1 && 0 <= c2) {
c1 = TOUPPER(c1);
c2 = TOUPPER(c2);
if (c1 != c2)
return INT2FIX(c1 < c2 ? -1 : 1);
}
else {
int r;
l1 = rb_enc_mbclen(p1, p1end, enc);
l2 = rb_enc_mbclen(p2, p2end, enc);
len = l1 < l2 ? l1 : l2;
r = memcmp(p1, p2, len);
if (r != 0)
return INT2FIX(r < 0 ? -1 : 1);
if (l1 != l2)
return INT2FIX(l1 < l2 ? -1 : 1);
}
p1 += l1;
p2 += l2;
}
}
if (RSTRING_LEN(str1) == RSTRING_LEN(str2)) return INT2FIX(0);
if (RSTRING_LEN(str1) > RSTRING_LEN(str2)) return INT2FIX(1);
return INT2FIX(-1);
}
#define rb_str_index(str, sub, offset) rb_strseq_index(str, sub, offset, 0)
static long
rb_strseq_index(VALUE str, VALUE sub, long offset, int in_byte)
{
const char *s, *sptr, *e;
long pos, len, slen;
int single_byte = single_byte_optimizable(str);
rb_encoding *enc;
enc = rb_enc_check(str, sub);
if (is_broken_string(sub)) return -1;
len = (in_byte || single_byte) ? RSTRING_LEN(str) : str_strlen(str, enc); /* rb_enc_check */
slen = in_byte ? RSTRING_LEN(sub) : str_strlen(sub, enc); /* rb_enc_check */
if (offset < 0) {
offset += len;
if (offset < 0) return -1;
}
if (len - offset < slen) return -1;
s = RSTRING_PTR(str);
e = RSTRING_END(str);
if (offset) {
if (!in_byte) offset = str_offset(s, e, offset, enc, single_byte);
s += offset;
}
if (slen == 0) return offset;
/* need proceed one character at a time */
sptr = RSTRING_PTR(sub);
slen = RSTRING_LEN(sub);
len = RSTRING_LEN(str) - offset;
for (;;) {
const char *t;
pos = rb_memsearch(sptr, slen, s, len, enc);
if (pos < 0) return pos;
t = rb_enc_right_char_head(s, s+pos, e, enc);
if (t == s + pos) break;
len -= t - s;
if (len <= 0) return -1;
offset += t - s;
s = t;
}
return pos + offset;
}
/*
* call-seq:
* str.index(substring [, offset]) -> fixnum or nil
* str.index(regexp [, offset]) -> fixnum or nil
*
* Returns the index of the first occurrence of the given <i>substring</i> or
* pattern (<i>regexp</i>) in <i>str</i>. Returns <code>nil</code> if not
* found. If the second parameter is present, it specifies the position in the
* string to begin the search.
*
* "hello".index('e') #=> 1
* "hello".index('lo') #=> 3
* "hello".index('a') #=> nil
* "hello".index(?e) #=> 1
* "hello".index(/[aeiou]/, -3) #=> 4
*/
static VALUE
rb_str_index_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);
}
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;
}
}
if (SPECIAL_CONST_P(sub)) goto generic;
switch (BUILTIN_TYPE(sub)) {
case 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));
pos = rb_reg_search(sub, str, pos, 0);
pos = rb_str_sublen(str, pos);
break;
generic:
default: {
VALUE tmp;
tmp = rb_check_string_type(sub);
if (NIL_P(tmp)) {
rb_raise(rb_eTypeError, "type mismatch: %s given",
rb_obj_classname(sub));
}
sub = tmp;
}
/* fall through */
case T_STRING:
pos = rb_str_index(str, sub, pos);
pos = rb_str_sublen(str, pos);
break;
}
if (pos == -1) return Qnil;
return LONG2NUM(pos);
}
#ifdef HAVE_MEMRCHR
static long
str_rindex(VALUE str, VALUE sub, const char *s, long pos, 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);
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;
} while (searchlen > 0);
return -1;
}
#else
static long
str_rindex(VALUE str, VALUE sub, const char *s, long pos, rb_encoding *enc)
{
long slen;
char *sbeg, *e, *t;
sbeg = RSTRING_PTR(str);
e = RSTRING_END(str);
t = RSTRING_PTR(sub);
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);
}
return -1;
}
#endif
static long
rb_str_rindex(VALUE str, VALUE sub, long pos)
{
long len, slen;
char *sbeg, *s;
rb_encoding *enc;
int singlebyte;
enc = rb_enc_check(str, sub);
if (is_broken_string(sub)) return -1;
singlebyte = single_byte_optimizable(str);
len = singlebyte ? RSTRING_LEN(str) : str_strlen(str, enc); /* rb_enc_check */
slen = str_strlen(sub, enc); /* rb_enc_check */
/* 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 = str_nth(sbeg, RSTRING_END(str), pos, enc, singlebyte);
return str_rindex(str, sub, s, pos, enc);
}
/*
* call-seq:
* str.rindex(substring [, fixnum]) -> fixnum or nil
* str.rindex(regexp [, fixnum]) -> fixnum or nil
*
* Returns the index of the last occurrence of the given <i>substring</i> or
* pattern (<i>regexp</i>) in <i>str</i>. Returns <code>nil</code> if not
* found. If the second parameter is present, it specifies the position in the
* string to end the search---characters beyond this point will not be
* considered.
*
* "hello".rindex('e') #=> 1
* "hello".rindex('l') #=> 3
* "hello".rindex('a') #=> nil
* "hello".rindex(?e) #=> 1
* "hello".rindex(/[aeiou]/, -2) #=> 1
*/
static VALUE
rb_str_rindex_m(int argc, VALUE *argv, VALUE str)
{
VALUE sub;
VALUE vpos;
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;
}
else {
pos = len;
}
if (SPECIAL_CONST_P(sub)) goto generic;
switch (BUILTIN_TYPE(sub)) {
case T_REGEXP:
/* enc = rb_get_check(str, sub); */
pos = str_offset(RSTRING_PTR(str), RSTRING_END(str), pos,
enc, single_byte_optimizable(str));
if (!RREGEXP(sub)->ptr || RREGEXP_SRC_LEN(sub)) {
pos = rb_reg_search(sub, str, pos, 1);
pos = rb_str_sublen(str, pos);
}
if (pos >= 0) return LONG2NUM(pos);
break;
generic:
default: {
VALUE tmp;
tmp = rb_check_string_type(sub);
if (NIL_P(tmp)) {
rb_raise(rb_eTypeError, "type mismatch: %s given",
rb_obj_classname(sub));
}
sub = tmp;
}
/* fall through */
case T_STRING:
pos = rb_str_rindex(str, sub, pos);
if (pos >= 0) return LONG2NUM(pos);
break;
}
return Qnil;
}
/*
* call-seq:
* str =~ obj -> fixnum or nil
*
* Match---If <i>obj</i> is a <code>Regexp</code>, use it as a pattern to match
* against <i>str</i>,and returns the position the match starts, or
* <code>nil</code> if there is no match. Otherwise, invokes
* <i>obj.=~</i>, passing <i>str</i> as an argument. The default
* <code>=~</code> in <code>Object</code> returns <code>nil</code>.
*
* Note: <code>str =~ regexp</code> is not the same as
* <code>regexp =~ str</code>. Strings captured from named capture groups
* are assigned to local variables only in the second case.
*
* "cat o' 9 tails" =~ /\d/ #=> 7
* "cat o' 9 tails" =~ 9 #=> nil
*/
static VALUE
rb_str_match(VALUE x, VALUE y)
{
if (SPECIAL_CONST_P(y)) goto generic;
switch (BUILTIN_TYPE(y)) {
case T_STRING:
rb_raise(rb_eTypeError, "type mismatch: String given");
case T_REGEXP:
return rb_reg_match(y, x);
generic:
default:
return rb_funcall(y, rb_intern("=~"), 1, x);
}
}
static VALUE get_pat(VALUE);
/*
* call-seq:
* str.match(pattern) -> matchdata or nil
* str.match(pattern, pos) -> matchdata or nil
*
* Converts <i>pattern</i> to a <code>Regexp</code> (if it isn't already one),
* then invokes its <code>match</code> method on <i>str</i>. If the second
* parameter is present, it specifies the position in the string to begin the
* search.
*
* 'hello'.match('(.)\1') #=> #<MatchData "ll" 1:"l">
* 'hello'.match('(.)\1')[0] #=> "ll"
* 'hello'.match(/(.)\1/)[0] #=> "ll"
* 'hello'.match('xx') #=> nil
*
* If a block is given, invoke the block with MatchData if match succeed, so
* that you can write
*
* str.match(pat) {|m| ...}
*
* instead of
*
* if m = str.match(pat)
* ...
* end
*
* The return value is a value from block execution in this case.
*/
static VALUE
rb_str_match_m(int argc, VALUE *argv, VALUE str)
{
VALUE re, result;
if (argc < 1)
rb_check_arity(argc, 1, 2);
re = argv[0];
argv[0] = str;
result = rb_funcall2(get_pat(re), rb_intern("match"), argc, argv);
if (!NIL_P(result) && rb_block_given_p()) {
return rb_yield(result);
}
return result;
}
enum neighbor_char {
NEIGHBOR_NOT_CHAR,
NEIGHBOR_FOUND,
NEIGHBOR_WRAPPED
};
static enum neighbor_char
enc_succ_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) + 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--)
p[i] = '\0';
if (i < 0)
return NEIGHBOR_WRAPPED;
++((unsigned char*)p)[i];
l = rb_enc_precise_mbclen(p, p+len, enc);
if (MBCLEN_CHARFOUND_P(l)) {
l = MBCLEN_CHARFOUND_LEN(l);
if (l == len) {
return NEIGHBOR_FOUND;
}
else {
memset(p+l, 0xff, len-l);
}
}
if (MBCLEN_INVALID_P(l) && i < len-1) {
long len2;
int l2;
for (len2 = len-1; 0 < len2; len2--) {
l2 = rb_enc_precise_mbclen(p, p+len2, enc);
if (!MBCLEN_INVALID_P(l2))
break;
}
memset(p+len2+1, 0xff, len-(len2+1));
}
}
}
static enum neighbor_char
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;
}
while (1) {
for (i = len-1; 0 <= i && (unsigned char)p[i] == 0; i--)
p[i] = '\xff';
if (i < 0)
return NEIGHBOR_WRAPPED;
--((unsigned char*)p)[i];
l = rb_enc_precise_mbclen(p, p+len, enc);
if (MBCLEN_CHARFOUND_P(l)) {
l = MBCLEN_CHARFOUND_LEN(l);
if (l == len) {
return NEIGHBOR_FOUND;
}
else {
memset(p+l, 0, len-l);
}
}
if (MBCLEN_INVALID_P(l) && i < len-1) {
long len2;
int l2;
for (len2 = len-1; 0 < len2; len2--) {
l2 = rb_enc_precise_mbclen(p, p+len2, enc);
if (!MBCLEN_INVALID_P(l2))
break;
}
memset(p+len2+1, 0, len-(len2+1));
}
}
}
/*
overwrite +p+ by succeeding letter in +enc+ and returns
NEIGHBOR_FOUND or NEIGHBOR_WRAPPED.
When NEIGHBOR_WRAPPED, carried-out letter is stored into carry.
assuming each ranges are successive, and mbclen
never change in each ranges.
NEIGHBOR_NOT_CHAR is returned if invalid character or the range has only one
character.
*/
static enum neighbor_char
enc_succ_alnum_char(char *p, long len, rb_encoding *enc, char *carry)
{
enum neighbor_char ret;
unsigned int c;
int ctype;
int range;
char save[ONIGENC_CODE_TO_MBC_MAXLEN];
c = rb_enc_mbc_to_codepoint(p, p+len, enc);
if (rb_enc_isctype(c, ONIGENC_CTYPE_DIGIT, enc))
ctype = ONIGENC_CTYPE_DIGIT;
else if (rb_enc_isctype(c, ONIGENC_CTYPE_ALPHA, enc))
ctype = ONIGENC_CTYPE_ALPHA;
else
return NEIGHBOR_NOT_CHAR;
MEMCPY(save, p, char, len);
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;
while (1) {
MEMCPY(save, p, char, len);
ret = enc_pred_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)) {
MEMCPY(p, save, char, len);
break;
}
}
else {
MEMCPY(p, save, char, len);
break;
}
range++;
}
if (range == 1) {
return NEIGHBOR_NOT_CHAR;
}
if (ctype != ONIGENC_CTYPE_DIGIT) {
MEMCPY(carry, p, char, len);
return NEIGHBOR_WRAPPED;
}
MEMCPY(carry, p, char, len);
enc_succ_char(carry, len, enc);
return NEIGHBOR_WRAPPED;
}
static VALUE str_succ(VALUE str);
/*
* call-seq:
* str.succ -> new_str
* str.next -> new_str
*
* Returns the successor to <i>str</i>. The successor is calculated by
* incrementing characters starting from the rightmost alphanumeric (or
* the rightmost character if there are no alphanumerics) in the
* string. Incrementing a digit always results in another digit, and
* incrementing a letter results in another letter of the same case.
* Incrementing nonalphanumerics uses the underlying character set's
* collating sequence.
*
* If the increment generates a ``carry,'' the character to the left of
* it is incremented. This process repeats until there is no carry,
* adding an additional character if necessary.
*
* "abcd".succ #=> "abce"
* "THX1138".succ #=> "THX1139"
* "<<koala>>".succ #=> "<<koalb>>"
* "1999zzz".succ #=> "2000aaa"
* "ZZZ9999".succ #=> "AAAA0000"
* "***".succ #=> "**+"
*/
VALUE
rb_str_succ(VALUE orig)
{
VALUE str;
str = rb_str_new_with_class(orig, RSTRING_PTR(orig), RSTRING_LEN(orig));
rb_enc_cr_str_copy_for_substr(str, orig);
OBJ_INFECT(str, orig);
return str_succ(str);
}
static VALUE
str_succ(VALUE str)
{
rb_encoding *enc;
char *sbeg, *s, *e, *last_alnum = 0;
int c = -1;
long l, slen;
char carry[ONIGENC_CODE_TO_MBC_MAXLEN] = "\1";
long carry_pos = 0, carry_len = 1;
enum neighbor_char neighbor = NEIGHBOR_FOUND;
slen = RSTRING_LEN(str);
if (slen == 0) return str;
enc = STR_ENC_GET(str);
sbeg = RSTRING_PTR(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) {
s = last_alnum;
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;
}
c = 1;
carry_pos = s - sbeg;
carry_len = l;
}
if (c == -1) { /* str contains no alnum */
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);
return str;
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);
}
if (!rb_enc_asciicompat(enc)) {
MEMCPY(carry, s, char, l);
carry_len = l;
}
carry_pos = s - sbeg;
}
}
RESIZE_CAPA(str, slen + carry_len);
sbeg = RSTRING_PTR(str);
s = sbeg + carry_pos;
memmove(s + carry_len, s, slen - carry_pos);
memmove(s, carry, carry_len);
slen += carry_len;
STR_SET_LEN(str, slen);
TERM_FILL(&sbeg[slen], rb_enc_mbminlen(enc));
rb_enc_str_coderange(str);
return str;
}
/*
* call-seq:
* str.succ! -> str
* str.next! -> str
*
* Equivalent to <code>String#succ</code>, but modifies the receiver in
* place.
*/
static VALUE
rb_str_succ_bang(VALUE str)
{
rb_str_modify(str);
str_succ(str);
return str;
}
static int
all_digits_p(const char *s, long len)
{
while (len-- > 0) {
if (!ISDIGIT(*s)) return 0;
s++;
}
return 1;
}
static VALUE str_upto_each(VALUE beg, VALUE end, int excl, int (*each)(VALUE, VALUE), VALUE);
static int
str_upto_i(VALUE str, VALUE arg)
{
rb_yield(str);
return 0;
}
/*
* call-seq:
* str.upto(other_str, exclusive=false) {|s| block } -> str
* str.upto(other_str, exclusive=false) -> an_enumerator
*
* Iterates through successive values, starting at <i>str</i> and
* ending at <i>other_str</i> inclusive, passing each value in turn to
* the block. The <code>String#succ</code> method is used to generate
* each value. If optional second argument exclusive is omitted or is false,
* the last value will be included; otherwise it will be excluded.
*
* If no block is given, an enumerator is returned instead.
*
* "a8".upto("b6") {|s| print s, ' ' }
* for s in "a8".."b6"
* print s, ' '
* end
*
* <em>produces:</em>
*
* a8 a9 b0 b1 b2 b3 b4 b5 b6
* a8 a9 b0 b1 b2 b3 b4 b5 b6
*
* If <i>str</i> and <i>other_str</i> contains only ascii numeric characters,
* both are recognized as decimal numbers. In addition, the width of
* string (e.g. leading zeros) is handled appropriately.
*
* "9".upto("11").to_a #=> ["9", "10", "11"]
* "25".upto("5").to_a #=> []
* "07".upto("11").to_a #=> ["07", "08", "09", "10", "11"]
*/
static VALUE
rb_str_upto(int argc, VALUE *argv, VALUE beg)
{
VALUE end, exclusive;
rb_scan_args(argc, argv, "11", &end, &exclusive);
RETURN_ENUMERATOR(beg, argc, argv);
return str_upto_each(beg, end, RTEST(exclusive), str_upto_i, Qnil);
}
static VALUE
str_upto_each(VALUE beg, VALUE end, int excl, int (*each)(VALUE, VALUE), VALUE arg)
{
VALUE current, after_end;
ID succ;
int n, ascii;
rb_encoding *enc;
CONST_ID(succ, "succ");
StringValue(end);
enc = rb_enc_check(beg, end);
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;
}
/* 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 ? '<' : rb_intern("<=");
VALUE args[2], fmt = rb_obj_freeze(rb_usascii_str_new_cstr("%.*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);
if (n > 0 || (excl && n == 0)) return beg;
after_end = rb_funcallv(end, succ, 0, 0);
current = rb_str_dup(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;
}
return beg;
}
static int
include_range_i(VALUE str, VALUE arg)
{
VALUE *argp = (VALUE *)arg;
if (!rb_equal(str, *argp)) return 0;
*argp = Qnil;
return 1;
}
VALUE
rb_str_include_range_p(VALUE beg, VALUE end, VALUE val, VALUE exclusive)
{
beg = rb_str_new_frozen(beg);
StringValue(end);
end = rb_str_new_frozen(end);
if (NIL_P(val)) return Qfalse;
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;
if (!RTEST(exclusive) && v == e) return Qtrue;
return Qfalse;
}
}
}
#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 */
}
#endif
}
str_upto_each(beg, end, RTEST(exclusive), include_range_i, (VALUE)&val);
return NIL_P(val) ? Qtrue : Qfalse;
}
static VALUE
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 Qnil;
}
static VALUE
rb_str_aref(VALUE str, VALUE indx)
{
long idx;
if (FIXNUM_P(indx)) {
idx = FIX2LONG(indx);
num_index:
str = rb_str_substr(str, idx, 1);
if (!NIL_P(str) && RSTRING_LEN(str) == 0) return Qnil;
return str;
}
if (SPECIAL_CONST_P(indx)) goto generic;
switch (BUILTIN_TYPE(indx)) {
case T_REGEXP:
return rb_str_subpat(str, indx, INT2FIX(0));
case T_STRING:
if (rb_str_index(str, indx, 0) != -1)
return rb_str_dup(indx);
return Qnil;
generic:
default:
/* check if indx is Range */
{
long beg, len;
VALUE tmp;
len = str_strlen(str, NULL);
switch (rb_range_beg_len(indx, &beg, &len, len, 0)) {
case Qfalse:
break;
case Qnil:
return Qnil;
default:
tmp = rb_str_substr(str, beg, len);
return tmp;
}
}
idx = NUM2LONG(indx);
goto num_index;
}
UNREACHABLE;
}
/*
* call-seq:
* str[index] -> new_str or nil
* str[start, length] -> new_str or nil
* str[range] -> new_str or nil
* str[regexp] -> new_str or nil
* str[regexp, capture] -> new_str or nil
* str[match_str] -> new_str or nil
* str.slice(index) -> new_str or nil
* str.slice(start, length) -> new_str or nil
* str.slice(range) -> new_str or nil
* str.slice(regexp) -> new_str or nil
* str.slice(regexp, capture) -> new_str or nil
* str.slice(match_str) -> new_str or nil
*
* Element Reference --- If passed a single +index+, returns a substring of
* one character at that index. If passed a +start+ index and a +length+,
* returns a substring containing +length+ characters starting at the
* +index+. If passed a +range+, its beginning and end are interpreted as
* offsets delimiting the substring to be returned.
*
* In these three cases, if an index is negative, it is counted from the end
* of the string. For the +start+ and +range+ cases the starting index
* is just before a character and an index matching the string's size.
* Additionally, an empty string is returned when the starting index for a
* character range is at the end of the string.
*
* Returns +nil+ if the initial index falls outside the string or the length
* is negative.
*
* If a +Regexp+ is supplied, the matching portion of the string is
* returned. If a +capture+ follows the regular expression, which may be a
* capture group index or name, follows the regular expression that component
* of the MatchData is returned instead.
*
* If a +match_str+ is given, that string is returned if it occurs in
* the string.
*
* Returns +nil+ if the regular expression does not match or the match string
* cannot be found.
*
* a = "hello there"
*
* a[1] #=> "e"
* a[2, 3] #=> "llo"
* a[2..3] #=> "ll"
*
* a[-3, 2] #=> "er"
* a[7..-2] #=> "her"
* a[-4..-2] #=> "her"
* a[-2..-4] #=> ""
*
* a[11, 0] #=> ""
* a[11] #=> nil
* a[12, 0] #=> nil
* a[12..-1] #=> nil
*
* a[/[aeiou](.)\1/] #=> "ell"
* a[/[aeiou](.)\1/, 0] #=> "ell"
* a[/[aeiou](.)\1/, 1] #=> "l"
* a[/[aeiou](.)\1/, 2] #=> nil
*
* a[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, "non_vowel"] #=> "l"
* a[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, "vowel"] #=> "e"
*
* a["lo"] #=> "lo"
* a["bye"] #=> nil
*/
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]);
}
return rb_str_substr(str, NUM2LONG(argv[0]), NUM2LONG(argv[1]));
}
rb_check_arity(argc, 1, 2);
return rb_str_aref(str, argv[0]);
}
VALUE
rb_str_drop_bytes(VALUE str, long len)
{
char *ptr = RSTRING_PTR(str);
long olen = RSTRING_LEN(str), nlen;
str_modifiable(str);
if (len > olen) len = olen;
nlen = olen - len;
if (nlen <= RSTRING_EMBED_LEN_MAX) {
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);
ptr = RSTRING(str)->as.ary;
memmove(ptr, oldptr + len, nlen);
if (fl == STR_NOEMBED) xfree(oldptr);
}
else {
if (!STR_SHARED_P(str)) rb_str_new_frozen(str);
ptr = RSTRING(str)->as.heap.ptr += len;
RSTRING(str)->as.heap.len = nlen;
}
ptr[nlen] = 0;
ENC_CODERANGE_CLEAR(str);
return str;
}
static void
rb_str_splice_0(VALUE str, long beg, long len, VALUE val)
{
char *sptr;
long slen, vlen = RSTRING_LEN(val);
if (beg == 0 && vlen == 0) {
rb_str_drop_bytes(str, len);
OBJ_INFECT(str, val);
return;
}
rb_str_modify(str);
RSTRING_GETMEM(str, sptr, slen);
if (len < vlen) {
/* expand string */
RESIZE_CAPA(str, slen + vlen - len);
sptr = RSTRING_PTR(str);
}
if (vlen != len) {
memmove(sptr + beg + vlen,
sptr + beg + len,
slen - (beg + len));
}
if (vlen < beg && len < 0) {
MEMZERO(sptr + slen, char, -len);
}
if (vlen > 0) {
memmove(sptr + beg, RSTRING_PTR(val), vlen);
}
slen += vlen - len;
STR_SET_LEN(str, slen);
TERM_FILL(&sptr[slen], TERM_LEN(str));
OBJ_INFECT(str, val);
}
void
rb_str_update(VALUE str, long beg, long len, VALUE val)
{
long slen;
char *p, *e;
rb_encoding *enc;
int singlebyte = single_byte_optimizable(str);
int cr;
if (len < 0) rb_raise(rb_eIndexError, "negative length %ld", len);
StringValue(val);
enc = rb_enc_check(str, val);
slen = str_strlen(str, enc); /* rb_enc_check */
if (slen < beg) {
out_of_range:
rb_raise(rb_eIndexError, "index %ld out of string", beg);
}
if (beg < 0) {
if (-beg > slen) {
goto out_of_range;
}
beg += slen;
}
if (slen < len || slen < beg + len) {
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);
if (!e) e = RSTRING_END(str);
/* error check */
beg = p - RSTRING_PTR(str); /* physical position */
len = e - p; /* physical length */
rb_str_splice_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);
}
#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)
{
int nth;
VALUE match;
long start, end, len;
rb_encoding *enc;
struct re_registers *regs;
if (rb_reg_search(re, str, 0, 0) < 0) {
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) {
out_of_range:
rb_raise(rb_eIndexError, "index %d out of regexp", nth);
}
if (nth < 0) {
if (-nth >= regs->num_regs) {
goto out_of_range;
}
nth += regs->num_regs;
}
start = BEG(nth);
if (start == -1) {
rb_raise(rb_eIndexError, "regexp group %d not matched", nth);
}
end = END(nth);
len = end - start;
StringValue(val);
enc = rb_enc_check(str, val);
rb_str_splice_0(str, start, len, val);
rb_enc_associate(str, enc);
}
static VALUE
rb_str_aset(VALUE str, VALUE indx, VALUE val)
{
long idx, beg;
if (FIXNUM_P(indx)) {
idx = FIX2LONG(indx);
num_index:
rb_str_splice(str, idx, 1, val);
return val;
}
if (SPECIAL_CONST_P(indx)) goto generic;
switch (TYPE(indx)) {
case T_REGEXP:
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;
generic:
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;
}
}
idx = NUM2LONG(indx);
goto num_index;
}
}
/*
* call-seq:
* str[fixnum] = new_str
* str[fixnum, fixnum] = new_str
* str[range] = aString
* str[regexp] = new_str
* str[regexp, fixnum] = new_str
* str[regexp, name] = new_str
* str[other_str] = new_str
*
* 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
* <code>String#[]</code>. If the replacement string is not the same length as