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++;
default:
}
}
- return nextWhitespace;
+ return endOfLink;
}
- private boolean isPunctuation(char character) {
+ private static boolean isPunctuation(char character) {
return (character == '.') || (character == ',');
}
}
@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());