Skip to content

Commit ab0ca26

Browse files
committed
symbol.c: simplify dup_string_for_create
NB: `str` may be a fake string when the caller is `intern3` or similar. - Use `rb_bare_fstring`, which accept STR_FAKESTR and non-bare strings. - Check for ASCII strings with encindex. - Skip checking for ASCII compatibility. - Skip duping the string.
1 parent 668046f commit ab0ca26

2 files changed

Lines changed: 26 additions & 13 deletions

File tree

symbol.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,9 @@ sym_set_cmp(VALUE a, VALUE b)
166166
return rb_str_hash_cmp(sym_set_sym_get_str(a), sym_set_sym_get_str(b)) == false;
167167
}
168168

169-
170169
static int
171170
sym_check_asciionly(VALUE str, bool fake_str)
172171
{
173-
if (!rb_enc_asciicompat(rb_enc_get(str))) return FALSE;
174172
switch (rb_enc_str_coderange(str)) {
175173
case ENC_CODERANGE_BROKEN:
176174
if (fake_str) {
@@ -187,19 +185,12 @@ sym_check_asciionly(VALUE str, bool fake_str)
187185
static VALUE
188186
dup_string_for_create(VALUE str)
189187
{
190-
rb_encoding *enc = rb_enc_get(str);
191-
192-
str = rb_enc_str_new(RSTRING_PTR(str), RSTRING_LEN(str), enc);
188+
int encidx = ENCODING_GET(str);
193189

194-
rb_encoding *ascii = rb_usascii_encoding();
195-
if (enc != ascii && sym_check_asciionly(str, false)) {
196-
rb_enc_associate(str, ascii);
190+
if (encidx != rb_usascii_encindex() && sym_check_asciionly(str, true)) {
191+
return rb_enc_interned_str(RSTRING_PTR(str), RSTRING_LEN(str), rb_usascii_encoding());
197192
}
198-
OBJ_FREEZE(str);
199-
200-
str = rb_fstring(str);
201-
202-
return str;
193+
return rb_bare_fstring(str);
203194
}
204195

205196
static int

test/ruby/test_symbol.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,28 @@ def test_intern
2020
assert_equal(':"foo=="', "foo==".intern.inspect)
2121
end
2222

23+
def test_intern_bare_string
24+
random = "str#{rand}"
25+
random.instance_variable_set(:@ivar, 1)
26+
random.freeze
27+
str = random.to_sym.name
28+
assert_equal [], str.instance_variables
29+
30+
random = Class.new(String).new("str#{rand}").freeze
31+
str = random.to_sym.name
32+
assert_equal String, str.class
33+
34+
random = "utf8™#{rand}"
35+
random.instance_variable_set(:@ivar, 1)
36+
random.freeze
37+
str = random.to_sym.name
38+
assert_equal [], str.instance_variables
39+
40+
random = Class.new(String).new("utf8™#{rand}").freeze
41+
str = random.to_sym.name
42+
assert_equal String, str.class
43+
end
44+
2345
def test_all_symbols
2446
x = Symbol.all_symbols
2547
assert_kind_of(Array, x)

0 commit comments

Comments
 (0)