Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
robinst committed Aug 31, 2017
1 parent 59f995e commit 724d6d0
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/main/java/org/nibor/autolink/internal/WwwScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,57 +14,58 @@ public class WwwScanner implements Scanner {
@Override
public LinkSpan scan(final CharSequence input, int triggerIndex, int rewindIndex) {
final int afterDot = triggerIndex + 4;
if (afterDot >= input.length() || !isWWW(input, triggerIndex)) {
if (afterDot >= input.length() || !isWww(input, triggerIndex)) {
return null;
}

final int first = findFirst(input, triggerIndex, rewindIndex);
if (first == -1) {
return null;
}

int last = findLast(input, afterDot);
if (last == -1) {
return null;
}

return new LinkSpanImpl(LinkType.WWW, first, last + 1);
}

private static int findFirst(final CharSequence input, final int beginIndex, final int rewindIndex) {
if (beginIndex == rewindIndex) {
return beginIndex;
}

// Is the character before www. allowed?
if (isAllowed(input.charAt(beginIndex - 1))) {
return beginIndex;
}

return -1;
}

private static int findLast(final CharSequence input, final int beginIndex) {
final int last = Scanners.findUrlEnd(input, beginIndex);

// Make sure there is at least one dot after the first dot,
// so www.something is not allowed, but www.something.co.uk is
int pointer = last;
while (--pointer > beginIndex) {
if (input.charAt(pointer) == '.' && pointer > beginIndex) return last;
if (input.charAt(pointer) == '.' && pointer > beginIndex) {
return last;
}
}

return -1;
}

private static boolean isAllowed(char c) {
return c != '.' && !Scanners.isAlnum(c);
}

private static boolean isWWW(final CharSequence input, final int triggerIndex) {
return
(input.charAt(triggerIndex + 1) == 'w')
&& (input.charAt(triggerIndex + 2) == 'w')
&& input.charAt(triggerIndex + 3) == '.';

private static boolean isWww(final CharSequence input, final int triggerIndex) {
return input.charAt(triggerIndex + 1) == 'w'
&& input.charAt(triggerIndex + 2) == 'w'
&& input.charAt(triggerIndex + 3) == '.';
}
}

0 comments on commit 724d6d0

Please sign in to comment.