JavaScript String Incrementer
4 28 Jan 2017 20:56 by u/xobodox
I needed this for something.. but, it could be used for a brute force attack on a hash, etc.. I don't like that a String object cannot be edited in place; but this runs with decent performance through NodeJS..
String.prototype.increment = function (min, max) {
var arr = Array.from(this);
var last = arr.length - 1;
if (last < 0) {
arr[0] = String.fromCharCode(min);
return arr.join('');
}
var charCode = arr[last].charCodeAt(0);
while(charCode >= max) {
arr[last--] = String.fromCharCode(min);
if (last < 0) break;
var charCode = arr[last].charCodeAt(0);
}
if (last < 0) {
arr[arr.length] = String.fromCharCode(min);
} else {
arr[last] = String.fromCharCode(arr[last].charCodeAt(0)+1);
}
return arr.join('');
}
// example usage
var x = "";
while(x.length < 2) {
//x = x.increment(32, 55); //32 is ASCII <space>, 55 is ASCII 7
//x = x.increment(48, 57); //48 is ASCII 0, 57 is ASCII 9
x = x.increment(65, 90); //65 is ASCII A, 90 is ASCII Z
console.log("x=[",x,"]");
}
1 comment
0 u/xobodox [OP] 29 Jan 2017 03:08
Looks like this could be used to check for memory leaks in NodeJS.. LOL!
Also, if you want to check performance between NodeJS and C..