Skip to content

Commit

Permalink
use slice instead of substring
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Apr 10, 2015
1 parent 392e543 commit 00e088c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
32 changes: 16 additions & 16 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,29 @@ var Router = (function () {
var j = i + 1;
count++;

this.insert(method, path.substring(0, i), null, PNODE);
this.insert(method, path.slice(0, i), null, PNODE);
// 47,`/`
for (; i < l && path.charCodeAt(i) !== 47; ++i) {}

// new param key `$n`
var param = '$' + count;
var prefix = path.substring(0, j) + param;
var prefix = path.slice(0, j) + param;
// store original param key
keys.push(path.substring(j, i));
keys.push(path.slice(j, i));
// override path
path = prefix + path.substring(i);
path = prefix + path.slice(i);
// override i, l
i = prefix.length;
l = path.length;

if (i === l) {
this.insert(method, path.substring(0, i), handler, 0);
this.insert(method, path.slice(0, i), handler, 0);
return;
}
this.insert(method, path.substring(0, i), null, 0);
this.insert(method, path.slice(0, i), null, 0);
} else if (ch === 42 /*'*'*/) {
this.insert(method, path.substring(0, i), null, CNODE);
this.insert(method, path.substring(0, l), handler, 0);
this.insert(method, path.slice(0, i), null, CNODE);
this.insert(method, path.slice(0, l), handler, 0);
}
}
this.insert(method, path, handler, SNODE, true);
Expand Down Expand Up @@ -122,12 +122,12 @@ var Router = (function () {
}
} else if (l < pl) {
// Split node
var n = new Node(cn.prefix.substring(l), cn.has, cn.handler, cn.edges);
var n = new Node(cn.prefix.slice(l), cn.has, cn.handler, cn.edges);
cn.edges = [n]; // Add to parent

// Reset parent node
cn.label = cn.prefix.charCodeAt(0);
cn.prefix = cn.prefix.substring(0, l);
cn.prefix = cn.prefix.slice(0, l);
cn.has = 0;
cn.handler = null;

Expand All @@ -136,12 +136,12 @@ var Router = (function () {
cn.handler = handler;
} else {
// Need to fork a node
var _n = new Node(search.substring(l), has, handler);
var _n = new Node(search.slice(l), has, handler);
// cn.edges.push(n);
cn.edges[bool ? 'unshift' : 'push'](_n);
}
} else if (l < sl) {
search = search.substring(l);
search = search.slice(l);
var e = cn.findEdge(search.charCodeAt(0));
if (e) {
// Go deeper
Expand Down Expand Up @@ -183,7 +183,7 @@ var Router = (function () {
var pl = cn.prefix.length;
var l = lcp(search, cn.prefix);
if (l === pl) {
search = search.substring(l);
search = search.slice(l);
}

var i = 0,
Expand All @@ -198,11 +198,11 @@ var Router = (function () {
for (; j < l && search.charCodeAt(j) !== 47 /*'/'*/; ++j) {}

params[n] = {
name: e.prefix.substring(1),
value: search.substring(0, j)
name: e.prefix.slice(1),
value: search.slice(0, j)
};
n++;
search = search.substring(j);
search = search.slice(j);
} else if (has === CNODE) {
params[n] = {
name: '_name',
Expand Down
32 changes: 16 additions & 16 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,29 @@ class Router {
let j = i + 1;
count++;

this.insert(method, path.substring(0, i), null, PNODE);
this.insert(method, path.slice(0, i), null, PNODE);
// 47,`/`
for (; i < l && (path.charCodeAt(i) !== 0x2F); ++i) {}

// new param key `$n`
let param = '$' + count;
let prefix = path.substring(0, j) + param;
let prefix = path.slice(0, j) + param;
// store original param key
keys.push(path.substring(j, i));
keys.push(path.slice(j, i));
// override path
path = prefix + path.substring(i);
path = prefix + path.slice(i);
// override i, l
i = prefix.length;
l = path.length;

if (i === l) {
this.insert(method, path.substring(0, i), handler, 0);
this.insert(method, path.slice(0, i), handler, 0);
return;
}
this.insert(method, path.substring(0, i), null, 0);
this.insert(method, path.slice(0, i), null, 0);
} else if (ch === 0x2A /*'*'*/ ) {
this.insert(method, path.substring(0, i), null, CNODE);
this.insert(method, path.substring(0, l), handler, 0);
this.insert(method, path.slice(0, i), null, CNODE);
this.insert(method, path.slice(0, l), handler, 0);
}
}
this.insert(method, path, handler, SNODE, true);
Expand All @@ -114,12 +114,12 @@ class Router {
}
} else if (l < pl) {
// Split node
let n = new Node(cn.prefix.substring(l), cn.has, cn.handler, cn.edges);
let n = new Node(cn.prefix.slice(l), cn.has, cn.handler, cn.edges);
cn.edges = [n]; // Add to parent

// Reset parent node
cn.label = cn.prefix.charCodeAt(0);
cn.prefix = cn.prefix.substring(0, l);
cn.prefix = cn.prefix.slice(0, l);
cn.has = 0;
cn.handler = null;

Expand All @@ -128,12 +128,12 @@ class Router {
cn.handler = handler;
} else {
// Need to fork a node
let n = new Node(search.substring(l), has, handler);
let n = new Node(search.slice(l), has, handler);
// cn.edges.push(n);
cn.edges[bool ? 'unshift' : 'push'](n);
}
} else if (l < sl) {
search = search.substring(l);
search = search.slice(l);
let e = cn.findEdge(search.charCodeAt(0));
if (e) {
// Go deeper
Expand Down Expand Up @@ -172,7 +172,7 @@ class Router {
let pl = cn.prefix.length;
let l = lcp(search, cn.prefix);
if (l === pl) {
search = search.substring(l);
search = search.slice(l);
}

let i = 0, k = cn.edges.length, e;
Expand All @@ -185,11 +185,11 @@ class Router {
for (; j < l && (search.charCodeAt(j) !== 0x2F /*'/'*/ ); ++j) {}

params[n] = {
name: e.prefix.substring(1),
value: search.substring(0, j)
name: e.prefix.slice(1),
value: search.slice(0, j)
};
n++;
search = search.substring(j);
search = search.slice(j);
} else if (has === CNODE) {
params[n] = {
name: '_name',
Expand Down

0 comments on commit 00e088c

Please sign in to comment.