-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cfaca9a
commit 4497465
Showing
5 changed files
with
271 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* QuoteMatcher matches quoted strings, respecting escaped quotes (\") and friends | ||
*/ | ||
export class QuoteMatcher { | ||
static escapeChar = "\\"; | ||
|
||
private quoteMap: boolean[] = []; | ||
|
||
constructor(char: string, corpus: string) { | ||
// Loop over corpus, marking quotes and respecting escape characters. | ||
for (let i = 0; i < corpus.length; i++) { | ||
if (corpus[i] === QuoteMatcher.escapeChar) { | ||
i += 1; | ||
continue; | ||
} | ||
this.quoteMap[i] = corpus[i] === char; | ||
} | ||
} | ||
|
||
findOpening(start: number): number { | ||
// First, search backwards to see if we could be inside a quote | ||
for (let i = start - 1; i >= 0; i--) { | ||
if (this.quoteMap[i]) { | ||
return i; | ||
} | ||
} | ||
|
||
// Didn't find one behind us, the string may start ahead of us. This happens | ||
// to be the same logic we use to search forwards. | ||
return this.findClosing(start); | ||
} | ||
|
||
findClosing(start: number): number { | ||
// Search forwards from start, looking for a non-escaped char | ||
return this.quoteMap.indexOf(true, start); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters