Skip to content

Commit 80eff96

Browse files
felixgetjfontaine
authored andcommitted
string_decoder: Add more comments
1 parent 9fbd0f0 commit 80eff96

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

lib/string_decoder.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ function assertEncoding(encoding) {
2525
}
2626
}
2727

28+
// StringDecoder provides an interface for efficiently splitting a series of
29+
// buffers into a series of JS strings without breaking apart multi-byte
30+
// characters. CESU-8 is handled as part of the UTF-8 encoding.
31+
//
32+
// @TODO Handling all encodings inside a single object makes it very difficult
33+
// to reason about this code, so it should be split up in the future.
34+
// @TODO There should be a utf8-strict encoding that rejects invalid UTF-8 code
35+
// points as used by CESU-8.
2836
var StringDecoder = exports.StringDecoder = function(encoding) {
2937
this.encoding = (encoding || 'utf8').toLowerCase().replace(/[-_]/, '');
3038
assertEncoding(encoding);
@@ -49,12 +57,25 @@ var StringDecoder = exports.StringDecoder = function(encoding) {
4957
return;
5058
}
5159

60+
// Enough space to store all bytes of a single character. UTF-8 needs 4
61+
// bytes, but CESU-8 may require up to 6 (3 bytes per surrogate).
5262
this.charBuffer = new Buffer(6);
63+
// Number of bytes received for the current incomplete multi-byte character.
5364
this.charReceived = 0;
65+
// Number of bytes expected for the current incomplete multi-byte character.
5466
this.charLength = 0;
5567
};
5668

5769

70+
// write decodes the given buffer and returns it as JS string that is
71+
// guaranteed to not contain any partial multi-byte characters. Any partial
72+
// character found at the end of the buffer is buffered up, and will be
73+
// returned when calling write again with the remaining bytes.
74+
//
75+
// Note: Converting a Buffer containing an orphan surrogate to a String
76+
// currently works, but converting a String to a Buffer (via `new Buffer`, or
77+
// Buffer#write) will replace incomplete surrogates with the unicode
78+
// replacement character. See https://codereview.chromium.org/121173009/ .
5879
StringDecoder.prototype.write = function(buffer) {
5980
var charStr = '';
6081
// if our last write ended with an incomplete multibyte character
@@ -123,6 +144,10 @@ StringDecoder.prototype.write = function(buffer) {
123144
return charStr;
124145
};
125146

147+
// detectIncompleteChar determines if there is an incomplete UTF-8 character at
148+
// the end of the given buffer. If so, it sets this.charLength to the byte
149+
// length that character, and sets this.charReceived to the number of bytes
150+
// that are available for this character.
126151
StringDecoder.prototype.detectIncompleteChar = function(buffer) {
127152
// determine how many bytes we have to check at the end of this buffer
128153
var i = (buffer.length >= 3) ? 3 : buffer.length;

0 commit comments

Comments
 (0)