Permalink
Browse files

a little faster, support empty expressions

  • Loading branch information...
1 parent a36db41 commit 879ebeef18d1476e766cd2e0cd558c5edcc21275 @elmerbulthuis committed Dec 15, 2011
Showing with 54 additions and 41 deletions.
  1. +53 −40 index.js
  2. +1 −1 package.json
View
93 index.js
@@ -152,48 +152,59 @@ module.exports = function(tokenCallback, expressionSet, options) {
if there is no cached token
*/
if(!token) {
- var type = typeof expression;
- switch(type) {
- /*
- if expression is a string we try
- to find for that string.
- */
- case 'string':
- var index = buffer.indexOf(expression);
+ if(expression) {
+ var type = typeof expression;
+ switch(type) {
+ /*
+ if expression is a string we try
+ to find for that string.
+ */
+ case 'string':
+ var index = buffer.indexOf(expression);
+ /*
+ if we found the string (remember, ~-1 == 0) then
+ create a token object
+ */
+ if(~index) {
+ token = extend([
+ expression
+ ], {
+ index: index
+ , category: category
+ });
+ }
+ break;
+
+ /*
+ if it's a function, assume it to be a RegExp. Execute it
+ and make it a token
+ */
+ case 'function':
+ var match = expression.exec(buffer);
+ if(match) {
+ token = extend(match, {
+ category: category
+ });
+ }
+ break;
+
+ default:
+ throw "expression '" + category + "' is '" + type + "', expecting 'string' or a RegExp ('function').";
+ }
/*
- if we found the string (remember, ~-1 == 0) then
- create a token object
+ if there is a match, cache it!
*/
- if(~index) {
- token = extend([
- expression
- ], {
- index: index
- , category: category
- });
- }
- break;
-
- /*
- if it's a function, assume it to be a RegExp. Execute it
- and make it a token
- */
- case 'function':
- var match = expression.exec(buffer);
- if(match) {
- token = extend(match, {
- category: category
- });
- }
- break;
-
- default:
- throw "expression '" + category + "' is '" + type + "', expecting 'string' or a RegExp ('function').";
- }
- /*
- if there is a match, cache it!
- */
- if(token) tokenSet[category] = token;
+ if(token) tokenSet[category] = token;
+ }
+ else {
+ /*
+ when there is no exrpression, there is always a match!
+ */
+ token = extend([], {
+ index: 0
+ , category: category
+ });
+ }
}
/*
@@ -203,6 +214,8 @@ module.exports = function(tokenCallback, expressionSet, options) {
if (token && (!foundToken || token.index < foundToken.index)) {
foundToken = token;
}
+
+ if(foundToken && foundToken.index <= 0) return false;
});
/*
if there is no token found, this will return null
View
2 package.json
@@ -1,7 +1,7 @@
{
"name": "2kenizer"
, "description": "efficient tokenizer"
- , "version": "0.0.3"
+ , "version": "0.0.4"
, "author": "Elmer Bulthuis <elmerbulthuis@gmail.com>"
, "repositories": [{
"type": "git",

0 comments on commit 879ebee

Please sign in to comment.