Created
November 10, 2010 23:59
-
-
Save bga/671752 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($G) | |
{ | |
var OldRegExp = $G.RegExp; | |
var _createHashTable = | |
Object.create && function() | |
{ | |
return Object.create(null); | |
} || | |
{}.__proto__ && function() | |
{ | |
var a = {__proto__: 1}; // SM workaround | |
delete(a.__proto__); | |
return a; | |
} || | |
(function() | |
{ | |
var dontEnumKeys = (function() | |
{ | |
var v = {}; | |
// http://stackoverflow.com/questions/85992/how-do-i-enumerate-the-properties-of-a-javascript-object | |
var shadowedKeys = | |
[ | |
//'constructor', | |
'toString', | |
'valueOf', | |
'toLocaleString', | |
//'prototype', | |
'isPrototypeOf', | |
'propertyIsEnumerable', | |
'hasOwnProperty', | |
'length', | |
'unique' | |
]; | |
var i, j; | |
var _indexOf = function(v) | |
{ | |
var i = this.length; | |
while(i-- && this[i] !== v) | |
; | |
return i; | |
}; | |
i = shadowedKeys.length; while(i--) | |
v[shadowedKeys[i]] = 1; | |
for(i in v) | |
{ | |
if((j = _indexOf.call(shadowedKeys, i)) > -1) | |
shadowedKeys.splice(j, 1); | |
} | |
return shadowedKeys; | |
})(); | |
var HashTable = function() | |
{ | |
}; | |
var HashTableProto = HashTable.prototype; | |
for(var i in HashTableProto) | |
{ | |
HashTableProto[i] = undefined; | |
} | |
if(dontEnumKeys.length > 0) | |
{ | |
var i = dontEnumKeys.length; while(i--) | |
{ | |
HashTableProto[dontEnumKeys[i]] = undefined; | |
} | |
} | |
return function() | |
{ | |
return new HashTable(); | |
} | |
})() | |
; | |
var cache = (function() | |
{ | |
var i = 'a'.charCodeAt(0) - 1, z = 'z'.charCodeAt(0); | |
var s = ''; | |
while(++i <= z) | |
{ | |
s += String.fromCharCode(i); | |
} | |
var re = new RegExp('\s+', s); | |
var acceptedFlags = String(re); | |
acceptedFlags = acceptedFlags.slice(acceptedFlags.lastIndexOf('/') + 1).split(''); | |
var cache = {}; | |
var _func = function(selectedFlags, restFlags) | |
{ | |
// recurse calls | |
var i = restFlags.length; while(i--) | |
{ | |
var newSelectedFlags = selectedFlags.slice(0); | |
newSelectedFlags.push(restFlags[i]); | |
var newRestFlags = restFlags.slice(0); | |
newRestFlags.splice(i, 1); | |
_func(newSelectedFlags, newRestFlags); | |
} | |
var subCache = {}; | |
var _combinations = function(flags, path) | |
{ | |
var i = flags.length; | |
if(i > 0) | |
{ | |
while(i--) | |
{ | |
var newFlags = flags.slice(0); | |
newFlags.splice(i, 1); | |
_combinations(newFlags, path + flags[i]); | |
} | |
} | |
else | |
{ | |
cache[path] = subCache; | |
} | |
}; | |
_combinations(selectedFlags, ''); | |
}; | |
_func([], acceptedFlags); | |
return cache; | |
})(); | |
// wrap native RegExp | |
// version by es standard | |
var RegExp = function(pattern) | |
{ | |
var flag = [].slice.call(arguments, 1).join(''); | |
var subCache = cache[flag]; | |
if(typeof(subCache) != 'object') | |
subCache = cache[flag] = {}; | |
var re = subCache[pattern]; | |
if(typeof(re) == 'undefined') | |
re = subCache[pattern] = new RegExp(pattern, flag); | |
return re; | |
}; | |
// simplified version | |
var RegExp = function(pattern, flag) | |
{ | |
if(flag == null) | |
flag = ''; | |
var subCache = cache[flag]; | |
if(typeof(subCache) != 'object') | |
subCache = cache[flag] = {}; | |
var re = subCache[pattern]; | |
if(typeof(re) == 'undefined') | |
re = subCache[pattern] = new RegExp(pattern, flag); | |
return re; | |
}; | |
var nativeRegExpToString = String(OldRegExp); | |
RegExp._extendFrom(OldRegExp); | |
RegExp.toString = function(){ return nativeRegExpToString }; | |
RegExp.prototype = OldRegExp.prototype; | |
OldRegExp.prototype.constructor = RegExp; | |
$G.RegExp = RegExp; | |
// OR make some creation method | |
RegExp._create = function(pattern, flag) | |
{ | |
if(flag == null) | |
flag = ''; | |
var subCache = cache[flag]; | |
if(typeof(subCache) != 'object') | |
subCache = cache[flag] = {}; | |
var re = subCache[pattern]; | |
if(typeof(re) == 'undefined') | |
re = subCache[pattern] = new RegExp(pattern, flag); | |
return re; | |
}; | |
})(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment