1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-09-18 17:02:27 +02:00

Minor code review

This commit is contained in:
Raymond Hill 2023-02-01 10:22:13 -05:00
parent 7881e1ace3
commit 6eec497ae8
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -111,15 +111,10 @@ const StaticExtFilteringHostnameDB = class {
retrieve(hostname, out, modifiers = 0) { retrieve(hostname, out, modifiers = 0) {
let hn = hostname; let hn = hostname;
if ( modifiers === 2 ) { hn = ''; } if ( modifiers === 2 ) { hn = ''; }
const mask = out.length - 1; // out.length must be power of two
for (;;) { for (;;) {
let iHn = this.hostnameToSlotIdMap.get(hn); const hnSlot = this.hostnameToSlotIdMap.get(hn);
if ( iHn !== undefined ) { if ( hnSlot !== undefined ) {
do { this.retrieveFromSlot(hnSlot, out);
const strId = this.hostnameSlots[iHn+0];
out[strId & mask].add(this.strSlots[strId >>> this.nBits]);
iHn = this.hostnameSlots[iHn+1];
} while ( iHn !== 0 );
} }
if ( hn === '' ) { break; } if ( hn === '' ) { break; }
const pos = hn.indexOf('.'); const pos = hn.indexOf('.');
@ -131,6 +126,7 @@ const StaticExtFilteringHostnameDB = class {
} }
} }
if ( modifiers !== 0 && modifiers !== 3 ) { return; } if ( modifiers !== 0 && modifiers !== 3 ) { return; }
if ( this.regexToSlotIdMap.size === 0 ) { return; }
// TODO: consider using a combined regex to test once for whether // TODO: consider using a combined regex to test once for whether
// iterating is worth it. // iterating is worth it.
for ( const restr of this.regexToSlotIdMap.keys() ) { for ( const restr of this.regexToSlotIdMap.keys() ) {
@ -139,15 +135,20 @@ const StaticExtFilteringHostnameDB = class {
this.regexMap.set(restr, (re = new RegExp(restr.slice(1,-1)))); this.regexMap.set(restr, (re = new RegExp(restr.slice(1,-1))));
} }
if ( re.test(hostname) === false ) { continue; } if ( re.test(hostname) === false ) { continue; }
let iHn = this.regexToSlotIdMap.get(restr); this.retrieveFromSlot(this.regexToSlotIdMap.get(restr), out);
do {
const strId = this.hostnameSlots[iHn+0];
out[strId & mask].add(this.strSlots[strId >>> this.nBits]);
iHn = this.hostnameSlots[iHn+1];
} while ( iHn !== 0 );
} }
} }
retrieveFromSlot(hnSlot, out) {
if ( hnSlot === undefined ) { return; }
const mask = out.length - 1; // out.length must be power of two
do {
const strId = this.hostnameSlots[hnSlot+0];
out[strId & mask].add(this.strSlots[strId >>> this.nBits]);
hnSlot = this.hostnameSlots[hnSlot+1];
} while ( hnSlot !== 0 );
}
toSelfie() { toSelfie() {
return { return {
hostnameToSlotIdMap: Array.from(this.hostnameToSlotIdMap), hostnameToSlotIdMap: Array.from(this.hostnameToSlotIdMap),