Enhance `#scan_integer` to check for valid character following it
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 45/100
Research direction
Start with the #scan_integer entry point and the parse_number example in the issue, then review the existing behavior described for inputs such as 123d and 123.32. Clarify the separator API and add coverage for accepted and rejected following characters while preserving current scans.
Written by the indexing model from the issue text.
Description
Recently the new method #scan_integer was introduced (see #113) to optimize scanning integer values.
The current implementation works regardless of what follows the integer, i.e. scanning 123, 123 something, 123,something, 123.32 and 123something all work and would return 123.
However, in - I suspect - many cases an integer may only be a valid integer if it is (not) followed by certain characters. One example is the input 123d which leads to an error when interpreted as Ruby code.
My use case is PDF syntax. There a token is an integer only when it is followed by a whitespace (ASCII decimal 0, 9, 10, 12, 13 and 32) or a delimiter (( ) < > [ ] / %) character (otherwise it is a generic token). To handle this the implementation using #scan_integer looks like this:
# Parses the number (integer or real) at the current position.
#
# See: PDF2.0 s7.3.3
def parse_number
prepare_string_scanner(20)
pos = self.pos
if (tmp = @ss.scan_integer)
if @ss.eos? || @ss.match?(WHITESPACE_OR_DELIMITER_RE)
# Handle object references, see PDF2.0 s7.3.10
prepare_string_scanner(10)
if @ss.scan(REFERENCE_RE)
tmp = if tmp > 0
Reference.new(tmp, @ss[1].to_i)
else
maybe_raise("Invalid indirect object reference (#{tmp},#{@ss[1].to_i})")
nil
end
end
return tmp
else
self.pos = pos
end
end
val = scan_until(WHITESPACE_OR_DELIMITER_RE) || @ss.scan(/.*/)
if val.match?(/\A[+-]?(?:\d+\.\d*|\.\d+)\z/)
val << '0' if val.getbyte(-1) == 46 # dot '.'
Float(val)
else
TOKEN_CACHE[val] # val is keyword
end
end
end
As you can see we
- need to store the current scan position,
- check if scanning an integer works at the current position,
- scan the content after the integer to verify that it is indeed an integer and work with it, or
- if the previous step didn't work, reset the scan position.
This could be simplified to just a call of #scan_integer if this method would optionally check the contents after it. Something like #scan_integer(separator: SEPARATOR_PATTERN) or maybe #scan_integer(separator_chars: STRING) (where STRING contains separator characters, similar to whole String#tr works).
Would it make sense to include such functionality?
- Dominant language
- C
- Stars
- 88
- Forks
- 51
- Avg merge
- 8m
- Merged PRs (30d)
- 1
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from ruby/strscan
-
Difficulty 3/5 1-2 days Newbie friendliness 54/100
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Similar issues
-
HTTP Request Smuggling: non-final chunked Transfer-Encoding not rejected (CWE-444)Possibly taken @code1a2024 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
jaredgei/wow-forever-android#97 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
openziti/ziti-sdk-c#1254 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
flatpak/xdg-desktop-portal#2215 ·
Maintainers usually reply within 2 days
-
http_fetch tests: the two no-child rows fail, not skip, where a task's children file does not existPossibly taken A pull request linked to this issue is open or already merged. Openarea:http kind:gate-defect
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
InauguralSystems/EigenScript#2397 ·
Maintainers usually reply within 1 day