diff options
| author | bptato <nincsnevem662@gmail.com> | 2025-02-05 00:40:01 +0100 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2025-02-05 00:46:52 +0100 |
| commit | 8db503b392040adec9742880991dc8a9d6f3bb7c (patch) | |
| tree | dd7674389866dd0fb5faa128276077758047bd03 /src | |
| parent | b0afdb827276f7d8516bf37659172bf8b28cda19 (diff) | |
| download | chawan-8db503b392040adec9742880991dc8a9d6f3bb7c.tar.gz | |
cssparser: fix an infinite loop
startsWithNumber was a poorly copied version of the standard algorithm,
which, as it turns out, is largely redundant.
So I removed most of it and inlined the rest.
Diffstat (limited to 'src')
| -rw-r--r-- | src/css/cssparser.nim | 32 |
1 files changed, 9 insertions, 23 deletions
diff --git a/src/css/cssparser.nim b/src/css/cssparser.nim index 751ff2f5..d92b4fe3 100644 --- a/src/css/cssparser.nim +++ b/src/css/cssparser.nim @@ -178,7 +178,7 @@ proc reconsume(state: var CSSTokenizerState) = func peek(state: CSSTokenizerState; i: int = 0): char = return state.buf[state.at + i] -proc has(state: var CSSTokenizerState; i: int = 0): bool = +func has(state: CSSTokenizerState; i: int = 0): bool = return state.at + i < state.buf.len # next, next(1) @@ -201,25 +201,6 @@ proc startsWithIdentSequence(state: var CSSTokenizerState): bool = else: return false -proc startsWithNumber(state: var CSSTokenizerState): bool = - if state.has(): - case state.peek() - of '+', '-': - if state.has(1): - if state.peek(1) in AsciiDigit: - return true - elif state.peek(1) == '.': - if state.has(2) and state.peek(2) in AsciiDigit: - return true - of '.': - if state.has(1) and state.peek(1) in AsciiDigit: - return true - elif state.peek() in AsciiDigit: - return true - else: - return false - return false - proc skipWhitespace(state: var CSSTokenizerState) = while state.has() and state.peek() in AsciiWhitespace: state.seek(1) @@ -424,14 +405,18 @@ proc consumeToken(state: var CSSTokenizerState): CSSToken = of '{': return CSSToken(t: cttLbrace) of '}': return CSSToken(t: cttRbrace) of '+': - if state.startsWithNumber(): + # starts with a number + if state.has() and state.peek() in AsciiDigit or + state.has(1) and state.peek() == '.' and state.peek(1) in AsciiDigit: state.reconsume() return state.consumeNumericToken() else: return CSSToken(t: cttDelim, cvalue: c) of ',': return CSSToken(t: cttComma) of '-': - if state.startsWithNumber(): + # starts with a number + if state.has() and state.peek() in AsciiDigit or + state.has(1) and state.peek() == '.' and state.peek(1) in AsciiDigit: state.reconsume() return state.consumeNumericToken() elif state.has(1) and state.peek() == '-' and state.peek(1) == '>': @@ -443,7 +428,8 @@ proc consumeToken(state: var CSSTokenizerState): CSSToken = else: return CSSToken(t: cttDelim, cvalue: c) of '.': - if state.startsWithNumber(): + # starts with a number + if state.has() and state.peek() in AsciiDigit: state.reconsume() return state.consumeNumericToken() else: |