From 5d18990ef25886997b4819137915f8fcca21c791 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 19 Oct 2016 19:20:52 +0200 Subject: [PATCH] Simplify and improve link parsing --- .../net/pterodactylus/sone/text/SoneTextParser.java | 19 ++++++------------- .../pterodactylus/sone/text/SoneTextParserTest.java | 7 +++++++ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java b/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java index 74dcb87..81ac751 100644 --- a/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java +++ b/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java @@ -300,19 +300,12 @@ public class SoneTextParser implements Parser { private int findEndOfLink(String line) { Matcher matcher = whitespacePattern.matcher(line); - if (!matcher.find(0)) { - return line.length(); - } - int nextWhitespace = matcher.start(); - int lastPunctuation = nextWhitespace; - while (isPunctuation(line.charAt(lastPunctuation - 1))) { - lastPunctuation -= 1; - } - if (lastPunctuation < nextWhitespace) { - return lastPunctuation; + int endOfLink = matcher.find() ? matcher.start() : line.length(); + while ((endOfLink > 0) && isPunctuation(line.charAt(endOfLink - 1))) { + endOfLink--; } int openParens = 0; - for (int i = 0; i < nextWhitespace; i++) { + for (int i = 0; i < endOfLink; i++) { switch (line.charAt(i)) { case '(': openParens++; @@ -325,10 +318,10 @@ public class SoneTextParser implements Parser { default: } } - return nextWhitespace; + return endOfLink; } - private boolean isPunctuation(char character) { + private static boolean isPunctuation(char character) { return (character == '.') || (character == ','); } diff --git a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java index bfe218e..6483171 100644 --- a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java +++ b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java @@ -281,6 +281,13 @@ public class SoneTextParserTest { } @Test + public void uskLinkEndsAtFirstNonNumericNonSlashCharacterAfterVersionNumber() { + Iterable parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0). Nice", null); + assertThat("Parts", parts, notNullValue()); + assertThat("Part Text", convertText(parts), is("Some link ([USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0|test|test]). Nice")); + } + + @Test public void httpLinkWithOpenedAndClosedParensEndsAtNextClosingParen() { Iterable parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc_(def)) – nice!", null); assertThat("Parts", parts, notNullValue()); -- 2.7.4