From 1490fd606bc8ff7d4b9a607f6374081410e7b4b8 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Fri, 10 Dec 2021 07:47:21 -0500 Subject: [PATCH] Simplify often-executed regex --- src/js/uri-utils.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/js/uri-utils.js b/src/js/uri-utils.js index c1a7b0232..a7dc90644 100644 --- a/src/js/uri-utils.js +++ b/src/js/uri-utils.js @@ -31,8 +31,8 @@ import punycode from '../lib/punycode.js'; // Originally: // https://github.com/gorhill/uBlock/blob/8b5733a58d3acf9fb62815e14699c986bd1c2fdc/src/js/uritools.js -const reCommonHostnameFromURL = - /^https?:\/\/([0-9a-z_][0-9a-z._-]*[0-9a-z])\//; +const reHostnameFromCommonURL = + /^https:\/\/[0-9a-z._-]+[0-9a-z]\//; const reAuthorityFromURI = /^(?:[^:\/?#]+:)?(\/\/[^\/?#]+)/; const reHostFromNakedAuthority = @@ -75,20 +75,20 @@ function entityFromDomain(domain) { } function hostnameFromURI(uri) { - let matches = reCommonHostnameFromURL.exec(uri); - if ( matches !== null ) { return matches[1]; } - matches = reAuthorityFromURI.exec(uri); - if ( matches === null ) { return ''; } - const authority = matches[1].slice(2); + let match = reHostnameFromCommonURL.exec(uri); + if ( match !== null ) { return match[0].slice(8, -1); } + match = reAuthorityFromURI.exec(uri); + if ( match === null ) { return ''; } + const authority = match[1].slice(2); if ( reHostFromNakedAuthority.test(authority) ) { return authority.toLowerCase(); } - matches = reHostFromAuthority.exec(authority); - if ( matches === null ) { - matches = reIPv6FromAuthority.exec(authority); - if ( matches === null ) { return ''; } + match = reHostFromAuthority.exec(authority); + if ( match === null ) { + match = reIPv6FromAuthority.exec(authority); + if ( match === null ) { return ''; } } - let hostname = matches[1]; + let hostname = match[1]; while ( hostname.endsWith('.') ) { hostname = hostname.slice(0, -1); } @@ -104,8 +104,10 @@ function hostnameFromNetworkURL(url) { } function originFromURI(uri) { - const matches = reOriginFromURI.exec(uri); - return matches !== null ? matches[0].toLowerCase() : ''; + let match = reHostnameFromCommonURL.exec(uri); + if ( match !== null ) { return match[0].slice(0, -1); } + match = reOriginFromURI.exec(uri); + return match !== null ? match[0].toLowerCase() : ''; } function isNetworkURI(uri) {