* string.c (tr_trans): should not be affected by the encoding of
replacement unless actually modified. [ruby-talk:328967] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22505 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
0ec57a60af
commit
bc59123dc0
@ -1,3 +1,8 @@
|
|||||||
|
Sun Feb 22 14:33:06 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* string.c (tr_trans): should not be affected by the encoding of
|
||||||
|
replacement unless actually modified. [ruby-talk:328967]
|
||||||
|
|
||||||
Sun Feb 22 13:38:44 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Sun Feb 22 13:38:44 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* ext/socket/init.c (s_recvfrom_nonblock): handles EAGAIN too.
|
* ext/socket/init.c (s_recvfrom_nonblock): handles EAGAIN too.
|
||||||
|
25
string.c
25
string.c
@ -4791,8 +4791,10 @@ tr_trans(VALUE str, VALUE src, VALUE repl, int sflag)
|
|||||||
char *buf = ALLOC_N(char, max), *t = buf;
|
char *buf = ALLOC_N(char, max), *t = buf;
|
||||||
|
|
||||||
while (s < send) {
|
while (s < send) {
|
||||||
c0 = c = rb_enc_codepoint(s, send, enc);
|
int may_modify = 0;
|
||||||
tlen = clen = rb_enc_codelen(c, enc);
|
c0 = c = rb_enc_codepoint(s, send, e1);
|
||||||
|
clen = rb_enc_codelen(c, e1);
|
||||||
|
tlen = enc == e1 ? clen : rb_enc_codelen(c, enc);
|
||||||
|
|
||||||
s += clen;
|
s += clen;
|
||||||
if (c < 256) {
|
if (c < 256) {
|
||||||
@ -4819,6 +4821,7 @@ tr_trans(VALUE str, VALUE src, VALUE repl, int sflag)
|
|||||||
else {
|
else {
|
||||||
save = -1;
|
save = -1;
|
||||||
c = c0;
|
c = c0;
|
||||||
|
if (enc != e1) may_modify = 1;
|
||||||
}
|
}
|
||||||
while (t - buf + tlen >= max) {
|
while (t - buf + tlen >= max) {
|
||||||
offset = t - buf;
|
offset = t - buf;
|
||||||
@ -4827,6 +4830,9 @@ tr_trans(VALUE str, VALUE src, VALUE repl, int sflag)
|
|||||||
t = buf + offset;
|
t = buf + offset;
|
||||||
}
|
}
|
||||||
rb_enc_mbcput(c, t, enc);
|
rb_enc_mbcput(c, t, enc);
|
||||||
|
if (may_modify && memcmp(s, t, tlen) != 0) {
|
||||||
|
modify = 1;
|
||||||
|
}
|
||||||
t += tlen;
|
t += tlen;
|
||||||
}
|
}
|
||||||
*t = '\0';
|
*t = '\0';
|
||||||
@ -4858,8 +4864,10 @@ tr_trans(VALUE str, VALUE src, VALUE repl, int sflag)
|
|||||||
char *buf = ALLOC_N(char, max), *t = buf;
|
char *buf = ALLOC_N(char, max), *t = buf;
|
||||||
|
|
||||||
while (s < send) {
|
while (s < send) {
|
||||||
c0 = c = rb_enc_codepoint(s, send, enc);
|
int may_modify = 0;
|
||||||
tlen = clen = rb_enc_codelen(c, enc);
|
c0 = c = rb_enc_codepoint(s, send, e1);
|
||||||
|
clen = rb_enc_codelen(c, e1);
|
||||||
|
tlen = enc == e1 ? clen : rb_enc_codelen(c, enc);
|
||||||
|
|
||||||
if (c < 256) {
|
if (c < 256) {
|
||||||
c = trans[c];
|
c = trans[c];
|
||||||
@ -4881,8 +4889,8 @@ tr_trans(VALUE str, VALUE src, VALUE repl, int sflag)
|
|||||||
modify = 1;
|
modify = 1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
modify = 1;
|
|
||||||
c = c0;
|
c = c0;
|
||||||
|
if (enc != e1) may_modify = 1;
|
||||||
}
|
}
|
||||||
while (t - buf + tlen >= max) {
|
while (t - buf + tlen >= max) {
|
||||||
offset = t - buf;
|
offset = t - buf;
|
||||||
@ -4890,7 +4898,12 @@ tr_trans(VALUE str, VALUE src, VALUE repl, int sflag)
|
|||||||
REALLOC_N(buf, char, max);
|
REALLOC_N(buf, char, max);
|
||||||
t = buf + offset;
|
t = buf + offset;
|
||||||
}
|
}
|
||||||
if (s != t) rb_enc_mbcput(c, t, enc);
|
if (s != t) {
|
||||||
|
rb_enc_mbcput(c, t, enc);
|
||||||
|
if (may_modify && memcmp(s, t, tlen) != 0) {
|
||||||
|
modify = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
s += clen;
|
s += clen;
|
||||||
t += tlen;
|
t += tlen;
|
||||||
}
|
}
|
||||||
|
@ -1395,6 +1395,9 @@ class TestString < Test::Unit::TestCase
|
|||||||
assert_equal(S("hippo"), S("hello").tr(S("el"), S("ip")))
|
assert_equal(S("hippo"), S("hello").tr(S("el"), S("ip")))
|
||||||
assert_equal(S("*e**o"), S("hello").tr(S("^aeiou"), S("*")))
|
assert_equal(S("*e**o"), S("hello").tr(S("^aeiou"), S("*")))
|
||||||
assert_equal(S("hal"), S("ibm").tr(S("b-z"), S("a-z")))
|
assert_equal(S("hal"), S("ibm").tr(S("b-z"), S("a-z")))
|
||||||
|
|
||||||
|
a = "abc".force_encoding(Encoding::US_ASCII)
|
||||||
|
assert_equal(Encoding::US_ASCII, a.tr(S("z"), S("\u0101")).encoding)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_tr!
|
def test_tr!
|
||||||
@ -1415,6 +1418,10 @@ class TestString < Test::Unit::TestCase
|
|||||||
a = S("ibm")
|
a = S("ibm")
|
||||||
assert_nil(a.tr!(S("B-Z"), S("A-Z")))
|
assert_nil(a.tr!(S("B-Z"), S("A-Z")))
|
||||||
assert_equal(S("ibm"), a)
|
assert_equal(S("ibm"), a)
|
||||||
|
|
||||||
|
a = "abc".force_encoding(Encoding::US_ASCII)
|
||||||
|
assert_nil(a.tr!(S("z"), S("\u0101")))
|
||||||
|
assert_equal(Encoding::US_ASCII, a.encoding)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_tr_s
|
def test_tr_s
|
||||||
|
Loading…
x
Reference in New Issue
Block a user