mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* string.c (rb_str_sub_bang): trivial optimization.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25564 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
5e74593701
commit
2aa4d69d78
1 changed files with 17 additions and 14 deletions
31
string.c
31
string.c
|
@ -3461,9 +3461,11 @@ rb_str_sub_bang(int argc, VALUE *argv, VALUE str)
|
||||||
struct re_registers *regs = RMATCH_REGS(match);
|
struct re_registers *regs = RMATCH_REGS(match);
|
||||||
long beg0 = BEG(0);
|
long beg0 = BEG(0);
|
||||||
long end0 = END(0);
|
long end0 = END(0);
|
||||||
|
char *p, *rp;
|
||||||
|
long len, rlen;
|
||||||
|
|
||||||
if (iter || !NIL_P(hash)) {
|
if (iter || !NIL_P(hash)) {
|
||||||
char *p = RSTRING_PTR(str); long len = RSTRING_LEN(str);
|
p = RSTRING_PTR(str); len = RSTRING_LEN(str);
|
||||||
|
|
||||||
if (iter) {
|
if (iter) {
|
||||||
repl = rb_obj_as_string(rb_yield(rb_reg_nth_match(0, match)));
|
repl = rb_obj_as_string(rb_yield(rb_reg_nth_match(0, match)));
|
||||||
|
@ -3481,9 +3483,9 @@ rb_str_sub_bang(int argc, VALUE *argv, VALUE str)
|
||||||
enc = rb_enc_compatible(str, repl);
|
enc = rb_enc_compatible(str, repl);
|
||||||
if (!enc) {
|
if (!enc) {
|
||||||
rb_encoding *str_enc = STR_ENC_GET(str);
|
rb_encoding *str_enc = STR_ENC_GET(str);
|
||||||
if (coderange_scan(RSTRING_PTR(str), beg0, str_enc) != ENC_CODERANGE_7BIT ||
|
p = RSTRING_PTR(str); len = RSTRING_LEN(str);
|
||||||
coderange_scan(RSTRING_PTR(str)+end0,
|
if (coderange_scan(p, beg0, str_enc) != ENC_CODERANGE_7BIT ||
|
||||||
RSTRING_LEN(str)-end0, 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_raise(rb_eEncCompatError, "incompatible character encodings: %s and %s",
|
||||||
rb_enc_name(str_enc),
|
rb_enc_name(str_enc),
|
||||||
rb_enc_name(STR_ENC_GET(repl)));
|
rb_enc_name(STR_ENC_GET(repl)));
|
||||||
|
@ -3503,18 +3505,19 @@ rb_str_sub_bang(int argc, VALUE *argv, VALUE str)
|
||||||
cr = cr2;
|
cr = cr2;
|
||||||
}
|
}
|
||||||
plen = end0 - beg0;
|
plen = end0 - beg0;
|
||||||
if (RSTRING_LEN(repl) > plen) {
|
rp = RSTRING_PTR(repl); rlen = RSTRING_LEN(repl);
|
||||||
RESIZE_CAPA(str, RSTRING_LEN(str) + RSTRING_LEN(repl) - plen);
|
len = RSTRING_LEN(str);
|
||||||
|
if (rlen > plen) {
|
||||||
|
RESIZE_CAPA(str, len + rlen - plen);
|
||||||
}
|
}
|
||||||
if (RSTRING_LEN(repl) != plen) {
|
p = RSTRING_PTR(str);
|
||||||
memmove(RSTRING_PTR(str) + beg0 + RSTRING_LEN(repl),
|
if (rlen != plen) {
|
||||||
RSTRING_PTR(str) + beg0 + plen,
|
memmove(p + beg0 + rlen, p + beg0 + plen, len - beg0 - plen);
|
||||||
RSTRING_LEN(str) - beg0 - plen);
|
|
||||||
}
|
}
|
||||||
memcpy(RSTRING_PTR(str) + beg0,
|
memcpy(p + beg0, rp, rlen);
|
||||||
RSTRING_PTR(repl), RSTRING_LEN(repl));
|
len += rlen - plen;
|
||||||
STR_SET_LEN(str, RSTRING_LEN(str) + RSTRING_LEN(repl) - plen);
|
STR_SET_LEN(str, len);
|
||||||
RSTRING_PTR(str)[RSTRING_LEN(str)] = '\0';
|
RSTRING_PTR(str)[len] = '\0';
|
||||||
ENC_CODERANGE_SET(str, cr);
|
ENC_CODERANGE_SET(str, cr);
|
||||||
if (tainted) OBJ_TAINT(str);
|
if (tainted) OBJ_TAINT(str);
|
||||||
if (untrusted) OBJ_UNTRUST(str);
|
if (untrusted) OBJ_UNTRUST(str);
|
||||||
|
|
Loading…
Reference in a new issue