summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-02-22 05:33:07 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-02-22 05:33:07 +0000
commitbc59123dc0b862362fca0cc44d9c43fd012dd767 (patch)
tree6124ec61df5c6472ee6db56a72684f19be460d5f /string.c
parent0ec57a60af78c82fbdcb36375c812dae33eeca6d (diff)
* 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
Diffstat (limited to 'string.c')
-rw-r--r--string.c25
1 files changed, 19 insertions, 6 deletions
diff --git a/string.c b/string.c
index da36213f7c..46cd729894 100644
--- a/string.c
+++ b/string.c
@@ -4791,8 +4791,10 @@ tr_trans(VALUE str, VALUE src, VALUE repl, int sflag)
char *buf = ALLOC_N(char, max), *t = buf;
while (s < send) {
- c0 = c = rb_enc_codepoint(s, send, enc);
- tlen = clen = rb_enc_codelen(c, enc);
+ int may_modify = 0;
+ 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;
if (c < 256) {
@@ -4819,6 +4821,7 @@ tr_trans(VALUE str, VALUE src, VALUE repl, int sflag)
else {
save = -1;
c = c0;
+ if (enc != e1) may_modify = 1;
}
while (t - buf + tlen >= max) {
offset = t - buf;
@@ -4827,6 +4830,9 @@ tr_trans(VALUE str, VALUE src, VALUE repl, int sflag)
t = buf + offset;
}
rb_enc_mbcput(c, t, enc);
+ if (may_modify && memcmp(s, t, tlen) != 0) {
+ modify = 1;
+ }
t += tlen;
}
*t = '\0';
@@ -4858,8 +4864,10 @@ tr_trans(VALUE str, VALUE src, VALUE repl, int sflag)
char *buf = ALLOC_N(char, max), *t = buf;
while (s < send) {
- c0 = c = rb_enc_codepoint(s, send, enc);
- tlen = clen = rb_enc_codelen(c, enc);
+ int may_modify = 0;
+ 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) {
c = trans[c];
@@ -4881,8 +4889,8 @@ tr_trans(VALUE str, VALUE src, VALUE repl, int sflag)
modify = 1;
}
else {
- modify = 1;
c = c0;
+ if (enc != e1) may_modify = 1;
}
while (t - buf + tlen >= max) {
offset = t - buf;
@@ -4890,7 +4898,12 @@ tr_trans(VALUE str, VALUE src, VALUE repl, int sflag)
REALLOC_N(buf, char, max);
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;
t += tlen;
}