Skip to content

Commit 78b7646

Browse files
committed
[Bug #21842] Let rb_interned_str return US-ASCII if possible
1 parent 8ca2f64 commit 78b7646

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

include/ruby/internal/intern/string.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,8 @@ VALUE rb_str_to_interned_str(VALUE str);
444444
* terminating NUL character.
445445
* @exception rb_eArgError `len` is negative.
446446
* @return A found or created instance of ::rb_cString, of `len` bytes
447-
* length, of "binary" encoding, whose contents are identical to
448-
* that of `ptr`.
447+
* length, of US-ASCII or "binary" encoding, whose contents are
448+
* identical to that of `ptr`.
449449
* @pre At least `len` bytes of continuous memory region shall be
450450
* accessible via `ptr`.
451451
*/

string.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12709,7 +12709,15 @@ VALUE
1270912709
rb_interned_str(const char *ptr, long len)
1271012710
{
1271112711
struct RString fake_str = {RBASIC_INIT};
12712-
return register_fstring(setup_fake_str(&fake_str, ptr, len, ENCINDEX_US_ASCII), true, false);
12712+
int encidx = ENCINDEX_US_ASCII;
12713+
int coderange = ENC_CODERANGE_7BIT;
12714+
if (len > 0 && search_nonascii(ptr, ptr + len)) {
12715+
encidx = ENCINDEX_ASCII_8BIT;
12716+
coderange = ENC_CODERANGE_VALID;
12717+
}
12718+
VALUE str = setup_fake_str(&fake_str, ptr, len, encidx);
12719+
ENC_CODERANGE_SET(str, coderange);
12720+
return register_fstring(str, true, false);
1271312721
}
1271412722

1271512723
VALUE

test/-ext-/string/test_interned_str.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,9 @@ def test_interned_str
99
src << "b" * 20
1010
assert_equal "a" * 20, interned_str
1111
end
12+
13+
def test_interned_str_encoding
14+
src = :ascii.name
15+
assert_equal Encoding::US_ASCII, Bug::String.rb_interned_str_dup(src).encoding
16+
end
1217
end

0 commit comments

Comments
 (0)