Enhance `#scan_integer` to check for valid character following it
還沒有人認領這個 Issue。
評估
研究方向
從 issue 中的 #scan_integer 入口點和 parse_number 範例開始,然後檢視針對 123d 和 123.32 等輸入所描述的現有行為。釐清分隔符 API,並在保留目前掃描行為的同時,為可接受和遭拒絕的後續字元增加涵蓋測試。
由索引模型根據 Issue 內容生成。
描述
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?
- 主要語言
- C
- 星號
- 88
- 分支
- 51
- 平均合併
- 5 天 2 小時
- 30 天內合併 PR
- 2
環境準備
這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
ruby/strscan 的其他 Issue
-
難度 3/5 1-2 天 新手友好度 54/100
-
難度 3/5 1-2 天 新手友好度 35/100
-
難度 3/5 1-2 天 新手友好度 35/100
-
難度 4/5 3-5 天 新手友好度 25/100
相似的 Issue
-
runtime: lf_f128_mod is documented as fmodq (truncating) but implements floor-based modulo; unused未關閉internal correctness low priority rt_lib
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆
-
bug
難度 2/5 1-3 小時 新手友好度 70/100
SanderMertens/flecs#2395 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 76/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 78/100
-
難度 2/5 1-3 小時 新手友好度 78/100
OpenXiangShan/NEMU#1287 ·
維護者通常 1 天內回覆