Simplify and improve link parsing
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 19 Oct 2016 17:20:52 +0000 (19:20 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 19 Oct 2016 17:20:52 +0000 (19:20 +0200)
src/main/java/net/pterodactylus/sone/text/SoneTextParser.java
src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java

index 74dcb87..81ac751 100644 (file)
@@ -300,19 +300,12 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
 
        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<SoneTextParserContext> {
                                default:
                        }
                }
-               return nextWhitespace;
+               return endOfLink;
        }
 
-       private boolean isPunctuation(char character) {
+       private static boolean isPunctuation(char character) {
                return (character == '.') || (character == ',');
        }
 
index bfe218e..6483171 100644 (file)
@@ -281,6 +281,13 @@ public class SoneTextParserTest {
        }
 
        @Test
+       public void uskLinkEndsAtFirstNonNumericNonSlashCharacterAfterVersionNumber() {
+               Iterable<Part> 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<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc_(def)) – nice!", null);
                assertThat("Parts", parts, notNullValue());