FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Added an unpacked size getter function. by redboltz · Pull Request #3 · msgpack/msgpack-javascript · GitHub

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .htm  (1) .js  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
8 changes: 7 additions & 1 deletion msgpack.codec.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ globalScope.msgpack = {
// toString:Boolean = false):ByteArray/ByteString/false
// [1][mix to String] msgpack.pack({}, true) -> "..."
// [2][mix to ByteArray] msgpack.pack({}) -> [...]
unpack: msgpackunpack // msgpack.unpack(data:BinaryString/ByteArray):Mix
unpack: msgpackunpack, // msgpack.unpack(data:BinaryString/ByteArray):Mix
// [1][String to mix] msgpack.unpack("...") -> {}
// [2][ByteArray to mix] msgpack.unpack([...]) -> {}
unpackedLength: msgpackunpackedLength // msgpack.unpackedLength() -> Number
};

var _bin2num = {}, // BinaryStringToNumber { "\00": 0, ... "\ff": 255 }
Expand Down Expand Up @@ -55,6 +56,11 @@ function msgpackunpack(data) { // @param BinaryString/ByteArray:
return decode(); // mix or undefined
}

// msgpack.unpackedLength
function msgpackunpackedLength() { // @return Number: unpacked length
return _idx + 1;
}

// inner - encoder
function encode(rv, // @param ByteArray: result
mix, // @param Mix: source data
Expand Down
8 changes: 7 additions & 1 deletion msgpack.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ globalScope.msgpack = {
// [2][ByteArray to mix] msgpack.unpack([...]) -> {}
worker: "msgpack.js", // msgpack.worker - WebWorkers script filename
upload: msgpackupload, // msgpack.upload(url:String, option:Hash, callback:Function)
download: msgpackdownload // msgpack.download(url:String, option:Hash, callback:Function)
download: msgpackdownload,// msgpack.download(url:String, option:Hash, callback:Function)
unpackedLength: msgpackunpackedLength // msgpack.unpackedLength() -> Number
};

var _ie = /MSIE/.test(navigator.userAgent),
Expand Down Expand Up @@ -70,6 +71,11 @@ function msgpackunpack(data) { // @param BinaryString/ByteArray:
return decode(); // mix or undefined
}

// msgpack.unpackedLength
function msgpackunpackedLength() { // @return Number: unpacked length
return _idx + 1;
}

// inner - encoder
function encode(rv, // @param ByteArray: result
mix, // @param Mix: source data
Expand Down
69 changes: 69 additions & 0 deletions test/unpacked_length.htm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<!DOCTYPE html><html lang="ja">
<head>
<meta charset="UTF-8" />
<title>msgpack unpacked length test</title>
<style>
.uutest ol { background-color:dodgerblue;color:white }
.uutest li { padding:5px;border:1px solid #ccc }
.uutest .li0 { background-color:red }
.uutest .li1 { background-color:green }
.uutest .li2 { background-color:red }
.uutest .li3 { background-color:#0a0 }
.uutestinfo { position:fixed;top:10px;right:10px;font-size:xx-large }
.uutestinfo a { border: 3px outset gray;padding:3px;background-color:red;color:white }
.uutestinfo .score { color:black;text-shadow:gray 1px 1px 1px }
.uutestinfo .ngzone {}
</style>
<script src="../msgpack.js"></script>
<script src="uupaa.js"></script>
<script>
uu.test = function(hash) {
var r, i;

for (i in hash) {
v = hash[i];
if (v === "") { // caption
uu.ok(i);
} else {
r = v();
uu.ok(i, r[0], r[1], r[2]);
}
}
uu.ok();
};
uu.ready(function(uu) {

uu.test({
"Get unpacked length": "",
"get unpacked length": function() {
var pack = msgpack.pack(12345);
var pl = pack.length;
var rv = msgpack.unpack(pack);
var ul = msgpack.unpackedLength();
return [ul, "==", pl];
},
"multiple unpack": function() {
var pack1 = msgpack.pack(1);
var pack2 = msgpack.pack(65535);
var pack3 = msgpack.pack(65536);
var pack = pack1.concat(pack2).concat(pack3);
var pl1 = pack1.length;
var pl2 = pack2.length;
var pl3 = pack3.length;

var rv1 = msgpack.unpack(pack);
var ul1 = msgpack.unpackedLength();
pack = pack.slice(ul1, pack.length);
var rv2 = msgpack.unpack(pack);
var ul2 = msgpack.unpackedLength();
pack = pack.slice(ul2, pack.length);
var rv3 = msgpack.unpack(pack);
var ul3 = msgpack.unpackedLength();
pack = pack.slice(ul3, pack.length);
var rest = pack.length;
return [[ul1, ul2, ul3, rest], "==", [pl1, pl2, pl3, 0]];
},
});
});
</script>
</head><body></body></html>

Back | FazBrowse Home | New Git URL