GeezerVison is showing github.com — string_decoder: reset decoder on end · styfle/node@d2a6110
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/string_decoder.js +12-3Lines changed: 12 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -210,8 +210,11 @@ function utf8Text(buf, i) { | 210 | 210 | // character. | 211 | 211 | function utf8End(buf) { | 212 | 212 | const r = (buf && buf.length ? this.write(buf) : ''); | 213 | | - if (this.lastNeed) | | 213 | + if (this.lastNeed) { | | 214 | + this.lastNeed = 0; | | 215 | + this.lastTotal = 0; | 214 | 216 | return r + '\ufffd'; | | 217 | + } | 215 | 218 | return r; | 216 | 219 | } | 217 | 220 |
| @@ -246,6 +249,8 @@ function utf16End(buf) { | 246 | 249 | const r = (buf && buf.length ? this.write(buf) : ''); | 247 | 250 | if (this.lastNeed) { | 248 | 251 | const end = this.lastTotal - this.lastNeed; | | 252 | + this.lastNeed = 0; | | 253 | + this.lastTotal = 0; | 249 | 254 | return r + this.lastChar.toString('utf16le', 0, end); | 250 | 255 | } | 251 | 256 | return r; | @@ -269,8 +274,12 @@ function base64Text(buf, i) { | 269 | 274 |
| 270 | 275 | function base64End(buf) { | 271 | 276 | const r = (buf && buf.length ? this.write(buf) : ''); | 272 | | - if (this.lastNeed) | 273 | | - return r + this.lastChar.toString('base64', 0, 3 - this.lastNeed); | | 277 | + if (this.lastNeed) { | | 278 | + const end = 3 - this.lastNeed; | | 279 | + this.lastNeed = 0; | | 280 | + this.lastTotal = 0; | | 281 | + return r + this.lastChar.toString('base64', 0, end); | | 282 | + } | 274 | 283 | return r; | 275 | 284 | } | 276 | 285 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-string-decoder-end.js +51Lines changed: 51 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,6 +39,46 @@ for (let i = 1; i <= 16; i++) { | 39 | 39 |
| 40 | 40 | encodings.forEach(testEncoding); | 41 | 41 |
| | 42 | +testEnd('utf8', Buffer.of(0xE2), Buffer.of(0x61), '\uFFFDa'); | | 43 | +testEnd('utf8', Buffer.of(0xE2), Buffer.of(0x82), '\uFFFD\uFFFD'); | | 44 | +testEnd('utf8', Buffer.of(0xE2), Buffer.of(0xE2), '\uFFFD\uFFFD'); | | 45 | +testEnd('utf8', Buffer.of(0xE2, 0x82), Buffer.of(0x61), '\uFFFDa'); | | 46 | +testEnd('utf8', Buffer.of(0xE2, 0x82), Buffer.of(0xAC), '\uFFFD\uFFFD'); | | 47 | +testEnd('utf8', Buffer.of(0xE2, 0x82), Buffer.of(0xE2), '\uFFFD\uFFFD'); | | 48 | +testEnd('utf8', Buffer.of(0xE2, 0x82, 0xAC), Buffer.of(0x61), '€a'); | | 49 | + | | 50 | +testEnd('utf16le', Buffer.of(0x3D), Buffer.of(0x61, 0x00), 'a'); | | 51 | +testEnd('utf16le', Buffer.of(0x3D), Buffer.of(0xD8, 0x4D, 0xDC), '\u4DD8'); | | 52 | +testEnd('utf16le', Buffer.of(0x3D, 0xD8), Buffer.of(), '\uD83D'); | | 53 | +testEnd('utf16le', Buffer.of(0x3D, 0xD8), Buffer.of(0x61, 0x00), '\uD83Da'); | | 54 | +testEnd( | | 55 | + 'utf16le', | | 56 | + Buffer.of(0x3D, 0xD8), | | 57 | + Buffer.of(0x4D, 0xDC), | | 58 | + '\uD83D\uDC4D' | | 59 | +); | | 60 | +testEnd('utf16le', Buffer.of(0x3D, 0xD8, 0x4D), Buffer.of(), '\uD83D'); | | 61 | +testEnd( | | 62 | + 'utf16le', | | 63 | + Buffer.of(0x3D, 0xD8, 0x4D), | | 64 | + Buffer.of(0x61, 0x00), | | 65 | + '\uD83Da' | | 66 | +); | | 67 | +testEnd('utf16le', Buffer.of(0x3D, 0xD8, 0x4D), Buffer.of(0xDC), '\uD83D'); | | 68 | +testEnd( | | 69 | + 'utf16le', | | 70 | + Buffer.of(0x3D, 0xD8, 0x4D, 0xDC), | | 71 | + Buffer.of(0x61, 0x00), | | 72 | + '👍a' | | 73 | +); | | 74 | + | | 75 | +testEnd('base64', Buffer.of(0x61), Buffer.of(), 'YQ=='); | | 76 | +testEnd('base64', Buffer.of(0x61), Buffer.of(0x61), 'YQ==YQ=='); | | 77 | +testEnd('base64', Buffer.of(0x61, 0x61), Buffer.of(), 'YWE='); | | 78 | +testEnd('base64', Buffer.of(0x61, 0x61), Buffer.of(0x61), 'YWE=YQ=='); | | 79 | +testEnd('base64', Buffer.of(0x61, 0x61, 0x61), Buffer.of(), 'YWFh'); | | 80 | +testEnd('base64', Buffer.of(0x61, 0x61, 0x61), Buffer.of(0x61), 'YWFhYQ=='); | | 81 | + | 42 | 82 | function testEncoding(encoding) { | 43 | 83 | bufs.forEach((buf) => { | 44 | 84 | testBuf(encoding, buf); | @@ -66,3 +106,14 @@ function testBuf(encoding, buf) { | 66 | 106 | assert.strictEqual(res1, res3, 'one byte at a time should match toString'); | 67 | 107 | assert.strictEqual(res2, res3, 'all bytes at once should match toString'); | 68 | 108 | } | | 109 | + | | 110 | +function testEnd(encoding, incomplete, next, expected) { | | 111 | + let res = ''; | | 112 | + const s = new SD(encoding); | | 113 | + res += s.write(incomplete); | | 114 | + res += s.end(); | | 115 | + res += s.write(next); | | 116 | + res += s.end(); | | 117 | + | | 118 | + assert.strictEqual(res, expected); | | 119 | +} |
|