提交 443d914e 编写于 作者: J Johannes Rieken

speed up getWordAtPosSlow, #2312

上级 3887f75d
...@@ -63,6 +63,12 @@ function reverse(str: string): string { ...@@ -63,6 +63,12 @@ function reverse(str: string): string {
} }
function getWordAtPosFast(column: number, wordDefinition: RegExp, text: string, textOffset: number): IWordAtPosition { function getWordAtPosFast(column: number, wordDefinition: RegExp, text: string, textOffset: number): IWordAtPosition {
// matches at the desired column, once to right
// and once to the left. The latter is achived
// by reversing the string. Falls back to getWordAtPosSlow
// when a word is longer than 100 characters. Will
// not work with regular expressions that check the
// shape of a word, like /aabb/
let pos = column - 1 - textOffset; let pos = column - 1 - textOffset;
wordDefinition.lastIndex = pos; wordDefinition.lastIndex = pos;
...@@ -83,13 +89,10 @@ function getWordAtPosFast(column: number, wordDefinition: RegExp, text: string, ...@@ -83,13 +89,10 @@ function getWordAtPosFast(column: number, wordDefinition: RegExp, text: string,
} else if (wordDefinition.lastIndex === 100) { } else if (wordDefinition.lastIndex === 100) {
// |W*100 -> very long word // |W*100 -> very long word
wordDefinition.lastIndex = 0; // reset! return getWordAtPosSlow(column, wordDefinition, text, textOffset);
return getWordAtTextSlow(column, wordDefinition, text, textOffset);
} }
} }
wordDefinition.lastIndex = 0; //reset!
if (!rightMatch && !leftMatch) { if (!rightMatch && !leftMatch) {
// nothing matched // nothing matched
return null; return null;
...@@ -118,34 +121,28 @@ function getWordAtPosFast(column: number, wordDefinition: RegExp, text: string, ...@@ -118,34 +121,28 @@ function getWordAtPosFast(column: number, wordDefinition: RegExp, text: string,
}; };
} }
export function getWordAtTextSlow(column: number, wordDefinition: RegExp, text: string, textOffset: number): IWordAtPosition { function getWordAtPosSlow(column: number, wordDefinition: RegExp, text: string, textOffset: number): IWordAtPosition {
// matches all words starting at the beginning
var words = text.match(wordDefinition), // of the input until it finds a match that encloses
k: number, // the desired column. slow but correct
startWord: number,
endWord: number, let pos = column - 1 - textOffset;
startColumn: number, wordDefinition.lastIndex = 0;
endColumn: number,
word: string; let match: RegExpMatchArray;
while (match = wordDefinition.exec(text)) {
if (words) {
for (k = 0; k < words.length; k++) { if (match.index > pos) {
word = words[k].trim(); // |nW -> matched only after the pos
if (word.length > 0) { return null;
startWord = text.indexOf(word, endWord);
endWord = startWord + word.length; } else if (wordDefinition.lastIndex >= pos) {
// W|W -> match encloses pos
startColumn = textOffset + startWord + 1; return {
endColumn = textOffset + endWord + 1; word: match[0],
startColumn: textOffset + 1 + match.index,
if (startColumn <= column && column <= endColumn) { endColumn: textOffset + 1 + wordDefinition.lastIndex
return { };
word: word,
startColumn: startColumn,
endColumn: endColumn
};
}
}
} }
} }
...@@ -153,5 +150,10 @@ export function getWordAtTextSlow(column: number, wordDefinition: RegExp, text: ...@@ -153,5 +150,10 @@ export function getWordAtTextSlow(column: number, wordDefinition: RegExp, text:
} }
export function getWordAtText(column: number, wordDefinition: RegExp, text: string, textOffset: number): IWordAtPosition { export function getWordAtText(column: number, wordDefinition: RegExp, text: string, textOffset: number): IWordAtPosition {
return getWordAtPosFast(column, wordDefinition, text, textOffset); const result = getWordAtPosFast(column, wordDefinition, text, textOffset);
// both (getWordAtPosFast and getWordAtPosSlow) leave the wordDefinition-RegExp
// in an undefined state and to not confuse other users of the wordDefinition
// we reset the lastIndex
wordDefinition.lastIndex = 0;
return result;
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册