Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
StringScanner#charpos
() -> ::Integer
charpos → character_position
ext/strscan/strscan.c
View on GitHub ↗
static VALUE
strscan_get_charpos(VALUE self)
{
struct strscanner *p;
const char *s;
GET_SCANNER(self, p);
s = EOS_P(p) ? S_PEND(p) : CURPTR(p);
return LEN2NUM(rb_enc_strlen(S_PBEG(p), s, rb_enc_get(p->str)));
}
Returns the character position (initially zero), which may be different from the byte position given by method pos:
Example
scanner = StringScanner.new(HIRAGANA_TEXT) scanner.string # => "こんにちは" scanner.getch # => "こ" # 3-byte character. scanner.getch # => "ん" # 3-byte character. put_situation(scanner) # Situation: # pos: 6 # charpos: 2 # rest: "にちは" # rest_size: 9
See also
#captures
#check
#beginning_of_line?
#check_until
also in
4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0