Skip to content

Commit efdc761

Browse files
committed
Rebuild
1 parent 95ca102 commit efdc761

2 files changed

Lines changed: 23 additions & 15 deletions

File tree

dist/binarypack.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! binarypack.js build:0.0.6, development. Copyright(c) 2012 Eric Zhang <eric@ericzhang.com> MIT Licensed */
1+
/*! binarypack.js build:0.0.7, development. Copyright(c) 2012 Eric Zhang <eric@ericzhang.com> MIT Licensed */
22
(function(exports){
33
var binaryFeatures = {};
44
binaryFeatures.useBlobBuilder = (function(){
@@ -62,11 +62,11 @@ exports.BinaryPack = {
6262
unpack: function(data){
6363
var unpacker = new Unpacker(data);
6464
return unpacker.unpack();
65-
},
66-
pack: function(data, utf8){
67-
var packer = new Packer(utf8);
65+
},
66+
pack: function(data){
67+
var packer = new Packer();
6868
packer.pack(data);
69-
var buffer = packer.getBuffer();
69+
var buffer = packer.getBuffer();
7070
return buffer;
7171
}
7272
};
@@ -309,8 +309,7 @@ Unpacker.prototype.read = function(length){
309309
}
310310
}
311311

312-
function Packer(utf8){
313-
this.utf8 = utf8;
312+
function Packer(){
314313
this.bufferBuilder = new BufferBuilder();
315314
}
316315

@@ -392,13 +391,8 @@ Packer.prototype.pack_bin = function(blob){
392391
}
393392

394393
Packer.prototype.pack_string = function(str){
395-
var length;
396-
if (this.utf8) {
397-
var blob = new Blob([str]);
398-
length = blob.size;
399-
} else {
400-
length = str.length;
401-
}
394+
var length = utf8Length(str);
395+
402396
if (length <= 0x0f){
403397
this.pack_uint8(0xb0 + length);
404398
} else if (length <= 0xffff){
@@ -562,5 +556,19 @@ Packer.prototype.pack_int64 = function(num){
562556
this.bufferBuilder.append((low & 0x0000ff00) >>> 8);
563557
this.bufferBuilder.append((low & 0x000000ff));
564558
}
559+
560+
function _utf8Replace(m){
561+
var code = m.charCodeAt(0);
562+
563+
if(code <= 0x7ff) return '00';
564+
if(code <= 0xffff) return '000';
565+
if(code <= 0x1fffff) return '0000';
566+
if(code <= 0x3ffffff) return '00000';
567+
return '000000';
568+
}
569+
570+
function utf8Length(str){
571+
return str.replace(/[^\u0000-\u007F]/g, _utf8Replace).length;
572+
}
565573

566574
})(this);

0 commit comments

Comments
 (0)