You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@annotator.apache.org by ge...@apache.org on 2020/11/20 12:26:30 UTC

[incubator-annotator] 07/19: Make CodePointSeeker.seekToChunk count units, not points

This is an automated email from the ASF dual-hosted git repository.

gerben pushed a commit to branch import-dom-seek
in repository https://gitbox.apache.org/repos/asf/incubator-annotator.git

commit 06ea5d5adc5031ffe1b70b88c9630ecc159774ac
Author: Gerben <ge...@treora.com>
AuthorDate: Wed Nov 18 18:57:38 2020 +0100

    Make CodePointSeeker.seekToChunk count units, not points
    
    We don’t actually ever want to count chunk offsets as code points. Note
    its (unused) offsetInChunk method also returns the number in code units.
    
    Ideally the caller would invoke the TextSeeker’s (= codeUnitSeeker’s)
    seekToChunk method. But, currently, doing so would not update the
    codePointSeeker’s position.
---
 packages/dom/src/code-point-seeker.ts | 23 ++---------------------
 1 file changed, 2 insertions(+), 21 deletions(-)

diff --git a/packages/dom/src/code-point-seeker.ts b/packages/dom/src/code-point-seeker.ts
index 10cb1b1..b97089e 100644
--- a/packages/dom/src/code-point-seeker.ts
+++ b/packages/dom/src/code-point-seeker.ts
@@ -61,10 +61,9 @@ export class CodePointSeeker<TChunk extends Chunk<string>> implements ChunkSeeke
   private _readOrSeekToChunk(read: true, target: TChunk, offset?: number): string[]
   private _readOrSeekToChunk(read: false, target: TChunk, offset?: number): void
   private _readOrSeekToChunk(read: boolean, target: TChunk, offset: number = 0) {
-    const oldPosition = this.position;
     const oldRawPosition = this.raw.position;
 
-    let s = this.raw.readToChunk(target, 0);
+    let s = this.raw.readToChunk(target, offset);
 
     const movedForward = this.raw.position >= oldRawPosition;
 
@@ -82,25 +81,7 @@ export class CodePointSeeker<TChunk extends Chunk<string>> implements ChunkSeeke
       ? this.position + result.length
       : this.position - result.length;
 
-    const targetPosition = this.position + offset;
-    if (!read) {
-      this.seekTo(targetPosition);
-    } else {
-      if (targetPosition >= this.position) {
-        // Read further until the target.
-        result = result.concat(this.readTo(targetPosition));
-      }
-      else if (targetPosition >= oldPosition) {
-        // We passed by our target position: step back.
-        this.seekTo(targetPosition);
-        result = result.slice(0, targetPosition - oldPosition);
-      } else {
-        // The target precedes our starting position: read backwards from there.
-        this.seekTo(oldPosition);
-        result = this.readTo(targetPosition);
-      }
-      return result;
-    }
+    if (read) return result;
   }
 
   private _readOrSeekTo(read: true, target: number, roundUp?: boolean): string[];