From 42afd0c3d0c5c0c5552fe6708df3ffe17ed84592 Mon Sep 17 00:00:00 2001 From: gorhill Date: Wed, 3 May 2017 19:43:09 -0400 Subject: [PATCH] code review: to/from punycode conversion only if needed --- src/js/dynamic-net-filtering.js | 55 ++++++++++++++++++--------------- src/js/hnswitches.js | 44 +++++++++++++------------- 2 files changed, 51 insertions(+), 48 deletions(-) diff --git a/src/js/dynamic-net-filtering.js b/src/js/dynamic-net-filtering.js index 784b9751d..f6514af0e 100644 --- a/src/js/dynamic-net-filtering.js +++ b/src/js/dynamic-net-filtering.js @@ -1,7 +1,7 @@ /******************************************************************************* uBlock Origin - a browser extension to block requests. - Copyright (C) 2014-2016 Raymond Hill + Copyright (C) 2014-2017 Raymond Hill This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -444,9 +444,10 @@ Matrix.prototype.desHostnameFromRule = function(rule) { /******************************************************************************/ Matrix.prototype.toString = function() { - var out = []; - var rule, type, val; - var srcHostname, desHostname; + var out = [], + rule, type, val, + srcHostname, desHostname, + toUnicode = punycode.toUnicode; for ( rule in this.rules ) { if ( this.rules.hasOwnProperty(rule) === false ) { continue; @@ -458,12 +459,16 @@ Matrix.prototype.toString = function() { continue; } val = this.evaluateCell(srcHostname, desHostname, type); - if ( val === 0 ) { - continue; + if ( val === 0 ) { continue; } + if ( srcHostname.indexOf('xn--') !== -1 ) { + srcHostname = toUnicode(srcHostname); + } + if ( desHostname.indexOf('xn--') !== -1 ) { + desHostname = toUnicode(desHostname); } out.push( - punycode.toUnicode(srcHostname) + ' ' + - punycode.toUnicode(desHostname) + ' ' + + srcHostname + ' ' + + desHostname + ' ' + type + ' ' + actionToNameMap[val] ); @@ -475,26 +480,18 @@ Matrix.prototype.toString = function() { /******************************************************************************/ Matrix.prototype.fromString = function(text, append) { - var textEnd = text.length; - var lineBeg = 0, lineEnd; - var line, pos, fields; - var srcHostname, desHostname, type, action; + var lineIter = new µBlock.LineIterator(text), + line, pos, fields, + srcHostname, desHostname, type, action, + reNotASCII = /[^\x20-\x7F]/, + toASCII = punycode.toASCII; if ( append !== true ) { this.reset(); } - while ( lineBeg < textEnd ) { - lineEnd = text.indexOf('\n', lineBeg); - if ( lineEnd < 0 ) { - lineEnd = text.indexOf('\r', lineBeg); - if ( lineEnd < 0 ) { - lineEnd = textEnd; - } - } - line = text.slice(lineBeg, lineEnd).trim(); - lineBeg = lineEnd + 1; - + while ( lineIter.eot() === false ) { + line = lineIter.next().trim(); pos = line.indexOf('# '); if ( pos !== -1 ) { line = line.slice(0, pos).trim(); @@ -527,8 +524,16 @@ Matrix.prototype.fromString = function(text, append) { continue; } - srcHostname = punycode.toASCII(fields[0]); - desHostname = punycode.toASCII(fields[1]); + // Performance: avoid punycoding if hostnames are made only of + // ASCII characters. + srcHostname = fields[0]; + if ( reNotASCII.test(srcHostname) ) { + srcHostname = toASCII(srcHostname); + } + desHostname = fields[1]; + if ( reNotASCII.test(desHostname) ) { + desHostname = toASCII(desHostname); + } // https://github.com/chrisaljoudi/uBlock/issues/1082 // Discard rules with invalid hostnames diff --git a/src/js/hnswitches.js b/src/js/hnswitches.js index b929b3f1a..b55f8628c 100644 --- a/src/js/hnswitches.js +++ b/src/js/hnswitches.js @@ -1,7 +1,7 @@ /******************************************************************************* uBlock Origin - a Chromium browser extension to black/white list requests. - Copyright (C) 2015-2016 Raymond Hill + Copyright (C) 2015-2017 Raymond Hill This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -258,9 +258,10 @@ HnSwitches.prototype.toResultString = function() { /******************************************************************************/ HnSwitches.prototype.toString = function() { - var out = []; - var switchName, val; - var hostname; + var out = [], + switchName, val, + hostname, + toUnicode = punycode.toUnicode; for ( hostname in this.switches ) { if ( this.switches.hasOwnProperty(hostname) === false ) { continue; @@ -270,8 +271,9 @@ HnSwitches.prototype.toString = function() { continue; } val = this.evaluate(switchName, hostname); - if ( val === 0 ) { - continue; + if ( val === 0 ) { continue; } + if ( hostname.indexOf('xn--') !== -1 ) { + hostname = toUnicode(hostname); } out.push(switchName + ': ' + hostname + ' ' + switchStateToNameMap[val]); } @@ -282,25 +284,16 @@ HnSwitches.prototype.toString = function() { /******************************************************************************/ HnSwitches.prototype.fromString = function(text) { - var textEnd = text.length; - var lineBeg = 0, lineEnd; - var line, pos; - var fields; - var switchName, hostname, state; + var lineIter = new µBlock.LineIterator(text), + line, pos, fields, + switchName, hostname, state, + reNotASCII = /[^\x20-\x7F]/, + toASCII = punycode.toASCII; this.reset(); - while ( lineBeg < textEnd ) { - lineEnd = text.indexOf('\n', lineBeg); - if ( lineEnd < 0 ) { - lineEnd = text.indexOf('\r', lineBeg); - if ( lineEnd < 0 ) { - lineEnd = textEnd; - } - } - line = text.slice(lineBeg, lineEnd).trim(); - lineBeg = lineEnd + 1; - + while ( lineIter.eot() === false ) { + line = lineIter.next().trim(); pos = line.indexOf('# '); if ( pos !== -1 ) { line = line.slice(0, pos).trim(); @@ -325,7 +318,12 @@ HnSwitches.prototype.fromString = function(text) { continue; } - hostname = punycode.toASCII(fields[1]); + // Performance: avoid punycoding if hostname is made only of + // ASCII characters. + hostname = fields[1]; + if ( reNotASCII.test(hostname) ) { + hostname = toASCII(hostname); + } state = fields[2]; if ( nameToSwitchStateMap.hasOwnProperty(state) === false ) {