Move javaUTF8Decode function to util.js

This commit is contained in:
Marco Castelluccio 2014-11-13 22:24:59 +01:00
Родитель 376252ece1
Коммит 1c002d7f57
2 изменённых файлов: 33 добавлений и 32 удалений

Просмотреть файл

@ -48,37 +48,6 @@ Reader.prototype.readDouble = function() {
return data;
}
// Decode Java's modified UTF-8 (JVM specs, $ 4.4.7)
// http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html#7963
function javaUTF8Decode(arr) {
var str = '';
var i = 0;
while (i < arr.length) {
var x = arr[i++];
if (x <= 0x7f) {
// Code points in the range '\u0001' to '\u007F' are represented by a
// single byte.
// The 7 bits of data in the byte give the value of the code point
// represented.
str += String.fromCharCode(x);
} else if (x <= 0xdf) {
// The null code point ('\u0000') and code points in the range '\u0080'
// to '\u07FF' are represented by a pair of bytes x and y.
var y = arr[i++];
str += String.fromCharCode(((x & 0x1f) << 6) + (y & 0x3f));
} else {
// Code points in the range '\u0800' to '\uFFFF' are represented by 3
// bytes x, y, and z.
var y = arr[i++];
var z = arr[i++];
str += String.fromCharCode(((x & 0xf) << 12) + ((y & 0x3f) << 6) + (z & 0x3f));
}
}
return str;
}
Reader.prototype.readString = function(length) {
// NB: no need to create a new slice.
var data = new Uint8Array(this.bytes.buffer, this.offset, length);
@ -90,7 +59,7 @@ Reader.prototype.readString = function(length) {
try {
return util.decodeUtf8Array(data);
} catch(e) {
return javaUTF8Decode(data);
return util.javaUTF8Decode(data);
}
}

32
util.js
Просмотреть файл

@ -31,6 +31,37 @@ var util = (function () {
return fallibleUtf8Decoder.decode(arr);
}
// Decode Java's modified UTF-8 (JVM specs, $ 4.4.7)
// http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html#7963
function javaUTF8Decode(arr) {
var str = '';
var i = 0;
while (i < arr.length) {
var x = arr[i++];
if (x <= 0x7f) {
// Code points in the range '\u0001' to '\u007F' are represented by a
// single byte.
// The 7 bits of data in the byte give the value of the code point
// represented.
str += String.fromCharCode(x);
} else if (x <= 0xdf) {
// The null code point ('\u0000') and code points in the range '\u0080'
// to '\u07FF' are represented by a pair of bytes x and y.
var y = arr[i++];
str += String.fromCharCode(((x & 0x1f) << 6) + (y & 0x3f));
} else {
// Code points in the range '\u0800' to '\uFFFF' are represented by 3
// bytes x, y, and z.
var y = arr[i++];
var z = arr[i++];
str += String.fromCharCode(((x & 0xf) << 12) + ((y & 0x3f) << 6) + (z & 0x3f));
}
}
return str;
}
function defaultValue(type) {
if (type === 'J')
return Long.ZERO;
@ -172,6 +203,7 @@ var util = (function () {
INT_MIN: INT_MIN,
decodeUtf8: decodeUtf8,
decodeUt8Array: decodeUtf8Array,
javaUTF8Decode: javaUTF8Decode,
defaultValue: defaultValue,
double2int: double2int,
double2long: double2long,