mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
string.c: wchar terminator
* string.c (rb_str_lstrip_bang, rb_str_rstrip_bang): terminate wchar strings with wchar 0. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48275 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
6c41b73dec
commit
3614f8bd1c
4 changed files with 41 additions and 9 deletions
|
@ -1,3 +1,8 @@
|
|||
Wed Nov 5 10:54:19 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* string.c (rb_str_lstrip_bang, rb_str_rstrip_bang): terminate
|
||||
wchar strings with wchar 0.
|
||||
|
||||
Tue Nov 4 21:23:22 2014 NAKAMURA Usaku <usa@ruby-lang.org>
|
||||
|
||||
* ext/win32/lib/Win32API.rb: Fiddle::Importer is defined in
|
||||
|
|
|
@ -19,8 +19,22 @@ bug_str_cstr_term(VALUE str)
|
|||
return INT2NUM(c);
|
||||
}
|
||||
|
||||
static VALUE
|
||||
bug_str_cstr_term_char(VALUE str)
|
||||
{
|
||||
long len;
|
||||
char *s;
|
||||
int c;
|
||||
rb_encoding *enc = rb_enc_get(str);
|
||||
|
||||
RSTRING_GETMEM(str, s, len);
|
||||
c = rb_enc_codepoint(&s[len], &s[len+rb_enc_mbminlen(enc)], enc);
|
||||
return c ? rb_enc_uint_chr((unsigned int)c, enc) : Qnil;
|
||||
}
|
||||
|
||||
void
|
||||
Init_cstr(VALUE klass)
|
||||
{
|
||||
rb_define_method(klass, "cstr_term", bug_str_cstr_term, 0);
|
||||
rb_define_method(klass, "cstr_term_char", bug_str_cstr_term_char, 0);
|
||||
}
|
||||
|
|
19
string.c
19
string.c
|
@ -7198,11 +7198,11 @@ static VALUE
|
|||
rb_str_lstrip_bang(VALUE str)
|
||||
{
|
||||
rb_encoding *enc;
|
||||
char *s, *t, *e;
|
||||
char *start, *s, *t, *e;
|
||||
|
||||
str_modify_keep_cr(str);
|
||||
enc = STR_ENC_GET(str);
|
||||
s = RSTRING_PTR(str);
|
||||
start = s = RSTRING_PTR(str);
|
||||
if (!s || RSTRING_LEN(str) == 0) return Qnil;
|
||||
e = t = RSTRING_END(str);
|
||||
/* remove spaces at head */
|
||||
|
@ -7215,9 +7215,10 @@ rb_str_lstrip_bang(VALUE str)
|
|||
}
|
||||
|
||||
if (s > RSTRING_PTR(str)) {
|
||||
STR_SET_LEN(str, t-s);
|
||||
memmove(RSTRING_PTR(str), s, RSTRING_LEN(str));
|
||||
RSTRING_PTR(str)[RSTRING_LEN(str)] = '\0';
|
||||
long len = t - s;
|
||||
memmove(start, s, len);
|
||||
STR_SET_LEN(str, len);
|
||||
TERM_FILL(start+len, rb_enc_mbminlen(enc));
|
||||
return str;
|
||||
}
|
||||
return Qnil;
|
||||
|
@ -7260,12 +7261,12 @@ static VALUE
|
|||
rb_str_rstrip_bang(VALUE str)
|
||||
{
|
||||
rb_encoding *enc;
|
||||
char *s, *t, *e;
|
||||
char *start, *s, *t, *e;
|
||||
|
||||
str_modify_keep_cr(str);
|
||||
enc = STR_ENC_GET(str);
|
||||
rb_str_check_dummy_enc(enc);
|
||||
s = RSTRING_PTR(str);
|
||||
start = s = RSTRING_PTR(str);
|
||||
if (!s || RSTRING_LEN(str) == 0) return Qnil;
|
||||
t = e = RSTRING_END(str);
|
||||
|
||||
|
@ -7284,10 +7285,10 @@ rb_str_rstrip_bang(VALUE str)
|
|||
}
|
||||
}
|
||||
if (t < e) {
|
||||
long len = t-RSTRING_PTR(str);
|
||||
long len = t-start;
|
||||
|
||||
STR_SET_LEN(str, len);
|
||||
RSTRING_PTR(str)[len] = '\0';
|
||||
TERM_FILL(start+len, rb_enc_mbminlen(enc));
|
||||
return str;
|
||||
}
|
||||
return Qnil;
|
||||
|
|
|
@ -39,4 +39,16 @@ class Test_StringCStr < Test::Unit::TestCase
|
|||
assert_equal(0, s.cstr_term)
|
||||
end
|
||||
end
|
||||
|
||||
def test_wchar_lstrip!
|
||||
str = Bug::String.new(" a".encode(Encoding::UTF_16BE))
|
||||
str.lstrip!
|
||||
assert_nil(str.cstr_term_char)
|
||||
end
|
||||
|
||||
def test_wchar_rstrip!
|
||||
str = Bug::String.new("a ".encode(Encoding::UTF_16BE))
|
||||
str.rstrip!
|
||||
assert_nil(str.cstr_term_char)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue