forked from protobufjs/protobuf.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongbits.js
192 lines (172 loc) · 4.89 KB
/
longbits.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"use strict";
module.exports = LongBits;
var util = require("../util");
/**
* Constructs new long bits.
* @classdesc Helper class for working with the low and high bits of a 64 bit value.
* @memberof util
* @constructor
* @param {number} lo Low bits
* @param {number} hi High bits
*/
function LongBits(lo, hi) { // make sure to always call this with unsigned 32bits for proper optimization
/**
* Low bits.
* @type {number}
*/
this.lo = lo;
/**
* High bits.
* @type {number}
*/
this.hi = hi;
}
/** @alias util.LongBits.prototype */
var LongBitsPrototype = LongBits.prototype;
/**
* Zero bits.
* @memberof util.LongBits
* @type {util.LongBits}
*/
var zero = new LongBits(0, 0);
zero.toNumber = function() { return 0; };
zero.zzEncode = zero.zzDecode = function() { return this; };
zero.length = function() { return 1; };
/**
* Constructs new long bits from the specified number.
* @param {number} value Value
* @returns {util.LongBits} Instance
*/
LongBits.fromNumber = function fromNumber(value) {
if (value === 0)
return zero;
var sign = value < 0;
value = Math.abs(value);
var lo = value >>> 0,
hi = (value - lo) / 4294967296 >>> 0;
if (sign) {
hi = ~hi >>> 0;
lo = ~lo >>> 0;
if (++lo > 4294967295) {
lo = 0;
if (++hi > 4294967295)
hi = 0;
}
}
return new LongBits(lo, hi);
};
/**
* Constrcuts new long bits from a number or long.
* @param {Long|number|string} value Value
* @returns {util.LongBits} Instance
*/
LongBits.from = function from(value) {
var type = typeof value, result=zero;
if(type === 'number')
result = LongBits.fromNumber(value);
else
{
if(type === 'string' && util.Long)
value = util.Long.fromString(value);
if(value.low || value.high)
result = new LongBits(value.low >>> 0, value.high >>> 0);
}
return result;
};
/**
* Converts this long bits to a possibly unsafe JavaScript number.
* @param {boolean} [unsigned=false] Whether unsigned or not
* @returns {number} Possibly unsafe number
*/
LongBitsPrototype.toNumber = function toNumber(unsigned) {
if (!unsigned && this.hi >>> 31) {
this.lo = ~this.lo + 1 >>> 0;
this.hi = ~this.hi >>> 0;
if (!this.lo)
this.hi = this.hi + 1 >>> 0;
return -(this.lo + this.hi * 4294967296);
}
return this.lo + this.hi * 4294967296;
};
/**
* Converts this long bits to a long.
* @param {boolean} [unsigned=false] Whether unsigned or not
* @returns {Long} Long
*/
LongBitsPrototype.toLong = function toLong(unsigned) {
return new util.Long(this.lo, this.hi, unsigned);
};
var charCodeAt = String.prototype.charCodeAt;
/**
* Constructs new long bits from the specified 8 characters long hash.
* @param {string} hash Hash
* @returns {util.LongBits} Bits
*/
LongBits.fromHash = function fromHash(hash) {
return new LongBits(
( charCodeAt.call(hash, 0)
| charCodeAt.call(hash, 1) << 8
| charCodeAt.call(hash, 2) << 16
| charCodeAt.call(hash, 3) << 24) >>> 0
,
( charCodeAt.call(hash, 4)
| charCodeAt.call(hash, 5) << 8
| charCodeAt.call(hash, 6) << 16
| charCodeAt.call(hash, 7) << 24) >>> 0
);
};
/**
* Converts this long bits to a 8 characters long hash.
* @returns {string} Hash
*/
LongBitsPrototype.toHash = function toHash() {
return String.fromCharCode(
this.lo & 255,
this.lo >>> 8 & 255,
this.lo >>> 16 & 255,
this.lo >>> 24 & 255,
this.hi & 255,
this.hi >>> 8 & 255,
this.hi >>> 16 & 255,
this.hi >>> 24 & 255
);
};
/**
* Zig-zag encodes this long bits.
* @returns {util.LongBits} `this`
*/
LongBitsPrototype.zzEncode = function zzEncode() {
var mask = this.hi >> 31;
this.hi = ((this.hi << 1 | this.lo >>> 31) ^ mask) >>> 0;
this.lo = ( this.lo << 1 ^ mask) >>> 0;
return this;
};
/**
* Zig-zag decodes this long bits.
* @returns {util.LongBits} `this`
*/
LongBitsPrototype.zzDecode = function zzDecode() {
var mask = -(this.lo & 1);
this.lo = ((this.lo >>> 1 | this.hi << 31) ^ mask) >>> 0;
this.hi = ( this.hi >>> 1 ^ mask) >>> 0;
return this;
};
/**
* Calculates the length of this longbits when encoded as a varint.
* @returns {number} Length
*/
LongBitsPrototype.length = function length() {
var part0 = this.lo,
part1 = (this.lo >>> 28 | this.hi << 4) >>> 0,
part2 = this.hi >>> 24;
if (part2 === 0) {
if (part1 === 0)
return part0 < 1 << 14
? part0 < 1 << 7 ? 1 : 2
: part0 < 1 << 21 ? 3 : 4;
return part1 < 1 << 14
? part1 < 1 << 7 ? 5 : 6
: part1 < 1 << 21 ? 7 : 8;
}
return part2 < 1 << 7 ? 9 : 10;
};