Skip to content

#scan_until behaves differently with Regexp and String patterns #131

Closed

Description

@andrykonchin

Wondering whether it's a correct behaviour of recently introduced matching with a String pattern. Use Ruby 3.4.1 and strscan v3.1.2

String pattern:

require 'strscan'

scanner = StringScanner.new("こんにちは")

scanner.scan_until("ち") # => "こんにち"
scanner.pre_match # => ""
scanner.matched # => "こんにち"
scanner.post_match # => "は"

Regexp pattern:

require 'strscan'

scanner = StringScanner.new("こんにちは")

scanner.scan_until(/ち/) # => "こんにち"
scanner.pre_match # => "こんに"
scanner.matched # => "ち"
scanner.post_match # => "は"

When pattern is String then #pre_match and #matched work differently - #matched returns a substring from the current position to the end of a found substring and #pre_match returns "".

Activity

  1. eregon commented on Feb 11, 2025

    @eregon
    Member

    This is a clear inconsistency so it seems a bug, I think the Regexp behavior is the correct one (and exists since longer).

    Discovered by writing more StringScanner specs while implementing new StringScanner methods on TruffleRuby.

  2. kou commented on Feb 11, 2025

    @kou
    Member

    I agree that the String pattern case has a bug. It should be same as the Regexp pattern.

    @naitoh Could you take a look at this? #106 may be related.

  3. naitoh commented on Feb 12, 2025

    @naitoh
    Contributor

    I agree that the String pattern case has a bug. It should be same as the Regexp pattern.

    Could you take a look at this? #106 may be related.

    Oh, sorry.
    I will check this issue.

  4. added 2 commits that reference this issue on Feb 15, 2025
    1d19d32
    94c8e3f
  5. added a commit that references this issue on Feb 16, 2025
    e1cec2e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions