ruby-changes:3279
From: ko1@a...
Date: 28 Dec 2007 23:56:09 +0900
Subject: [ruby-changes:3279] akr - Ruby:r14772 (trunk): * ext/strscan/strscan.c (str_new): new function for allocate an string
akr 2007-12-28 23:55:43 +0900 (Fri, 28 Dec 2007) New Revision: 14772 Modified files: trunk/ChangeLog trunk/ext/strscan/strscan.c trunk/include/ruby/encoding.h trunk/test/strscan/test_stringscanner.rb Log: * ext/strscan/strscan.c (str_new): new function for allocate an string with encoding propagation. (extract_range): use str_new. (extract_beg_len): ditto. (strscan_peek): ditto. (strscan_rest): ditto. http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=14772&r2=14771 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/include/ruby/encoding.h?r1=14772&r2=14771 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/strscan/test_stringscanner.rb?r1=14772&r2=14771 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ext/strscan/strscan.c?r1=14772&r2=14771 Index: include/ruby/encoding.h =================================================================== --- include/ruby/encoding.h (revision 14771) +++ include/ruby/encoding.h (revision 14772) @@ -51,7 +51,7 @@ rb_encoding* rb_enc_check(VALUE,VALUE); void rb_enc_associate_index(VALUE, int); void rb_enc_associate(VALUE, rb_encoding*); -void rb_enc_copy(VALUE, VALUE); +void rb_enc_copy(VALUE dst, VALUE src); VALUE rb_enc_str_new(const char*, long len, rb_encoding*); long rb_enc_strlen(const char*, const char*, rb_encoding*); Index: ChangeLog =================================================================== --- ChangeLog (revision 14771) +++ ChangeLog (revision 14772) @@ -1,3 +1,12 @@ +Fri Dec 28 23:53:18 2007 Tanaka Akira <akr@f...> + + * ext/strscan/strscan.c (str_new): new function for allocate an string + with encoding propagation. + (extract_range): use str_new. + (extract_beg_len): ditto. + (strscan_peek): ditto. + (strscan_rest): ditto. + Fri Dec 28 20:18:42 2007 WATANABE Hirofumi <eban@r...> * golf_prelude.rb (Object.say): derived from Perl 5.10. Index: ext/strscan/strscan.c =================================================================== --- ext/strscan/strscan.c (revision 14771) +++ ext/strscan/strscan.c (revision 14772) @@ -127,12 +127,20 @@ } static VALUE +str_new(struct strscanner *p, const char *ptr, long len) +{ + VALUE str = rb_str_new(ptr, len); + rb_enc_copy(str, p->str); + return str; +} + +static VALUE extract_range(struct strscanner *p, long beg_i, long end_i) { if (beg_i > S_LEN(p)) return Qnil; if (end_i > S_LEN(p)) end_i = S_LEN(p); - return infect(rb_str_new(S_PBEG(p) + beg_i, end_i - beg_i), p); + return infect(str_new(p, S_PBEG(p) + beg_i, end_i - beg_i), p); } static VALUE @@ -141,7 +149,7 @@ if (beg_i > S_LEN(p)) return Qnil; if (beg_i + len > S_LEN(p)) len = S_LEN(p) - beg_i; - return infect(rb_str_new(S_PBEG(p) + beg_i, len), p); + return infect(str_new(p, S_PBEG(p) + beg_i, len), p); } /* ======================================================================= @@ -737,7 +745,7 @@ len = NUM2LONG(vlen); if (EOS_P(p)) - return infect(rb_str_new("", 0), p); + return infect(str_new(p, "", 0), p); if (p->curr + len > S_LEN(p)) len = S_LEN(p) - p->curr; @@ -999,7 +1007,7 @@ GET_SCANNER(self, p); if (EOS_P(p)) { - return infect(rb_str_new("", 0), p); + return infect(str_new(p, "", 0), p); } return extract_range(p, p->curr, S_LEN(p)); } Index: test/strscan/test_stringscanner.rb =================================================================== --- test/strscan/test_stringscanner.rb (revision 14771) +++ test/strscan/test_stringscanner.rb (revision 14772) @@ -289,7 +289,7 @@ assert_nil s.getch s = StringScanner.new("\244\242".force_encoding("euc-jp")) - assert_equal "\244\242", s.getch + assert_equal "\244\242".force_encoding("euc-jp"), s.getch assert_nil s.getch s = StringScanner.new('test') @@ -317,8 +317,8 @@ assert_nil s.get_byte s = StringScanner.new("\244\242".force_encoding("euc-jp")) - assert_equal "\244", s.get_byte - assert_equal "\242", s.get_byte + assert_equal "\244".force_encoding("euc-jp"), s.get_byte + assert_equal "\242".force_encoding("euc-jp"), s.get_byte assert_nil s.get_byte s = StringScanner.new('test') @@ -414,7 +414,7 @@ s = StringScanner.new("\244\242".force_encoding("euc-jp")) s.getch - assert_equal "\244\242", s[0] + assert_equal "\244\242".force_encoding("euc-jp"), s[0] str = 'test' str.taint @@ -536,4 +536,9 @@ s.terminate assert_nil s.matched_size end + + def test_encoding + ss = StringScanner.new("\xA1\xA2".force_encoding("euc-jp")) + assert_equal(Encoding::EUC_JP, ss.scan(/./e).encoding) + end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml