Function isSubString

  • 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:

    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.

    Parameters

    • smallString: string
    • offset: number
    • row: number
    • col: number
    • bigString: string

    Returns [number, number, number]