more compact versions of decodeBase128 and decodeBase256

This commit is contained in:
rolux 2011-12-17 04:42:18 +05:30
parent 6374f6de6d
commit d2d92c1ae9

View file

@ -130,7 +130,7 @@
str = Ox.char(num & 127) + str;
num >>= 7;
}
return str || '0';
return str;
};
/*@
@ -139,11 +139,9 @@
1685487
@*/
Ox.decodeBase128 = function(str) {
var num = 0, len = str.length;
Ox.forEach(str, function(char, i) {
num += char.charCodeAt(0) << (len - i - 1) * 7;
});
return num;
return str.split('').reverse().reduce(function(p, v, i) {
return p + (v.charCodeAt(0) << i * 7);
}, 0);
};
/*@
@ -166,11 +164,9 @@
6713199
@*/
Ox.decodeBase256 = function(str) {
var num = 0, len = str.length;
Ox.forEach(str, function(char, i) {
num += char.charCodeAt(0) << (len - i - 1) * 8;
});
return num;
return str.split('').reverse().reduce(function(p, v, i) {
return p + (v.charCodeAt(0) << i * 8);
}, 0);
};
/*@