fixed IndexOutOfBounds when rendering quotes

This commit is contained in:
Daniel Gultsch 2021-10-31 10:20:34 +01:00
parent 226eb739bd
commit ba4a47204b
2 changed files with 24 additions and 26 deletions

View File

@ -370,9 +370,7 @@ public class MessageAdapter extends ArrayAdapter<Message> {
char current = body.length() > i ? body.charAt(i) : '\n'; char current = body.length() > i ? body.charAt(i) : '\n';
if (lineStart == -1) { if (lineStart == -1) {
if (previous == '\n') { if (previous == '\n') {
if ( if (i < body.length() && QuoteHelper.isPositionQuoteStart(body, i)) {
QuoteHelper.isPositionQuoteStart(body, i)
) {
// Line start with quote // Line start with quote
lineStart = i; lineStart = i;
if (quoteStart == -1) quoteStart = i; if (quoteStart == -1) quoteStart = i;
@ -806,12 +804,12 @@ public class MessageAdapter extends ArrayAdapter<Message> {
} else if (message.treatAsDownloadable()) { } else if (message.treatAsDownloadable()) {
try { try {
final URI uri = new URI(message.getBody()); final URI uri = new URI(message.getBody());
displayDownloadableMessage(viewHolder, displayDownloadableMessage(viewHolder,
message, message,
activity.getString(R.string.check_x_filesize_on_host, activity.getString(R.string.check_x_filesize_on_host,
UIHelper.getFileDescriptionString(activity, message), UIHelper.getFileDescriptionString(activity, message),
uri.getHost()), uri.getHost()),
darkBackground); darkBackground);
} catch (Exception e) { } catch (Exception e) {
displayDownloadableMessage(viewHolder, displayDownloadableMessage(viewHolder,
message, message,

View File

@ -11,47 +11,47 @@ public class QuoteHelper {
public static final char QUOTE_ALT_CHAR = '»'; public static final char QUOTE_ALT_CHAR = '»';
public static final char QUOTE_ALT_END_CHAR = '«'; public static final char QUOTE_ALT_END_CHAR = '«';
public static boolean isPositionQuoteCharacter(CharSequence body, int pos){ public static boolean isPositionQuoteCharacter(CharSequence body, int pos) {
// second part of logical check actually goes against the logic indicated in the method name, since it also checks for context // second part of logical check actually goes against the logic indicated in the method name, since it also checks for context
// but it's very useful // but it's very useful
return body.charAt(pos) == QUOTE_CHAR || isPositionAltQuoteStart(body, pos); return body.charAt(pos) == QUOTE_CHAR || isPositionAltQuoteStart(body, pos);
} }
public static boolean isPositionQuoteEndCharacter(CharSequence body, int pos){ public static boolean isPositionQuoteEndCharacter(CharSequence body, int pos) {
return body.charAt(pos) == QUOTE_END_CHAR; return body.charAt(pos) == QUOTE_END_CHAR;
} }
public static boolean isPositionAltQuoteCharacter (CharSequence body, int pos){ public static boolean isPositionAltQuoteCharacter(CharSequence body, int pos) {
return body.charAt(pos) == QUOTE_ALT_CHAR; return body.charAt(pos) == QUOTE_ALT_CHAR;
} }
public static boolean isPositionAltQuoteEndCharacter(CharSequence body, int pos){ public static boolean isPositionAltQuoteEndCharacter(CharSequence body, int pos) {
return body.charAt(pos) == QUOTE_ALT_END_CHAR; return body.charAt(pos) == QUOTE_ALT_END_CHAR;
} }
public static boolean isPositionAltQuoteStart(CharSequence body, int pos){ public static boolean isPositionAltQuoteStart(CharSequence body, int pos) {
return isPositionAltQuoteCharacter(body, pos) && !isPositionFollowedByAltQuoteEnd(body, pos); return isPositionAltQuoteCharacter(body, pos) && !isPositionFollowedByAltQuoteEnd(body, pos);
} }
public static boolean isPositionFollowedByQuoteChar(CharSequence body, int pos) { public static boolean isPositionFollowedByQuoteChar(CharSequence body, int pos) {
return body.length() > pos + 1 && isPositionQuoteCharacter(body, pos +1 ); return body.length() > pos + 1 && isPositionQuoteCharacter(body, pos + 1);
} }
// 'Prequote' means anything we require or can accept in front of a QuoteChar // 'Prequote' means anything we require or can accept in front of a QuoteChar
public static boolean isPositionPrecededByPrequote(CharSequence body, int pos){ public static boolean isPositionPrecededByPreQuote(CharSequence body, int pos) {
return UIHelper.isPositionPrecededByLineStart(body, pos); return UIHelper.isPositionPrecededByLineStart(body, pos);
} }
public static boolean isPositionQuoteStart (CharSequence body, int pos){ public static boolean isPositionQuoteStart(CharSequence body, int pos) {
return (isPositionQuoteCharacter(body, pos) return (isPositionQuoteCharacter(body, pos)
&& isPositionPrecededByPrequote(body, pos) && isPositionPrecededByPreQuote(body, pos)
&& (UIHelper.isPositionFollowedByQuoteableCharacter(body, pos) && (UIHelper.isPositionFollowedByQuoteableCharacter(body, pos)
|| isPositionFollowedByQuoteChar(body, pos))); || isPositionFollowedByQuoteChar(body, pos)));
} }
public static boolean bodyContainsQuoteStart (CharSequence body){ public static boolean bodyContainsQuoteStart(CharSequence body) {
for (int i = 0; i < body.length(); i++){ for (int i = 0; i < body.length(); i++) {
if (isPositionQuoteStart(body, i)){ if (isPositionQuoteStart(body, i)) {
return true; return true;
} }
} }
@ -76,7 +76,7 @@ public class QuoteHelper {
return false; return false;
} }
public static boolean isNestedTooDeeply (CharSequence line){ public static boolean isNestedTooDeeply(CharSequence line) {
if (isPositionQuoteStart(line, 0)) { if (isPositionQuoteStart(line, 0)) {
int nestingDepth = 1; int nestingDepth = 1;
for (int i = 1; i < line.length(); i++) { for (int i = 1; i < line.length(); i++) {
@ -91,9 +91,9 @@ public class QuoteHelper {
return false; return false;
} }
public static String replaceAltQuoteCharsInText(String text){ public static String replaceAltQuoteCharsInText(String text) {
for (int i = 0; i < text.length(); i++){ for (int i = 0; i < text.length(); i++) {
if (isPositionAltQuoteStart(text, i)){ if (isPositionAltQuoteStart(text, i)) {
text = text.substring(0, i) + QUOTE_CHAR + text.substring(i + 1); text = text.substring(0, i) + QUOTE_CHAR + text.substring(i + 1);
} }
} }