mirror of
https://github.com/adelphes/android-dev-ext.git
synced 2025-12-22 17:39:19 +00:00
code tidy - fix lint warnings for unused fns, params and locals
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
const {
|
const {
|
||||||
DebugSession,
|
DebugSession,
|
||||||
ContinuedEvent, InitializedEvent, ExitedEvent, TerminatedEvent, StoppedEvent, BreakpointEvent, ThreadEvent, OutputEvent, Event,
|
InitializedEvent, TerminatedEvent, StoppedEvent, BreakpointEvent, ThreadEvent, OutputEvent,
|
||||||
Thread, StackFrame, Scope, Source, Handles, Breakpoint } = require('vscode-debugadapter');
|
Thread, StackFrame, Scope, Source, Breakpoint } = require('vscode-debugadapter');
|
||||||
|
|
||||||
// node and external modules
|
// node and external modules
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
@@ -16,7 +16,6 @@ const xpath = require('xpath');
|
|||||||
const { ADBClient } = require('./adbclient');
|
const { ADBClient } = require('./adbclient');
|
||||||
const { Debugger } = require('./debugger');
|
const { Debugger } = require('./debugger');
|
||||||
const $ = require('./jq-promise');
|
const $ = require('./jq-promise');
|
||||||
const NumberBaseConverter = require('./nbc');
|
|
||||||
const { AndroidThread } = require('./threads');
|
const { AndroidThread } = require('./threads');
|
||||||
const { D, isEmptyObject } = require('./util');
|
const { D, isEmptyObject } = require('./util');
|
||||||
const { AndroidVariables } = require('./variables');
|
const { AndroidVariables } = require('./variables');
|
||||||
@@ -82,7 +81,7 @@ class AndroidDebugSession extends DebugSession {
|
|||||||
* The 'initialize' request is the first request called by the frontend
|
* The 'initialize' request is the first request called by the frontend
|
||||||
* to interrogate the features the debug adapter provides.
|
* to interrogate the features the debug adapter provides.
|
||||||
*/
|
*/
|
||||||
initializeRequest(response/*: DebugProtocol.InitializeResponse*/, args/*: DebugProtocol.InitializeRequestArguments*/) {
|
initializeRequest(response/*: DebugProtocol.InitializeResponse, args: DebugProtocol.InitializeRequestArguments*/) {
|
||||||
|
|
||||||
// This debug adapter implements the configurationDoneRequest.
|
// This debug adapter implements the configurationDoneRequest.
|
||||||
response.body.supportsConfigurationDoneRequest = true;
|
response.body.supportsConfigurationDoneRequest = true;
|
||||||
@@ -503,7 +502,7 @@ class AndroidDebugSession extends DebugSession {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
configurationDoneRequest(response, args) {
|
configurationDoneRequest(response/*, args*/) {
|
||||||
this.waitForConfigurationDone.resolve();
|
this.waitForConfigurationDone.resolve();
|
||||||
this.sendResponse(response);
|
this.sendResponse(response);
|
||||||
}
|
}
|
||||||
@@ -518,7 +517,7 @@ class AndroidDebugSession extends DebugSession {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnectRequest(response, args) {
|
disconnectRequest(response/*, args*/) {
|
||||||
D('disconnectRequest');
|
D('disconnectRequest');
|
||||||
this._isDisconnecting = true;
|
this._isDisconnecting = true;
|
||||||
// if we're connected, ask ADB to terminate the app
|
// if we're connected, ask ADB to terminate the app
|
||||||
@@ -633,7 +632,7 @@ class AndroidDebugSession extends DebugSession {
|
|||||||
javabp.vsbp.order = idx;
|
javabp.vsbp.order = idx;
|
||||||
javabp_arr.push(javabp);
|
javabp_arr.push(javabp);
|
||||||
}).
|
}).
|
||||||
then(javabp => _setup_breakpoints(o, ++idx, javabp_arr));
|
then((/*javabp*/) => _setup_breakpoints(o, ++idx, javabp_arr));
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!this._set_breakpoints_queue) {
|
if (!this._set_breakpoints_queue) {
|
||||||
@@ -702,7 +701,7 @@ class AndroidDebugSession extends DebugSession {
|
|||||||
};
|
};
|
||||||
this.sendResponse(response);
|
this.sendResponse(response);
|
||||||
})
|
})
|
||||||
.fail(e => {
|
.fail(() => {
|
||||||
response.success = false;
|
response.success = false;
|
||||||
this.sendResponse(response);
|
this.sendResponse(response);
|
||||||
});
|
});
|
||||||
@@ -732,7 +731,6 @@ class AndroidDebugSession extends DebugSession {
|
|||||||
const endFrame = Math.min(startFrame + maxLevels, frames.length);
|
const endFrame = Math.min(startFrame + maxLevels, frames.length);
|
||||||
var stack = [], totalFrames = frames.length, highest_known_source=0;
|
var stack = [], totalFrames = frames.length, highest_known_source=0;
|
||||||
const android_src_path = this._android_sources_path || '{Android SDK}';
|
const android_src_path = this._android_sources_path || '{Android SDK}';
|
||||||
const device_api_level = this.dbgr.session.apilevel || '25';
|
|
||||||
for (var i= startFrame; i < endFrame; i++) {
|
for (var i= startFrame; i < endFrame; i++) {
|
||||||
// the stack_frame_id must be unique across all threads
|
// the stack_frame_id must be unique across all threads
|
||||||
const stack_frame_id = x.thread.addStackFrameVariable(frames[i], i).frameId;
|
const stack_frame_id = x.thread.addStackFrameVariable(frames[i], i).frameId;
|
||||||
@@ -781,7 +779,7 @@ class AndroidDebugSession extends DebugSession {
|
|||||||
};
|
};
|
||||||
this.sendResponse(response);
|
this.sendResponse(response);
|
||||||
})
|
})
|
||||||
.fail((e,x) => {
|
.fail(() => {
|
||||||
this.failRequest('No call stack is available', response);
|
this.failRequest('No call stack is available', response);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -809,13 +807,13 @@ class AndroidDebugSession extends DebugSession {
|
|||||||
scopes.unshift(new Scope("Exception: "+ex_local.type.typename, last_exception.scopeRef, false));
|
scopes.unshift(new Scope("Exception: "+ex_local.type.typename, last_exception.scopeRef, false));
|
||||||
this.sendResponse(response);
|
this.sendResponse(response);
|
||||||
})
|
})
|
||||||
.fail(e => { this.sendResponse(response); });
|
.fail((/*e*/) => { this.sendResponse(response); });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.sendResponse(response);
|
this.sendResponse(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
sourceRequest(response/*: DebugProtocol.SourceResponse*/, args/*: DebugProtocol.SourceArguments*/) {
|
sourceRequest(response/*: DebugProtocol.SourceResponse, args: DebugProtocol.SourceArguments*/) {
|
||||||
var content =
|
var content =
|
||||||
`/*
|
`/*
|
||||||
The source for this class is unavailable.
|
The source for this class is unavailable.
|
||||||
@@ -1070,7 +1068,7 @@ class AndroidDebugSession extends DebugSession {
|
|||||||
if (!this._evals_queue.length) {
|
if (!this._evals_queue.length) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var {response, args, getvars, thread} = this._evals_queue[0];
|
var {response, args, getvars} = this._evals_queue[0];
|
||||||
|
|
||||||
// wait for any locals in the given context to be retrieved
|
// wait for any locals in the given context to be retrieved
|
||||||
getvars.then((thread, locals, vars) => {
|
getvars.then((thread, locals, vars) => {
|
||||||
@@ -1231,9 +1229,11 @@ class AndroidDebugSession extends DebugSession {
|
|||||||
// current or higher precendence
|
// current or higher precendence
|
||||||
if (binary_operator[0]==='?') {
|
if (binary_operator[0]==='?') {
|
||||||
res = { condition:res, operator:binary_operator[0], ternary_true:null, ternary_false:null };
|
res = { condition:res, operator:binary_operator[0], ternary_true:null, ternary_false:null };
|
||||||
res.ternary_true = parse_expression(o);
|
res.ternary_true = parse_expression(e);
|
||||||
symbol(e,':');
|
if (e.expr[0] === ':') {
|
||||||
res.ternary_false = parse_expression(o);
|
e.expr = e.expr.slice(1).trim();
|
||||||
|
res.ternary_false = parse_expression(e);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
res = { lhs:res, operator:binary_operator[0], rhs:parse_expression(e) };
|
res = { lhs:res, operator:binary_operator[0], rhs:parse_expression(e) };
|
||||||
}
|
}
|
||||||
@@ -1481,7 +1481,7 @@ class AndroidDebugSession extends DebugSession {
|
|||||||
}.bind(this,arr_local))
|
}.bind(this,arr_local))
|
||||||
.then(els => els[0])
|
.then(els => els[0])
|
||||||
}
|
}
|
||||||
const evaluate_methodcall = (m, obj_local) => {
|
const evaluate_methodcall = (/*m, obj_local*/) => {
|
||||||
return reject_evaluation('Error: method calls are not supported');
|
return reject_evaluation('Error: method calls are not supported');
|
||||||
}
|
}
|
||||||
const evaluate_member = (m, obj_local) => {
|
const evaluate_member = (m, obj_local) => {
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
// vscode stuff
|
|
||||||
const { EventEmitter, Uri } = require('vscode');
|
|
||||||
// node and external modules
|
// node and external modules
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const os = require('os');
|
const os = require('os');
|
||||||
@@ -38,7 +36,7 @@ class LogcatContent {
|
|||||||
onlog: this.onLogcatContent.bind(this),
|
onlog: this.onLogcatContent.bind(this),
|
||||||
onclose: this.onLogcatDisconnect.bind(this),
|
onclose: this.onLogcatDisconnect.bind(this),
|
||||||
});
|
});
|
||||||
}).then(x => {
|
}).then(() => {
|
||||||
this._state = 'connected';
|
this._state = 'connected';
|
||||||
this._initwait = null;
|
this._initwait = null;
|
||||||
resolve(this.content);
|
resolve(this.content);
|
||||||
@@ -55,20 +53,20 @@ class LogcatContent {
|
|||||||
return this.htmlBootstrap({connected:true, status:'',oldlogs:''});
|
return this.htmlBootstrap({connected:true, status:'',oldlogs:''});
|
||||||
// if we're in the disconnected state, and this.content is called, it means the user has requested
|
// if we're in the disconnected state, and this.content is called, it means the user has requested
|
||||||
// this logcat again - check if the device has reconnected
|
// this logcat again - check if the device has reconnected
|
||||||
return this._initwait = new Promise((resolve, reject) => {
|
return this._initwait = new Promise((resolve/*, reject*/) => {
|
||||||
// clear the logs first - if we successfully reconnect, we will be retrieving the entire logcat again
|
// clear the logs first - if we successfully reconnect, we will be retrieving the entire logcat again
|
||||||
this._prevlogs = {_logs: this._logs, _htmllogs: this._htmllogs, _oldhtmllogs: this._oldhtmllogs };
|
this._prevlogs = {_logs: this._logs, _htmllogs: this._htmllogs, _oldhtmllogs: this._oldhtmllogs };
|
||||||
this._logs = []; this._htmllogs = []; this._oldhtmllogs = [];
|
this._logs = []; this._htmllogs = []; this._oldhtmllogs = [];
|
||||||
this._adbclient.logcat({
|
this._adbclient.logcat({
|
||||||
onlog: this.onLogcatContent.bind(this),
|
onlog: this.onLogcatContent.bind(this),
|
||||||
onclose: this.onLogcatDisconnect.bind(this),
|
onclose: this.onLogcatDisconnect.bind(this),
|
||||||
}).then(x => {
|
}).then(() => {
|
||||||
// we successfully reconnected
|
// we successfully reconnected
|
||||||
this._state = 'connected';
|
this._state = 'connected';
|
||||||
this._prevlogs = null;
|
this._prevlogs = null;
|
||||||
this._initwait = null;
|
this._initwait = null;
|
||||||
resolve(this.content);
|
resolve(this.content);
|
||||||
}).fail(e => {
|
}).fail((/*e*/) => {
|
||||||
// reconnection failed - put the logs back and return the cached info
|
// reconnection failed - put the logs back and return the cached info
|
||||||
this._logs = this._prevlogs._logs;
|
this._logs = this._prevlogs._logs;
|
||||||
this._htmllogs = this._prevlogs._htmllogs;
|
this._htmllogs = this._prevlogs._htmllogs;
|
||||||
@@ -161,7 +159,7 @@ class LogcatContent {
|
|||||||
this.renotify();
|
this.renotify();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
onLogcatDisconnect(e) {
|
onLogcatDisconnect(/*e*/) {
|
||||||
if (this._state === 'disconnected') return;
|
if (this._state === 'disconnected') return;
|
||||||
this._state = 'disconnected';
|
this._state = 'disconnected';
|
||||||
this.sendDisconnectMsg();
|
this.sendDisconnectMsg();
|
||||||
@@ -215,7 +213,7 @@ LogcatContent.initWebSocketServer = function () {
|
|||||||
this.wss = null;
|
this.wss = null;
|
||||||
LogcatContent._wssdone.resolveWith(LogcatContent, []);
|
LogcatContent._wssdone.resolveWith(LogcatContent, []);
|
||||||
});
|
});
|
||||||
this.wss.on('error', err => {
|
this.wss.on('error', (/*err*/) => {
|
||||||
if (!LogcatContent._wss) {
|
if (!LogcatContent._wss) {
|
||||||
// listen failed -try the next port
|
// listen failed -try the next port
|
||||||
this.retries++ , this.port++;
|
this.retries++ , this.port++;
|
||||||
@@ -245,7 +243,7 @@ function openLogcatWindow(vscode) {
|
|||||||
var adbpath = path.join(process.env.ANDROID_HOME, 'platform-tools', /^win/.test(process.platform)?'adb.exe':'adb');
|
var adbpath = path.join(process.env.ANDROID_HOME, 'platform-tools', /^win/.test(process.platform)?'adb.exe':'adb');
|
||||||
var adbargs = ['-P',''+adbport,'start-server'];
|
var adbargs = ['-P',''+adbport,'start-server'];
|
||||||
try {
|
try {
|
||||||
var stdout = require('child_process').execFileSync(adbpath, adbargs, {cwd:process.env.ANDROID_HOME, encoding:'utf8'});
|
/*var stdout = */require('child_process').execFileSync(adbpath, adbargs, {cwd:process.env.ANDROID_HOME, encoding:'utf8'});
|
||||||
} catch (ex) {} // if we fail, it doesn't matter - the device query will fail and the user will have to work it out themselves
|
} catch (ex) {} // if we fail, it doesn't matter - the device query will fail and the user will have to work it out themselves
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -282,7 +280,7 @@ function openLogcatWindow(vscode) {
|
|||||||
return vscode.commands.executeCommand("vscode.previewHtml",uri,vscode.ViewColumn.Two);
|
return vscode.commands.executeCommand("vscode.previewHtml",uri,vscode.ViewColumn.Two);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.fail(e => {
|
.fail((/*e*/) => {
|
||||||
vscode.window.showInformationMessage('Logcat cannot be displayed. Querying the connected devices list failed. Is ADB running?');
|
vscode.window.showInformationMessage('Logcat cannot be displayed. Querying the connected devices list failed. Is ADB running?');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
187
src/util.js
187
src/util.js
@@ -1,12 +1,6 @@
|
|||||||
const crypto = require('crypto');
|
|
||||||
|
|
||||||
var nofn=function(){};
|
var nofn=function(){};
|
||||||
var D=exports.D=console.log.bind(console);
|
var D=exports.D=console.log.bind(console);
|
||||||
var E=exports.E=console.error.bind(console);
|
|
||||||
var W=exports.W=console.warn.bind(console);
|
|
||||||
var DD=nofn,cl=D,printf=D;
|
|
||||||
var print_jdwp_data = nofn;// _print_jdwp_data;
|
|
||||||
var print_packet = nofn;//_print_packet;
|
|
||||||
|
|
||||||
Array.first = function(arr, fn, defaultvalue) {
|
Array.first = function(arr, fn, defaultvalue) {
|
||||||
var idx = Array.indexOfFirst(arr, fn);
|
var idx = Array.indexOfFirst(arr, fn);
|
||||||
@@ -21,7 +15,7 @@ Array.indexOfFirst = function(arr, fn) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
var isEmptyObject = exports.isEmptyObject = function(o) {
|
exports.isEmptyObject = function(o) {
|
||||||
return typeof(o)==='object' && !Object.keys(o).length;
|
return typeof(o)==='object' && !Object.keys(o).length;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,13 +25,13 @@ var leftpad = exports.leftpad = function(char, len, s) {
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
var intToHex = exports.intToHex = function(i, minlen) {
|
exports.intToHex = function(i, minlen) {
|
||||||
var s = i.toString(16);
|
var s = i.toString(16);
|
||||||
if (minlen) s = leftpad('0', minlen, s);
|
if (minlen) s = leftpad('0', minlen, s);
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
var intFromHex = exports.intFromHex = function(s, maxlen, defaultvalue) {
|
exports.intFromHex = function(s, maxlen, defaultvalue) {
|
||||||
s = s.slice(0, maxlen);
|
s = s.slice(0, maxlen);
|
||||||
if (!/^[0-9a-fA-F]+$/.test(s)) return defaultvalue;
|
if (!/^[0-9a-fA-F]+$/.test(s)) return defaultvalue;
|
||||||
return parseInt(s, 16);
|
return parseInt(s, 16);
|
||||||
@@ -54,12 +48,6 @@ var index_of_file_fdn = function(n) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
var get_file_fd_from_fdn = function(n) {
|
|
||||||
var idx = index_of_file_fdn(n);
|
|
||||||
if (idx < 0) return null;
|
|
||||||
return fdcache[idx];
|
|
||||||
}
|
|
||||||
|
|
||||||
var remove_fd_from_cache = function(fd) {
|
var remove_fd_from_cache = function(fd) {
|
||||||
if (!fd) return;
|
if (!fd) return;
|
||||||
var idx = index_of_file_fdn(fd.n);
|
var idx = index_of_file_fdn(fd.n);
|
||||||
@@ -69,7 +57,7 @@ var remove_fd_from_cache = function(fd) {
|
|||||||
// add an offset so we don't conflict with tcp socketIds
|
// add an offset so we don't conflict with tcp socketIds
|
||||||
var min_fd_num = 100000;
|
var min_fd_num = 100000;
|
||||||
var _new_fd_count = 0;
|
var _new_fd_count = 0;
|
||||||
var new_fd = this.new_fd = function(name, raw) {
|
this.new_fd = function(name, raw) {
|
||||||
var rwpipe = raw ? new Uint8Array(0) : [];
|
var rwpipe = raw ? new Uint8Array(0) : [];
|
||||||
var fd = {
|
var fd = {
|
||||||
name: name,
|
name: name,
|
||||||
@@ -259,22 +247,6 @@ var new_fd = this.new_fd = function(name, raw) {
|
|||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
var intToCharString = function(n) {
|
|
||||||
return String.fromCharCode(
|
|
||||||
(n>>0)&255,
|
|
||||||
(n>>8)&255,
|
|
||||||
(n>>16)&255,
|
|
||||||
(n>>24)&255
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
var stringToUint8Array = function(s) {
|
|
||||||
var x = new Uint8Array(s.length);
|
|
||||||
for (var i=0; i < s.length; i++)
|
|
||||||
x[i] = s.charCodeAt(i);
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
var uint8ArrayToString = function(a) {
|
var uint8ArrayToString = function(a) {
|
||||||
var s = new Array(a.byteLength);
|
var s = new Array(a.byteLength);
|
||||||
for (var i=0; i < a.byteLength; i++)
|
for (var i=0; i < a.byteLength; i++)
|
||||||
@@ -355,162 +327,13 @@ var iterate_repeat = function(arr, count, o, j) {
|
|||||||
}
|
}
|
||||||
iterate_repeat(arr, count, o, (j||0)+1);
|
iterate_repeat(arr, count, o, (j||0)+1);
|
||||||
},
|
},
|
||||||
error:function(err) {
|
error:function(/*err*/) {
|
||||||
o.error && o.error();
|
o.error && o.error();
|
||||||
o.complete && o.complete();
|
o.complete && o.complete();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert from an ArrayBuffer to a string.
|
|
||||||
* @param {ArrayBuffer} buffer The array buffer to convert.
|
|
||||||
* @return {string} The textual representation of the array.
|
|
||||||
*/
|
|
||||||
var arrayBufferToString = exports.arrayBufferToString = function(buffer) {
|
|
||||||
var array = new Uint8Array(buffer);
|
|
||||||
var str = '';
|
|
||||||
for (var i = 0; i < array.length; ++i) {
|
|
||||||
str += String.fromCharCode(array[i]);
|
|
||||||
}
|
|
||||||
return str;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert from an UTF-8 array to UTF-8 string.
|
|
||||||
* @param {array} UTF-8 array
|
|
||||||
* @return {string} UTF-8 string
|
|
||||||
*/
|
|
||||||
var ary2utf8 = (function() {
|
|
||||||
|
|
||||||
var patterns = [
|
|
||||||
{pattern: '0xxxxxxx', bytes: 1},
|
|
||||||
{pattern: '110xxxxx', bytes: 2},
|
|
||||||
{pattern: '1110xxxx', bytes: 3},
|
|
||||||
{pattern: '11110xxx', bytes: 4},
|
|
||||||
{pattern: '111110xx', bytes: 5},
|
|
||||||
{pattern: '1111110x', bytes: 6}
|
|
||||||
];
|
|
||||||
patterns.forEach(function(item) {
|
|
||||||
item.header = item.pattern.replace(/[^10]/g, '');
|
|
||||||
item.pattern01 = item.pattern.replace(/[^10]/g, '0');
|
|
||||||
item.pattern01 = parseInt(item.pattern01, 2);
|
|
||||||
item.mask_length = item.header.length;
|
|
||||||
item.data_length = 8 - item.header.length;
|
|
||||||
var mask = '';
|
|
||||||
for (var i = 0, len = item.mask_length; i < len; i++) {
|
|
||||||
mask += '1';
|
|
||||||
}
|
|
||||||
for (var i = 0, len = item.data_length; i < len; i++) {
|
|
||||||
mask += '0';
|
|
||||||
}
|
|
||||||
item.mask = mask;
|
|
||||||
item.mask = parseInt(item.mask, 2);
|
|
||||||
});
|
|
||||||
|
|
||||||
return function(ary) {
|
|
||||||
var codes = [];
|
|
||||||
var cur = 0;
|
|
||||||
while(cur < ary.length) {
|
|
||||||
var first = ary[cur];
|
|
||||||
var pattern = null;
|
|
||||||
for (var i = 0, len = patterns.length; i < len; i++) {
|
|
||||||
if ((first & patterns[i].mask) == patterns[i].pattern01) {
|
|
||||||
pattern = patterns[i];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (pattern == null) {
|
|
||||||
throw 'utf-8 decode error';
|
|
||||||
}
|
|
||||||
var rest = ary.slice(cur + 1, cur + pattern.bytes);
|
|
||||||
cur += pattern.bytes;
|
|
||||||
var code = '';
|
|
||||||
code += ('00000000' + (first & (255 ^ pattern.mask)).toString(2)).slice(-pattern.data_length);
|
|
||||||
for (var i = 0, len = rest.length; i < len; i++) {
|
|
||||||
code += ('00000000' + (rest[i] & parseInt('111111', 2)).toString(2)).slice(-6);
|
|
||||||
}
|
|
||||||
codes.push(parseInt(code, 2));
|
|
||||||
}
|
|
||||||
return String.fromCharCode.apply(null, codes);
|
|
||||||
};
|
|
||||||
|
|
||||||
})();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert from an UTF-8 string to UTF-8 array.
|
|
||||||
* @param {string} UTF-8 string
|
|
||||||
* @return {array} UTF-8 array
|
|
||||||
*/
|
|
||||||
var utf82ary = (function() {
|
|
||||||
|
|
||||||
var patterns = [
|
|
||||||
{pattern: '0xxxxxxx', bytes: 1},
|
|
||||||
{pattern: '110xxxxx', bytes: 2},
|
|
||||||
{pattern: '1110xxxx', bytes: 3},
|
|
||||||
{pattern: '11110xxx', bytes: 4},
|
|
||||||
{pattern: '111110xx', bytes: 5},
|
|
||||||
{pattern: '1111110x', bytes: 6}
|
|
||||||
];
|
|
||||||
patterns.forEach(function(item) {
|
|
||||||
item.header = item.pattern.replace(/[^10]/g, '');
|
|
||||||
item.mask_length = item.header.length;
|
|
||||||
item.data_length = 8 - item.header.length;
|
|
||||||
item.max_bit_length = (item.bytes - 1) * 6 + item.data_length;
|
|
||||||
});
|
|
||||||
|
|
||||||
var code2utf8array = function(code) {
|
|
||||||
var pattern = null;
|
|
||||||
var code01 = code.toString(2);
|
|
||||||
for (var i = 0, len = patterns.length; i < len; i++) {
|
|
||||||
if (code01.length <= patterns[i].max_bit_length) {
|
|
||||||
pattern = patterns[i];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (pattern == null) {
|
|
||||||
throw 'utf-8 encode error';
|
|
||||||
}
|
|
||||||
var ary = [];
|
|
||||||
for (var i = 0, len = pattern.bytes - 1; i < len; i++) {
|
|
||||||
ary.unshift(parseInt('10' + ('000000' + code01.slice(-6)).slice(-6), 2));
|
|
||||||
code01 = code01.slice(0, -6);
|
|
||||||
}
|
|
||||||
ary.unshift(parseInt(pattern.header + ('00000000' + code01).slice(-pattern.data_length), 2));
|
|
||||||
return ary;
|
|
||||||
};
|
|
||||||
|
|
||||||
return function(str) {
|
|
||||||
var codes = [];
|
|
||||||
for (var i = 0, len = str.length; i < len; i++) {
|
|
||||||
var code = str.charCodeAt(i);
|
|
||||||
Array.prototype.push.apply(codes, code2utf8array(code));
|
|
||||||
}
|
|
||||||
return codes;
|
|
||||||
};
|
|
||||||
|
|
||||||
})();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert a string to an ArrayBuffer.
|
|
||||||
* @param {string} string The string to convert.
|
|
||||||
* @return {ArrayBuffer} An array buffer whose bytes correspond to the string.
|
|
||||||
*/
|
|
||||||
var stringToArrayBuffer = exports.stringToArrayBuffer = function(string) {
|
|
||||||
var buffer = new ArrayBuffer(string.length);
|
|
||||||
var bufferView = new Uint8Array(buffer);
|
|
||||||
for (var i = 0; i < string.length; i++) {
|
|
||||||
bufferView[i] = string.charCodeAt(i);
|
|
||||||
}
|
|
||||||
return buffer;
|
|
||||||
};
|
|
||||||
|
|
||||||
var str2ab = exports.str2ab = stringToArrayBuffer;
|
|
||||||
var ab2str = exports.ab2str = arrayBufferToString;
|
|
||||||
var str2u8arr = exports.str2u8arr = function(s) {
|
|
||||||
return new Uint8Array(str2ab(s));
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.getutf8bytes = function(str) {
|
exports.getutf8bytes = function(str) {
|
||||||
var utf8 = [];
|
var utf8 = [];
|
||||||
for (var i=0; i < str.length; i++) {
|
for (var i=0; i < str.length; i++) {
|
||||||
|
|||||||
Reference in New Issue
Block a user