Skip to content

Caret lost its meaning #95

@akimd

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!

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