From 8458e709ab4fd1c095b6281fe74f44e4dd4090d5 Mon Sep 17 00:00:00 2001 From: nobu Date: Sun, 30 Jul 2017 02:56:29 +0000 Subject: [PATCH] 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 --- internal.h | 1 + string.c | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/internal.h b/internal.h index 2e66a6431b..90758f86a4 100644 --- a/internal.h +++ b/internal.h @@ -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, rb_encoding *from, int ecflags, VALUE ecopts); 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 #define STR_NOEMBED FL_USER1 #define STR_SHARED FL_USER2 /* = ELTS_SHARED */ diff --git a/string.c b/string.c index 3e30634ba5..63bf6ba9e2 100644 --- a/string.c +++ b/string.c @@ -932,6 +932,15 @@ rb_str_cat_conv_enc_opts(VALUE newstr, long ofs, const char *ptr, long len, 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 str_cat_conv_enc_opts(VALUE newstr, long ofs, const char *ptr, long len, 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 * 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); + rb_str_initialize(str, ptr, len, eenc); } return str; }