Use Matcher.quoteReplacement() for escaping

This commit is contained in:
cketti 2018-10-25 01:03:10 +02:00
parent ba7c8a77f9
commit 04111290fc
2 changed files with 19 additions and 3 deletions

View file

@ -1,6 +1,8 @@
package com.fsck.k9.message.quote;
import java.util.regex.Matcher;
import android.content.res.Resources;
import com.fsck.k9.Account.QuoteStyle;
@ -42,9 +44,7 @@ public class TextQuoteCreator {
final String wrappedText = Utility.wrap(body, REPLY_WRAP_LINE_WIDTH - prefix.length());
// "$" and "\" in the quote prefix have to be escaped for
// the replaceAll() invocation.
final String escapedPrefix = prefix.replaceAll("(\\\\|\\$)", "\\\\$1");
final String escapedPrefix = Matcher.quoteReplacement(prefix);
quotedText.append(wrappedText.replaceAll("(?m)^", escapedPrefix));
return quotedText.toString();

View file

@ -69,6 +69,22 @@ class TextQuoteCreatorTest : RobolectricTest() {
""".trimIndent().crlf())
}
@Test
fun prefixQuote_withPrefixThatNeedsEncoding() {
val messageBody = "Line 1\r\nLine 2"
val quoteStyle = QuoteStyle.PREFIX
val quotePrefix = "$1\\t "
val quote = createQuote(messageBody, quoteStyle, quotePrefix)
assertThat(quote).isEqualTo("""
On January 18, 1970 7:53:41 PM UTC, Alice <alice@sender.example> wrote:
$1\t Line 1
$1\t Line 2
""".trimIndent().crlf())
}
@Test
fun headerQuote() {
val messageBody = "Line 1\r\nLine 2\r\nLine 3"