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

ruby-changes:3822

From: ko1@a...
Date: Tue, 29 Jan 2008 17:04:16 +0900 (JST)
Subject: [ruby-changes:3822] akr - Ruby:r15311 (trunk): * insns.def (toregexp): generate a regexp from strings instead of one

akr	2008-01-29 17:03:51 +0900 (Tue, 29 Jan 2008)

  New Revision: 15311

  Modified files:
    trunk/ChangeLog
    trunk/compile.c
    trunk/insns.def
    trunk/re.c
    trunk/test/ruby/test_m17n.rb

  Log:
    * insns.def (toregexp): generate a regexp from strings instead of one
      string.
    
    * re.c (rb_reg_new_ary): defined for toregexp.  it concatenates
      strings after each string is preprocessed. 
    
    * compile.c (compile_dstr_fragments): split from compile_dstr.
      (compile_dstr): call compile_dstr_fragments.
      (compile_dregx): defined for dynamic regexp.
      (iseq_compile_each): use compile_dregx for dynamic regexp.
    
      [ruby-dev:33400]


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/compile.c?r1=15311&r2=15310&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=15311&r2=15310&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/re.c?r1=15311&r2=15310&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/insns.def?r1=15311&r2=15310&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/ruby/test_m17n.rb?r1=15311&r2=15310&diff_format=u

Index: re.c
===================================================================
--- re.c	(revision 15310)
+++ re.c	(revision 15311)
@@ -1949,36 +1949,53 @@
     return Qnil;
 }
 
-#if 0
 static VALUE
-rb_reg_preprocess_obj(VALUE str,
-        rb_encoding **fixed_enc, onig_errmsg_buffer err)
+rb_reg_preprocess_dregexp(VALUE ary)
 {
-    VALUE buf;
-    char *p, *end;
-    rb_encoding *enc;
+    rb_encoding *fixed_enc = 0;
+    onig_errmsg_buffer err = "";
+    int i;
+    VALUE result = 0;
+    int argc = RARRAY_LEN(ary);
+    VALUE *argv = RARRAY_PTR(ary);
 
-    StringValue(str);
-    p = RSTRING_PTR(str);
-    end = p + RSTRING_LEN(str);
-    enc = rb_enc_get(str);
+    if (argc == 0) {
+        rb_raise(rb_eArgError, "no arguments given");
+    }
 
-    buf = rb_reg_preprocess(p, end, enc, fixed_enc, err);
-    RB_GC_GUARD(str);
-    return buf;
-}
+    for (i = 0; i < argc; i++) {
+        VALUE str = argv[i];
+        VALUE buf;
+        char *p, *end;
+        rb_encoding *enc;
 
-static VALUE
-rb_reg_preprocess_m(VALUE klass, VALUE obj)
-{
-    rb_encoding *fixed_enc = 0;
-    onig_errmsg_buffer err = "";
-    VALUE str = rb_reg_preprocess_obj(obj, &fixed_enc, err);
-    if (str == Qnil)
-        rb_raise(rb_eArgError, "%s", err);
-    return rb_assoc_new(str, fixed_enc ? Qtrue : Qfalse);
+        StringValue(str);
+        p = RSTRING_PTR(str);
+        end = p + RSTRING_LEN(str);
+        enc = rb_enc_get(str);
+
+        buf = rb_reg_preprocess(p, end, enc, &fixed_enc, err);
+        RB_GC_GUARD(str);
+
+        if (buf == Qnil)
+            rb_raise(rb_eArgError, "%s", err);
+
+        if (i == 0) {
+            /* The encoding of the first fragment is the encoding 
+             * given by the regexp option or script encoding. */
+            if (fixed_enc == 0) {
+                rb_enc_copy(buf, str);
+            }
+        }
+
+        if (!result)
+            result = buf;
+        else
+            rb_str_buf_append(result, buf);
+    }
+
+    return result;
 }
-#endif
 
 static int
 rb_reg_initialize(VALUE obj, const char *s, int len, rb_encoding *enc,
@@ -2085,6 +2102,12 @@
 }
 
 VALUE
+rb_reg_new_ary(VALUE ary, int opt)
+{
+    return rb_reg_new_str(rb_reg_preprocess_dregexp(ary), opt);
+}
+
+VALUE
 rb_enc_reg_new(const char *s, long len, rb_encoding *enc, int options)
 {
     VALUE re = rb_reg_s_alloc(rb_cRegexp);
@@ -3042,10 +3065,6 @@
     rb_define_singleton_method(rb_cRegexp, "last_match", rb_reg_s_last_match, -1);
     rb_define_singleton_method(rb_cRegexp, "try_convert", rb_reg_s_try_convert, 1);
 
-#if 0
-    rb_define_singleton_method(rb_cRegexp, "preprocess", rb_reg_preprocess_m, 1);
-#endif
-
     rb_define_method(rb_cRegexp, "initialize", rb_reg_initialize_m, -1);
     rb_define_method(rb_cRegexp, "initialize_copy", rb_reg_init_copy, 1);
     rb_define_method(rb_cRegexp, "hash", rb_reg_hash, 0);
Index: insns.def
===================================================================
--- insns.def	(revision 15310)
+++ insns.def	(revision 15311)
@@ -397,12 +397,19 @@
  */
 DEFINE_INSN
 toregexp
-(rb_num_t opt)
-(VALUE str)
-(VALUE val)
+(rb_num_t opt, rb_num_t cnt)
+(...)
+(VALUE val) // inc += 1 - cnt;
 {
-    volatile VALUE tmp = str; /* for GC */
-    val = rb_reg_new_str(str, opt);
+    VALUE rb_reg_new_ary(VALUE ary, int options);
+    int i;
+    VALUE ary = rb_ary_new2(cnt);
+    RBASIC(ary)->klass = 0;
+    for (i = 0; i < cnt; i++) {
+        rb_ary_store(ary, cnt-i-1, TOPN(i));
+    }
+    POPN(cnt);
+    val = rb_reg_new_ary(ary, opt);
 }
 
 /**
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 15310)
+++ ChangeLog	(revision 15311)
@@ -1,3 +1,18 @@
+Tue Jan 29 16:59:01 2008  Tanaka Akira  <akr@f...>
+
+	* insns.def (toregexp): generate a regexp from strings instead of one
+	  string.
+
+	* re.c (rb_reg_new_ary): defined for toregexp.  it concatenates
+	  strings after each string is preprocessed. 
+
+	* compile.c (compile_dstr_fragments): split from compile_dstr.
+	  (compile_dstr): call compile_dstr_fragments.
+	  (compile_dregx): defined for dynamic regexp.
+	  (iseq_compile_each): use compile_dregx for dynamic regexp.
+
+	  [ruby-dev:33400]
+
 Tue Jan 29 16:25:26 2008  NARUSE, Yui  <naruse@r...>
 
 	* common.mk, ext/extmkf.rb: always make encdb.h.
Index: compile.c
===================================================================
--- compile.c	(revision 15310)
+++ compile.c	(revision 15311)
@@ -1853,7 +1853,7 @@
 
 
 static int
-compile_dstr(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node)
+compile_dstr_fragments(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int *cntp)
 {
     NODE *list = node->nd_next;
     VALUE lit = node->nd_lit;
@@ -1867,12 +1867,30 @@
 	cnt++;
 	list = list->nd_next;
     }
+    *cntp = cnt;
 
+    return COMPILE_OK;
+}
+
+static int
+compile_dstr(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node)
+{
+    int cnt;
+    compile_dstr_fragments(iseq, ret, node, &cnt);
     ADD_INSN1(ret, nd_line(node), concatstrings, INT2FIX(cnt));
     return COMPILE_OK;
 }
 
 static int
+compile_dregx(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node)
+{
+    int cnt;
+    compile_dstr_fragments(iseq, ret, node, &cnt);
+    ADD_INSN2(ret, nd_line(node), toregexp, INT2FIX(node->nd_cflag), INT2FIX(cnt));
+    return COMPILE_OK;
+}
+
+static int
 compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * cond,
 			 LABEL *then_label, LABEL *else_label)
 {
@@ -4111,8 +4129,7 @@
 	break;
       }
       case NODE_DREGX:{
-	compile_dstr(iseq, ret, node);
-	ADD_INSN1(ret, nd_line(node), toregexp, INT2FIX(node->nd_cflag));
+	compile_dregx(iseq, ret, node);
 
 	if (poped) {
 	    ADD_INSN(ret, nd_line(node), pop);
@@ -4128,8 +4145,7 @@
 	ADD_INSN2(ret, nd_line(node), onceinlinecache, 0, lend);
 	ADD_INSN(ret, nd_line(node), pop);
 
-	compile_dstr(iseq, ret, node);
-	ADD_INSN1(ret, nd_line(node), toregexp, INT2FIX(node->nd_cflag));
+	compile_dregx(iseq, ret, node);
 
 	ADD_INSN1(ret, nd_line(node), setinlinecache, lstart);
 	ADD_LABEL(ret, lend);
Index: test/ruby/test_m17n.rb
===================================================================
--- test/ruby/test_m17n.rb	(revision 15310)
+++ test/ruby/test_m17n.rb	(revision 15311)
@@ -442,14 +442,14 @@
     assert_raise(ArgumentError) { eval(s("/\#{r}\xc2\xa1/s")) }
 
     r = /\xc2\xa1/e
-    #assert_raise(ArgumentError) { eval(s("/\xc2\xa1\#{r}/s")) }
-    #assert_raise(ArgumentError) { eval(s("/\#{r}\xc2\xa1/s")) }
+    assert_raise(ArgumentError) { eval(s("/\xc2\xa1\#{r}/s")) }
+    assert_raise(ArgumentError) { eval(s("/\#{r}\xc2\xa1/s")) }
 
     r = eval(e("/\xc2\xa1/"))
-    #assert_raise(ArgumentError) { /\xc2\xa1#{r}/s }
+    assert_raise(ArgumentError) { /\xc2\xa1#{r}/s }
 
     r = /\xc2\xa1/e
-    #assert_raise(ArgumentError) { /\xc2\xa1#{r}/s }
+    assert_raise(ArgumentError) { /\xc2\xa1#{r}/s }
   end
 
   def test_begin_end_offset
@@ -574,7 +574,7 @@
     }
     assert_regexp_fixed_ascii8bit(/#{}\xc2\xa1/n)
     assert_regexp_fixed_ascii8bit(/\xc2\xa1#{}/n)
-    #assert_raise(SyntaxError) { s1, s2 = s('\xc2'), s('\xa1'); /#{s1}#{s2}/ }
+    assert_nothing_raised { s1, s2 = a('\xc2'), a('\xa1'); /#{s1}#{s2}/ }
   end
 
   def test_dynamic_eucjp_regexp
@@ -584,7 +584,7 @@
     assert_raise(SyntaxError) { eval('/\xc2#{}/e') }
     assert_raise(SyntaxError) { eval('/#{}\xc2/e') }
     assert_raise(SyntaxError) { eval('/\xc2#{}\xa1/e') }
-    #assert_raise(SyntaxError) { s1, s2 = e('\xc2'), e('\xa1'); /#{s1}#{s2}/ }
+    assert_raise(ArgumentError) { s1, s2 = e('\xc2'), e('\xa1'); /#{s1}#{s2}/ }
   end
 
   def test_dynamic_sjis_regexp
@@ -594,7 +594,7 @@
     assert_raise(SyntaxError) { eval('/\x81#{}/s') }
     assert_raise(SyntaxError) { eval('/#{}\x81/s') }
     assert_raise(SyntaxError) { eval('/\x81#{}\xa1/s') }
-    #assert_raise(SyntaxError) { s1, s2 = s('\x81'), s('\xa1'); /#{s1}#{s2}/ }
+    assert_raise(ArgumentError) { s1, s2 = s('\x81'), s('\xa1'); /#{s1}#{s2}/ }
   end
 
   def test_dynamic_utf8_regexp
@@ -604,7 +604,7 @@
     assert_raise(SyntaxError) { eval('/\xc2#{}/u') }
     assert_raise(SyntaxError) { eval('/#{}\xc2/u') }
     assert_raise(SyntaxError) { eval('/\xc2#{}\xa1/u') }
-    #assert_raise(SyntaxError) { s1, s2 = u('\xc2'), u('\xa1'); /#{s1}#{s2}/ }
+    assert_raise(ArgumentError) { s1, s2 = u('\xc2'), u('\xa1'); /#{s1}#{s2}/ }
   end
 
   def test_regexp_unicode
@@ -1080,6 +1080,6 @@
     assert_regexp_usascii_literal('/\u1234#{%q"\x80"}/', nil, SyntaxError)
     assert_regexp_usascii_literal('/\u1234#{"\x80"}/', nil, SyntaxError)
     assert_regexp_usascii_literal('/\u1234\x80/', nil, SyntaxError)
-    assert_regexp_usascii_literal('/\u1234#{}\x80/', nil, RegexpError)
+    assert_regexp_usascii_literal('/\u1234#{}\x80/', nil, ArgumentError)
   end
 end

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

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