diff options
author | tomoya ishida <tomoyapenguin@gmail.com> | 2024-01-18 12:42:15 +0900 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2024-01-18 03:42:21 +0000 |
commit | fd1bafc11f74cb2bb74bf97bcba4ef694a533aec (patch) | |
tree | a66d030d6a8d72e9ad780f3deddd3b792b32df5a /ext | |
parent | 4095191f2c037a665353caf8824c7f5eef67efe4 (diff) |
[ruby/stringio] Fix ascii_only? flag in strio_write
(https://github.com/ruby/stringio/pull/77)
Followup of #79
`rb_str_resize()` was changed by https://github.com/ruby/ruby/commit/b0b9f7201acab05c2a3ad92c3043a1f01df3e17f .
```c
rb_str_resize(string, shorter) // clear ENC_CODERANGE in some case
rb_str_resize(string, longer) // does not clear ENC_CODERANGE anymore
```
```c
// rb_str_resize in string.c
if (slen > len && ENC_CODERANGE(str) != ENC_CODERANGE_7BIT) {
ENC_CODERANGE_CLEAR(str);
}
```
I think this change is based on an assumption that appending null bytes
will not change flag `ascii_only?`.
`strio_extend()` will make the string longer if needed, and update the
flags correctly for appending null bytes.
Before `memmove()`, we need to `rb_str_modify()` because updated flags are not
updated for `memmove()`.
https://github.com/ruby/stringio/commit/b31a538576
Diffstat (limited to 'ext')
-rw-r--r-- | ext/stringio/stringio.c | 10 |
1 files changed, 1 insertions, 9 deletions
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index 4eb1511787..27c7f65408 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -915,9 +915,6 @@ strio_extend(struct StringIO *ptr, long pos, long len) if (pos > olen) MEMZERO(RSTRING_PTR(ptr->string) + olen, char, pos - olen); } - else { - rb_str_modify(ptr->string); - } } /* @@ -1464,14 +1461,9 @@ strio_write(VALUE self, VALUE str) } } else { - int cr0 = ENC_CODERANGE(ptr->string); - int cr = ENC_CODERANGE_UNKNOWN; - if (rb_enc_asciicompat(enc) && rb_enc_asciicompat(enc2)) { - cr = ENC_CODERANGE_AND(cr0, ENC_CODERANGE(str)); - } strio_extend(ptr, ptr->pos, len); + rb_str_modify(ptr->string); memmove(RSTRING_PTR(ptr->string)+ptr->pos, RSTRING_PTR(str), len); - if (cr != cr0) ENC_CODERANGE_SET(ptr->string, cr); } RB_GC_GUARD(str); ptr->pos += len; |