Skip to content

Improve specs for StringScanner#pos and StringScanner#charpos #1256

@herwinw

Description

@herwinw

Recently specs for StringScanner#charpos got added. The description of this method (from https://docs.ruby-lang.org/en/3.4/StringScanner.html#method-i-charpos) shows the differences from StringScanner#pos

Returns the character position (initially zero), which may be different from the byte position given by method pos:

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

The documentation for #pos (https://docs.ruby-lang.org/en/3.4/StringScanner.html#method-i-pos) has a similar example

The current specs do not reflect these differences. The following setup (default MRI 3.4 install):

$ ruby -v -rstrscan -e 'p StringScanner::Version'
ruby 3.4.2 (2025-02-15 revision d2930f8e7a) +PRISM [x86_64-linux]
"3.1.2"

passes the charpos tests when replacing the method call with pos, and passes the pos checks when called with charpos.

We should improve these specs to illustrate the difference between these two methods.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions