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
5 changes: 1 addition & 4 deletions ext/strscan/strscan.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,13 +445,10 @@ static VALUE
strscan_get_charpos(VALUE self)
{
struct strscanner *p;
VALUE substr;

GET_SCANNER(self, p);

substr = rb_funcall(p->str, id_byteslice, 2, INT2FIX(0), LONG2NUM(p->curr));

return rb_str_length(substr);
return LONG2NUM(rb_enc_strlen(S_PBEG(p), CURPTR(p), rb_enc_get(p->str)));
}

/*
Expand Down
17 changes: 17 additions & 0 deletions test/strscan/test_stringscanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,23 @@ def test_pos_unicode
assert_equal 11, s.charpos
end

def test_charpos_not_use_string_methods
string = +'abcädeföghi'
scanner = create_string_scanner(string)

class << string
EnvUtil.suppress_warning do
undef_method(*instance_methods)
end
end

assert_equal 0, scanner.charpos
assert_equal "abcä", scanner.scan_until(/ä/)
assert_equal 4, scanner.charpos
assert_equal "defö", scanner.scan_until(/ö/)
assert_equal 8, scanner.charpos
end

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