[前][次][番号順一覧][スレッド一覧]

ruby-changes:34962

From: nobu <ko1@a...>
Date: Sun, 3 Aug 2014 10:56:40 +0900 (JST)
Subject: [ruby-changes:34962] nobu:r47044 (trunk): strscan.c: encoding in messages

nobu	2014-08-03 10:56:31 +0900 (Sun, 03 Aug 2014)

  New Revision: 47044

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=47044

  Log:
    strscan.c: encoding in messages
    
    * ext/strscan/strscan.c (strscan_aref): preserve argument encoding
      in error messages.

  Modified files:
    trunk/ext/strscan/strscan.c
    trunk/test/strscan/test_stringscanner.rb
Index: ext/strscan/strscan.c
===================================================================
--- ext/strscan/strscan.c	(revision 47043)
+++ ext/strscan/strscan.c	(revision 47044)
@@ -976,7 +976,7 @@ strscan_matched_size(VALUE self) https://github.com/ruby/ruby/blob/trunk/ext/strscan/strscan.c#L976
 }
 
 static int
-name_to_backref_number(struct re_registers *regs, VALUE regexp, const char* name, const char* name_end)
+name_to_backref_number(struct re_registers *regs, VALUE regexp, const char* name, const char* name_end, rb_encoding *enc)
 {
     int num;
 
@@ -986,9 +986,8 @@ name_to_backref_number(struct re_registe https://github.com/ruby/ruby/blob/trunk/ext/strscan/strscan.c#L986
 	return num;
     }
     else {
-	VALUE s = rb_str_new(name, (long )(name_end - name));
-	rb_raise(rb_eIndexError, "undefined group name reference: %s",
-				 StringValuePtr(s));
+	rb_enc_raise(enc, rb_eIndexError, "undefined group name reference: %.*s",
+					  rb_long2int(name_end - name), name);
     }
 
     UNREACHABLE;
@@ -1033,13 +1032,10 @@ strscan_aref(VALUE self, VALUE idx) https://github.com/ruby/ruby/blob/trunk/ext/strscan/strscan.c#L1032
     switch (TYPE(idx)) {
         case T_SYMBOL:
             idx = rb_sym2str(idx);
-            name = RSTRING_PTR(idx);
-            goto name_to_backref;
-            break;
+            /* fall through */
         case T_STRING:
-            name = StringValuePtr(idx);
-        name_to_backref:
-	    i = name_to_backref_number(&(p->regs), p->regex, name, name + strlen(name));
+            RSTRING_GETMEM(idx, name, i);
+            i = name_to_backref_number(&(p->regs), p->regex, name, name + i, rb_enc_get(idx));
             break;
         default:
             i = NUM2LONG(idx);
Index: test/strscan/test_stringscanner.rb
===================================================================
--- test/strscan/test_stringscanner.rb	(revision 47043)
+++ test/strscan/test_stringscanner.rb	(revision 47044)
@@ -471,6 +471,7 @@ class TestStringScanner < Test::Unit::Te https://github.com/ruby/ruby/blob/trunk/test/strscan/test_stringscanner.rb#L471
     assert_equal 'foo', s['a']
     assert_equal 'bar', s['b']
     assert_raise(IndexError) { s['c'] }
+    assert_raise_with_message(IndexError, /\u{30c6 30b9 30c8}/) { s["\u{30c6 30b9 30c8}"] }
   end
 
   def test_pre_match

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]