Skip to content

Commit

Permalink
Merge pull request #1784 from enebo/fix_line_calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton authored Nov 7, 2023
2 parents d76f295 + 67a28e5 commit 4a459b2
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions templates/java/org/prism/Nodes.java.erb
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,22 @@ public abstract class Nodes {
return Arrays.copyOf(lineOffsets, lineOffsetsSize);
}

// 1-based
public int line(int byteOffset) {
return startLine + findLine(byteOffset);
}

// 0-based
public int findLine(int byteOffset) {
assert byteOffset >= 0 && byteOffset < bytes.length : byteOffset;
int index = Arrays.binarySearch(lineOffsets, byteOffset);
int line;
if (index < 0) {
line = -index - 1;
line = -index - 2;
} else {
line = index + 1;
line = index;
}
assert line >= 1 && line <= getLineCount() : line;
assert line >= 0 && line <= getLineCount() : line;
return line;
}

Expand Down

0 comments on commit 4a459b2

Please sign in to comment.