1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

string.c: rb_str_initialize

* string.c (rb_str_initialize): new function to (re)initialize a
  string with data and encoding.  extracted from
  rb_external_str_new_with_enc.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59448 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2017-07-30 02:56:29 +00:00
parent c3c215e28a
commit 8458e709ab
2 changed files with 11 additions and 3 deletions

View file

@ -1630,6 +1630,7 @@ VALUE rb_external_str_with_enc(VALUE str, rb_encoding *eenc);
VALUE rb_str_cat_conv_enc_opts(VALUE newstr, long ofs, const char *ptr, long len, VALUE rb_str_cat_conv_enc_opts(VALUE newstr, long ofs, const char *ptr, long len,
rb_encoding *from, int ecflags, VALUE ecopts); rb_encoding *from, int ecflags, VALUE ecopts);
VALUE rb_enc_str_scrub(rb_encoding *enc, VALUE str, VALUE repl); VALUE rb_enc_str_scrub(rb_encoding *enc, VALUE str, VALUE repl);
VALUE rb_str_initialize(VALUE str, const char *ptr, long len, rb_encoding *enc);
#endif #endif
#define STR_NOEMBED FL_USER1 #define STR_NOEMBED FL_USER1
#define STR_SHARED FL_USER2 /* = ELTS_SHARED */ #define STR_SHARED FL_USER2 /* = ELTS_SHARED */

View file

@ -932,6 +932,15 @@ rb_str_cat_conv_enc_opts(VALUE newstr, long ofs, const char *ptr, long len,
ecflags, ecopts); ecflags, ecopts);
} }
VALUE
rb_str_initialize(VALUE str, const char *ptr, long len, rb_encoding *enc)
{
STR_SET_LEN(str, 0);
rb_enc_associate(str, enc);
rb_str_cat(str, ptr, len);
return str;
}
static VALUE static VALUE
str_cat_conv_enc_opts(VALUE newstr, long ofs, const char *ptr, long len, str_cat_conv_enc_opts(VALUE newstr, long ofs, const char *ptr, long len,
rb_encoding *from, rb_encoding *to, rb_encoding *from, rb_encoding *to,
@ -1024,9 +1033,7 @@ rb_external_str_new_with_enc(const char *ptr, long len, rb_encoding *eenc)
/* when the conversion failed for some reason, just ignore the /* when the conversion failed for some reason, just ignore the
* default_internal and result in the given encoding as-is. */ * 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))) { if (NIL_P(rb_str_cat_conv_enc_opts(str, 0, ptr, len, eenc, 0, Qnil))) {
STR_SET_LEN(str, 0); rb_str_initialize(str, ptr, len, eenc);
rb_enc_associate(str, eenc);
rb_str_cat(str, ptr, len);
} }
return str; return str;
} }