Skip to content

Commit

Permalink
Merge branch 'master' into add_typeurl
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorcode committed Oct 16, 2020
2 parents d178e45 + ade223d commit 61b3f62
Show file tree
Hide file tree
Showing 8 changed files with 520 additions and 164 deletions.
14 changes: 9 additions & 5 deletions README.md
Expand Up @@ -285,11 +285,15 @@ The library utilizes JSON descriptors that are equivalent to a .proto definition
// awesome.json
{
"nested": {
"AwesomeMessage": {
"fields": {
"awesomeField": {
"type": "string",
"id": 1
"awesomepackage": {
"nested": {
"AwesomeMessage": {
"fields": {
"awesomeField": {
"type": "string",
"id": 1
}
}
}
}
}
Expand Down
45 changes: 18 additions & 27 deletions lib/utf8/index.js
Expand Up @@ -38,36 +38,27 @@ utf8.length = function utf8_length(string) {
* @returns {string} String read
*/
utf8.read = function utf8_read(buffer, start, end) {
var len = end - start;
if (len < 1)
if (end - start < 1) {
return "";
var parts = null,
chunk = [],
i = 0, // char offset
t; // temporary
while (start < end) {
t = buffer[start++];
if (t < 128)
chunk[i++] = t;
else if (t > 191 && t < 224)
chunk[i++] = (t & 31) << 6 | buffer[start++] & 63;
else if (t > 239 && t < 365) {
t = ((t & 7) << 18 | (buffer[start++] & 63) << 12 | (buffer[start++] & 63) << 6 | buffer[start++] & 63) - 0x10000;
chunk[i++] = 0xD800 + (t >> 10);
chunk[i++] = 0xDC00 + (t & 1023);
} else
chunk[i++] = (t & 15) << 12 | (buffer[start++] & 63) << 6 | buffer[start++] & 63;
if (i > 8191) {
(parts || (parts = [])).push(String.fromCharCode.apply(String, chunk));
i = 0;
}
}
if (parts) {
if (i)
parts.push(String.fromCharCode.apply(String, chunk.slice(0, i)));
return parts.join("");

var str = "";
for (var i = start; i < end;) {
var t = buffer[i++];
if (t <= 0x7F) {
str += String.fromCharCode(t);
} else if (t >= 0xC0 && t < 0xE0) {
str += String.fromCharCode((t & 0x1F) << 6 | buffer[i++] & 0x3F);
} else if (t >= 0xE0 && t < 0xF0) {
str += String.fromCharCode((t & 0xF) << 12 | (buffer[i++] & 0x3F) << 6 | buffer[i++] & 0x3F);
} else if (t >= 0xF0) {
var t2 = ((t & 7) << 18 | (buffer[i++] & 0x3F) << 12 | (buffer[i++] & 0x3F) << 6 | buffer[i++] & 0x3F) - 0x10000;
str += String.fromCharCode(0xD800 + (t2 >> 10));
str += String.fromCharCode(0xDC00 + (t2 & 0x3FF));
}
}
return String.fromCharCode.apply(String, chunk.slice(0, i));

return str;
};

/**
Expand Down
207 changes: 207 additions & 0 deletions lib/utf8/tests/data/surrogate_pair_bug.txt

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions lib/utf8/tests/index.js
Expand Up @@ -5,6 +5,9 @@ var utf8 = require("..");
var data = require("fs").readFileSync(require.resolve("./data/utf8.txt")),
dataStr = data.toString("utf8");

var surrogatePairErr = require("fs").readFileSync(require.resolve("./data/surrogate_pair_bug.txt")),
surrogatePairErrStr = data.toString("utf8");

tape.test("utf8", function(test) {

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

comp = utf8.read(surrogatePairErr, 0, surrogatePairErr.length);
test.equal(comp, surrogatePairErr.toString("utf8"), "should decode to the same byte data as node buffers (surrogate pair over chunk)");

test.end();
});

Expand Down

0 comments on commit 61b3f62

Please sign in to comment.