ruby / ruby/strscan

Caret lost its meaning

Open
#95 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
88
Forks
50
Avg merge
2d 3h
Merged PRs (30d)
2

Description

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!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in /tmp/foo.rb and the StringScanner entry point used by require 'strscan'. Determine whether the ^ anchor can preserve line-start semantics during scanning; done means "bar" is not stripped from "foobarbaz", or the StringScanner documentation clearly warns about the current behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.