Skip to content

Commit

Permalink
fix(parsers/string): correctly determine pos without getting out of b…
Browse files Browse the repository at this point in the history
…ounds

In some cases, e.g. when used with `lookahead`, the resulting pos in failure result could point beyound input length.
  • Loading branch information
norskeld committed Jan 16, 2023
1 parent 9dc2b1c commit 9927128
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/parsers/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { size } from '@utils/unicode'
export function string(match: string): Parser<string> {
return {
parse(input, pos) {
const nextPos = pos + match.length
const nextPos = Math.min(pos + match.length, input.length)
const slice = input.substring(pos, nextPos)

switch (slice === match) {
Expand Down Expand Up @@ -45,7 +45,7 @@ export function string(match: string): Parser<string> {
export function ustring(match: string): Parser<string> {
return {
parse(input, pos) {
const nextPos = pos + size(match)
const nextPos = Math.min(pos + size(match), input.length)
const slice = input.substring(pos, nextPos)

switch (slice === match) {
Expand Down

0 comments on commit 9927128

Please sign in to comment.