Skip to content

Commit d2f234d

Browse files
eamonnmcmanusgoogle-java-format Team
authored andcommitted
Remove the requirement for CharStream patterns to be anchored with ^.
This apparently stemmed only from the use of `Matcher.find()` where `.lookingAt()` would be more appropriate. Also inline `fullCommentPattern()`. There doesn't seem to be any reason for it alone to be a separate method, and it was flagged by static analysis with a suggestion that it should in fact be a static final. PiperOrigin-RevId: 884437383
1 parent 71f52d5 commit d2f234d

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

core/src/main/java/com/google/googlejavaformat/java/javadoc/CharStream.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
package com.google.googlejavaformat.java.javadoc;
1616

17-
import static com.google.common.base.Preconditions.checkArgument;
1817
import static com.google.common.base.Preconditions.checkNotNull;
1918

2019
import java.util.regex.Matcher;
@@ -50,10 +49,9 @@ boolean tryConsume(String expected) {
5049
*/
5150
boolean tryConsumeRegex(Pattern pattern) {
5251
Matcher matcher = pattern.matcher(input).region(start, input.length());
53-
if (!matcher.find()) {
52+
if (!matcher.lookingAt()) {
5453
return false;
5554
}
56-
checkArgument(matcher.start() == start);
5755
tokenEnd = matcher.end();
5856
return true;
5957
}

core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocLexer.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ private static ImmutableList<Token> optionalizeSpacesAfterLinks(List<Token> inpu
393393
ImmutableList.Builder<Token> output = ImmutableList.builder();
394394

395395
for (PeekingIterator<Token> tokens = peekingIterator(input.iterator()); tokens.hasNext(); ) {
396-
if (tokens.peek().getType() == LITERAL && tokens.peek().getValue().matches("^href=[^>]*>")) {
396+
if (tokens.peek().getType() == LITERAL && tokens.peek().getValue().matches("href=[^>]*>")) {
397397
output.add(tokens.next());
398398

399399
if (tokens.peek().getType() == WHITESPACE) {
@@ -519,18 +519,18 @@ private static boolean hasMultipleNewlines(String s) {
519519
* We'd remove the trailing whitespace later on (in JavaCommentsHelper.rewrite), but I feel safer
520520
* stripping it now: It otherwise might confuse our line-length count, which we use for wrapping.
521521
*/
522-
private static final Pattern NEWLINE_PATTERN = compile("^[ \t]*\n[ \t]*[*]?[ \t]?");
522+
private static final Pattern NEWLINE_PATTERN = compile("[ \t]*\n[ \t]*[*]?[ \t]?");
523523

524524
// We ensure elsewhere that we match this only at the beginning of a line.
525525
// Only match tags that start with a lowercase letter, to avoid false matches on unescaped
526526
// annotations inside code blocks.
527527
// Match "@param <T>" specially in case the <T> is a <P> or other HTML tag we treat specially.
528-
private static final Pattern FOOTER_TAG_PATTERN = compile("^@(param\\s+<\\w+>|[a-z]\\w*)");
528+
private static final Pattern FOOTER_TAG_PATTERN = compile("@(param\\s+<\\w+>|[a-z]\\w*)");
529529
private static final Pattern MOE_BEGIN_STRIP_COMMENT_PATTERN =
530-
compile("^<!--\\s*M" + "OE:begin_intracomment_strip\\s*-->");
530+
compile("<!--\\s*M" + "OE:begin_intracomment_strip\\s*-->");
531531
private static final Pattern MOE_END_STRIP_COMMENT_PATTERN =
532-
compile("^<!--\\s*M" + "OE:end_intracomment_strip\\s*-->");
533-
private static final Pattern HTML_COMMENT_PATTERN = fullCommentPattern();
532+
compile("<!--\\s*M" + "OE:end_intracomment_strip\\s*-->");
533+
private static final Pattern HTML_COMMENT_PATTERN = compile("<!--.*?-->", DOTALL);
534534
private static final Pattern PRE_OPEN_PATTERN = openTagPattern("pre");
535535
private static final Pattern PRE_CLOSE_PATTERN = closeTagPattern("pre");
536536
private static final Pattern CODE_OPEN_PATTERN = openTagPattern("code");
@@ -548,8 +548,8 @@ private static boolean hasMultipleNewlines(String s) {
548548
private static final Pattern BLOCKQUOTE_OPEN_PATTERN = openTagPattern("blockquote");
549549
private static final Pattern BLOCKQUOTE_CLOSE_PATTERN = closeTagPattern("blockquote");
550550
private static final Pattern BR_PATTERN = openTagPattern("br");
551-
private static final Pattern SNIPPET_TAG_OPEN_PATTERN = compile("^[{]@snippet\\b");
552-
private static final Pattern INLINE_TAG_OPEN_PATTERN = compile("^[{]@\\w*");
551+
private static final Pattern SNIPPET_TAG_OPEN_PATTERN = compile("[{]@snippet\\b");
552+
private static final Pattern INLINE_TAG_OPEN_PATTERN = compile("[{]@\\w*");
553553
/*
554554
* We exclude < so that we don't swallow following HTML tags. This lets us fix up "foo<p>" (~400
555555
* hits in Google-internal code). We will join unnecessarily split "words" (like "foo<b>bar</b>")
@@ -560,18 +560,14 @@ private static boolean hasMultipleNewlines(String s) {
560560
* with matching only one character here. That would eliminate the need for the regex entirely.
561561
* That might be faster or slower than what we do now.
562562
*/
563-
private static final Pattern LITERAL_PATTERN = compile("^.[^ \t\n@<{}*]*", DOTALL);
564-
565-
private static Pattern fullCommentPattern() {
566-
return compile("^<!--.*?-->", DOTALL);
567-
}
563+
private static final Pattern LITERAL_PATTERN = compile(".[^ \t\n@<{}*]*", DOTALL);
568564

569565
private static Pattern openTagPattern(String namePattern) {
570-
return compile(format("^<(?:%s)\\b[^>]*>", namePattern), CASE_INSENSITIVE);
566+
return compile(format("<(?:%s)\\b[^>]*>", namePattern), CASE_INSENSITIVE);
571567
}
572568

573569
private static Pattern closeTagPattern(String namePattern) {
574-
return compile(format("^</(?:%s)\\b[^>]*>", namePattern), CASE_INSENSITIVE);
570+
return compile(format("</(?:%s)\\b[^>]*>", namePattern), CASE_INSENSITIVE);
575571
}
576572

577573
static class LexException extends Exception {}

0 commit comments

Comments
 (0)