Skip to content

Commit 61b3f62

Browse files
author
Taylor McIntyre
authored
Merge branch 'master' into add_typeurl
2 parents d178e45 + ade223d commit 61b3f62

File tree

8 files changed

+520
-164
lines changed

8 files changed

+520
-164
lines changed

README.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,15 @@ The library utilizes JSON descriptors that are equivalent to a .proto definition
285285
// awesome.json
286286
{
287287
"nested": {
288-
"AwesomeMessage": {
289-
"fields": {
290-
"awesomeField": {
291-
"type": "string",
292-
"id": 1
288+
"awesomepackage": {
289+
"nested": {
290+
"AwesomeMessage": {
291+
"fields": {
292+
"awesomeField": {
293+
"type": "string",
294+
"id": 1
295+
}
296+
}
293297
}
294298
}
295299
}

lib/utf8/index.js

+18-27
Original file line numberDiff line numberDiff line change
@@ -38,36 +38,27 @@ utf8.length = function utf8_length(string) {
3838
* @returns {string} String read
3939
*/
4040
utf8.read = function utf8_read(buffer, start, end) {
41-
var len = end - start;
42-
if (len < 1)
41+
if (end - start < 1) {
4342
return "";
44-
var parts = null,
45-
chunk = [],
46-
i = 0, // char offset
47-
t; // temporary
48-
while (start < end) {
49-
t = buffer[start++];
50-
if (t < 128)
51-
chunk[i++] = t;
52-
else if (t > 191 && t < 224)
53-
chunk[i++] = (t & 31) << 6 | buffer[start++] & 63;
54-
else if (t > 239 && t < 365) {
55-
t = ((t & 7) << 18 | (buffer[start++] & 63) << 12 | (buffer[start++] & 63) << 6 | buffer[start++] & 63) - 0x10000;
56-
chunk[i++] = 0xD800 + (t >> 10);
57-
chunk[i++] = 0xDC00 + (t & 1023);
58-
} else
59-
chunk[i++] = (t & 15) << 12 | (buffer[start++] & 63) << 6 | buffer[start++] & 63;
60-
if (i > 8191) {
61-
(parts || (parts = [])).push(String.fromCharCode.apply(String, chunk));
62-
i = 0;
63-
}
6443
}
65-
if (parts) {
66-
if (i)
67-
parts.push(String.fromCharCode.apply(String, chunk.slice(0, i)));
68-
return parts.join("");
44+
45+
var str = "";
46+
for (var i = start; i < end;) {
47+
var t = buffer[i++];
48+
if (t <= 0x7F) {
49+
str += String.fromCharCode(t);
50+
} else if (t >= 0xC0 && t < 0xE0) {
51+
str += String.fromCharCode((t & 0x1F) << 6 | buffer[i++] & 0x3F);
52+
} else if (t >= 0xE0 && t < 0xF0) {
53+
str += String.fromCharCode((t & 0xF) << 12 | (buffer[i++] & 0x3F) << 6 | buffer[i++] & 0x3F);
54+
} else if (t >= 0xF0) {
55+
var t2 = ((t & 7) << 18 | (buffer[i++] & 0x3F) << 12 | (buffer[i++] & 0x3F) << 6 | buffer[i++] & 0x3F) - 0x10000;
56+
str += String.fromCharCode(0xD800 + (t2 >> 10));
57+
str += String.fromCharCode(0xDC00 + (t2 & 0x3FF));
58+
}
6959
}
70-
return String.fromCharCode.apply(String, chunk.slice(0, i));
60+
61+
return str;
7162
};
7263

7364
/**

lib/utf8/tests/data/surrogate_pair_bug.txt

+207
Large diffs are not rendered by default.

lib/utf8/tests/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ var utf8 = require("..");
55
var data = require("fs").readFileSync(require.resolve("./data/utf8.txt")),
66
dataStr = data.toString("utf8");
77

8+
var surrogatePairErr = require("fs").readFileSync(require.resolve("./data/surrogate_pair_bug.txt")),
9+
surrogatePairErrStr = data.toString("utf8");
10+
811
tape.test("utf8", function(test) {
912

1013
test.test(test.name + " - length", function(test) {
@@ -30,6 +33,9 @@ tape.test("utf8", function(test) {
3033
comp = utf8.read(chunkData, 0, chunkData.length);
3134
test.equal(comp, chunkData.toString("utf8"), "should decode to the same byte data as node buffers (chunk size)");
3235

36+
comp = utf8.read(surrogatePairErr, 0, surrogatePairErr.length);
37+
test.equal(comp, surrogatePairErr.toString("utf8"), "should decode to the same byte data as node buffers (surrogate pair over chunk)");
38+
3339
test.end();
3440
});
3541

0 commit comments

Comments
 (0)