When making a fast parser, you want to avoid allocation as much as
possible. That means you never want to mess with the source string, only
keep track of an offset into that string.
Example
Here is a simple example:
isSubString("let", offset, row, col, "let x = 4 in x") // => [newOffset, newRow, newCol]
You are looking for "let" at a given offset. On failure, the
newOffset is -1. On success, the newOffset is the new offset.
Our "let" example would be offset + 3.
You also provide the current row and col which do not align with
offset in a clean way. For example, when you see a \n you are at
row = row + 1 and col = 1. Furthermore, some UTF16 characters are
two words wide, so even if there are no newlines, offset and col
may not be equal.
Check if one string is a sub-string of another.
Remarks
When making a fast parser, you want to avoid allocation as much as possible. That means you never want to mess with the source string, only keep track of an offset into that string.
Example
Here is a simple example:
You are looking for
"let"
at a givenoffset
. On failure, thenewOffset
is-1
. On success, thenewOffset
is the new offset. Our"let"
example would beoffset + 3
.You also provide the current
row
andcol
which do not align withoffset
in a clean way. For example, when you see a\n
you are atrow = row + 1
andcol = 1
. Furthermore, some UTF16 characters are two words wide, so even if there are no newlines,offset
andcol
may not be equal.