aboutsummaryrefslogtreecommitdiffhomepage
path: root/js/binary/utils.js
diff options
context:
space:
mode:
authorGravatar Jisi Liu <jisi.liu@gmail.com>2016-04-28 14:34:59 -0700
committerGravatar Jisi Liu <jisi.liu@gmail.com>2016-04-28 14:34:59 -0700
commitcf14183bcd5485b4a71541599ddce0b35eb71352 (patch)
tree12f6e5eb731d7a70cdac4cdafc8b3131629413e2 /js/binary/utils.js
parentf00300d7f04f1c38a7d70e271f9232b94dd0e326 (diff)
Down integrate from Google internal.
Diffstat (limited to 'js/binary/utils.js')
-rw-r--r--js/binary/utils.js50
1 files changed, 50 insertions, 0 deletions
diff --git a/js/binary/utils.js b/js/binary/utils.js
index 875ff955..51405553 100644
--- a/js/binary/utils.js
+++ b/js/binary/utils.js
@@ -568,6 +568,56 @@ jspb.utils.hash64ArrayToDecimalStrings = function(hashes, signed) {
/**
+ * Converts a signed or unsigned decimal string into its hash string
+ * representation.
+ * @param {string} dec
+ * @return {string}
+ */
+jspb.utils.decimalStringToHash64 = function(dec) {
+ goog.asserts.assert(dec.length > 0);
+
+ // Check for minus sign.
+ var minus = false;
+ if (dec[0] === '-') {
+ minus = true;
+ dec = dec.slice(1);
+ }
+
+ // Store result as a byte array.
+ var resultBytes = [0, 0, 0, 0, 0, 0, 0, 0];
+
+ // Set result to m*result + c.
+ function muladd(m, c) {
+ for (var i = 0; i < 8 && (m !== 1 || c > 0); i++) {
+ var r = m * resultBytes[i] + c;
+ resultBytes[i] = r & 0xFF;
+ c = r >>> 8;
+ }
+ }
+
+ // Negate the result bits.
+ function neg() {
+ for (var i = 0; i < 8; i++) {
+ resultBytes[i] = (~resultBytes[i]) & 0xFF;
+ }
+ }
+
+ // For each decimal digit, set result to 10*result + digit.
+ for (var i = 0; i < dec.length; i++) {
+ muladd(10, jspb.utils.DIGITS.indexOf(dec[i]));
+ }
+
+ // If there's a minus sign, convert into two's complement.
+ if (minus) {
+ neg();
+ muladd(1, 1);
+ }
+
+ return String.fromCharCode.apply(null, resultBytes);
+};
+
+
+/**
* Converts an 8-character hash string into its hexadecimal representation.
* @param {string} hash
* @return {string}