Improve detection of line boundary assertions

This commit is contained in:
IllusionMH 2021-02-17 04:28:34 +02:00
parent b3f8737839
commit 9dd7226c36

View file

@ -256,7 +256,7 @@ export class FindModelBoundToEditorModel {
let isUsingLineStops = this._state.isRegex && (
this._state.searchString.indexOf('^') >= 0
|| this._state.searchString.indexOf('$') >= 0
);
) && hasLineBoundaryAssertion(this._state.searchString).any;
let { lineNumber, column } = before;
let model = this._editor.getModel();
@ -350,7 +350,7 @@ export class FindModelBoundToEditorModel {
let isUsingLineStops = this._state.isRegex && (
this._state.searchString.indexOf('^') >= 0
|| this._state.searchString.indexOf('$') >= 0
);
) && hasLineBoundaryAssertion(this._state.searchString).any;
let { lineNumber, column } = after;
let model = this._editor.getModel();
@ -594,3 +594,50 @@ export class FindModelBoundToEditorModel {
}
}
}
/**
* Checks if given regular expression has line boundary assertions (`^` and `$)
*
* @param str source text of regular expression
* @returns
*/
function hasLineBoundaryAssertion(str: string) {
const res = {
any: false,
start: false,
end: false
};
let i = 0;
let inCharactersClass = false;
while (i < str.length) {
const char = str[i];
switch (char) {
case '^':
if (!inCharactersClass) {
res.any = true;
res.start = true;
}
break;
case '$':
if (!inCharactersClass) {
res.any = true;
res.end = true;
}
break;
case '[':
inCharactersClass = true;
break;
case ']':
if (inCharactersClass) {
inCharactersClass = false;
}
break;
case '\\':
i++;
break;
}
i++;
}
return res;
}