Skip to content

Commit

Permalink
Refactored argument order of utf8 for plausibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 10, 2016
1 parent 14c207e commit f3a9589
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 214 deletions.
193 changes: 97 additions & 96 deletions dist/protobuf.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions src/util/base64/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ var base64 = exports;

/**
* Calculates the base64 byte length of a string.
* @param {string} str Base64 encoded string
* @param {string} string Base64 encoded string
* @returns {number} Byte length
*/
base64.length = function length(str) {
var p = str.length;
base64.length = function length(string) {
var p = string.length;
if (!p)
return 0;
var n = 0;
while (--p % 4 > 1 && str.charAt(p) === '=')
while (--p % 4 > 1 && string.charAt(p) === '=')
++n;
return Math.ceil(str.length * 3) / 4 - n;
return Math.ceil(string.length * 3) / 4 - n;
};

// Base64 encoding table
Expand All @@ -38,37 +38,37 @@ var b64 = [
* @returns {string} Base64 encoded string
*/
base64.encode = function encode(buffer, start, end) {
var str = new Array(Math.ceil((end - start) / 3) * 4);
var string = new Array(Math.ceil((end - start) / 3) * 4);
var i = 0, // output index
j = 0, // goto index
t; // temporary
while (start < end) {
var b = buffer[start++];
switch (j) {
case 0:
str[i++] = b64[b >> 2];
string[i++] = b64[b >> 2];
t = (b & 3) << 4;
j = 1;
break;
case 1:
str[i++] = b64[t | b >> 4];
string[i++] = b64[t | b >> 4];
t = (b & 15) << 2;
j = 2;
break;
case 2:
str[i++] = b64[t | b >> 6];
str[i++] = b64[b & 63];
string[i++] = b64[t | b >> 6];
string[i++] = b64[b & 63];
j = 0;
break;
}
}
if (j) {
str[i++] = b64[t];
str[i ] = 61;
string[i++] = b64[t];
string[i ] = 61;
if (j === 1)
str[i + 1] = 61;
string[i + 1] = 61;
}
return String.fromCharCode.apply(String, str);
return String.fromCharCode.apply(String, string);
};

// Base64 decoding table
Expand All @@ -77,18 +77,18 @@ var invalidEncoding = "invalid encoding";

/**
* Decodes a base64 encoded string to a buffer.
* @param {string} src Source string
* @param {string} string Source string
* @param {Uint8Array} buffer Destination buffer
* @param {number} offset Destination offset
* @returns {number} Number of bytes written
* @throws {Error} If encoding is invalid
*/
base64.decode = function decode(src, buffer, offset) {
base64.decode = function decode(string, buffer, offset) {
var start = offset;
var j = 0, // goto index
t; // temporary
for (var i = 0; i < src.length;) {
var c = src.charCodeAt(i++);
for (var i = 0; i < string.length;) {
var c = string.charCodeAt(i++);
if (c === 61 && j > 1)
break;
if ((c = s64[c]) === undefined)
Expand Down
107 changes: 55 additions & 52 deletions src/util/utf8/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ var utf8 = exports;

/**
* Calculates the UTF8 byte length of a string.
* @param {string} str String
* @param {string} string String
* @returns {number} Byte length
*/
utf8.length = function length(str) {
var strlen = str.length >>> 0;
utf8.length = function length(string) {
var strlen = string.length >>> 0;
var len = 0,
c = 0;
for (var i = 0; i < strlen; ++i) {
c = str.charCodeAt(i);
c = string.charCodeAt(i);
if (c < 128)
len += 1;
else if (c < 2048)
len += 2;
else if ((c & 0xFC00) === 0xD800 && (str.charCodeAt(i + 1) & 0xFC00) === 0xDC00) {
else if ((c & 0xFC00) === 0xD800 && (string.charCodeAt(i + 1) & 0xFC00) === 0xDC00) {
++i;
len += 4;
} else
Expand All @@ -31,64 +31,67 @@ utf8.length = function length(str) {
return len;
};

/**
* Writes a string as UTF8 bytes.
* @param {Uint8Array} buf Destination buffer
* @param {number} pos Destination offset
* @param {string} str Source string
* @returns {number} Bytes written
*/
utf8.write = function(buf, pos, str) {
var start = pos;
for (var i = 0; i < str.length; ++i) {
var c1 = str.charCodeAt(i), c2;
if (c1 < 128) {
buf[pos++] = c1;
} else if (c1 < 2048) {
buf[pos++] = c1 >> 6 | 192;
buf[pos++] = c1 & 63 | 128;
} else if ((c1 & 0xFC00) === 0xD800 && ((c2 = str.charCodeAt(i + 1)) & 0xFC00) === 0xDC00) {
c1 = 0x10000 + ((c1 & 0x03FF) << 10) + (c2 & 0x03FF);
++i;
buf[pos++] = c1 >> 18 | 240;
buf[pos++] = c1 >> 12 & 63 | 128;
buf[pos++] = c1 >> 6 & 63 | 128;
buf[pos++] = c1 & 63 | 128;
} else {
buf[pos++] = c1 >> 12 | 224;
buf[pos++] = c1 >> 6 & 63 | 128;
buf[pos++] = c1 & 63 | 128;
}
}
return pos - start;
};

/**
* Reads UTF8 bytes as a string.
* @param {Uint8Array} buf Source buffer
* @param {number} pos Source offset
* @param {number} len Source length
* @param {Uint8Array} buffer Source buffer
* @param {number} start Source start
* @param {number} end Source end
* @returns {string} String read
*/
utf8.read = function(buf, pos, len) {
if (len) {
var out = [],
utf8.read = function(buffer, start, end) {
var len = end - start;
if (len > 0) {
var string = [],
i = 0, // char offset
t; // temporary
while (pos < len) {
t = buf[pos++];
while (start < end) {
t = buffer[start++];
if (t < 128)
out[i++] = t;
string[i++] = t;
else if (t > 191 && t < 224)
out[i++] = (t & 31) << 6 | buf[pos++] & 63;
string[i++] = (t & 31) << 6 | buffer[start++] & 63;
else if (t > 239 && t < 365) {
t = ((t & 7) << 18 | (buf[pos++] & 63) << 12 | (buf[pos++] & 63) << 6 | buf[pos++] & 63) - 0x10000;
out[i++] = 0xD800 + (t >> 10);
out[i++] = 0xDC00 + (t & 1023);
t = ((t & 7) << 18 | (buffer[start++] & 63) << 12 | (buffer[start++] & 63) << 6 | buffer[start++] & 63) - 0x10000;
string[i++] = 0xD800 + (t >> 10);
string[i++] = 0xDC00 + (t & 1023);
} else
out[i++] = (t & 15) << 12 | (buf[pos++] & 63) << 6 | buf[pos++] & 63;
string[i++] = (t & 15) << 12 | (buffer[start++] & 63) << 6 | buffer[start++] & 63;
}
return String.fromCharCode.apply(String, out.slice(0, i));
return String.fromCharCode.apply(String, string.slice(0, i));
}
return "";
};

/**
* Writes a string as UTF8 bytes.
* @param {string} string Source string
* @param {Uint8Array} buffer Destination buffer
* @param {number} offset Destination offset
* @returns {number} Bytes written
*/
utf8.write = function(string, buffer, offset) {
var start = offset,
c1, // character 1
c2; // character 2
for (var i = 0; i < string.length; ++i) {
c1 = string.charCodeAt(i);
if (c1 < 128) {
buffer[offset++] = c1;
} else if (c1 < 2048) {
buffer[offset++] = c1 >> 6 | 192;
buffer[offset++] = c1 & 63 | 128;
} else if ((c1 & 0xFC00) === 0xD800 && ((c2 = string.charCodeAt(i + 1)) & 0xFC00) === 0xDC00) {
c1 = 0x10000 + ((c1 & 0x03FF) << 10) + (c2 & 0x03FF);
++i;
buffer[offset++] = c1 >> 18 | 240;
buffer[offset++] = c1 >> 12 & 63 | 128;
buffer[offset++] = c1 >> 6 & 63 | 128;
buffer[offset++] = c1 & 63 | 128;
} else {
buffer[offset++] = c1 >> 12 | 224;
buffer[offset++] = c1 >> 6 & 63 | 128;
buffer[offset++] = c1 & 63 | 128;
}
}
return offset - start;
};
Loading

0 comments on commit f3a9589

Please sign in to comment.