Skip to content

Problematic behaviour of #scan_integer(base: 16) when parses "0x" #140

@andrykonchin

Description

@andrykonchin

The method #scan_integer called with base: 16 is supposed to parse hexadecimal integer literals. There is an edge case when "0x" is not followed by any hex character (e.g. 0x.!@#). In this case #scan_integer doesn't match an integer literal and returns nil:

StringScanner.new("0x.!@#").scan_integer(base: 16)
# => nil

But there are might be pretty valid cases when 0 is expected to be returned instead:

StringScanner.new("0xyz").scan_integer(base: 16) # `"xyz"` is a plain text and `0` is expected

Wondering if it makes sense to match "0" and return 0 in the following cases:

  • "0x<non-hex-char>"
  • "+0x<non-hex-char>"
  • "-0x<non-hex-char>"
  • "0x<end-of-string>"
  • "+0x<end-of-string>"
  • "-0x<end-of-string>"

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