From 8a4f514ccc2b297cdae0a11818b63f8e9a046aff Mon Sep 17 00:00:00 2001 From: akr Date: Sat, 16 Feb 2008 20:08:35 +0000 Subject: * include/ruby/re.h (struct rmatch_offset): new struct for character offsets. (struct rmatch): new struct. (struct RMatch): reference struct rmatch. (RMATCH_REGS): new macro. * re.c (match_alloc): initialize struct rmatch. (pair_byte_cmp): new function. (update_char_offset): update character offsets. (match_init_copy): copy regexp and character offsets. (match_sublen): removed. (match_offset): use update_char_offset. (match_begin): ditto. (match_end): ditto. (rb_reg_search): make character offset updated flag false. (match_size): use RMATCH_REGS. (match_backref_number): ditto. (rb_reg_nth_defined): ditto. (rb_reg_nth_match): ditto. (rb_reg_match_pre): ditto. (rb_reg_match_post): ditto. (rb_reg_match_last): ditto. (match_array): ditto. (match_aref): ditto. (match_values_at): ditto. (match_inspect): ditto. * string.c (rb_str_subpat_set): use RMATCH_REGS. (rb_str_sub_bang): ditto. (str_gsub): ditto. (rb_str_split_m): ditto. (scan_once): ditto. * gc.c (obj_free): free character offsets. git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@15513 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- string.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'string.c') diff --git a/string.c b/string.c index 3a78f66a9..109b2f846 100644 --- a/string.c +++ b/string.c @@ -2716,27 +2716,29 @@ rb_str_subpat_set(VALUE str, VALUE re, int nth, VALUE val) VALUE match; long start, end, len; rb_encoding *enc; + struct re_registers *regs; if (rb_reg_search(re, str, 0, 0) < 0) { rb_raise(rb_eIndexError, "regexp not matched"); } match = rb_backref_get(); - if (nth >= RMATCH(match)->regs->num_regs) { + regs = RMATCH_REGS(match); + if (nth >= regs->num_regs) { out_of_range: rb_raise(rb_eIndexError, "index %d out of regexp", nth); } if (nth < 0) { - if (-nth >= RMATCH(match)->regs->num_regs) { + if (-nth >= regs->num_regs) { goto out_of_range; } - nth += RMATCH(match)->regs->num_regs; + nth += regs->num_regs; } - start = RMATCH(match)->BEG(nth); + start = BEG(nth); if (start == -1) { rb_raise(rb_eIndexError, "regexp group %d not matched", nth); } - end = RMATCH(match)->END(nth); + end = END(nth); len = end - start; StringValue(val); enc = rb_enc_check(str, val); @@ -2967,7 +2969,7 @@ rb_str_sub_bang(int argc, VALUE *argv, VALUE str) int cr = ENC_CODERANGE(str); match = rb_backref_get(); - regs = RMATCH(match)->regs; + regs = RMATCH_REGS(match); if (iter || !NIL_P(hash)) { char *p = RSTRING_PTR(str); long len = RSTRING_LEN(str); @@ -3114,7 +3116,7 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang) do { n++; match = rb_backref_get(); - regs = RMATCH(match)->regs; + regs = RMATCH_REGS(match); if (iter || !NIL_P(hash)) { if (iter) { rb_match_busy(match); @@ -4751,7 +4753,7 @@ rb_str_split_m(int argc, VALUE *argv, VALUE str) struct re_registers *regs; while ((end = rb_reg_search(spat, str, start, 0)) >= 0) { - regs = RMATCH(rb_backref_get())->regs; + regs = RMATCH_REGS(rb_backref_get()); if (start == end && BEG(0) == END(0)) { if (!RSTRING_PTR(str)) { rb_ary_push(result, rb_str_new("", 0)); @@ -5397,7 +5399,7 @@ scan_once(VALUE str, VALUE pat, long *start) enc = STR_ENC_GET(str); if (rb_reg_search(pat, str, *start, 0) >= 0) { match = rb_backref_get(); - regs = RMATCH(match)->regs; + regs = RMATCH_REGS(match); if (BEG(0) == END(0)) { /* * Always consume at least one character of the input string -- cgit