@@ -36,6 +36,14 @@ function assertEncoding(encoding) {
3636 }
3737}
3838
39+ // StringDecoder provides an interface for efficiently splitting a series of
40+ // buffers into a series of JS strings without breaking apart multi-byte
41+ // characters. CESU-8 is handled as part of the UTF-8 encoding.
42+ //
43+ // @TODO Handling all encodings inside a single object makes it very difficult
44+ // to reason about this code, so it should be split up in the future.
45+ // @TODO There should be a utf8-strict encoding that rejects invalid UTF-8 code
46+ // points as used by CESU-8.
3947var StringDecoder = exports . StringDecoder = function ( encoding ) {
4048 this . encoding = ( encoding || 'utf8' ) . toLowerCase ( ) . replace ( / [ - _ ] / , '' ) ;
4149 assertEncoding ( encoding ) ;
@@ -60,37 +68,50 @@ var StringDecoder = exports.StringDecoder = function(encoding) {
6068 return ;
6169 }
6270
71+ // Enough space to store all bytes of a single character. UTF-8 needs 4
72+ // bytes, but CESU-8 may require up to 6 (3 bytes per surrogate).
6373 this . charBuffer = new Buffer ( 6 ) ;
74+ // Number of bytes received for the current incomplete multi-byte character.
6475 this . charReceived = 0 ;
76+ // Number of bytes expected for the current incomplete multi-byte character.
6577 this . charLength = 0 ;
6678} ;
6779
6880
81+ // write decodes the given buffer and returns it as JS string that is
82+ // guaranteed to not contain any partial multi-byte characters. Any partial
83+ // character found at the end of the buffer is buffered up, and will be
84+ // returned when calling write again with the remaining bytes.
85+ //
86+ // Note: Converting a Buffer containing an orphan surrogate to a String
87+ // currently works, but converting a String to a Buffer (via `new Buffer`, or
88+ // Buffer#write) will replace incomplete surrogates with the unicode
89+ // replacement character. See https://codereview.chromium.org/121173009/ .
6990StringDecoder . prototype . write = function ( buffer ) {
7091 var charStr = '' ;
71- var offset = 0 ;
72-
7392 // if our last write ended with an incomplete multibyte character
7493 while ( this . charLength ) {
7594 // determine how many remaining bytes this buffer has to offer for this char
76- var i = ( buffer . length >= this . charLength - this . charReceived ) ?
77- this . charLength - this . charReceived :
78- buffer . length ;
95+ var available = ( buffer . length >= this . charLength - this . charReceived ) ?
96+ this . charLength - this . charReceived :
97+ buffer . length ;
7998
8099 // add the new bytes to the char buffer
81- buffer . copy ( this . charBuffer , this . charReceived , offset , i ) ;
82- this . charReceived += ( i - offset ) ;
83- offset = i ;
100+ buffer . copy ( this . charBuffer , this . charReceived , 0 , available ) ;
101+ this . charReceived += available ;
84102
85103 if ( this . charReceived < this . charLength ) {
86104 // still not enough chars in this buffer? wait for more ...
87105 return '' ;
88106 }
89107
108+ // remove bytes belonging to the current character from the buffer
109+ buffer = buffer . slice ( available , buffer . length ) ;
110+
90111 // get the character that was split
91112 charStr = this . charBuffer . slice ( 0 , this . charLength ) . toString ( this . encoding ) ;
92113
93- // lead surrogate (D800-DBFF) is also the incomplete character
114+ // CESU-8: lead surrogate (D800-DBFF) is also the incomplete character
94115 var charCode = charStr . charCodeAt ( charStr . length - 1 ) ;
95116 if ( charCode >= 0xD800 && charCode <= 0xDBFF ) {
96117 this . charLength += this . surrogateSize ;
@@ -100,41 +121,44 @@ StringDecoder.prototype.write = function(buffer) {
100121 this . charReceived = this . charLength = 0 ;
101122
102123 // if there are no more bytes in this buffer, just emit our char
103- if ( i == buffer . length ) return charStr ;
104-
105- // otherwise cut off the characters end from the beginning of this buffer
106- buffer = buffer . slice ( i , buffer . length ) ;
124+ if ( buffer . length === 0 ) {
125+ return charStr ;
126+ }
107127 break ;
108128 }
109129
110- var lenIncomplete = this . detectIncompleteChar ( buffer ) ;
130+ // determine and set charLength / charReceived
131+ this . detectIncompleteChar ( buffer ) ;
111132
112133 var end = buffer . length ;
113134 if ( this . charLength ) {
114135 // buffer the incomplete character bytes we got
115- buffer . copy ( this . charBuffer , 0 , buffer . length - lenIncomplete , end ) ;
116- this . charReceived = lenIncomplete ;
117- end -= lenIncomplete ;
136+ buffer . copy ( this . charBuffer , 0 , buffer . length - this . charReceived , end ) ;
137+ end -= this . charReceived ;
118138 }
119139
120140 charStr += buffer . toString ( this . encoding , 0 , end ) ;
121141
122142 var end = charStr . length - 1 ;
123143 var charCode = charStr . charCodeAt ( end ) ;
124- // lead surrogate (D800-DBFF) is also the incomplete character
144+ // CESU-8: lead surrogate (D800-DBFF) is also the incomplete character
125145 if ( charCode >= 0xD800 && charCode <= 0xDBFF ) {
126146 var size = this . surrogateSize ;
127147 this . charLength += size ;
128148 this . charReceived += size ;
129149 this . charBuffer . copy ( this . charBuffer , size , 0 , size ) ;
130- this . charBuffer . write ( charStr . charAt ( charStr . length - 1 ) , this . encoding ) ;
150+ buffer . copy ( this . charBuffer , 0 , 0 , size ) ;
131151 return charStr . substring ( 0 , end ) ;
132152 }
133153
134154 // or just emit the charStr
135155 return charStr ;
136156} ;
137157
158+ // detectIncompleteChar determines if there is an incomplete UTF-8 character at
159+ // the end of the given buffer. If so, it sets this.charLength to the byte
160+ // length that character, and sets this.charReceived to the number of bytes
161+ // that are available for this character.
138162StringDecoder . prototype . detectIncompleteChar = function ( buffer ) {
139163 // determine how many bytes we have to check at the end of this buffer
140164 var i = ( buffer . length >= 3 ) ? 3 : buffer . length ;
@@ -164,8 +188,7 @@ StringDecoder.prototype.detectIncompleteChar = function(buffer) {
164188 break ;
165189 }
166190 }
167-
168- return i ;
191+ this . charReceived = i ;
169192} ;
170193
171194StringDecoder . prototype . end = function ( buffer ) {
@@ -188,13 +211,11 @@ function passThroughWrite(buffer) {
188211}
189212
190213function utf16DetectIncompleteChar ( buffer ) {
191- var incomplete = this . charReceived = buffer . length % 2 ;
192- this . charLength = incomplete ? 2 : 0 ;
193- return incomplete ;
214+ this . charReceived = buffer . length % 2 ;
215+ this . charLength = this . charReceived ? 2 : 0 ;
194216}
195217
196218function base64DetectIncompleteChar ( buffer ) {
197- var incomplete = this . charReceived = buffer . length % 3 ;
198- this . charLength = incomplete ? 3 : 0 ;
199- return incomplete ;
219+ this . charReceived = buffer . length % 3 ;
220+ this . charLength = this . charReceived ? 3 : 0 ;
200221}
0 commit comments