diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-07-14 17:21:41 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-07-14 17:21:41 +0000 |
commit | a7481aae3fc2d9e83dcf548e614209025c09fc83 (patch) | |
tree | 3f8963be47d6d45e6a97c78d0e08b4e112881906 /string.c | |
parent | 6a7fd8cbe7144ab8152017439486d7c5256a594d (diff) | |
download | ruby-a7481aae3fc2d9e83dcf548e614209025c09fc83.tar.gz |
string.c: consider old terminator
* string.c (str_fill_term): consider old terminator length, and should
not use rb_enc_ascget since it depends on the current encoding which
may not be compatible with the new terminator. [Bug #8634]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41967 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r-- | string.c | 35 |
1 files changed, 18 insertions, 17 deletions
@@ -1482,6 +1482,15 @@ rb_string_value_ptr(volatile VALUE *ptr) return RSTRING_PTR(str); } +static int +zero_filled(const char *s, int n) +{ + for (; n > 0; --n) { + if (*s++) return 0; + } + return 1; +} + static const char * str_null_char(const char *s, long len, const int minlen, rb_encoding *enc) { @@ -1489,30 +1498,22 @@ str_null_char(const char *s, long len, const int minlen, rb_encoding *enc) const char *e = s + len; for (; s + minlen <= e; s += n) { - if (!rb_enc_codepoint_len(s, e, &n, enc)) return s; + if (zero_filled(s, minlen)) return s; } return 0; } static char * -str_fill_term(VALUE str, char *s, long len, int termlen, rb_encoding *enc) +str_fill_term(VALUE str, char *s, long len, int oldtermlen, int termlen) { - int oldtermlen = rb_enc_mbminlen(enc); - long capa = rb_str_capacity(str) + oldtermlen; - int n; + long capa = rb_str_capacity(str) + 1; if (capa < len + termlen) { - rb_str_modify_expand(str, len + termlen - capa); + rb_str_modify_expand(str, termlen); } - else { - const char *e = s + len; - int diff = 0; - if (termlen > oldtermlen) diff = termlen - oldtermlen; - if (!diff && str_independent(str) && - !rb_enc_ascget(e, e + oldtermlen, &n, enc)) { - return s; - } - str_make_independent_expand(str, diff); + else if (!str_independent(str)) { + if (zero_filled(s + len, termlen)) return s; + str_make_independent(str); } s = RSTRING_PTR(str); TERM_FILL(s + len, termlen); @@ -1532,7 +1533,7 @@ rb_string_value_cstr(volatile VALUE *ptr) if (str_null_char(s, len, minlen, enc)) { rb_raise(rb_eArgError, "string contains null char"); } - return str_fill_term(str, s, len, minlen, enc); + return str_fill_term(str, s, len, minlen, minlen); } if (!s || memchr(s, 0, len)) { rb_raise(rb_eArgError, "string contains null byte"); @@ -1551,7 +1552,7 @@ rb_str_fill_terminator(VALUE str, const int newminlen) char *s = RSTRING_PTR(str); long len = RSTRING_LEN(str); rb_encoding *enc = rb_enc_get(str); - str_fill_term(str, s, len, newminlen, enc); + str_fill_term(str, s, len, rb_enc_mbminlen(enc), newminlen); } VALUE |