From 3a1d18fbefa10218988a0fbbb6e1fada012397bf Mon Sep 17 00:00:00 2001 From: Google Java Core Libraries Date: Tue, 25 Jul 2023 07:13:10 -0700 Subject: [PATCH] Eliminate unnecessary string splitting inside of InternetDomainName#findSuffixOfType. RELNOTES=n/a PiperOrigin-RevId: 550877213 --- .../google/common/net/InternetDomainName.java | 22 +++++-------------- .../google/common/net/InternetDomainName.java | 22 +++++-------------- 2 files changed, 12 insertions(+), 32 deletions(-) diff --git a/android/guava/src/com/google/common/net/InternetDomainName.java b/android/guava/src/com/google/common/net/InternetDomainName.java index d71a9d5c3c6c..4f85fb67c124 100644 --- a/android/guava/src/com/google/common/net/InternetDomainName.java +++ b/android/guava/src/com/google/common/net/InternetDomainName.java @@ -167,6 +167,12 @@ private int findSuffixOfType(Optional 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; @@ -178,10 +184,6 @@ private int findSuffixOfType(Optional desiredType) { if (PublicSuffixPatterns.EXCLUDED.containsKey(ancestorName)) { return i + 1; } - - if (matchesWildcardSuffixType(desiredType, ancestorName)) { - return i; - } } return NO_SUFFIX_FOUND; @@ -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 desiredType, String domain) { - List 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. diff --git a/guava/src/com/google/common/net/InternetDomainName.java b/guava/src/com/google/common/net/InternetDomainName.java index d71a9d5c3c6c..4f85fb67c124 100644 --- a/guava/src/com/google/common/net/InternetDomainName.java +++ b/guava/src/com/google/common/net/InternetDomainName.java @@ -167,6 +167,12 @@ private int findSuffixOfType(Optional 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; @@ -178,10 +184,6 @@ private int findSuffixOfType(Optional desiredType) { if (PublicSuffixPatterns.EXCLUDED.containsKey(ancestorName)) { return i + 1; } - - if (matchesWildcardSuffixType(desiredType, ancestorName)) { - return i; - } } return NO_SUFFIX_FOUND; @@ -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 desiredType, String domain) { - List 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.