Skip to content

Commit befd510

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 812eaf3 commit befd510

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

symbol.c

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

170-
171170
static int
172171
sym_check_asciionly(VALUE str, bool fake_str)
173172
{
174-
if (!rb_enc_asciicompat(rb_enc_get(str))) return FALSE;
175173
switch (rb_enc_str_coderange(str)) {
176174
case ENC_CODERANGE_BROKEN:
177175
if (fake_str) {
@@ -188,19 +186,12 @@ sym_check_asciionly(VALUE str, bool fake_str)
188186
static VALUE
189187
dup_string_for_create(VALUE str)
190188
{
191-
rb_encoding *enc = rb_enc_get(str);
192-
193-
str = rb_enc_str_new(RSTRING_PTR(str), RSTRING_LEN(str), enc);
189+
int encidx = ENCODING_GET(str);
194190

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

206197
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy