summaryrefslogtreecommitdiffstats
path: root/re.c
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-01-03 17:48:06 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-01-03 17:48:06 +0000
commit6edc21d2c63f6fd191345966a41471ec2b27b91c (patch)
tree8d310bc0d94fd3e8174031d6d30c1152d62790f5 /re.c
parent84f81ccc45c694ac7f61d76d99a4e5934d5b3d83 (diff)
downloadruby-6edc21d2c63f6fd191345966a41471ec2b27b91c.tar.gz
ruby-6edc21d2c63f6fd191345966a41471ec2b27b91c.tar.xz
ruby-6edc21d2c63f6fd191345966a41471ec2b27b91c.zip
* re.c (rb_reg_search): iterate onig_match for reverse mode.
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@14876 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 're.c')
-rw-r--r--re.c31
1 files changed, 23 insertions, 8 deletions
diff --git a/re.c b/re.c
index 7924b2e82..35ad59b1a 100644
--- a/re.c
+++ b/re.c
@@ -1036,6 +1036,7 @@ rb_reg_search(VALUE re, VALUE str, int pos, int reverse)
VALUE match;
static struct re_registers regs;
int range;
+ rb_encoding *enc = rb_enc_get(str);
if (pos > RSTRING_LEN(str) || pos < 0) {
rb_backref_set(Qnil);
@@ -1045,19 +1046,33 @@ rb_reg_search(VALUE re, VALUE str, int pos, int reverse)
rb_reg_prepare_re(re, str);
if (reverse) {
- range = -pos;
+ char *p = RSTRING_PTR(str) + pos;
+ while (1) {
+ result = onig_match(RREGEXP(re)->ptr,
+ (UChar*)(RSTRING_PTR(str)),
+ ((UChar*)(RSTRING_PTR(str)) + RSTRING_LEN(str)),
+ (UChar*)p,
+ &regs,
+ ONIG_OPTION_NONE);
+ if (result != ONIG_MISMATCH) {
+ result = p - RSTRING_PTR(str);
+ break;
+ }
+ if (RSTRING_PTR(str) == p)
+ break;
+ p = rb_enc_prev_char(RSTRING_PTR(str), p, enc);
+ }
}
else {
range = RSTRING_LEN(str) - pos;
+ result = onig_search(RREGEXP(re)->ptr,
+ (UChar*)(RSTRING_PTR(str)),
+ ((UChar*)(RSTRING_PTR(str)) + RSTRING_LEN(str)),
+ ((UChar*)(RSTRING_PTR(str)) + pos),
+ ((UChar*)(RSTRING_PTR(str)) + pos + range),
+ &regs, ONIG_OPTION_NONE);
}
- result = onig_search(RREGEXP(re)->ptr,
- (UChar*)(RSTRING_PTR(str)),
- ((UChar*)(RSTRING_PTR(str)) + RSTRING_LEN(str)),
- ((UChar*)(RSTRING_PTR(str)) + pos),
- ((UChar*)(RSTRING_PTR(str)) + pos + range),
- &regs, ONIG_OPTION_NONE);
-
if (result < 0) {
if (result == ONIG_MISMATCH) {
rb_backref_set(Qnil);