Skip to content

Commit

Permalink
Avoid infinite loop when normalizer deletes characters
Browse files Browse the repository at this point in the history
FIX: Fix an infinite loop in `SearchCursor` when the normalizer function
deletes characters.

See https://discuss.codemirror.net/t/modifying-tolowercase-function-to-support-diacritic-insensitive-search-causes-infinite-loop/8762
  • Loading branch information
marijnh committed Nov 1, 2024
1 parent d0797ab commit 31d4bbc
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class SearchCursor implements Iterator<{from: number, to: number}>{
let str = fromCodePoint(next), start = this.bufferStart + this.bufferPos
this.bufferPos += codePointSize(next)
let norm = this.normalize(str)
for (let i = 0, pos = start;; i++) {
if (norm.length) for (let i = 0, pos = start;; i++) {
let code = norm.charCodeAt(i)
let match = this.match(code, pos, this.bufferPos + this.bufferStart)
if (i == norm.length - 1) {
Expand Down
4 changes: 4 additions & 0 deletions test/test-cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ describe("SearchCursor", () => {
it("produces the correct range for astral chars that get normalized to non-astral", () => {
testMatches(new SearchCursor(Text.of(["𝜎"]), "𝜎"), [[0, 2]])
})

it("can handle normalizers that remove text", () => {
testMatches(new SearchCursor(Text.of(["hello"]), "halal", 0, 5, s => s.replace(/[aeuoi]/g, "")), [[0, 4]])
})
})

describe("RegExpCursor", () => {
Expand Down

0 comments on commit 31d4bbc

Please sign in to comment.