string.c: cut down intermediate string

* string.c (rb_external_str_new_with_enc): cut down intermediate
  string for conversion source, by appending with conversion.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58709 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2017-05-14 00:21:00 +00:00
Родитель 7323de517d
Коммит f3a49ebc92
1 изменённых файлов: 29 добавлений и 2 удалений

Просмотреть файл

@ -997,10 +997,37 @@ rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
VALUE
rb_external_str_new_with_enc(const char *ptr, long len, rb_encoding *eenc)
{
rb_encoding *ienc;
VALUE str;
const int eidx = rb_enc_to_index(eenc);
str = rb_tainted_str_new_with_enc(ptr, len, eenc);
return rb_external_str_with_enc(str, eenc);
/* ASCII-8BIT case, no conversion */
if ((eidx == rb_ascii8bit_encindex()) ||
(eidx == rb_usascii_encindex() && search_nonascii(ptr, ptr + len))) {
return rb_tainted_str_new(ptr, len);
}
/* no default_internal or same encoding, no conversion */
ienc = rb_default_internal_encoding();
if (!ienc || eenc == ienc) {
return rb_tainted_str_new_with_enc(ptr, len, eenc);
}
/* ASCII compatible, and ASCII only string, no conversion in
* default_internal */
if ((eidx == rb_ascii8bit_encindex()) ||
(eidx == rb_usascii_encindex()) ||
(rb_enc_asciicompat(eenc) && !search_nonascii(ptr, ptr + len))) {
return rb_tainted_str_new_with_enc(ptr, len, ienc);
}
/* convert from the given encoding to default_internal */
str = rb_tainted_str_new_with_enc(NULL, 0, ienc);
/* when the conversion failed for some reason, just ignore the
* default_internal and result in the given encoding as-is. */
if (NIL_P(rb_str_cat_conv_enc_opts(str, 0, ptr, len, eenc, 0, Qnil))) {
STR_SET_LEN(str, 0);
rb_enc_associate(str, eenc);
rb_str_cat(str, ptr, len);
}
return str;
}
VALUE