Skip to content

Commit

Permalink
fix: Fix string splitting bug and optimize performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsbasjes committed Aug 1, 2023
1 parent 59e62d6 commit a7d19e6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
import java.util.regex.Pattern;

import static java.lang.Boolean.TRUE;
import static nl.basjes.parse.useragent.UserAgent.AGENT_CLASS;
Expand Down Expand Up @@ -383,6 +384,8 @@ private boolean newVersionIsBetter(MutableAgentField currentVersion, String vers
(versionHasMinor);
}

private static final Pattern DOT_SPLITTER = Pattern.compile("\\.");

public void improveLayoutEngineAndAgentInfo(MutableUserAgent userAgent, ClientHints clientHints) {
// Improve the Agent info.
List<Brand> versionList = clientHints.getFullVersionList();
Expand All @@ -401,7 +404,7 @@ public void improveLayoutEngineAndAgentInfo(MutableUserAgent userAgent, ClientHi
Brand chromium = versionMap.get("Chromium");
if (chromium != null) {
String version = chromium.getVersion();
String[] versionSplits = version.split("\\.");
String[] versionSplits = DOT_SPLITTER.split(version);
String majorVersion = versionSplits[0];

// Work around the major in minor hack/feature of Chrome ~v99
Expand Down Expand Up @@ -465,7 +468,7 @@ public void improveLayoutEngineAndAgentInfo(MutableUserAgent userAgent, ClientHi
// So we have "Chrome" and nothing else
MutableAgentField currentVersion = userAgent.get(AGENT_VERSION);
String version = chrome.getVersion();
String[] versionSplits = version.split("\\.");
String[] versionSplits = DOT_SPLITTER.split(version);
String majorVersion = versionSplits[0];

// Work around the major in minor hack/feature of Chrome ~v99
Expand Down Expand Up @@ -500,7 +503,7 @@ public void improveLayoutEngineAndAgentInfo(MutableUserAgent userAgent, ClientHi
if ("Opera".equals(currentAgent.getValue())) {
String currentVersion = userAgent.get(AGENT_VERSION).getValue();
// We only consider the Client Hints if the version new enough.
String currentMajorVersion = currentVersion.substring(0, currentVersion.indexOf('.'));
String currentMajorVersion = DOT_SPLITTER.split(currentVersion, 2)[0];
int currentMajorVersionInt = Integer.parseInt(currentMajorVersion);
if (currentMajorVersionInt < 98) {
versionMap.remove("Opera");
Expand Down Expand Up @@ -556,11 +559,11 @@ public void improveLayoutEngineAndAgentInfo(MutableUserAgent userAgent, ClientHi
// We only update the version if we have a better version number.
// We always update the AgentName because I think the Client hints are always "better"...
String newVersion = brand.getVersion();
String newMajorVersion = newVersion.split("\\.")[0];
String newMajorVersion = DOT_SPLITTER.split(newVersion, 2)[0];

// The values we are going to set at the end.
String setVersion = versionFieldValue;
String setMajorVersion = versionFieldValue.split("\\.")[0];
String setMajorVersion = DOT_SPLITTER.split(versionFieldValue, 2)[0];

// If the original is a major version only then the new one is better
if (versionIsMajorVersionOnly) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.regex.Pattern;

import static nl.basjes.parse.useragent.utils.publicsuffixlist.DomainType.ICANN;

Expand Down Expand Up @@ -164,6 +165,8 @@ private static String extractCompanyFromSoftwareRepositoryUrl(String url) {
return null;
}

private static final Pattern DOT_SPLITTER = Pattern.compile("\\.");

private static String extractCompanyFromHostName(String hostname, Set<String> blackList) {
if (hostname == null) {
return null;
Expand All @@ -177,7 +180,7 @@ private static String extractCompanyFromHostName(String hostname, Set<String> bl
if (root == null) {
return null;
}
return Normalize.brand(root.split("\\.", 2)[0]);
return Normalize.brand(DOT_SPLITTER.split(root, 2)[0]);
}

}

0 comments on commit a7d19e6

Please sign in to comment.