Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

8037 lines (7231 sloc) 201.485 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 "ruby/ruby.h"
#include "ruby/re.h"
#include "ruby/encoding.h"
#include "internal.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 numberof(array) (int)(sizeof(array) / sizeof((array)[0]))
#undef rb_str_new_cstr
#undef rb_tainted_str_new_cstr
#undef rb_usascii_str_new_cstr
#undef rb_external_str_new_cstr
#undef rb_locale_str_new_cstr
#undef rb_str_new2
#undef rb_str_new3
#undef rb_str_new4
#undef rb_str_new5
#undef rb_tainted_str_new2
#undef rb_usascii_str_new2
#undef rb_str_dup_frozen
#undef rb_str_buf_new_cstr
#undef rb_str_buf_new2
#undef rb_str_buf_cat2
#undef rb_str_cat2
static VALUE rb_str_clear(VALUE str);
VALUE rb_cString;
VALUE rb_cSymbol;
#define RUBY_MAX_CHAR_LEN 16
#define STR_TMPLOCK FL_USER7
#define STR_NOEMBED FL_USER1
#define STR_SHARED FL_USER2 /* = ELTS_SHARED */
#define STR_ASSOC FL_USER3
#define STR_SHARED_P(s) FL_ALL((s), STR_NOEMBED|ELTS_SHARED)
#define STR_ASSOC_P(s) FL_ALL((s), STR_NOEMBED|STR_ASSOC)
#define STR_NOCAPA (STR_NOEMBED|ELTS_SHARED|STR_ASSOC)
#define STR_NOCAPA_P(s) (FL_TEST((s),STR_NOEMBED) && FL_ANY((s),ELTS_SHARED|STR_ASSOC))
#define STR_UNSET_NOCAPA(s) do {\
if (FL_TEST((s),STR_NOEMBED)) FL_UNSET((s),(ELTS_SHARED|STR_ASSOC));\
} while (0)
#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)
#define STR_EMBED_P(str) (!FL_TEST((str), STR_NOEMBED))
#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 RESIZE_CAPA(str,capacity) do {\
if (STR_EMBED_P(str)) {\
if ((capacity) > RSTRING_EMBED_LEN_MAX) {\
char *tmp = ALLOC_N(char, (capacity)+1);\
memcpy(tmp, RSTRING_PTR(str), RSTRING_LEN(str));\
RSTRING(str)->as.heap.ptr = tmp;\
RSTRING(str)->as.heap.len = RSTRING_LEN(str);\
STR_SET_NOEMBED(str);\
RSTRING(str)->as.heap.aux.capa = (capacity);\
}\
}\
else {\
REALLOC_N(RSTRING(str)->as.heap.ptr, char, (capacity)+1);\
if (!STR_NOCAPA_P(str))\
RSTRING(str)->as.heap.aux.capa = (capacity);\
}\
} while (0)
#define is_ascii_string(str) (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT)
#define is_broken_string(str) (rb_enc_str_coderange(str) == ENC_CODERANGE_BROKEN)
#define STR_ENC_GET(str) rb_enc_from_index(ENCODING_GET(str))
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_VALUE == 8
# define NONASCII_MASK 0x8080808080808080ULL
#elif SIZEOF_VALUE == 4
# define NONASCII_MASK 0x80808080UL
#endif
#ifdef NONASCII_MASK
if ((int)sizeof(VALUE) * 2 < e - p) {
const VALUE *s, *t;
const VALUE lowbits = sizeof(VALUE) - 1;
s = (const VALUE*)(~lowbits & ((VALUE)p + lowbits));
while (p < (const char *)s) {
if (!ISASCII(*p))
return p;
p++;
}
t = (const VALUE*)(~lowbits & (VALUE)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) == 0) {
/* 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;
}
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);
if (p < e) {
p = search_nonascii(p, e);
if (!p) {
return ENC_CODERANGE_VALID;
}
}
}
if (e < p) {
return ENC_CODERANGE_BROKEN;
}
return ENC_CODERANGE_VALID;
}
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);
}
if (e < p) {
return ENC_CODERANGE_BROKEN;
}
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) == 0) {
/* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
p = search_nonascii(p, e);
*cr = (!p && *cr != ENC_CODERANGE_VALID) ? ENC_CODERANGE_7BIT : ENC_CODERANGE_VALID;
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;
}
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);
if (p < e) {
p = search_nonascii(p, e);
if (!p) {
*cr = ENC_CODERANGE_VALID;
return e - s;
}
}
}
*cr = e < p ? ENC_CODERANGE_BROKEN: ENC_CODERANGE_VALID;
return p - s;
}
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 = e < p ? ENC_CODERANGE_BROKEN: ENC_CODERANGE_VALID;
return p - 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);
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:
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);
}
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) {
rb_encoding *enc = STR_ENC_GET(str);
cr = coderange_scan(RSTRING_PTR(str), RSTRING_LEN(str), enc);
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 (STR_NOCAPA_P(str)) {
return RSTRING(str)->as.heap.len;
}
else {
return RSTRING(str)->as.heap.aux.capa;
}
}
static inline VALUE
str_alloc(VALUE klass)
{
NEWOBJ(str, struct RString);
OBJSETUP(str, klass, T_STRING);
str->as.heap.ptr = 0;
str->as.heap.len = 0;
str->as.heap.aux.capa = 0;
return (VALUE)str;
}
static VALUE
str_new(VALUE klass, const char *ptr, long len)
{
VALUE str;
if (len < 0) {
rb_raise(rb_eArgError, "negative string size (or size too big)");
}
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+1);
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);
RSTRING_PTR(str)[len] = '\0';
return str;
}
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_enc_str_new(const char *ptr, long len, rb_encoding *enc)
{
VALUE str = rb_str_new(ptr, len);
rb_enc_associate(str, enc);
return str;
}
VALUE
rb_str_new_cstr(const char *ptr)
{
if (!ptr) {
rb_raise(rb_eArgError, "NULL pointer given");
}
return rb_str_new(ptr, strlen(ptr));
}
RUBY_ALIAS_FUNCTION(rb_str_new2(const char *ptr), rb_str_new_cstr, (ptr))
#define rb_str_new2 rb_str_new_cstr
VALUE
rb_usascii_str_new_cstr(const char *ptr)
{
VALUE str = rb_str_new2(ptr);
ENCODING_CODERANGE_SET(str, rb_usascii_encindex(), ENC_CODERANGE_7BIT);
return str;
}
RUBY_ALIAS_FUNCTION(rb_usascii_str_new2(const char *ptr), rb_usascii_str_new_cstr, (ptr))
#define rb_usascii_str_new2 rb_usascii_str_new_cstr
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_new2(ptr);
OBJ_TAINT(str);
return str;
}
RUBY_ALIAS_FUNCTION(rb_tainted_str_new2(const char *ptr), rb_tainted_str_new_cstr, (ptr))
#define rb_tainted_str_new2 rb_tainted_str_new_cstr
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;
VALUE newstr;
const unsigned char *sp;
unsigned char *dp;
if (!to) return str;
if (!from) from = rb_enc_get(str);
if (from == to) return str;
if ((rb_enc_asciicompat(to) && ENC_CODERANGE(str) == ENC_CODERANGE_7BIT) ||
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);
retry:
ec = rb_econv_open_opts(from->name, to->name, ecflags, ecopts);
if (!ec) return str;
sp = (unsigned char*)RSTRING_PTR(str);
dp = (unsigned char*)RSTRING_PTR(newstr);
ret = rb_econv_convert(ec, &sp, (unsigned char*)RSTRING_END(str),
&dp, (unsigned char*)RSTRING_END(newstr), 0);
rb_econv_close(ec);
switch (ret) {
case econv_destination_buffer_full:
/* destination buffer short */
len = len < 2 ? 2 : len * 2;
rb_str_resize(newstr, len);
goto retry;
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);
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(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);
RSTRING(str2)->as.heap.aux.shared = str;
FL_SET(str2, ELTS_SHARED);
}
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);
}
static VALUE
str_new3(VALUE klass, VALUE str)
{
return str_new_shared(klass, str);
}
VALUE
rb_str_new_shared(VALUE str)
{
VALUE str2 = str_new3(rb_obj_class(str), str);
OBJ_INFECT(str2, str);
return str2;
}
RUBY_ALIAS_FUNCTION(rb_str_new3(VALUE str), rb_str_new_shared, (str))
#define rb_str_new3 rb_str_new_shared
static VALUE
str_new4(VALUE klass, VALUE str)
{
VALUE str2;
str2 = str_alloc(klass);
STR_SET_NOEMBED(str2);
RSTRING(str2)->as.heap.len = RSTRING_LEN(str);
RSTRING(str2)->as.heap.ptr = RSTRING_PTR(str);
if (STR_SHARED_P(str)) {
VALUE shared = RSTRING(str)->as.heap.aux.shared;
assert(OBJ_FROZEN(shared));
FL_SET(str2, ELTS_SHARED);
RSTRING(str2)->as.heap.aux.shared = shared;
}
else {
FL_SET(str, ELTS_SHARED);
RSTRING(str)->as.heap.aux.shared = str2;
}
rb_enc_cr_str_exact_copy(str2, 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_SHARED_P(orig) && (str = RSTRING(orig)->as.heap.aux.shared)) {
long ofs;
assert(OBJ_FROZEN(str));
ofs = RSTRING_LEN(str) - RSTRING_LEN(orig);
if ((ofs > 0) || (klass != RBASIC(str)->klass) ||
((RBASIC(str)->flags ^ RBASIC(orig)->flags) & (FL_TAINT|FL_UNTRUSTED)) ||
ENCODING_GET(str) != ENCODING_GET(orig)) {
str = str_new3(klass, str);
RSTRING(str)->as.heap.ptr += ofs;
RSTRING(str)->as.heap.len -= ofs;
rb_enc_cr_str_exact_copy(str, orig);
OBJ_INFECT(str, orig);
}
}
else if (STR_EMBED_P(orig)) {
str = str_new(klass, RSTRING_PTR(orig), RSTRING_LEN(orig));
rb_enc_cr_str_exact_copy(str, orig);
OBJ_INFECT(str, orig);
}
else if (STR_ASSOC_P(orig)) {
VALUE assoc = RSTRING(orig)->as.heap.aux.shared;
FL_UNSET(orig, STR_ASSOC);
str = str_new4(klass, orig);
FL_SET(str, STR_ASSOC);
RSTRING(str)->as.heap.aux.shared = assoc;
}
else {
str = str_new4(klass, orig);
}
OBJ_FREEZE(str);
return str;
}
RUBY_ALIAS_FUNCTION(rb_str_new4(VALUE orig), rb_str_new_frozen, (orig))
#define rb_str_new4 rb_str_new_frozen
VALUE
rb_str_new_with_class(VALUE obj, const char *ptr, long len)
{
return str_new(rb_obj_class(obj), ptr, len);
}
RUBY_ALIAS_FUNCTION(rb_str_new5(VALUE obj, const char *ptr, long len),
rb_str_new_with_class, (obj, ptr, len))
#define rb_str_new5 rb_str_new_with_class
static VALUE
str_new_empty(VALUE str)
{
VALUE v = rb_str_new5(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;
}
RUBY_ALIAS_FUNCTION(rb_str_buf_new2(const char *ptr), rb_str_buf_new_cstr, (ptr))
#define rb_str_buf_new2 rb_str_buf_new_cstr
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 (!STR_EMBED_P(str) && !STR_SHARED_P(str)) {
xfree(RSTRING(str)->as.heap.ptr);
}
}
RUBY_FUNC_EXPORTED size_t
rb_str_memsize(VALUE str)
{
if (!STR_EMBED_P(str) && !STR_SHARED_P(str)) {
return RSTRING(str)->as.heap.aux.capa;
}
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);
return;
}
STR_SET_NOEMBED(str);
STR_UNSET_NOCAPA(str);
RSTRING(str)->as.heap.ptr = RSTRING_PTR(str2);
RSTRING(str)->as.heap.len = RSTRING_LEN(str2);
if (STR_NOCAPA_P(str2)) {
FL_SET(str, RBASIC(str2)->flags & STR_NOCAPA);
RSTRING(str)->as.heap.aux.shared = RSTRING(str2)->as.heap.aux.shared;
}
else {
RSTRING(str)->as.heap.aux.capa = RSTRING(str2)->as.heap.aux.capa;
}
STR_SET_EMBED(str2); /* abandon 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_ASSOC_P(str2)) {
str2 = rb_str_new4(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);
FL_SET(str, ELTS_SHARED);
FL_UNSET(str, STR_ASSOC);
RSTRING(str)->as.heap.aux.shared = 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)
{
return str_replace(str_alloc(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;
}
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);
}
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);
}
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;
}
#ifdef NONASCII_MASK
#define is_utf8_lead_byte(c) (((c)&0xC0) != 0x80)
/*
* UTF-8 leading bytes have either 0xxxxxxx or 11xxxxxx
* bit represention. (see http://en.wikipedia.org/wiki/UTF-8)
* Therefore, following pseudo code can detect UTF-8 leading byte.
*
* if (!(byte & 0x80))
* byte |= 0x40; // turn on bit6
* return ((byte>>6) & 1); // bit6 represent it's leading byte or not.
*
* This function calculate every bytes in the argument word `s'
* using the above logic concurrently. and gather every bytes result.
*/
static inline VALUE
count_utf8_lead_bytes_with_word(const VALUE *s)
{
VALUE d = *s;
/* Transform into bit0 represent UTF-8 leading or not. */
d |= ~(d>>1);
d >>= 6;
d &= NONASCII_MASK >> 7;
/* Gather every bytes. */
d += (d>>8);
d += (d>>16);
#if SIZEOF_VALUE == 8
d += (d>>32);
#endif
return (d&0xF);
}
#endif
static long
str_strlen(VALUE str, rb_encoding *enc)
{
const char *p, *e;
long n;
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);
#ifdef NONASCII_MASK
if (ENC_CODERANGE(str) == ENC_CODERANGE_VALID &&
enc == rb_utf8_encoding()) {
VALUE len = 0;
if ((int)sizeof(VALUE) * 2 < e - p) {
const VALUE *s, *t;
const VALUE lowbits = sizeof(VALUE) - 1;
s = (const VALUE*)(~lowbits & ((VALUE)p + lowbits));
t = (const VALUE*)(~lowbits & (VALUE)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
n = rb_enc_strlen_cr(p, e, enc, &cr);
if (cr) {
ENC_CODERANGE_SET(str, cr);
}
return n;
}
long
rb_str_strlen(VALUE str)
{
return str_strlen(str, STR_ENC_GET(str));
}
/*
* call-seq:
* str.length -> integer
* str.size -> integer
*
* Returns the character length of <i>str</i>.
*/
VALUE
rb_str_length(VALUE str)
{
long len;
len = str_strlen(str, STR_ENC_GET(str));
return LONG2NUM(len);
}
/*
* 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;
StringValue(str2);
enc = rb_enc_check(str1, str2);
str3 = rb_str_new(0, RSTRING_LEN(str1)+RSTRING_LEN(str2));
memcpy(RSTRING_PTR(str3), RSTRING_PTR(str1), RSTRING_LEN(str1));
memcpy(RSTRING_PTR(str3) + RSTRING_LEN(str1),
RSTRING_PTR(str2), RSTRING_LEN(str2));
RSTRING_PTR(str3)[RSTRING_LEN(str3)] = '\0';
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)));
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;
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");
}
str2 = rb_str_new5(str, 0, len *= RSTRING_LEN(str));
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);
}
ptr2[RSTRING_LEN(str2)] = '\0';
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)
{
volatile VALUE tmp = rb_check_array_type(arg);
if (!NIL_P(tmp)) {
return rb_str_format(RARRAY_LENINT(tmp), RARRAY_PTR(tmp), str);
}
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);
if (!OBJ_UNTRUSTED(str) && rb_safe_level() >= 4)
rb_raise(rb_eSecurityError, "Insecure: can't modify string");
}
static inline int
str_independent(VALUE str)
{
str_modifiable(str);
if (!STR_SHARED_P(str)) return 1;
if (STR_EMBED_P(str)) return 1;
return 0;
}
static void
str_make_independent_expand(VALUE str, long expand)
{
char *ptr;
long len = RSTRING_LEN(str);
long capa = len + expand;
if (len > capa) len = capa;
ptr = ALLOC_N(char, capa + 1);
if (RSTRING_PTR(str)) {
memcpy(ptr, RSTRING_PTR(str), len);
}
STR_SET_NOEMBED(str);
STR_UNSET_NOCAPA(str);
ptr[len] = 0;
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;
if (!STR_EMBED_P(str)) {
REALLOC_N(RSTRING(str)->as.heap.ptr, char, capa+1);
RSTRING(str)->as.heap.aux.capa = capa;
}
else if (capa > RSTRING_EMBED_LEN_MAX) {
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_SHARED_P(str) && !STR_EMBED_P(str)) {
xfree(RSTRING_PTR(str));
RSTRING(str)->as.heap.ptr = 0;
RSTRING(str)->as.heap.len = 0;
}
}
void
rb_str_associate(VALUE str, VALUE add)
{
/* sanity check */
rb_check_frozen(str);
if (STR_ASSOC_P(str)) {
/* already associated */
rb_ary_concat(RSTRING(str)->as.heap.aux.shared, add);
}
else {
if (STR_SHARED_P(str)) {
VALUE assoc = RSTRING(str)->as.heap.aux.shared;
str_make_independent(str);
if (STR_ASSOC_P(assoc)) {
assoc = RSTRING(assoc)->as.heap.aux.shared;
rb_ary_concat(assoc, add);
add = assoc;
}
}
else if (STR_EMBED_P(str)) {
str_make_independent(str);
}
else if (RSTRING(str)->as.heap.aux.capa != RSTRING_LEN(str)) {
RESIZE_CAPA(str, RSTRING_LEN(str));
}
FL_SET(str, STR_ASSOC);
RBASIC(add)->klass = 0;
RSTRING(str)->as.heap.aux.shared = add;
}
}
VALUE
rb_str_associated(VALUE str)
{
if (STR_SHARED_P(str)) str = RSTRING(str)->as.heap.aux.shared;
if (STR_ASSOC_P(str)) {
return RSTRING(str)->as.heap.aux.shared;
}
return Qfalse;
}
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);
}
char *
rb_string_value_cstr(volatile VALUE *ptr)
{
VALUE str = rb_string_value(ptr);
char *s = RSTRING_PTR(str);
long len = RSTRING_LEN(str);
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;
}
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_VALUE * 2 < e - p && (int)SIZEOF_VALUE * 2 < nth) {
const VALUE *s, *t;
const VALUE lowbits = sizeof(VALUE) - 1;
s = (const VALUE*)(~lowbits & ((VALUE)p + lowbits));
t = (const VALUE*)(~lowbits & (VALUE)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(VALUE) <= 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_LEN(str) == beg + len &&
RSTRING_EMBED_LEN_MAX < len) {
str2 = rb_str_new_shared(rb_str_new_frozen(str));
rb_str_drop_bytes(str2, beg);
}
else {
str2 = rb_str_new5(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;
}
static 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;
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 && p + len == RSTRING_END(str)) {
str2 = rb_str_new4(str);
str2 = str_new3(rb_obj_class(str2), str2);
RSTRING(str2)->as.heap.ptr += RSTRING(str2)->as.heap.len - len;
RSTRING(str2)->as.heap.len = len;
}
else {
str2 = rb_str_new5(str, p, len);
rb_enc_cr_str_copy_for_substr(str2, str);
OBJ_INFECT(str2, str);
RB_GC_GUARD(str);
}
return str2;
}
VALUE
rb_str_freeze(VALUE str)
{
if (STR_ASSOC_P(str)) {
VALUE ary = RSTRING(str)->as.heap.aux.shared;
OBJ_FREEZE(ary);
}
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;
}
void
rb_str_set_len(VALUE str, long len)
{
long capa;
str_modifiable(str);
if (STR_SHARED_P(str)) {
rb_raise(rb_eRuntimeError, "can't set length of shared string");
}
if (len > (capa = (long)rb_str_capacity(str))) {
rb_bug("probable buffer overflow: %ld for %ld", len, capa);
}
STR_SET_LEN(str, len);
RSTRING_PTR(str)[len] = '\0';
}
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);
if (len != slen) {
if (STR_EMBED_P(str)) {
if (len <= RSTRING_EMBED_LEN_MAX) {
STR_SET_EMBED_LEN(str, len);
RSTRING(str)->as.ary[len] = '\0';
return str;
}
str_make_independent_expand(str, len - slen);
STR_SET_NOEMBED(str);
}
else if (len <= RSTRING_EMBED_LEN_MAX) {
char *ptr = RSTRING(str)->as.heap.ptr;
STR_SET_EMBED(str);
if (slen > len) slen = len;
if (slen > 0) MEMCPY(RSTRING(str)->as.ary, ptr, char, slen);
RSTRING(str)->as.ary[len] = '\0';
STR_SET_EMBED_LEN(str, len);
if (independent) xfree(ptr);
return str;
}
else if (!independent) {
str_make_independent_expand(str, len - slen);
}
else if (slen < len || slen - len > 1024) {
REALLOC_N(RSTRING(str)->as.heap.ptr, char, len+1);
}
if (!STR_NOCAPA_P(str)) {
RSTRING(str)->as.heap.aux.capa = len;
}
RSTRING(str)->as.heap.len = len;
RSTRING(str)->as.heap.ptr[len] = '\0'; /* sentinel */
}
return str;
}
static VALUE
str_buf_cat(VALUE str, const char *ptr, long len)
{
long capa, total, off = -1;
if (ptr >= RSTRING_PTR(str) && ptr <= RSTRING_END(str)) {
off = ptr - RSTRING_PTR(str);
}
rb_str_modify(str);
if (len == 0) return 0;
if (STR_ASSOC_P(str)) {
FL_UNSET(str, STR_ASSOC);
capa = RSTRING(str)->as.heap.aux.capa = RSTRING_LEN(str);
}
else if (STR_EMBED_P(str)) {
capa = RSTRING_EMBED_LEN_MAX;
}
else {
capa = RSTRING(str)->as.heap.aux.capa;
}
if (RSTRING_LEN(str) >= LONG_MAX - len) {
rb_raise(rb_eArgError, "string sizes too big");
}
total = RSTRING_LEN(str)+len;
if (capa <= total) {
while (total > capa) {
if (capa + 1 >= LONG_MAX / 2) {
capa = (total + 4095) / 4096;
break;
}
capa = (capa + 1) * 2;
}
RESIZE_CAPA(str, capa);
}
if (off != -1) {
ptr = RSTRING_PTR(str) + off;
}
memcpy(RSTRING_PTR(str) + RSTRING_LEN(str), ptr, len);
STR_SET_LEN(str, total);
RSTRING_PTR(str)[total] = '\0'; /* sentinel */
return str;
}
#define str_buf_cat2(str, ptr) str_buf_cat((str), (ptr), strlen(ptr))
VALUE
rb_str_buf_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_buf_cat2(VALUE str, const char *ptr)
{
return rb_str_buf_cat(str, ptr, strlen(ptr));
}
VALUE
rb_str_cat(VALUE str, const char *ptr, long len)
{
if (len < 0) {
rb_raise(rb_eArgError, "negative string size (or size too big)");
}
if (STR_ASSOC_P(str)) {
char *p;
rb_str_modify_expand(str, len);
p = RSTRING(str)->as.heap.ptr;
memcpy(p + RSTRING(str)->as.heap.len, ptr, len);
len = RSTRING(str)->as.heap.len += len;
p[len] = '\0'; /* sentinel */
return str;
}
return rb_str_buf_cat(str, ptr, len);
}
VALUE
rb_str_cat2(VALUE str, const char *ptr)
{
return rb_str_cat(str, ptr, strlen(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;
str_cr = ENC_CODERANGE(str);
if (str_encindex == ptr_encindex) {
if (str_cr == ENC_CODERANGE_UNKNOWN)
ptr_cr = ENC_CODERANGE_UNKNOWN;
else if (ptr_cr == ENC_CODERANGE_UNKNOWN) {
ptr_cr = coderange_scan(ptr, len, rb_enc_from_index(ptr_encindex));
}
}
else {
rb_encoding *str_enc = rb_enc_from_index(str_encindex);
rb_encoding *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) {
incompatible:
rb_raise(rb_eEncCompatError, "incompatible character encodings: %s and %s",
rb_enc_name(rb_enc_from_index(str_encindex)),
rb_enc_name(rb_enc_from_index(ptr_encindex)));
}
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)
{
rb_encoding *enc;
int cr, cr2;
long len2;
StringValue(str2);
if ((len2 = RSTRING_LEN(str2)) > 0 && STR_ASSOC_P(str)) {
long len = RSTRING_LEN(str) + len2;
enc = rb_enc_check(str, str2);
cr = ENC_CODERANGE(str);
if ((cr2 = ENC_CODERANGE(str2)) > cr) cr = cr2;
rb_str_modify_expand(str, len2);
memcpy(RSTRING(str)->as.heap.ptr + RSTRING(str)->as.heap.len,
RSTRING_PTR(str2), len2+1);
RSTRING(str)->as.heap.len = len;
rb_enc_associate(str, enc);
ENC_CODERANGE_SET(str, cr);
OBJ_INFECT(str, str2);
return str;
}
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_memhash(const void *ptr, long len)
{
return st_hash(ptr, len, rb_hash_start((st_index_t)len));
}
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 and content.
*/
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
*
* Equality---If <i>obj</i> is not a <code>String</code>, returns
* <code>false</code>. Otherwise, returns <code>true</code> if <i>str</i>
* <code><=></code> <i>obj</i> returns zero.
*/
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:
* str <=> other_str -> -1, 0, +1 or nil
*
* Comparison---Returns -1 if <i>other_str</i> is greater than, 0 if
* <i>other_str</i> is equal to, and +1 if <i>other_str</i> is less than
* <i>str</i>. 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. In older versions of Ruby, setting
* <code>$=</code> allowed case-insensitive comparisons; this is now deprecated
* in favor of using <code>String#casecmp</code>.
*
* <code><=></code> is the basis for the methods <code><</code>,
* <code><=</code>, <code>></code>, <code>>=</code>, and <code>between?</code>,
* included from module <code>Comparable</code>. The method
* <code>String#==</code> does not use <code>Comparable#==</code>.
*
* "abcdef" <=> "abcde" #=> 1
* "abcdef" <=> "abcdef" #=> 0
* "abcdef" <=> "abcdefg" #=> -1
* "abcdef" <=> "ABCDEF" #=> 1
*/
static VALUE
rb_str_cmp_m(VALUE str1, VALUE str2)
{
long result;
if (!RB_TYPE_P(str2, T_STRING)) {
if (!rb_respond_to(str2, rb_intern("to_str"))) {
return Qnil;
}
else if (!rb_respond_to(str2, rb_intern("<=>"))) {
return Qnil;
}
else {
VALUE tmp = rb_funcall(str2, rb_intern("<=>"), 1, str1);
if (NIL_P(tmp)) return Qnil;
if (!FIXNUM_P(tmp)) {
return rb_funcall(LONG2FIX(0), '-', 1, tmp);
}
result = -FIX2LONG(tmp);
}
}
else {
result = rb_str_cmp(str1, str2);
}
return LONG2NUM(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);
}
static long
rb_str_index(VALUE str, VALUE sub, long offset)
{
long pos;
char *s, *sptr, *e;
long len, slen;
rb_encoding *enc;
enc = rb_enc_check(str, sub);
if (is_broken_string(sub)) {
return -1;
}
len = str_strlen(str, enc);
slen = str_strlen(sub, enc);
if (offset < 0) {
offset += len;
if (offset < 0) return -1;
}
if (len - offset < slen) return -1;
s = RSTRING_PTR(str);
e = s + RSTRING_LEN(str);
if (offset) {
offset = str_offset(s, RSTRING_END(str), offset, enc, single_byte_optimizable(str));
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 (;;) {
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;
if ((len -= t - s) <= 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, STR_ENC_GET(str));
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, STR_ENC_GET(str)))
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);
}
static long
rb_str_rindex(VALUE str, VALUE sub, long pos)
{
long len, slen;
char *s, *sbeg, *e, *t;
rb_encoding *enc;
int singlebyte = single_byte_optimizable(str);
enc = rb_enc_check(str, sub);
if (is_broken_string(sub)) {
return -1;
}
len = str_strlen(str, enc);
slen = str_strlen(sub, enc);
/* 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);
e = RSTRING_END(str);
t = RSTRING_PTR(sub);
slen = RSTRING_LEN(sub);
s = str_nth(sbeg, e, pos, enc, singlebyte);
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;
}
/*
* 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);
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,
STR_ENC_GET(str), 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, int);
/*
* 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, 0), 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;
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;
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;
}
/*
* 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)
{
rb_encoding *enc;
VALUE str;
char *sbeg, *s, *e, *last_alnum = 0;
int c = -1;
long l;
char carry[ONIGENC_CODE_TO_MBC_MAXLEN] = "\1";
long carry_pos = 0, carry_len = 1;
enum neighbor_char neighbor = NEIGHBOR_FOUND;
str = rb_str_new5(orig, RSTRING_PTR(orig), RSTRING_LEN(orig));
rb_enc_cr_str_copy_for_substr(str, orig);
OBJ_INFECT(str, orig);
if (RSTRING_LEN(str) == 0) return str;
enc = STR_ENC_GET(orig);
sbeg = RSTRING_PTR(str);
s = e = sbeg + RSTRING_LEN(str);
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;
}
}
if ((l = rb_enc_precise_mbclen(s, e, enc)) <= 0) continue;
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;
if ((l = rb_enc_precise_mbclen(s, e, enc)) <= 0) continue;
neighbor = enc_succ_char(s, l, enc);
if (neighbor == NEIGHBOR_FOUND)
return str;
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, RSTRING_LEN(str) + carry_len);
s = RSTRING_PTR(str) + carry_pos;
memmove(s + carry_len, s, RSTRING_LEN(str) - carry_pos);
memmove(s, carry, carry_len);
STR_SET_LEN(str, RSTRING_LEN(str) + carry_len);
RSTRING_PTR(str)[RSTRING_LEN(str)] = '\0';
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_shared_replace(str, rb_str_succ(str));
return str;
}
/*
* 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;
VALUE current, after_end;
ID succ;
int n, excl, ascii;
rb_encoding *enc;
rb_scan_args(argc, argv, "11", &end, &exclusive);
RETURN_ENUMERATOR(beg, argc, argv);
excl = RTEST(exclusive);
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 (;;) {
rb_yield(rb_enc_str_new(&c, 1, enc));
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])) {
char *s, *send;
VALUE b, e;
int width;
s = RSTRING_PTR(beg); send = RSTRING_END(beg);
width = rb_long2int(send - s);
while (s < send) {
if (!ISDIGIT(*s)) goto no_digits;
s++;
}
s = RSTRING_PTR(end); send = RSTRING_END(end);
while (s < send) {
if (!ISDIGIT(*s)) goto no_digits;
s++;
}
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;
rb_yield(rb_enc_sprintf(usascii, "%.*ld", width, bi));
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;
rb_yield(rb_str_format(numberof(args), args, fmt));
b = rb_funcall(b, succ, 0, 0);
}
}
return beg;
}
/* normal case */
no_digits:
n = rb_str_cmp(beg, end);
if (n > 0 || (excl && n == 0)) return beg;
after_end = rb_funcall(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_funcall(current, succ, 0, 0);
rb_yield(current);
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 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, STR_ENC_GET(str));
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|ELTS_SHARED));
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_new4(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)
{
if (beg == 0 && RSTRING_LEN(val) == 0) {
rb_str_drop_bytes(str, len);
OBJ_INFECT(str, val);
return;
}
rb_str_modify(str);
if (len < RSTRING_LEN(val)) {
/* expand string */
RESIZE_CAPA(str, RSTRING_LEN(str) + RSTRING_LEN(val) - len + 1);
}
if (RSTRING_LEN(val) != len) {
memmove(RSTRING_PTR(str) + beg + RSTRING_LEN(val),
RSTRING_PTR(str) + beg + len,
RSTRING_LEN(str) - (beg + len));
}
if (RSTRING_LEN(val) < beg && len < 0) {
MEMZERO(RSTRING_PTR(str) + RSTRING_LEN(str), char, -len);
}
if (RSTRING_LEN(val) > 0) {
memmove(RSTRING_PTR(str)+beg, RSTRING_PTR(val), RSTRING_LEN(val));
}
STR_SET_LEN(str, RSTRING_LEN(str) + RSTRING_LEN(val) - len);
if (RSTRING_PTR(str)) {
RSTRING_PTR(str)[RSTRING_LEN(str)] = '\0';
}
OBJ_INFECT(str, val);
}
static void
rb_str_splice(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);
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);
}
void
rb_str_update(VALUE str, long beg, long len, VALUE val)
{
rb_str_splice(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, 0), val);
return val;
generic:
default:
/* check if indx is Range */
{
long beg, len;
if (rb_range_beg_len(indx, &beg, &len, str_strlen(str, 0), 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
* 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, <code>IndexError</code> is raised. If the regular expression
* form is used, the optional second <code>Fixnum</code> allows you to specify
* which portion of the match to replace (effectively using the
* <code>MatchData</code> indexing rules. The forms that take a
* <code>Fixnum</code> will raise an <code>IndexError</code> if the value is
* out of range; the <code>Range</code> form will raise a
* <code>RangeError</code>, and the <code>Regexp</code> and <code>String</code>
* forms will silently ignore the assignment.
*/
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];
}
rb_check_arity(argc, 2, 3);
return rb_str_aset(str, argv[0], argv[1]);
}
/*
* call-seq:
* str.insert(index, other_str) -> str
*
* Inserts <i>other_str</i> before the character at the given
* <i>index</i>, modifying <i>str</i>. Negative indices count from the
* end of the string, and insert <em>after</em> the given character.
* The intent is insert <i>aString</i> so that it starts at the given
* <i>index</i>.
*
* "abcd".insert(0, 'X') #=> "Xabcd"
* "abcd".insert(3, 'X') #=> "abcXd"
* "abcd".insert(4, 'X') #=> "abcdX"
* "abcd".insert(-3, 'X') #=> "abXcd"
* "abcd".insert(-1, 'X') #=> "abcdX"
*/
static VALUE
rb_str_insert(VALUE str, VALUE idx, VALUE str2)
{
long pos = NUM2LONG(idx);
if (pos == -1) {
return rb_str_append(str, str2);
}
else if (pos < 0) {
pos++;
}
rb_str_splice(str, pos, 0, str2);
return str;
}
/*
* call-seq:
* str.slice!(fixnum) -> fixnum or nil
* str.slice!(fixnum, fixnum) -> new_str or nil
* str.slice!(range) -> new_str or nil
* str.slice!(regexp) -> new_str or nil
* str.slice!(other_str) -> new_str or nil
*
* Deletes the specified portion from <i>str</i>, and returns the portion
* deleted.
*
* string = "this is a string"
* string.slice!(2) #=> "i"
* string.slice!(3..6) #=> " is "
* string.slice!(/s.*t/) #=> "sa st"
* string.slice!("r") #=> "r"
* string #=> "thing"
*/
static VALUE
rb_str_slice_bang(int argc, VALUE *argv, VALUE str)
{
VALUE result;
VALUE buf[3];
int i;
rb_check_arity(argc, 1, 2);
for (i=0; i<argc; i++) {
buf[i] = argv[i];
}
str_modify_keep_cr(str);
result = rb_str_aref_m(argc, buf, str);
if (!NIL_P(result)) {
buf[i] = rb_str_new(0,0);
rb_str_aset_m(argc+1, buf, str);
}
return result;
}
static VALUE
get_pat(VALUE pat, int quote)
{
VALUE val;
switch (TYPE(pat)) {
case T_REGEXP:
return pat;
case T_STRING:
break;
default:
val = rb_check_string_type(pat);
if (NIL_P(val)) {
Check_Type(pat, T_REGEXP);
}
pat = val;
}
if (quote) {
pat = rb_reg_quote(pat);
}
return rb_reg_regcomp(pat);
}
/*
* call-seq:
* str.sub!(pattern, replacement) -> str or nil
* str.sub!(pattern) {|match| block } -> str or nil
*
* Performs the same substitution as String#sub in-place.
*
* Returns +str+ if a substitution was performed or +nil+ if no substitution
* was performed.
*/
static VALUE
rb_str_sub_bang(int argc, VALUE *argv, VALUE str)
{
VALUE pat, repl, hash = Qnil;
int iter = 0;
int tainted = 0;
int untrusted = 0;
long plen;
int min_arity = rb_block_given_p() ? 1 : 2;
rb_check_arity(argc, min_arity, 2);
if (argc == 1) {
iter = 1;
}
else {
repl = argv[1];
hash = rb_check_hash_type(argv[1]);
if (NIL_P(hash)) {
StringValue(repl);
}
if (OBJ_TAINTED(repl)) tainted = 1;
if (OBJ_UNTRUSTED(repl)) untrusted = 1;
}
pat = get_pat(argv[0], 1);
str_modifiable(str);
if (rb_reg_search(pat, str, 0, 0) >= 0) {
rb_encoding *enc;
int cr = ENC_CODERANGE(str);
VALUE match = rb_backref_get();
struct re_registers *regs = RMATCH_REGS(match);
long beg0 = BEG(0);
long end0 = END(0);
char *p, *rp;
long len, rlen;
if (iter || !NIL_P(hash)) {
p = RSTRING_PTR(str); len = RSTRING_LEN(str);
if (iter) {
repl = rb_obj_as_string(rb_yield(rb_reg_nth_match(0, match)));
}
else {
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, 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) {
rb_raise(rb_eEncCompatError, "incompatible character encodings: %s and %s",
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 (OBJ_TAINTED(repl)) tainted = 1;
if (OBJ_UNTRUSTED(repl)) untrusted = 1;
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;
rp = RSTRING_PTR(repl); 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);
}
memcpy(p + beg0, rp, rlen);
len += rlen - plen;
STR_SET_LEN(str, len);
RSTRING_PTR(str)[len] = '\0';
ENC_CODERANGE_SET(str, cr);
if (tainted) OBJ_TAINT(str);
if (untrusted) OBJ_UNTRUST(str);
return str;
}
return Qnil;
}
/*
* call-seq:
* str.sub(pattern, replacement) -> new_str
* str.sub(pattern, hash) -> new_str
* str.sub(pattern) {|match| block } -> new_str
*
* Returns a copy of +str+ with the _first_ occurrence of +pattern+
* replaced by the second argument. The +pattern+ is typically a Regexp; if
* given as a String, any regular expression metacharacters it contains will
* be interpreted literally, e.g. <code>'\\\d'</code> will match a backlash
* followed by 'd', instead of a digit.
*
* If +replacement+ is a String it will be substituted for the matched text.
* It may contain back-references to the pattern's capture groups of the form
* <code>"\\d"</code>, where <i>d</i> is a group number, or
* <code>"\\k<n>"</code>, where <i>n</i> is a group name. If it is a
* double-quoted string, both back-references must be preceded by an
* additional backslash. However, within +replacement+ the special match
* variables, such as <code>&$</code>, will not refer to the current match.
*
* If the second argument is a Hash, and the matched text is one of its keys,
* the corresponding value is the replacement string.
*
* In the block form, the current match string is passed in as a parameter,
* and variables such as <code>$1</code>, <code>$2</code>, <code>$`</code>,
* <code>$&</code>, and <code>$'</code> will be set appropriately. The value
* returned by the block will be substituted for the match on each call.
*
* The result inherits any tainting in the original string or any supplied
* replacement string.
*
* "hello".sub(/[aeiou]/, '*') #=> "h*llo"
* "hello".sub(/([aeiou])/, '<\1>') #=> "h<e>llo"
* "hello".sub(/./) {|s| s.ord.to_s + ' ' } #=> "104 ello"
* "hello".sub(/(?<foo>[aeiou])/, '*\k<foo>*') #=> "h*e*llo"
* 'Is SHELL your preferred shell?'.sub(/[[:upper:]]{2,}/, ENV)
* #=> "Is /bin/bash your preferred shell?"
*/
static VALUE
rb_str_sub(int argc, VALUE *argv, VALUE str)
{
str = rb_str_dup(str);
rb_str_sub_bang(argc, argv, str);
return str;
}
static VALUE
str_gsub(int argc, VALUE *argv, VALUE str, int bang)
{
VALUE pat, val, repl, match, dest, hash = Qnil;
struct re_registers *regs;
long beg, n;
long beg0, end0;
long offset, blen, slen, len, last;
int iter = 0;
char *sp, *cp;
int tainted = 0;
rb_encoding *str_enc;
switch (argc) {
case 1:
RETURN_ENUMERATOR(str, argc, argv);
iter = 1;
break;
case 2:
repl = argv[1];
hash = rb_check_hash_type(argv[1]);
if (NIL_P(hash)) {
StringValue(repl);
}
if (OBJ_TAINTED(repl)) tainted = 1;
break;
default:
rb_check_arity(argc, 1, 2);
}
pat = get_pat(argv[0], 1);
beg = rb_reg_search(pat, str, 0, 0);
if (beg < 0) {
if (bang) return Qnil; /* no match, no substitution */
return rb_str_dup(str);
}
offset = 0;
n = 0;
blen = RSTRING_LEN(str) + 30; /* len + margin */
dest = rb_str_buf_new(blen);
sp = RSTRING_PTR(str);
slen = RSTRING_LEN(str);
cp = sp;
str_enc = STR_ENC_GET(str);
rb_enc_associate(dest, str_enc);
ENC_CODERANGE_SET(dest, rb_enc_asciicompat(str_enc) ? ENC_CODERANGE_7BIT : ENC_CODERANGE_VALID);
do {
n++;
match = rb_backref_get();
regs = RMATCH_REGS(match);
beg0 = BEG(0);
end0 = END(0);
if (iter || !NIL_P(hash)) {
if (iter) {
val = rb_obj_as_string(rb_yield(rb_reg_nth_match(0, match)));
}
else {
val = rb_hash_aref(hash, rb_str_subseq(str, BEG(0), END(0) - BEG(0)));
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 {
val = rb_reg_regsub(repl, str, regs, pat);
}
if (OBJ_TAINTED(val)) tainted = 1;
len = beg - 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);
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_reg_search(pat, str, offset, 0);
} while (beg >= 0);
if (RSTRING_LEN(str) > offset) {
rb_enc_str_buf_cat(dest, cp, RSTRING_LEN(str) - offset, str_enc);
}
rb_reg_search(pat, str, last, 0);
if (bang) {
rb_str_shared_replace(str, dest);
}
else {
RBASIC(dest)->klass = rb_obj_class(str);
OBJ_INFECT(dest, str);
str = dest;
}
if (tainted) OBJ_TAINT(str);
return str;
}
/*
* call-seq:
* str.gsub!(pattern, replacement) -> str or nil
* str.gsub!(pattern) {|match| block } -> str or nil
* str.gsub!(pattern) -> an_enumerator
*
* Performs the substitutions of <code>String#gsub</code> in place, returning
* <i>str</i>, or <code>nil</code> if no substitutions were performed.
* If no block and no <i>replacement</i> is given, an enumerator is returned instead.
*/
static VALUE
rb_str_gsub_bang(int argc, VALUE *argv, VALUE str)
{
str_modify_keep_cr(str);
return str_gsub(argc, argv, str, 1);
}
/*
* call-seq:
* str.gsub(pattern, replacement) -> new_str
* str.gsub(pattern, hash) -> new_str
* str.gsub(pattern) {|match| block } -> new_str
* str.gsub(pattern) -> enumerator
*
* Returns a copy of <i>str</i> with the <em>all</em> occurrences of
* <i>pattern</i> substituted for the second argument. The <i>pattern</i> is
* typically a <code>Regexp</code>; if given as a <code>String</code>, any
* regular expression metacharacters it contains will be interpreted
* literally, e.g. <code>'\\\d'</code> will match a backlash followed by 'd',
* instead of a digit.
*
* If <i>replacement</i> is a <code>String</code> it will be substituted for
* the matched text. It may contain back-references to the pattern's capture
* groups of the form <code>\\\d</code>, where <i>d</i> is a group number, or
* <code>\\\k<n></code>, where <i>n</i> is a group name. If it is a
* double-quoted string, both back-references must be preceded by an
* additional backslash. However, within <i>replacement</i> the special match
* variables, such as <code>&$</code>, will not refer to the current match.
*
* If the second argument is a <code>Hash</code>, and the matched text is one
* of its keys, the corresponding value is the replacement string.
*
* In the block form, the current match string is passed in as a parameter,
* and variables such as <code>$1</code>, <code>$2</code>, <code>$`</code>,
* <code>$&</code>, and <code>$'</code> will be set appropriately. The value
* returned by the block will be substituted for the match on each call.
*
* The result inherits any tainting in the original string or any supplied
* replacement string.
*
* When neither a block nor a second argument is supplied, an
* <code>Enumerator</code> is returned.
*
* "hello".gsub(/[aeiou]/, '*') #=> "h*ll*"
* "hello".gsub(/([aeiou])/, '<\1>') #=> "h<e>ll<o>"
* "hello".gsub(/./) {|s| s.ord.to_s + ' '} #=> "104 101 108 108 111 "
* "hello".gsub(/(?<foo>[aeiou])/, '{\k<foo>}') #=> "h{e}ll{o}"
* 'hello'.gsub(/[eo]/, 'e' => 3, 'o' => '*') #=> "h3ll*"
*/
static VALUE
rb_str_gsub(int argc, VALUE *argv, VALUE str)
{
return str_gsub(argc, argv, str, 0);
}
/*
* call-seq:
* str.replace(other_str) -> str
*
* Replaces the contents and taintedness of <i>str</i> with the corresponding
* values in <i>other_str</i>.
*
* s = "hello" #=> "hello"
* s.replace "world" #=> "world"
*/
VALUE
rb_str_replace(VALUE str, VALUE str2)
{
str_modifiable(str);
if (str == str2) return str;
StringValue(str2);
str_discard(str);
return str_replace(str, str2);
}
/*
* call-seq:
* string.clear -> string
*
* Makes string empty.
*
* a = "abcde"
* a.clear #=> ""
*/
static VALUE
rb_str_clear(VALUE str)
{
str_discard(str);
STR_SET_EMBED(str);
STR_SET_EMBED_LEN(str, 0);
RSTRING_PTR(str)[0] = 0;
if (rb_enc_asciicompat(STR_ENC_GET(str)))
ENC_CODERANGE_SET(str, ENC_CODERANGE_7BIT);
else
ENC_CODERANGE_SET(str, ENC_CODERANGE_VALID);
return str;
}
/*
* call-seq:
* string.chr -> string
*
* Returns a one-character string at the beginning of the string.
*
* a = "abcde"
* a.chr #=> "a"