From: David ‘Bombe’ Roden Date: Wed, 19 Oct 2016 17:20:52 +0000 (+0200) Subject: Simplify and improve link parsing X-Git-Tag: 0.9.6^2~17 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=5d18990ef25886997b4819137915f8fcca21c791;p=Sone.git Simplify and improve link parsing --- 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());