From 8f1522fd9ba30c59cad9263d4799440e99245644 Mon Sep 17 00:00:00 2001 From: zswang Date: Tue, 1 Mar 2016 10:51:30 +0800 Subject: [PATCH] depend jstrs: encodeUTF8 & decodeUTF8 --- bower.json | 2 +- jpacks.dev.js | 22 ++++++++++++++++++++-- jpacks.js | 22 ++++++++++++++++++++-- jpacks.min.js | 2 +- package.json | 7 ++++--- src/schemas/string.js | 31 ++++++++----------------------- 6 files changed, 54 insertions(+), 32 deletions(-) diff --git a/bower.json b/bower.json index cea1d9a..aaa0689 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "jpacks", "description": "Binary data packing and unpacking.", - "version": "0.6.3", + "version": "0.6.5", "homepage": "https://github.com/zswang/jpacks", "authors": { "name": "zswang", diff --git a/jpacks.dev.js b/jpacks.dev.js index adb0b22..1503c3c 100644 --- a/jpacks.dev.js +++ b/jpacks.dev.js @@ -5,8 +5,8 @@ * Binary data packing and unpacking. * @author * zswang (http://weibo.com/zswang) - * @version 0.6.3 - * @date 2016-02-29 + * @version 0.6.5 + * @date 2016-03-01 */ function createSchema() { /** @@ -1008,10 +1008,18 @@ fn.args = args; }); Schema.register('enums', enums); + /**/ /** * 对字符串进行 utf8 编码 * * param {string} str 原始字符串 + '''''' + * @example encodeUTF8():base + ```js + console.log(jstrs.encodeUTF8('汉')); + // > 汉 + ``` + '''''' */ function encodeUTF8(str) { if (/[\u0080-\uffff]/.test(str)) { @@ -1019,10 +1027,19 @@ } return str; } + /**/ + /**/ /** * 对 utf8 字符串进行解码 * * @param {string} str 编码字符串 + '''''' + * @example decodeUTF8():base + ```js + console.log(jstrs.decodeUTF8('汉')); + // > 汉 + ``` + '''''' */ function decodeUTF8(str) { if (/[\u00c0-\u00df][\u0080-\u00bf]/.test(str) || @@ -1031,6 +1048,7 @@ } return str; } + /**/ /** * 将字符串转换为字节数组 * diff --git a/jpacks.js b/jpacks.js index a6787fe..ab09b5e 100644 --- a/jpacks.js +++ b/jpacks.js @@ -5,8 +5,8 @@ * Binary data packing and unpacking. * @author * zswang (http://weibo.com/zswang) - * @version 0.6.3 - * @date 2016-02-29 + * @version 0.6.5 + * @date 2016-03-01 */ function createSchema() { /** @@ -945,10 +945,18 @@ fn.args = args; }); Schema.register('enums', enums); + /**/ /** * 对字符串进行 utf8 编码 * * param {string} str 原始字符串 + '''''' + * @example encodeUTF8():base + ```js + console.log(jstrs.encodeUTF8('汉')); + // > 汉 + ``` + '''''' */ function encodeUTF8(str) { if (/[\u0080-\uffff]/.test(str)) { @@ -956,10 +964,19 @@ } return str; } + /**/ + /**/ /** * 对 utf8 字符串进行解码 * * @param {string} str 编码字符串 + '''''' + * @example decodeUTF8():base + ```js + console.log(jstrs.decodeUTF8('汉')); + // > 汉 + ``` + '''''' */ function decodeUTF8(str) { if (/[\u00c0-\u00df][\u0080-\u00bf]/.test(str) || @@ -968,6 +985,7 @@ } return str; } + /**/ /** * 将字符串转换为字节数组 * diff --git a/jpacks.min.js b/jpacks.min.js index 7b63b40..31d971c 100644 --- a/jpacks.min.js +++ b/jpacks.min.js @@ -1 +1 @@ -!function(n){function r(){function n(n){n=n||{};var r=this;Object.keys(n).forEach(function(e){r[e]=n[e]})}function r(n){n=n||{},Object.keys(n).forEach(function(r){f[r]=n[r]})}function e(n){if(n instanceof ArrayBuffer)return n;var r=new ArrayBuffer(n.length),e=new Uint8Array(r,0,n.length);return e.set(n),r}function t(r,t,a,u){var i=n.from(r);return t=e(t),a=a||{},u=u||[0],Object.keys(f).forEach(function(n){"undefined"==typeof a[n]&&(a[n]=f[n])}),i.unpack(t,a,u)}function a(r,e,t,a){var u=n.from(r);return a=a||[],t=t||{},Object.keys(f).forEach(function(n){"undefined"==typeof t[n]&&(t[n]=f[n])}),u.pack(e,t,a),a}function u(n,r,e){if(n.length<=0)return n;var t=function(){var t=[];if([].push.apply(t,e),[].push.apply(t,arguments),t.length>=n.length)return n.apply(null,t);var a=u(n,r,t);return"function"==typeof r&&r(a,t),a};return t}function i(n){function r(n){if(null===n)return"null";if(!n)return n;if(n.namespace)return"number"===n.namespace?"'"+n.name+"'":n.args?n.namespace+"("+i.apply(null,n.args)+")":"'"+n.namespace+"'";if("function"==typeof n){if(!n.name)return n.namespace="$fn",n.namespace}else if("object"==typeof n){var e=new n.constructor;return Object.keys(n).forEach(function(t){e[t]=r(n[t])}),e}return n.name?"'"+n.name+"'":"string"==typeof n?"'"+n+"'":n}if(arguments.length>1){for(var e=[],t=0;tm;m++)c.push(w.unpack(o,t,a,u));return c},pack:function(t,a,u){var i=w.from(n);if(t||e){var c;if(e?(c=t?t.length:0,w.pack(e,c,a,u)):c=null===r?(t||[]).length:r,t&&c){if(i.array&&(a.littleEndian||1===i.size)){var o=i.size*c,f=new ArrayBuffer(o),s=new i.array(f);s.set(t.slice(0,c));var p=new Uint8Array(f);return void[].push.apply(u,p)}for(var g=0;c>g;g++)w.pack(i,(t||[])[g],a,u)}}},namespace:"array",args:arguments,size:"number"===r?itemSchema.size*r:void 0})}function e(n){return A(n,"uint8")}function t(n){return A(n,"uint16")}function a(n){return A(n,"uint32")}function u(n){return w.array("uint8",n)}function i(n){if(n instanceof w)return n;var r=Object.keys(n);return new w({unpack:function(e,t,a){var u=new n.constructor,i=t.$scope;return t.$scope={target:u,offsets:new n.constructor,schema:n},r.forEach(function(r){t.$scope.offsets[r]=a[0],u[r]=w.unpack(n[r],e,t,a)}),t.$scope=i,u},pack:function(e,t,a){var u=t.$scope;t.$scope={target:e,offsets:new n.constructor,schema:n},r.forEach(function(r){t.$scope.offsets[r]=a.length,w.pack(n[r],e[r],t,a)}),t.$scope=u},args:arguments,namespace:"object"})}function c(n,r){var e=Object.keys(n);return new w({unpack:function(t,a,u){var i=u[0],c={};return e.forEach(function(r){u[0]=i,c[r]=w.unpack(n[r],t,a,u)}),u[0]+=r,c},pack:function(t,a,u){var i=new ArrayBuffer(r),c=new Uint8Array(i);e.forEach(function(r){if("undefined"!=typeof t[r]){var e=[];w.pack(n[r],t[r],a,e),c.set(e)}}),[].push.apply(u,c)},size:r,args:arguments,namespace:"union"})}function o(n,r){if(r=w.from(r),n instanceof Array){var e={};n.forEach(function(n,r){e[n]=r}),n=e}var t=Object.keys(n);return new w({unpack:function(e,a,u){var i,c=w.unpack(r,e,a,u);return t.every(function(r){return n[r]===c?(i=r,!1):!0}),i||c},pack:function(e,a,u){if("number"==typeof e)return void w.pack(r,e,a,u);if(t.every(function(t){return t===e?(w.pack(r,n[t],a,u),!1):!0}))throw new Error('Not find enum "'+e+'".')},namespace:"enums",args:arguments})}function f(n){return/[\u0080-\uffff]/.test(n)?unescape(encodeURIComponent(n)):n}function s(n){return/[\u00c0-\u00df][\u0080-\u00bf]/.test(n)||/[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/.test(n)?decodeURIComponent(escape(n)):n}function p(n,r){return r.browser||"undefined"==typeof Buffer?f(n).split("").map(function(n){return n.charCodeAt()}):new Buffer(n,r.encoding)}function g(n){var r=w.array("uint8",n);return new w({unpack:function(n,e,t){var a=w.unpack(r,n,e,t);return e.browser||"undefined"==typeof Buffer?s(String.fromCharCode.apply(String,a)):new Buffer(a).toString(e.encoding)},pack:function(n,e,t){w.pack(r,p(n,e),e,t)},namespace:"string",args:arguments})}function y(n){return new w({unpack:function(r,e,t){var a;a=null===n?new Uint8Array(r,t[0]):w.unpack(w.bytes(n),r,e,t);for(var u=0;a[u];)u++;var i=w.unpack(w.string(u),a,e);return null===n&&(t[0]+=u+1),i},pack:function(r,e,t){var a=[0];[].unshift.apply(a,w.stringBytes(r,e)),w.pack(w.bytes(n),a,e,t)},namespace:"cstring",args:arguments})}function l(n,r){return new w({unpack:function(e,t,a){var u=t.$scope.target[n];return w.unpack(r(u),e,t,a)},pack:function(e,t,a){var u=t.$scope.target[n];w.pack(r(u),e,t,a)},namespace:"depend",args:arguments})}function m(n,r){return B(n,w.array(r))}function v(n,r,e,t){var a=w.bytes(t);return new w({unpack:function(n,t,u){var i=r(w.unpack(a,n,t,u));return w.unpack(e,i,t)},pack:function(r,t,u){var i=n(w.pack(e,r,t));w.pack(a,i,t,u)},namespace:"parse",args:arguments})}function h(n,r){return new w({unpack:function(){return/string|number/.test(typeof n)?n+r:"function"==typeof n?n(r):r},pack:function(){},args:arguments,namespace:"virtual"})}function k(n,r){return new w({unpack:function(e,t,a){var u=t.$scope.target[n],i=w.from(r);return w.unpack(w.array(i,u),e,t,a)},pack:function(e,t,a){for(var u=t.$scope.schema[n],i=w.from(r),c=w.pack(u,e.length,t),o=t.$scope.offsets[n],f=0;f=n.length)return n.apply(null,t);var a=i(n,r,t);return"function"==typeof r&&r(a,t),a};return t}function u(n){function r(n){if(null===n)return"null";if(!n)return n;if(n.namespace)return"number"===n.namespace?"'"+n.name+"'":n.args?n.namespace+"("+u.apply(null,n.args)+")":"'"+n.namespace+"'";if("function"==typeof n){if(!n.name)return n.namespace="$fn",n.namespace}else if("object"==typeof n){var e=new n.constructor;return Object.keys(n).forEach(function(t){e[t]=r(n[t])}),e}return n.name?"'"+n.name+"'":"string"==typeof n?"'"+n+"'":n}if(arguments.length>1){for(var e=[],t=0;tv;v++)c.push(w.unpack(o,t,a,i));return c},pack:function(t,a,i){var u=w.from(n);if(t||e){var c;if(e?(c=t?t.length:0,w.pack(e,c,a,i)):c=null===r?(t||[]).length:r,t&&c){if(u.array&&(a.littleEndian||1===u.size)){var o=u.size*c,f=new ArrayBuffer(o),s=new u.array(f);s.set(t.slice(0,c));var p=new Uint8Array(f);return void[].push.apply(i,p)}for(var y=0;c>y;y++)w.pack(u,(t||[])[y],a,i)}}},namespace:"array",args:arguments,size:"number"===r?itemSchema.size*r:void 0})}function e(n){return A(n,"uint8")}function t(n){return A(n,"uint16")}function a(n){return A(n,"uint32")}function i(n){return w.array("uint8",n)}function u(n){if(n instanceof w)return n;var r=Object.keys(n);return new w({unpack:function(e,t,a){var i=new n.constructor,u=t.$scope;return t.$scope={target:i,offsets:new n.constructor,schema:n},r.forEach(function(r){t.$scope.offsets[r]=a[0],i[r]=w.unpack(n[r],e,t,a)}),t.$scope=u,i},pack:function(e,t,a){var i=t.$scope;t.$scope={target:e,offsets:new n.constructor,schema:n},r.forEach(function(r){t.$scope.offsets[r]=a.length,w.pack(n[r],e[r],t,a)}),t.$scope=i},args:arguments,namespace:"object"})}function c(n,r){var e=Object.keys(n);return new w({unpack:function(t,a,i){var u=i[0],c={};return e.forEach(function(r){i[0]=u,c[r]=w.unpack(n[r],t,a,i)}),i[0]+=r,c},pack:function(t,a,i){var u=new ArrayBuffer(r),c=new Uint8Array(u);e.forEach(function(r){if("undefined"!=typeof t[r]){var e=[];w.pack(n[r],t[r],a,e),c.set(e)}}),[].push.apply(i,c)},size:r,args:arguments,namespace:"union"})}function o(n,r){if(r=w.from(r),n instanceof Array){var e={};n.forEach(function(n,r){e[n]=r}),n=e}var t=Object.keys(n);return new w({unpack:function(e,a,i){var u,c=w.unpack(r,e,a,i);return t.every(function(r){return n[r]===c?(u=r,!1):!0}),u||c},pack:function(e,a,i){if("number"==typeof e)return void w.pack(r,e,a,i);if(t.every(function(t){return t===e?(w.pack(r,n[t],a,i),!1):!0}))throw new Error('Not find enum "'+e+'".')},namespace:"enums",args:arguments})}function f(n){return/[\u0080-\uffff]/.test(n)?unescape(encodeURIComponent(n)):n}function s(n){return/[\u00c0-\u00df][\u0080-\u00bf]/.test(n)||/[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/.test(n)?decodeURIComponent(escape(n)):n}function p(n,r){return r.browser||"undefined"==typeof Buffer?f(n).split("").map(function(n){return n.charCodeAt()}):new Buffer(n,r.encoding)}function y(n){var r=w.array("uint8",n);return new w({unpack:function(n,e,t){var a=w.unpack(r,n,e,t);return e.browser||"undefined"==typeof Buffer?s(String.fromCharCode.apply(String,a)):new Buffer(a).toString(e.encoding)},pack:function(n,e,t){w.pack(r,p(n,e),e,t)},namespace:"string",args:arguments})}function g(n){return new w({unpack:function(r,e,t){var a;a=null===n?new Uint8Array(r,t[0]):w.unpack(w.bytes(n),r,e,t);for(var i=0;a[i];)i++;var u=w.unpack(w.string(i),a,e);return null===n&&(t[0]+=i+1),u},pack:function(r,e,t){var a=[0];[].unshift.apply(a,w.stringBytes(r,e)),w.pack(w.bytes(n),a,e,t)},namespace:"cstring",args:arguments})}function l(n,r){return new w({unpack:function(e,t,a){var i=t.$scope.target[n];return w.unpack(r(i),e,t,a)},pack:function(e,t,a){var i=t.$scope.target[n];w.pack(r(i),e,t,a)},namespace:"depend",args:arguments})}function v(n,r){return B(n,w.array(r))}function m(n,r,e,t){var a=w.bytes(t);return new w({unpack:function(n,t,i){var u=r(w.unpack(a,n,t,i));return w.unpack(e,u,t)},pack:function(r,t,i){var u=n(w.pack(e,r,t));w.pack(a,u,t,i)},namespace:"parse",args:arguments})}function h(n,r){return new w({unpack:function(){return/string|number/.test(typeof n)?n+r:"function"==typeof n?n(r):r},pack:function(){},args:arguments,namespace:"virtual"})}function k(n,r){return new w({unpack:function(e,t,a){var i=t.$scope.target[n],u=w.from(r);return w.unpack(w.array(u,i),e,t,a)},pack:function(e,t,a){for(var i=t.$scope.schema[n],u=w.from(r),c=w.pack(i,e.length,t),o=t.$scope.offsets[n],f=0;f*/ - /** - * 对字符串进行 utf8 编码 - * - * param {string} str 原始字符串 - */ - function encodeUTF8(str) { - if (/[\u0080-\uffff]/.test(str)) { - return unescape(encodeURIComponent(str)); - } - return str; - } - /** - * 对 utf8 字符串进行解码 - * - * @param {string} str 编码字符串 - */ - function decodeUTF8(str) { - if (/[\u00c0-\u00df][\u0080-\u00bf]/.test(str) || - /[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/.test(str)) { - return decodeURIComponent(escape(str)); - } - return str; - } + + /**/ + var jstrs = require('jstrs'); + var encodeUTF8 = jstrs.encodeUTF8; + var decodeUTF8 = jstrs.decodeUTF8; + /**/ + /** * 将字符串转换为字节数组 *