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
6 changes: 5 additions & 1 deletion ext/jruby/org/jruby/ext/strscan/RubyStringScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,12 @@ public IRubyObject charpos(ThreadContext context) {

ByteList strBL = str.getByteList();
int strBeg = strBL.begin();
int strSize = strBL.getRealSize();
int pos = curr;

if (pos > strSize) pos = strSize;

return runtime.newFixnum(StringSupport.strLength(strBL.getEncoding(), strBL.unsafeBytes(), strBeg, strBeg + curr));
return runtime.newFixnum(StringSupport.strLength(strBL.getEncoding(), strBL.unsafeBytes(), strBeg, strBeg + pos));
}

private IRubyObject extractRange(Ruby runtime, int beg, int end) {
Expand Down
6 changes: 4 additions & 2 deletions ext/strscan/strscan.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ struct strscanner
#define CURPTR(s) (S_PBEG(s) + (s)->curr)
#define S_RESTLEN(s) (S_LEN(s) - (s)->curr)

#define EOS_P(s) ((s)->curr >= RSTRING_LEN(p->str))
#define EOS_P(s) ((s)->curr >= RSTRING_LEN((s)->str))

#define GET_SCANNER(obj,var) do {\
(var) = check_strscan(obj);\
Expand Down Expand Up @@ -573,10 +573,12 @@ static VALUE
strscan_get_charpos(VALUE self)
{
struct strscanner *p;
const char *s;

GET_SCANNER(self, p);

return LONG2NUM(rb_enc_strlen(S_PBEG(p), CURPTR(p), rb_enc_get(p->str)));
s = EOS_P(p) ? S_PEND(p) : CURPTR(p);
return LONG2NUM(rb_enc_strlen(S_PBEG(p), s, rb_enc_get(p->str)));
}

/*
Expand Down
2 changes: 1 addition & 1 deletion lib/strscan/truffleruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def pos=(new_pos)
end
alias_method :pointer=, :pos=

def charpos = Primitive.string_byte_index_to_character_index(@string, @pos)
def charpos = (eos? ? @string.size : Primitive.string_byte_index_to_character_index(@string, @pos))

def rest = @string.byteslice(@pos, @string.bytesize)

Expand Down
8 changes: 8 additions & 0 deletions test/strscan/test_stringscanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ class << string
assert_equal(8, scanner.charpos)
end

def test_charpos_when_shrunk
s = "\u{e9}" * 64
sc = StringScanner.new(s)
sc.scan(/(?:\u{e9})+/)
s.replace("z")
assert_equal(s.length, sc.charpos)
end

def test_concat
s = create_string_scanner('a'.dup)
s.scan(/a/)
Expand Down
Loading