-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathhelpers.js
143 lines (133 loc) · 4.45 KB
/
helpers.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
// helpers
//? if (NODE) {
/**
* @type {!Buffer}
* @inner
*/
var EMPTY_BUFFER = Buffer.alloc(0);
//? } else {
/**
* @type {!ArrayBuffer}
* @inner
*/
var EMPTY_BUFFER = new ArrayBuffer(0);
//? }
//? if (!INLINE) {
/**
* Asserts that a value is an integer and returns the type-safe value.
* @param {number} value Value to assert
* @param {boolean=} unsigned Whether explicitly unsigned
* @returns {number} Type-safe value
* @throws {TypeError} If `value` is not an integer
* @inner
*/
function assertInteger(value, unsigned) {
if (typeof value !== 'number' || value % 1 !== 0)
throw TypeError("Illegal value: "+offset+" (not an integer)");
return unsigned ? value >>> 0 : value | 0;
}
/**
* Asserts that a value is an integer or Long.
* @param {number|!Long} value Value to assert
* @param {boolean=} unsigned Whether explicitly unsigned
* @returns {number|!Long} Type-safe value
* @throws {TypeError} If `value` is not an integer or Long
* @inner
*/
function assertLong(value, unsigned) {
if (typeof value === 'number') {
return Long.fromNumber(value, unsigned);
} else if (typeof value === 'string') {
return Long.fromString(value, unsigned);
} else if (value && value instanceof Long) {
if (typeof unsigned !== 'undefined') {
if (unsigned && !value.unsigned) return value.toUnsigned();
if (!unsigned && value.unsigned) return value.toSigned();
}
return value;
} else
throw TypeError("Illegal value: "+value+" (not an integer or Long)");
}
/**
* Asserts that `min <= offset <= cap-size` and returns the type-safe offset.
* @param {number} offset Offset to assert
* @param {number} min Minimum offset
* @param {number} cap Cap offset
* @param {number} size Required size in bytes
* @returns {number} Type-safe offset
* @throws {TypeError} If `offset` is not an integer
* @throws {RangeError} If `offset < min || offset > cap-size`
* @inner
*/
function assertOffset(offset, min, cap, size) {
if (typeof offset !== 'number' || offset % 1 !== 0)
throw TypeError("Illegal offset: "+offset+" (not an integer)");
offset = offset | 0;
if (offset < min || offset > cap-size)
throw RangeError("Illegal offset: "+min+" <= "+value+" <= "+cap+"-"+size);
return offset;
}
/**
* assertRange return value.
* @type {Array.<number>}
*/
var rangeVal = new Array(2);
/**
* Asserts that `min <= begin <= end <= cap`. Updates `rangeVal` with the type-safe range.
* @param {number} begin Begin offset
* @param {number} end End offset
* @param {number} min Minimum offset
* @param {number} cap Cap offset
* @throws {TypeError} If `begin` or `end` is not an integer
* @throws {RangeError} If `begin < min || begin > end || end > cap`
* @inner
*/
function assertRange(begin, end, min, cap) {
if (typeof begin !== 'number' || begin % 1 !== 0)
throw TypeError("Illegal begin: "+begin+" (not a number)");
begin = begin | 0;
if (typeof end !== 'number' || end % 1 !== 0)
throw TypeError("Illegal end: "+range[1]+" (not a number)");
end = end | 0;
if (begin < min || begin > end || end > cap)
throw RangeError("Illegal range: "+min+" <= "+begin+" <= "+end+" <= "+cap);
rangeVal[0] = begin; rangeVal[1] = end;
}
//? }
//? if (BASE64 || UTF8) {
/**
* String.fromCharCode reference for compile-time renaming.
* @type {function(...number):string}
* @inner
*/
var stringFromCharCode = String.fromCharCode;
/**
* Creates a source function for a string.
* @param {string} s String to read from
* @returns {function():number|null} Source function returning the next char code respectively `null` if there are
* no more characters left.
* @throws {TypeError} If the argument is invalid
* @inner
*/
function stringSource(s) {
var i=0; return function() {
return i < s.length ? s.charCodeAt(i++) : null;
};
}
/**
* Creates a destination function for a string.
* @returns {function(number=):undefined|string} Destination function successively called with the next char code.
* Returns the final string when called without arguments.
* @inner
*/
function stringDestination() {
var cs = [], ps = []; return function() {
if (arguments.length === 0)
return ps.join('')+stringFromCharCode.apply(String, cs);
if (cs.length + arguments.length > 1024)
ps.push(stringFromCharCode.apply(String, cs)),
cs.length = 0;
Array.prototype.push.apply(cs, arguments);
};
}
//? }