string.c: preserve coderange when interning a string

Since `str_do_hash` will most likely scan the string to
compute the coderange, we might as well copy it over in the
interned string in case it's useful later.
This commit is contained in:
Jean Boussier 2024-11-13 13:17:49 +01:00
parent fae86a701e
commit 37a16c7812
Notes: git 2024-11-13 13:14:42 +00:00

View File

@ -435,6 +435,9 @@ fstr_update_callback(st_data_t *key, st_data_t *value, st_data_t data, int exist
return ST_STOP;
}
else {
// Unless the string is empty or binary, its coderange has been precomputed.
int coderange = ENC_CODERANGE(str);
if (FL_TEST_RAW(str, STR_FAKESTR)) {
if (arg->copy) {
VALUE new_str;
@ -481,6 +484,8 @@ fstr_update_callback(st_data_t *key, st_data_t *value, st_data_t data, int exist
str = str_new_frozen(rb_cString, str);
}
}
ENC_CODERANGE_SET(str, coderange);
RBASIC(str)->flags |= RSTRING_FSTR;
*key = *value = arg->fstr = str;