Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions ext/jruby/org/jruby/ext/strscan/RubyStringScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,13 @@ private IRubyObject scan(ThreadContext context, IRubyObject regex, boolean succp
if (ByteList.memcmp(strBL.unsafeBytes(), currPtr, patternBL.unsafeBytes(), patternBL.begin(), patternSize) != 0) {
return context.nil;
}
setRegisters(patternSize);
setRegisters(0, patternSize);
} else {
int pos = StringSupport.index(strBL, patternBL, currPtr, patternEnc);
if (pos == -1) {
return context.nil;
}
setRegisters(patternSize + pos - curr);
setRegisters(pos - curr, patternSize);
}
}

Expand Down Expand Up @@ -371,11 +371,11 @@ private int restLen() {
}

// MRI: set_registers
private void setRegisters(int length) {
private void setRegisters(int pos, int length) {
if (fixedAnchor) {
regs = Region.newRegion(curr, curr + length);
regs = Region.newRegion(pos + curr, pos + curr + length);
} else {
regs = Region.newRegion(0, length);
regs = Region.newRegion(pos, pos + length);
}
}

Expand Down
13 changes: 7 additions & 6 deletions ext/strscan/strscan.c
Original file line number Diff line number Diff line change
Expand Up @@ -571,19 +571,20 @@ match_target(struct strscanner *p)
}

static inline void
set_registers(struct strscanner *p, size_t length)
set_registers(struct strscanner *p, size_t pos, size_t length)
{
const int at = 0;
OnigRegion *regs = &(p->regs);
onig_region_clear(regs);
if (onig_region_set(regs, at, 0, 0)) return;
if (p->fixed_anchor_p) {
regs->beg[at] = p->curr;
regs->end[at] = p->curr + length;
regs->beg[at] = pos + p->curr;
regs->end[at] = pos + p->curr + length;
}
else
{
regs->end[at] = length;
regs->beg[at] = pos;
regs->end[at] = pos + length;
}
}

Expand Down Expand Up @@ -731,7 +732,7 @@ strscan_do_scan(VALUE self, VALUE pattern, int succptr, int getstr, int headonly
if (memcmp(CURPTR(p), RSTRING_PTR(pattern), RSTRING_LEN(pattern)) != 0) {
return Qnil;
}
set_registers(p, RSTRING_LEN(pattern));
set_registers(p, 0, RSTRING_LEN(pattern));
}
else {
rb_encoding *enc = rb_enc_check(p->str, pattern);
Expand All @@ -740,7 +741,7 @@ strscan_do_scan(VALUE self, VALUE pattern, int succptr, int getstr, int headonly
if (pos == -1) {
return Qnil;
}
set_registers(p, RSTRING_LEN(pattern) + pos);
set_registers(p, pos, RSTRING_LEN(pattern));
}
}

Expand Down
67 changes: 61 additions & 6 deletions test/strscan/test_stringscanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -409,12 +409,8 @@ def test_matched
s = create_string_scanner('stra strb strc')
s.scan(/\w+/)
assert_equal('stra', s.matched)
s.scan(/\s+/)
assert_equal(' ', s.matched)
s.scan('st')
assert_equal('st', s.matched)
s.scan(/\w+/)
assert_equal('rb', s.matched)
s.scan_until(/\w+/)
assert_equal('strb', s.matched)
s.scan(/\s+/)
assert_equal(' ', s.matched)
s.scan(/\w+/)
Expand All @@ -432,6 +428,23 @@ def test_matched
assert_equal('t', s.matched)
end

def test_matched_string
omit("not implemented on TruffleRuby") if RUBY_ENGINE == "truffleruby"
s = create_string_scanner('stra strb strc')
s.scan('stra')
assert_equal('stra', s.matched)
s.scan_until('strb')
assert_equal('strb', s.matched)
s.scan(' ')
assert_equal(' ', s.matched)
s.scan('strc')
assert_equal('strc', s.matched)
s.scan('c')
assert_nil(s.matched)
s.getch
assert_nil(s.matched)
end

def test_AREF
s = create_string_scanner('stra strb strc')

Expand Down Expand Up @@ -522,6 +535,27 @@ def test_pre_match
assert_nil(s.pre_match)
end

def test_pre_match_string
omit("not implemented on TruffleRuby") if RUBY_ENGINE == "truffleruby"
s = create_string_scanner('a b c d e')
s.scan('a')
assert_equal('', s.pre_match)
s.skip(' ')
assert_equal('a', s.pre_match)
s.scan('b')
assert_equal('a ', s.pre_match)
s.scan_until('c')
assert_equal('a b ', s.pre_match)
s.getch
assert_equal('a b c', s.pre_match)
s.get_byte
assert_equal('a b c ', s.pre_match)
s.get_byte
assert_equal('a b c d', s.pre_match)
s.scan('never match')
assert_nil(s.pre_match)
end

def test_post_match
s = create_string_scanner('a b c d e')
s.scan(/\w/)
Expand All @@ -546,6 +580,27 @@ def test_post_match
assert_nil(s.post_match)
end

def test_post_match_string
omit("not implemented on TruffleRuby") if RUBY_ENGINE == "truffleruby"
s = create_string_scanner('a b c d e')
s.scan('a')
assert_equal(' b c d e', s.post_match)
s.skip(' ')
assert_equal('b c d e', s.post_match)
s.scan('b')
assert_equal(' c d e', s.post_match)
s.scan_until('c')
assert_equal(' d e', s.post_match)
s.getch
assert_equal('d e', s.post_match)
s.get_byte
assert_equal(' e', s.post_match)
s.get_byte
assert_equal('e', s.post_match)
s.scan('never match')
assert_nil(s.post_match)
end

def test_terminate
s = create_string_scanner('ssss')
s.getch
Expand Down