-
Notifications
You must be signed in to change notification settings - Fork 153
/
vstring.js
92 lines (89 loc) · 2.78 KB
/
vstring.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
//? if (VSTRING && VARINTS && VARINT32) {
// types/strings/vstring
/**
* Writes a length as varint32 prefixed UTF8 encoded string.
* @param {string} str String to write
* @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
* written if omitted.
* @returns {!ByteBuffer|number} `this` if `offset` is omitted, else the actual number of bytes written
* @expose
* @see ByteBuffer#writeVarint32
*/
ByteBuffer.prototype.writeVString = function(str, offset) {
//? RELATIVE()
if (!this.noAssert) {
if (typeof str !== 'string')
throw(new TypeError("Illegal str: Not a string"));
//? ASSERT_OFFSET();
}
var start = offset,
k, l;
//? if (NODE) {
var buffer = new Buffer(str, "utf8");
l = ByteBuffer.calculateVarint32(buffer.length);
k = buffer.length;
//? ENSURE_CAPACITY('l+k');
offset += this.writeVarint32(buffer.length, offset);
buffer.copy(this.buffer, offset);
offset += buffer.length;
//? } else {
k = utf8_calc_string(str);
l = ByteBuffer.calculateVarint32(k);
//? ENSURE_CAPACITY('l+k');
offset += this.writeVarint32(k, offset);
k = str.length;
for (var i=0; i<k; ++i) {
offset += utf8_encode_char(str.codePointAt(i), this, offset);
}
//? }
if (relative) {
this.offset = offset;
return this;
}
return offset - start;
};
/**
* Reads a length as varint32 prefixed UTF8 encoded string.
* @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
* read if omitted.
* @returns {string|!{string: string, length: number}} The string read if offset is omitted, else the string
* read and the actual number of bytes read.
* @expose
* @see ByteBuffer#readVarint32
*/
ByteBuffer.prototype.readVString = function(offset) {
//? RELATIVE();
if (!this.noAssert) {
//? ASSERT_OFFSET(1);
}
var temp = this.readVarint32(offset),
start = offset,
str;
offset += temp['length'];
temp = temp['value'];
//? if (NODE) {
if (offset + temp > this.buffer.length)
throw(new RangeError("Index out of bounds: "+offset+" + "+val.value+" <= "+this.buffer.length));
str = this.buffer.slice(offset, offset + temp).toString("utf8");
offset += temp;
//? } else {
var k = offset + temp,
out = [];
while (offset < k) {
temp = utf8_decode_char(this, offset);
offset += temp['length'];
out.push(temp['codePoint']);
}
str = String.fromCodePoint.apply(String, out);
//? }
if (relative) {
this.offset = offset;
return str;
} else {
return {
'string': str,
'length': offset - start
};
}
};
//? }