Skip to content

Commit

Permalink
Eliminate unnecessary string splitting inside of InternetDomainName#f…
Browse files Browse the repository at this point in the history
…indSuffixOfType.

RELNOTES=n/a
PiperOrigin-RevId: 550877213
  • Loading branch information
java-team-github-bot authored and Google Java Core Libraries committed Jul 25, 2023
1 parent 230b0cd commit 3a1d18f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 32 deletions.
22 changes: 6 additions & 16 deletions android/guava/src/com/google/common/net/InternetDomainName.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ private int findSuffixOfType(Optional<PublicSuffixType> desiredType) {
for (int i = 0; i < partsSize; i++) {
String ancestorName = DOT_JOINER.join(parts.subList(i, partsSize));

if (i > 0
&& matchesType(
desiredType, Optional.fromNullable(PublicSuffixPatterns.UNDER.get(ancestorName)))) {
return i - 1;
}

if (matchesType(
desiredType, Optional.fromNullable(PublicSuffixPatterns.EXACT.get(ancestorName)))) {
return i;
Expand All @@ -178,10 +184,6 @@ private int findSuffixOfType(Optional<PublicSuffixType> desiredType) {
if (PublicSuffixPatterns.EXCLUDED.containsKey(ancestorName)) {
return i + 1;
}

if (matchesWildcardSuffixType(desiredType, ancestorName)) {
return i;
}
}

return NO_SUFFIX_FOUND;
Expand Down Expand Up @@ -591,18 +593,6 @@ public static boolean isValid(String name) {
}
}

/**
* Does the domain name match one of the "wildcard" patterns (e.g. {@code "*.ar"})? If a {@code
* desiredType} is specified, the wildcard pattern must also match that type.
*/
private static boolean matchesWildcardSuffixType(
Optional<PublicSuffixType> desiredType, String domain) {
List<String> pieces = DOT_SPLITTER.limit(2).splitToList(domain);
return pieces.size() == 2
&& matchesType(
desiredType, Optional.fromNullable(PublicSuffixPatterns.UNDER.get(pieces.get(1))));
}

/**
* If a {@code desiredType} is specified, returns true only if the {@code actualType} is
* identical. Otherwise, returns true as long as {@code actualType} is present.
Expand Down
22 changes: 6 additions & 16 deletions guava/src/com/google/common/net/InternetDomainName.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ private int findSuffixOfType(Optional<PublicSuffixType> desiredType) {
for (int i = 0; i < partsSize; i++) {
String ancestorName = DOT_JOINER.join(parts.subList(i, partsSize));

if (i > 0
&& matchesType(
desiredType, Optional.fromNullable(PublicSuffixPatterns.UNDER.get(ancestorName)))) {
return i - 1;
}

if (matchesType(
desiredType, Optional.fromNullable(PublicSuffixPatterns.EXACT.get(ancestorName)))) {
return i;
Expand All @@ -178,10 +184,6 @@ private int findSuffixOfType(Optional<PublicSuffixType> desiredType) {
if (PublicSuffixPatterns.EXCLUDED.containsKey(ancestorName)) {
return i + 1;
}

if (matchesWildcardSuffixType(desiredType, ancestorName)) {
return i;
}
}

return NO_SUFFIX_FOUND;
Expand Down Expand Up @@ -591,18 +593,6 @@ public static boolean isValid(String name) {
}
}

/**
* Does the domain name match one of the "wildcard" patterns (e.g. {@code "*.ar"})? If a {@code
* desiredType} is specified, the wildcard pattern must also match that type.
*/
private static boolean matchesWildcardSuffixType(
Optional<PublicSuffixType> desiredType, String domain) {
List<String> pieces = DOT_SPLITTER.limit(2).splitToList(domain);
return pieces.size() == 2
&& matchesType(
desiredType, Optional.fromNullable(PublicSuffixPatterns.UNDER.get(pieces.get(1))));
}

/**
* If a {@code desiredType} is specified, returns true only if the {@code actualType} is
* identical. Otherwise, returns true as long as {@code actualType} is present.
Expand Down

0 comments on commit 3a1d18f

Please sign in to comment.