ruby-changes:36195
From: nobu <ko1@a...>
Date: Wed, 5 Nov 2014 10:54:44 +0900 (JST)
Subject: [ruby-changes:36195] nobu:r48275 (trunk): string.c: wchar terminator
nobu 2014-11-05 10:54:22 +0900 (Wed, 05 Nov 2014) New Revision: 48275 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=48275 Log: string.c: wchar terminator * string.c (rb_str_lstrip_bang, rb_str_rstrip_bang): terminate wchar strings with wchar 0. Modified files: trunk/ChangeLog trunk/ext/-test-/string/cstr.c trunk/string.c trunk/test/-ext-/string/test_cstr.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 48274) +++ ChangeLog (revision 48275) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Wed Nov 5 10:54:19 2014 Nobuyoshi Nakada <nobu@r...> + + * 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@r...> * ext/win32/lib/Win32API.rb: Fiddle::Importer is defined in Index: string.c =================================================================== --- string.c (revision 48274) +++ string.c (revision 48275) @@ -7198,11 +7198,11 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/string.c#L7198 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) https://github.com/ruby/ruby/blob/trunk/string.c#L7215 } 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 https://github.com/ruby/ruby/blob/trunk/string.c#L7261 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) https://github.com/ruby/ruby/blob/trunk/string.c#L7285 } } 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; Index: ext/-test-/string/cstr.c =================================================================== --- ext/-test-/string/cstr.c (revision 48274) +++ ext/-test-/string/cstr.c (revision 48275) @@ -19,8 +19,22 @@ bug_str_cstr_term(VALUE str) https://github.com/ruby/ruby/blob/trunk/ext/-test-/string/cstr.c#L19 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); } Index: test/-ext-/string/test_cstr.rb =================================================================== --- test/-ext-/string/test_cstr.rb (revision 48274) +++ test/-ext-/string/test_cstr.rb (revision 48275) @@ -39,4 +39,16 @@ class Test_StringCStr < Test::Unit::Test https://github.com/ruby/ruby/blob/trunk/test/-ext-/string/test_cstr.rb#L39 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/