Skip to content
This repository was archived by the owner on Nov 15, 2017. It is now read-only.

Commit a6d06f7

Browse files
committed
more simplifications
1 parent 2020159 commit a6d06f7

File tree

2 files changed

+29
-34
lines changed

2 files changed

+29
-34
lines changed

js/abp-filters.js

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,16 @@ var reToken = /[%0-9A-Za-z]{2,}/g;
7979

8080
/******************************************************************************/
8181

82-
var FilterEntry = function(token) {
83-
this.token = token;
84-
this.prefix = '';
85-
this.suffix = '';
82+
var FilterEntry = function(s, tokenBeg, tokenLen) {
83+
this.s = s;
84+
this.tokenBeg = tokenBeg;
85+
this.tokenLen = tokenLen;
8686
};
8787

88-
FilterEntry.prototype.matchString = function(s, tokenBeg, tokenEnd) {
89-
if ( s.indexOf(this.suffix, tokenEnd) !== tokenEnd ) {
90-
return false;
91-
}
92-
tokenBeg -= this.prefix.length;
93-
return s.indexOf(this.prefix, tokenBeg) === tokenBeg;
88+
FilterEntry.prototype.matchString = function(s, tokenBeg) {
89+
// rhill 2014-03-05: Benchmarking shows that's the fastest way to do this.
90+
var filterBeg = tokenBeg - this.tokenBeg;
91+
return s.indexOf(this.s, filterBeg) === filterBeg;
9492
};
9593

9694
/******************************************************************************/
@@ -167,6 +165,18 @@ var add = function(s) {
167165
// Remove pipes
168166
s = s.replace(/^\|\|/, '');
169167

168+
// Remove leading and trailing wildcards
169+
var pos = 0;
170+
while ( s.charAt(pos) === '*' ) {
171+
pos += 1;
172+
}
173+
s = s.slice(pos);
174+
pos = s.length;
175+
while ( s.charAt(pos-1) === '*' ) {
176+
pos -= 1;
177+
}
178+
s = s.slice(0, pos);
179+
170180
// Already in dictionary?
171181
var filter = filterDict[s];
172182
if ( filter !== undefined ) {
@@ -175,33 +185,18 @@ var add = function(s) {
175185

176186
// Index based on 1st good token
177187
var matches = findGoodToken(s);
178-
if ( !matches ) {
188+
if ( !matches || !matches[0].length ) {
179189
return false;
180190
}
181191
var token = matches[0];
192+
var tokenBeg = matches.index;
193+
var tokenEnd = reToken.lastIndex;
182194

183-
filter = new FilterEntry(token);
195+
filter = new FilterEntry(s, tokenBeg, token.length);
184196
filterDict[s] = filter;
185197

186-
var prefix = s.slice(0, matches.index);
187-
// Eliminate leading wildcards
188-
var pos = 0;
189-
while ( prefix.charAt(pos) === '*' ) {
190-
pos += 1;
191-
}
192-
prefix = prefix.slice(pos);
193-
filter.prefix = prefix;
194-
var prefixKey = prefix.length > 0 ? prefix.charAt(prefix.length-1) : '0';
195-
196-
var suffix = s.slice(reToken.lastIndex);
197-
// Eliminate trailing wildcards
198-
pos = suffix.length;
199-
while ( suffix.charAt(pos-1) === '*' ) {
200-
pos -= 1;
201-
}
202-
suffix = suffix.slice(0, pos);
203-
filter.suffix = suffix;
204-
var suffixKey = suffix.length > 0 ? suffix.charAt(0) : '0';
198+
var prefixKey = tokenBeg > 0 ? s.charAt(tokenBeg-1) : '0';
199+
var suffixKey = tokenEnd < s.length ? s.charAt(tokenEnd) : '0';
205200

206201
var fidx = filterIndex;
207202
var tokenKey = prefixKey + token + suffixKey;
@@ -229,7 +224,7 @@ var freeze = function() {
229224
var matchFromFilterArray = function(s, tokenBeg, tokenEnd, filters) {
230225
var i = filters.length;
231226
while ( i-- ) {
232-
if ( filters[i].matchString(s, tokenBeg, tokenEnd) ) {
227+
if ( filters[i].matchString(s, tokenBeg) ) {
233228
return true;
234229
}
235230
}
@@ -243,7 +238,7 @@ var matchFromSomething = function(s, tokenBeg, tokenEnd, something) {
243238
return false;
244239
}
245240
if ( something instanceof FilterEntry ) {
246-
return something.matchString(s, tokenBeg, tokenEnd);
241+
return something.matchString(s, tokenBeg);
247242
}
248243
return matchFromFilterArray(s, tokenBeg, tokenEnd, something);
249244
};

js/profiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Profiler.prototype.stop = function(s) {
4242
this.count += 1;
4343
if ( this.count > 0 ) {
4444
var now = Date.now();
45-
this.time += now - this._start;
45+
this.time += (now - this._start);
4646
if ( (now - this._lastlog) > 10000 ) {
4747
console.log('HTTP Switchboard Profiler() > %s: %f ms per iteration', s, this.avg());
4848
this._lastlog = now;

0 commit comments

Comments
 (0)