forked from nodejs/node-v0.x-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.js
More file actions
89 lines (69 loc) · 1.86 KB
/
buffer.js
File metadata and controls
89 lines (69 loc) · 1.86 KB
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
var Buffer = process.binding('buffer').Buffer;
exports.Buffer = Buffer;
function toHex (n) {
if (n < 16) return "0" + n.toString(16);
return n.toString(16);
}
Buffer.isBuffer = function (b) {
return b instanceof Buffer;
};
Buffer.prototype.inspect = function () {
var out = [],
len = this.length;
for (var i = 0; i < len; i++) {
out[i] = toHex(this[i]);
}
return "<Buffer " + out.join(" ") + ">";
};
Buffer.prototype.toString = function (encoding, start, stop) {
encoding = String(encoding || 'utf8').toLowerCase();
start = +start || 0;
if (typeof stop == "undefined") stop = this.length;
// Fastpath empty strings
if (+stop == start) {
return '';
}
switch (encoding) {
case 'utf8':
case 'utf-8':
return this.utf8Slice(start, stop);
case 'ascii':
return this.asciiSlice(start, stop);
case 'binary':
return this.binarySlice(start, stop);
case 'base64':
return this.base64Slice(start, stop);
default:
throw new Error('Unknown encoding');
}
};
Buffer.prototype.write = function (string, offset, encoding) {
// Support both (string, offset, encoding)
// and the legacy (string, encoding, offset)
if (!isFinite(offset)) {
var swap = encoding;
encoding = offset;
offset = swap;
}
offset = +offset || 0;
encoding = String(encoding || 'utf8').toLowerCase();
switch (encoding) {
case 'utf8':
case 'utf-8':
return this.utf8Write(string, offset);
case 'ascii':
return this.asciiWrite(string, offset);
case 'binary':
return this.binaryWrite(string, offset);
case 'base64':
return this.base64Write(string, offset);
default:
throw new Error('Unknown encoding');
}
};
Buffer.prototype.get = function (index) {
return this[index];
};
Buffer.prototype.set = function (index, value) {
return this[index] = value;
};