1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2026-03-05 02:45:45 +01:00

fixed hintmatching with wordstartswith, thanks Daniel

This commit is contained in:
Martin Stubenschrott
2009-02-03 22:32:40 +01:00
parent e7c57052fa
commit 125f3057e9

View File

@@ -399,87 +399,51 @@ function Hints() //{{{
let wordSplitRegex = RegExp(options["wordseparators"]); let wordSplitRegex = RegExp(options["wordseparators"]);
// What the **** does this do? --Kris // What the **** does this do? --Kris
//
// This function matches hintStrings like 'hekho' to links like 'Hey Kris, how are you?' -> [HE]y [K]ris [HO]w are you --Daniel
function charsAtBeginningOfWords(chars, words, allowWordOverleaping) function charsAtBeginningOfWords(chars, words, allowWordOverleaping)
{ {
let charIdx = 0; function charMatches(charIdx, chars, wordIdx, words, inWordIdx, allowWordOverleaping)
let numMatchedWords = 0;
for (let [,word] in Iterator(words))
{ {
if (word.length == 0) let matches = (chars[charIdx] == words[wordIdx][inWordIdx]);
continue; if ((matches == false && allowWordOverleaping) || words[wordIdx].length == 0)
let wcIdx = 0;
// Check if the current word matches same characters as the previous word.
// Each already matched word has matched at least one character.
if (charIdx > numMatchedWords)
{ {
let matchingStarted = false; let nextWordIdx = wordIdx + 1;
for (let i in util.range(numMatchedWords, charIdx)) if (nextWordIdx == words.length)
{ return false;
if (chars[i] == word[wcIdx])
{ return charMatches(charIdx, chars, nextWordIdx, words, 0, allowWordOverleaping);
matchingStarted = true;
wcIdx++;
}
else if (matchingStarted)
{
wcIdx = 0;
break;
}
}
} }
// the current word matches same characters as the previous word if (matches)
let prevCharIdx;
if (wcIdx > 0)
{ {
prevCharIdx = charIdx; let nextCharIdx = charIdx + 1;
// now check if it matches additional characters if (nextCharIdx == chars.length)
for (; wcIdx < word.length && charIdx < chars.length; wcIdx++, charIdx++) return true;
{
if (word[wcIdx] != chars[charIdx])
break;
}
// the word doesn't match additional characters, now check if the let nextWordIdx = wordIdx + 1;
// already matched characters are equal to the next characters for matching, let beyondLastWord = (nextWordIdx == words.length);
// if yes, then consume them let charMatched = false;
if (prevCharIdx == charIdx) if (beyondLastWord == false)
{ charMatched = charMatches(nextCharIdx, chars, nextWordIdx, words, 0, allowWordOverleaping)
for (let i = 0; i < wcIdx && charIdx < chars.length; i++, charIdx++)
{
if (word[i] != chars[charIdx])
break;
}
}
numMatchedWords++; if (charMatched)
} return true;
// the current word doesn't match same characters as the previous word, just
// try to match the next characters
else
{
prevCharIdx = charIdx;
for (let i = 0; i < word.length && charIdx < chars.length; i++, charIdx++)
{
if (word[i] != chars[charIdx])
break;
}
if (prevCharIdx == charIdx) if (charMatched == false || beyondLastWord == true)
{ {
if (!allowWordOverleaping) let nextInWordIdx = inWordIdx + 1;
if (nextInWordIdx == words[wordIdx].length)
return false; return false;
return charMatches(nextCharIdx, chars, wordIdx, words, nextInWordIdx, allowWordOverleaping);
} }
else
numMatchedWords++;
} }
if (charIdx == chars.length) return false;
return true;
} }
return (charIdx == chars.length); return charMatches(0, chars, 0, words, 0, allowWordOverleaping);
} }
function stringsAtBeginningOfWords(strings, words, allowWordOverleaping) function stringsAtBeginningOfWords(strings, words, allowWordOverleaping)