From 5cf4396d5df7db7203524896a272b6ec4134aa62 Mon Sep 17 00:00:00 2001 From: Steven Luo Date: Sat, 14 Jun 2014 22:41:39 -0700 Subject: [PATCH] EmulatorView: don't crash when link ends on the last column in a line When linkifying, endRow/endCol currently represent the screen position immediately after the last character in the link; this fits naturally with Java subsequence APIs (where the end index is always one after the last position in the subsequence), but results in endRow being too large if the link in question ends on the last column of a line. Instead, have endRow/endCol point to the last character of the link, avoiding this problem (and fixing an off-by-one error in the FullUnicodeLine case, where we were already calculating endRow/endCol this way). Fixes #341. --- .../androidterm/emulatorview/EmulatorView.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/libraries/emulatorview/src/jackpal/androidterm/emulatorview/EmulatorView.java b/libraries/emulatorview/src/jackpal/androidterm/emulatorview/EmulatorView.java index 2c389b0..ba192f9 100644 --- a/libraries/emulatorview/src/jackpal/androidterm/emulatorview/EmulatorView.java +++ b/libraries/emulatorview/src/jackpal/androidterm/emulatorview/EmulatorView.java @@ -358,11 +358,14 @@ public class EmulatorView extends View implements GestureDetector.OnGestureListe int endRow; int endCol; if (textIsBasic) { + /* endRow/endCol must be the last character of the link, + * not one after -- otherwise endRow might be too large */ + int spanLastPos = spanEnd - 1; // Basic line -- can assume one char per column startRow = spanStart / mColumns; startCol = spanStart % mColumns; - endRow = spanEnd / mColumns; - endCol = spanEnd % mColumns; + endRow = spanLastPos / mColumns; + endCol = spanLastPos % mColumns; } else { /* Iterate over the line to get starting and ending columns * for this span */ @@ -403,9 +406,9 @@ public class EmulatorView extends View implements GestureDetector.OnGestureListe for(int i=startRow; i <= endRow; ++i) { int runStart = (i == startRow) ? startCol: 0; - int runEnd = (i == endRow) ? endCol : mColumns; + int runEnd = (i == endRow) ? endCol : mColumns - 1; - Arrays.fill(linkRows[i], runStart, runEnd, url); + Arrays.fill(linkRows[i], runStart, runEnd + 1, url); } }