Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing StringScanner methods #11259

Open
postmodern opened this issue Sep 29, 2021 · 8 comments
Open

Add missing StringScanner methods #11259

postmodern opened this issue Sep 29, 2021 · 8 comments

Comments

@postmodern
Copy link
Contributor

postmodern commented Sep 29, 2021

Crystal's StringScanner does not support every method which Ruby's StringScanner does.

  • #beginning_of_line?
  • #get_byte
  • #getch
  • #match
  • #match?
  • #matched?
  • #matched_size
  • #exist?
  • #pos
  • #rest?
  • #rest_size
  • #pre_match
  • #post_match
  • #<<
  • #concat
  • #unscan
@postmodern
Copy link
Contributor Author

I particularly need #getch to step through the string if #scan does not match the current position.

@asterite
Copy link
Member

I don't know what you are doing, but << would be impossible to implement because strings are immutable.

@asterite
Copy link
Member

Also, could you describe what you are doing?

@postmodern
Copy link
Contributor Author

I am porting this code to Crystal which walks a string attempt to match a series of regexes at each index, and if one of them matches wraps the matching string in ANSI styling: https://github.com/postmodern/hexdump.rb/blob/ead8dc6b45b281423ad7d4ea86d88f533ea0fbb4/lib/hexdump/theme/rule.rb#L129-L146

I even tried rewriting the code to only use String#match(regex,pos), but match(regex,pos) behaves differently from StringScanner#scan (will only match the regex at the current position, where as String#match will incrementally walk the string until it finds a match).

Yeah mutable methods like #<<, #concat, or even #string= are questionable, but I need #getch.

@HertzDevil
Copy link
Contributor

HertzDevil commented Sep 30, 2021

Have you tried Regex#match with Regex::Options::ANCHORED?

getch should be equivalent to scan(/./), though a Crystal implementation is certainly faster than that.

@postmodern
Copy link
Contributor Author

@HertzDevil ah clever and now the specs pass. Thank you!

I still think StringScanner should support more of Ruby's StringScanner to make porting to Crystal easier. getch, get_byte, match* methods seem like good candidates.

@asterite
Copy link
Member

I agree, though I'll probably name them read_byte, read_char, etc., to match IO

@Kanezoh
Copy link
Contributor

Kanezoh commented Jan 30, 2022

@asterite Hi! I also need those methods when porting Ruby code to Crystal, so tried implementing #read_byte and #read_char methods.
Please take a look at #11785 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants