Skip to content

Commit

Permalink
Merge pull request #100482 from IllusionMH/search-step-over-surrogate…
Browse files Browse the repository at this point in the history
…s-100134

Step over surrogate pairs on zero-lenth matches (fixes #100134)
  • Loading branch information
alexdima authored Jun 22, 2020
2 parents 1f06ff4 + 206c06b commit 60da9d8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/vs/editor/common/model/textModelSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,12 @@ export class Searcher {
if (matchStartIndex === this._prevMatchStartIndex && matchLength === this._prevMatchLength) {
if (matchLength === 0) {
// the search result is an empty string and won't advance `regex.lastIndex`, so `regex.exec` will stuck here
// we attempt to recover from that by advancing by one
this._searchRegex.lastIndex += 1;
// we attempt to recover from that by advancing by two if surrogate pair found and by one otherwise
if (strings.getNextCodePoint(text, textLength, this._searchRegex.lastIndex) > 0xFFFF) {
this._searchRegex.lastIndex += 2;
} else {
this._searchRegex.lastIndex += 1;
}
continue;
}
// Exit early if the regex matches the same range twice
Expand Down
25 changes: 25 additions & 0 deletions src/vs/editor/test/common/model/textModelSearch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -781,4 +781,29 @@ suite('TextModelSearch', () => {

model.dispose();
});

test('issue #100134. Zero-length matches should properly step over surrogate pairs', () => {
// 1[Laptop]1 - there shoud be no matches inside of [Laptop] emoji
assertFindMatches('1\uD83D\uDCBB1', '()', true, false, null,
[
[1, 1, 1, 1],
[1, 2, 1, 2],
[1, 4, 1, 4],
[1, 5, 1, 5],

]
);
// 1[Hacker Cat]1 = 1[Cat Face][ZWJ][Laptop]1 - there shoud be matches between emoji and ZWJ
// there shoud be no matches inside of [Cat Face] and [Laptop] emoji
assertFindMatches('1\uD83D\uDC31\u200D\uD83D\uDCBB1', '()', true, false, null,
[
[1, 1, 1, 1],
[1, 2, 1, 2],
[1, 4, 1, 4],
[1, 5, 1, 5],
[1, 7, 1, 7],
[1, 8, 1, 8]
]
);
});
});

0 comments on commit 60da9d8

Please sign in to comment.