Skip to content

Caret lost its meaning #95

Open
Open

Description

@akimd

Hi!

Is there any way StringScanner could keep ^ meaningful? As it is, it denotes the current the beginning of the rest, but not whether we're at the start of a line.

In the following example, "bar" is not expected to be stripped.

$ cat /tmp/foo.rb
require 'strscan'

def strip(str)
  res = +''
  s = StringScanner.new(str)
  while !s.eos?
    res <<
      if s.scan(/foo/)
        ''
      elsif s.scan(/^bar/)
        ''
      else
        s.scan(/./)
        s.matched
      end
  end
  res
end

p strip('foobarbaz')
$ ruby /tmp/foo.rb
"baz"

If there's no way for StringScanner to "fix" the ^ anchor, the documentation should warn about that.
Cheers!

Activity

  1. kou commented on May 13, 2024

    @kou
    Member

    How about using beginning_of_line??

    require 'strscan'
    
    def strip(str)
      res = +''
      s = StringScanner.new(str)
      while !s.eos?
        res <<
          if s.scan(/foo/)
            ''
          elsif s.beginning_of_line? and s.scan(/bar/)
            ''
          else
            s.scan(/./)
            s.matched
          end
      end
      res
    end
    
    p strip('foobarbaz')
  2. akimd commented on May 13, 2024

    @akimd
    Author

    I totally missed it... Thanks, it does solve my issue!

    However the suggestion remains: there's no way to have ^ mean what it means?
    And I think a warning (plus a reference to beginning_of_line?) would help.

    Thanks a lot for the super fast answer.

  3. kou commented on May 13, 2024

    @kou
    Member

    However the suggestion remains: there's no way to have ^ mean what it means?

    Right.

    And I think a warning (plus a reference to beginning_of_line?) would help.

    I think that there is no way to detect ^ from a Regexp object...

  4. akimd commented on May 13, 2024

    @akimd
    Author

    I think that there is no way to detect ^ from a Regexp object...

    I agree on this. Not to mention that the anchor can be anywhere in the Regexp, not just at its start. And maybe in an optional part, or an alternation.

    I was rather thinking of a way to tell the RE engine that it is not at a beginning of line so ^ should not match.

    I don't know its internals well enough, but that path seems way safer and exact to me rather than trying to massage the regex.

  5. akimd commented on May 13, 2024

    @akimd
    Author

    And I think a warning (plus a reference to beginning_of_line?) would help.

    To be clear: I was referring to the documentation, not a warning from strscan.

  6. kou commented on May 13, 2024

    @kou
    Member

    Ah, sorry. I misunderstood.

    Could you open a PR to add a warning document?

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