Skip to content

Commit 3a1d18f

Browse files
java-team-github-botGoogle Java Core Libraries
authored and
Google Java Core Libraries
committedJul 25, 2023
Eliminate unnecessary string splitting inside of InternetDomainName#findSuffixOfType.
RELNOTES=n/a PiperOrigin-RevId: 550877213
1 parent 230b0cd commit 3a1d18f

File tree

2 files changed

+12
-32
lines changed

2 files changed

+12
-32
lines changed
 

‎android/guava/src/com/google/common/net/InternetDomainName.java

+6-16
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ private int findSuffixOfType(Optional<PublicSuffixType> desiredType) {
167167
for (int i = 0; i < partsSize; i++) {
168168
String ancestorName = DOT_JOINER.join(parts.subList(i, partsSize));
169169

170+
if (i > 0
171+
&& matchesType(
172+
desiredType, Optional.fromNullable(PublicSuffixPatterns.UNDER.get(ancestorName)))) {
173+
return i - 1;
174+
}
175+
170176
if (matchesType(
171177
desiredType, Optional.fromNullable(PublicSuffixPatterns.EXACT.get(ancestorName)))) {
172178
return i;
@@ -178,10 +184,6 @@ private int findSuffixOfType(Optional<PublicSuffixType> desiredType) {
178184
if (PublicSuffixPatterns.EXCLUDED.containsKey(ancestorName)) {
179185
return i + 1;
180186
}
181-
182-
if (matchesWildcardSuffixType(desiredType, ancestorName)) {
183-
return i;
184-
}
185187
}
186188

187189
return NO_SUFFIX_FOUND;
@@ -591,18 +593,6 @@ public static boolean isValid(String name) {
591593
}
592594
}
593595

594-
/**
595-
* Does the domain name match one of the "wildcard" patterns (e.g. {@code "*.ar"})? If a {@code
596-
* desiredType} is specified, the wildcard pattern must also match that type.
597-
*/
598-
private static boolean matchesWildcardSuffixType(
599-
Optional<PublicSuffixType> desiredType, String domain) {
600-
List<String> pieces = DOT_SPLITTER.limit(2).splitToList(domain);
601-
return pieces.size() == 2
602-
&& matchesType(
603-
desiredType, Optional.fromNullable(PublicSuffixPatterns.UNDER.get(pieces.get(1))));
604-
}
605-
606596
/**
607597
* If a {@code desiredType} is specified, returns true only if the {@code actualType} is
608598
* identical. Otherwise, returns true as long as {@code actualType} is present.

‎guava/src/com/google/common/net/InternetDomainName.java

+6-16
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ private int findSuffixOfType(Optional<PublicSuffixType> desiredType) {
167167
for (int i = 0; i < partsSize; i++) {
168168
String ancestorName = DOT_JOINER.join(parts.subList(i, partsSize));
169169

170+
if (i > 0
171+
&& matchesType(
172+
desiredType, Optional.fromNullable(PublicSuffixPatterns.UNDER.get(ancestorName)))) {
173+
return i - 1;
174+
}
175+
170176
if (matchesType(
171177
desiredType, Optional.fromNullable(PublicSuffixPatterns.EXACT.get(ancestorName)))) {
172178
return i;
@@ -178,10 +184,6 @@ private int findSuffixOfType(Optional<PublicSuffixType> desiredType) {
178184
if (PublicSuffixPatterns.EXCLUDED.containsKey(ancestorName)) {
179185
return i + 1;
180186
}
181-
182-
if (matchesWildcardSuffixType(desiredType, ancestorName)) {
183-
return i;
184-
}
185187
}
186188

187189
return NO_SUFFIX_FOUND;
@@ -591,18 +593,6 @@ public static boolean isValid(String name) {
591593
}
592594
}
593595

594-
/**
595-
* Does the domain name match one of the "wildcard" patterns (e.g. {@code "*.ar"})? If a {@code
596-
* desiredType} is specified, the wildcard pattern must also match that type.
597-
*/
598-
private static boolean matchesWildcardSuffixType(
599-
Optional<PublicSuffixType> desiredType, String domain) {
600-
List<String> pieces = DOT_SPLITTER.limit(2).splitToList(domain);
601-
return pieces.size() == 2
602-
&& matchesType(
603-
desiredType, Optional.fromNullable(PublicSuffixPatterns.UNDER.get(pieces.get(1))));
604-
}
605-
606596
/**
607597
* If a {@code desiredType} is specified, returns true only if the {@code actualType} is
608598
* identical. Otherwise, returns true as long as {@code actualType} is present.

0 commit comments

Comments
 (0)