--- re.c.ruby_orig	2002-10-27 00:57:24.000000000 +0900
+++ re.c	2004-04-26 12:21:52.000000000 +0900
@@ -380,7 +380,8 @@ make_regexp(s, len, flag)
     int len, flag;
 {
     Regexp *rp;
-    char *err;
+    char err[ONIG_MAX_ERROR_MESSAGE_LEN];
+    int r;
 
     /* Handle escaped characters first. */
 
@@ -389,16 +390,17 @@ make_regexp(s, len, flag)
        from that.
     */
 
-    rp = ALLOC(Regexp);
-    MEMZERO((char *)rp, Regexp, 1);
-    rp->buffer = ALLOC_N(char, 16);
-    rp->allocated = 16;
-    rp->fastmap = ALLOC_N(char, 256);
+    r = re_alloc_pattern(&rp);
+    if (r) {
+      re_error_code_to_str(err, r);
+      rb_reg_raise(s, len, err, 0);
+    }
+      
     if (flag) {
 	rp->options = flag;
     }
-    err = re_compile_pattern(s, len, rp);
-    if (err != NULL) {
+    r = re_compile_pattern(s, len, rp, err);
+    if (r != 0) {
 	rb_reg_raise(s, len, err, 0);
     }
 
@@ -532,14 +534,14 @@ rb_reg_prepare_re(re)
     }
 
     if (need_recompile) {
-	char *err;
+	char err[ONIG_MAX_ERROR_MESSAGE_LEN];
+	int r;
 
 	if (FL_TEST(re, KCODE_FIXED))
 	    kcode_set_option(re);
 	rb_reg_check(re);
-	RREGEXP(re)->ptr->fastmap_accurate = 0;
-	err = re_compile_pattern(RREGEXP(re)->str, RREGEXP(re)->len, RREGEXP(re)->ptr);
-	if (err != NULL) {
+	r = re_recompile_pattern(RREGEXP(re)->str, RREGEXP(re)->len, RREGEXP(re)->ptr, err);
+	if (r != 0) {
 	    rb_reg_raise(RREGEXP(re)->str, RREGEXP(re)->len, err, re);
 	}
     }
@@ -606,14 +608,16 @@ rb_reg_search(re, str, pos, reverse)
     if (FL_TEST(re, KCODE_FIXED))
 	kcode_reset_option();
 
-    if (result == -2) {
-	rb_reg_raise(RREGEXP(re)->str, RREGEXP(re)->len,
-		  "Stack overflow in regexp matcher", re);
-    }
-
     if (result < 0) {
-	rb_backref_set(Qnil);
-	return result;
+      if (result == ONIG_MISMATCH) {
+        rb_backref_set(Qnil);
+        return result;
+      }
+      else {
+        char err[ONIG_MAX_ERROR_MESSAGE_LEN];
+        re_error_code_to_str((UChar* )err, result);
+        rb_reg_raise(RREGEXP(re)->str, RREGEXP(re)->len, err, 0);
+      }
     }
 
     if (rb_thread_scope_shared_p()) {
@@ -796,6 +800,85 @@ match_aref(argc, argv, match)
     return rb_reg_nth_match(FIX2INT(idx), match);
 }
 
+#ifdef LIST_OF_CAPTURES
+static struct re_registers*
+capture_log(match, group)
+     VALUE match;
+     int group;
+{
+  struct re_registers *regs;
+
+  regs = RMATCH(match)->regs;
+  if (group > regs->num_regs) {
+    return (struct re_registers* )0;
+  }
+  if (group < 0) {
+    group += regs->num_regs;
+  }
+  if (group <= 0) return (struct re_registers* )0;
+
+  if (! ONIG_IS_LIST_OF_CAPTURES(regs, group))
+    return (struct re_registers* )0;
+
+  return regs->list[group];
+}
+
+static VALUE
+match_capture_log_length(match, gnum)
+     VALUE match;
+     VALUE gnum;
+{
+  int group, idx;
+  struct re_registers *list;
+
+  group = NUM2INT(gnum);
+  list = capture_log(match, group);
+  if (! list) return Qnil;
+
+  return INT2FIX(list->num_regs);
+}
+
+static VALUE
+match_capture_log_begin(match, gnum, num)
+     VALUE match;
+     VALUE gnum;
+     VALUE num;
+{
+  int group, idx;
+  struct re_registers *list;
+
+  group = NUM2INT(gnum);
+  list = capture_log(match, group);
+  if (! list) return Qnil;
+
+  idx = NUM2INT(num);
+  if (idx < 0 || idx >= list->num_regs)
+    return Qnil;
+
+  return INT2FIX(list->beg[idx]);
+}
+
+static VALUE
+match_capture_log_end(match, gnum, num)
+     VALUE match;
+     VALUE gnum;
+     VALUE num;
+{
+  int group, idx;
+  struct re_registers *list;
+
+  group = NUM2INT(gnum);
+  list = capture_log(match, group);
+  if (! list) return Qnil;
+
+  idx = NUM2INT(num);
+  if (idx < 0 || idx >= list->num_regs)
+    return Qnil;
+
+  return INT2FIX(list->end[idx]);
+}
+#endif /* LIST_OF_CAPTURES */
+
 static VALUE
 match_to_s(match)
     VALUE match;
@@ -1429,4 +1512,10 @@ Init_Regexp()
     rb_define_method(rb_cMatch, "to_s", match_to_s, 0);
     rb_define_method(rb_cMatch, "string", match_string, 0);
     rb_define_method(rb_cMatch, "inspect", rb_any_to_s, 0);
+
+#ifdef LIST_OF_CAPTURES
+    rb_define_method(rb_cMatch, "capture_log_length", match_capture_log_length, 1);
+    rb_define_method(rb_cMatch, "capture_log_begin", match_capture_log_begin, 2);
+    rb_define_method(rb_cMatch, "capture_log_end", match_capture_log_end, 2);
+#endif
 }
