Skip to content

Commit b31a538

Browse files
authored
Fix ascii_only? flag in strio_write (#77)
Followup of #79 `rb_str_resize()` was changed by ruby/ruby@b0b9f72 . ```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()`.
1 parent 8230552 commit b31a538

2 files changed

Lines changed: 7 additions & 9 deletions

File tree

‎ext/stringio/stringio.c‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -915,9 +915,6 @@ strio_extend(struct StringIO *ptr, long pos, long len)
915915
if (pos>olen)
916916
MEMZERO(RSTRING_PTR(ptr->string) +olen, char, pos-olen);
917917
}
918-
else {
919-
rb_str_modify(ptr->string);
920-
}
921918
}
922919

923920
/*
@@ -1464,14 +1461,9 @@ strio_write(VALUE self, VALUE str)
14641461
}
14651462
}
14661463
else {
1467-
intcr0=ENC_CODERANGE(ptr->string);
1468-
intcr=ENC_CODERANGE_UNKNOWN;
1469-
if (rb_enc_asciicompat(enc) &&rb_enc_asciicompat(enc2)) {
1470-
cr=ENC_CODERANGE_AND(cr0, ENC_CODERANGE(str));
1471-
}
14721464
strio_extend(ptr, ptr->pos, len);
1465+
rb_str_modify(ptr->string);
14731466
memmove(RSTRING_PTR(ptr->string)+ptr->pos, RSTRING_PTR(str), len);
1474-
if (cr!=cr0) ENC_CODERANGE_SET(ptr->string, cr);
14751467
}
14761468
RB_GC_GUARD(str);
14771469
ptr->pos+=len;

‎test/stringio/test_stringio.rb‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,12 @@ def test_coderange_after_overwrite
961961
assert_predicate(s.string,:ascii_only?)
962962
s.write"\u{431 43e 433 443 441}"
963963
assert_not_predicate(s.string,:ascii_only?)
964+
965+
s=StringIO.new("\u{3042}")
966+
s.rewind
967+
assert_not_predicate(s.string,:ascii_only?)
968+
s.write('aaaa')
969+
assert_predicate(s.string,:ascii_only?)
964970
end
965971

966972
defassert_string(content,encoding,str,mesg=nil)

0 commit comments

Comments
 (0)