This is an automated email from the ASF dual-hosted git repository.
gerben pushed a commit to branch faster-describeTextQuote
in repository https://gitbox.apache.org/repos/asf/incubator-annotator.git
The following commit(s) were added to refs/heads/faster-describeTextQuote by
this push:
new 8300e73 Inline helper function
8300e73 is described below
commit 8300e738b4e719562687ae005c4cfa1817f41990
Author: Gerben <[email protected]>
AuthorDate: Fri Sep 18 16:09:25 2020 +0200
Inline helper function
---
packages/dom/src/text-quote/describe.ts | 44 +++++++++++----------------------
1 file changed, 15 insertions(+), 29 deletions(-)
diff --git a/packages/dom/src/text-quote/describe.ts
b/packages/dom/src/text-quote/describe.ts
index 50a5653..0103cab 100644
--- a/packages/dom/src/text-quote/describe.ts
+++ b/packages/dom/src/text-quote/describe.ts
@@ -74,21 +74,21 @@ function calculateContextForDisambiguation(
// Skip the found match if it is the actual target.
if (matchStartIndex === targetStartIndex) continue;
- // Count how many characters before & after them the false match and
target have in common.
- const sufficientPrefixLength = charactersNeededToBeUnique(
- scopeText,
- targetStartIndex,
- matchStartIndex,
- true,
- prefix.length,
- );
- const sufficientSuffixLength = charactersNeededToBeUnique(
- scopeText,
- targetEndIndex,
- matchEndIndex,
- false,
- suffix.length,
- );
+ // Count how many characters we’d need as a prefix to disqualify this
match.
+ let sufficientPrefixLength = prefix.length + 1;
+ const firstChar = (offset: number) => scopeText[offset -
sufficientPrefixLength];
+ while (firstChar(targetStartIndex) && firstChar(targetStartIndex) ===
firstChar(matchStartIndex))
+ sufficientPrefixLength++;
+ if (!firstChar(targetStartIndex)) // We reached the start of scopeText;
prefix won’t work.
+ sufficientPrefixLength = Infinity;
+
+ // Count how many characters we’d need as a suffix to disqualify this
match.
+ let sufficientSuffixLength = suffix.length + 1;
+ const lastChar = (offset: number) => scopeText[offset +
sufficientSuffixLength - 1];
+ while (lastChar(targetEndIndex) && lastChar(targetEndIndex) ===
lastChar(matchEndIndex))
+ sufficientSuffixLength++;
+ if (!lastChar(targetEndIndex)) // We reached the end of scopeText; suffix
won’t work.
+ sufficientSuffixLength = Infinity;
// Use either the prefix or suffix, whichever is shortest.
if (sufficientPrefixLength <= sufficientSuffixLength) {
@@ -109,20 +109,6 @@ function calculateContextForDisambiguation(
return { prefix, suffix };
}
-function charactersNeededToBeUnique(
- text: string,
- target: number,
- impostor: number,
- reverse = false,
- overlap = 0,
-): number {
- const nextChar = (offset: number) => reverse ? text[offset - 1 - overlap] :
text[offset + overlap];
- while (nextChar(target) && nextChar(target) === nextChar(impostor))
- overlap++;
- if (!nextChar(target)) return Infinity; // end/start of string reached.
- else return overlap + 1;
-}
-
// Get the index of the first character of range within the text of scope.
function getRangeTextPosition(range: Range, scope: Range): number {
const iter = ownerDocument(scope).createNodeIterator(