日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

js_long.php,protobuf.js 与 Long.js的使用详解

發布時間:2024/9/27 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 js_long.php,protobuf.js 与 Long.js的使用详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這次給大家帶來protobuf.js 與 Long.js的使用詳解,是急用protobuf.js 與 Long.js的注意事項有哪些,下面就是實戰案例,一起來看一下。

protobuf.js的結構和webpack的加載之后的結構很相似。這樣的模塊化組合是個不錯的結構方式。1個是適應了不同的加載方式,2個模塊直接很獨立。webpack的功能更全一點。但如果自己封裝js庫這樣夠用了。而且模塊對外統一接口 module.exports。這和node很像。(function(global, undefined) { "use strict";

(function prelude(modules, cache, entries) { function $require(name) { var $module = cache[name]; //沒有就去加載

if (!$module)

modules[name][0].call($module = cache[name] = { exports: {} }, $require, $module, $module.exports); return $module.exports;

} //曝光成全局

var proto = global.proto = $require(entries[0]); // AMD

if (typeof define === "function" && define.amd) {

define(["long"], function(Long) { if (Long && Long.isLong) {

proto.util.Long = Long;

proto.configure();

}

}); return proto;

} //CommonJS

if (typeof module === "object" && module && module.exports)

module.exports = proto;

}) //傳參 ({ 1: [function (require, module, exports) { function first() {

console.log("first");

}

module.exports = first;

}, {}], 2: [function(require, module, exports) { function second() {

console.log("second");

}

module.exports = second;

}], 3: [function (require, module, exports) { var proto = {};

proto.first = require(1);

proto.second = require(2);

proto.build = "full";

module.exports = proto;

}]

}, {}, [3]);

})(typeof window==="object"&&window||typeof self==="object"&&self||this)

在處理超過16位的整形就得使用Long.js了。 主要是fromString和toString。function fromString(str, unsigned, radix) { if (str.length === 0) throw Error('empty string'); if (str === "NaN" || str === "Infinity" || str === "+Infinity" || str === "-Infinity") return ZERO; if (typeof unsigned === 'number') { // For goog.math.long compatibility

radix = unsigned,

unsigned = false;

} else {

unsigned = !!unsigned;

}

radix = radix || 10; if (radix < 2 || 36 < radix) throw RangeError('radix'); var p; if ((p = str.indexOf('-')) > 0) throw Error('interior hyphen'); else if (p === 0) { return fromString(str.substring(1), unsigned, radix).neg();

} // Do several (8) digits each time through the loop, so as to

// minimize the calls to the very expensive emulated p.

var radixToPower = fromNumber(pow_dbl(radix, 8)); var result = ZERO; for (var i = 0; i < str.length; i += 8) { var size = Math.min(8, str.length - i), value = parseInt(str.substring(i, i + size), radix); if (size < 8) { var power = fromNumber(pow_dbl(radix, size));

result = result.mul(power).add(fromNumber(value));

} else { result = result.mul(radixToPower);

result = result.add(fromNumber(value));

}

}

result.unsigned = unsigned; return result;

}

fromstring的思路是把字符串8位一個截取。然后轉成Long型(高位,地位,符號位) 加起來。最后是一個Long型。 4294967296 是2的32次方。每次操作之前都會有一個基數的操作 mul(radixToPower)或者mul(power)這兩者都是保證result的位數是正確的。

比如{low:123} 和{low:1} 相加之前,先要讓{low:123}乘以10,得到{low:1230}再與{low:1}進行位操作。因為第一個是高位,不能直接相加。

function fromBits(lowBits, highBits, unsigned) { return new Long(lowBits, highBits, unsigned);

}

fromBits 即轉為Long對象。value%4294967296 得到低位。/得到高位。結果通過位移合并起來。mul是bit的乘法,add是bit的加法。 原理是講一個64位的拆成四段。分別16位。this.low左移16位 就得到 low的32-17位是啥。 然后和addend對象的同位相加

最后的合并是通過|運算。位移之后再還原確實很巧妙。一時看上去都不大理解。LongPrototype.add = function add(addend) { if (!isLong(addend))

addend = fromValue(addend); // pide each number into 4 chunks of 16 bits, and then sum the chunks.

var a48 = this.high >>> 16; var a32 = this.high & 0xFFFF; var a16 = this.low >>> 16; var a00 = this.low & 0xFFFF; var b48 = addend.high >>> 16; var b32 = addend.high & 0xFFFF; var b16 = addend.low >>> 16; var b00 = addend.low & 0xFFFF; var c48 = 0, c32 = 0, c16 = 0, c00 = 0;

c00 += a00 + b00;

c16 += c00 >>> 16;

c00 &= 0xFFFF;

c16 += a16 + b16;

c32 += c16 >>> 16;

c16 &= 0xFFFF;

c32 += a32 + b32;

c48 += c32 >>> 16;

c32 &= 0xFFFF;

c48 += a48 + b48;

c48 &= 0xFFFF; return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned);

};

>>>和>>有什么區別??。

toStringLongPrototype.toString = function toString(radix) {

radix = radix || 10; if (radix < 2 || 36 < radix) throw RangeError('radix'); if (this.isZero()) return '0'; if (this.isNegative()) { // Unsigned Longs are never negative

if (this.eq(MIN_VALUE)) { // We need to change the Long value before it can be negated, so we remove

// the bottom-most digit in this base and then recurse to do the rest.

var radixLong = fromNumber(radix),

p = this.p(radixLong),

rem1 = p.mul(radixLong).sub(this); return p.toString(radix) + rem1.toInt().toString(radix);

} else

return '-' + this.neg().toString(radix);

} // Do several (6) digits each time through the loop, so as to

// minimize the calls to the very expensive emulated p.

var radixToPower = fromNumber(pow_dbl(radix, 6), this.unsigned),

rem = this; var result = ''; while (true) { var remp = rem.p(radixToPower),

intval = rem.sub(remp.mul(radixToPower)).toInt() >>> 0,

digits = intval.toString(radix);

rem = remp; if (rem.isZero()) return digits + result; else { while (digits.length < 6)

digits = '0' + digits;

result = '' + digits + result;

}

}

};

也是sub之后拼出來的。也就是fromstring的反向操作。

相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

總結

以上是生活随笔為你收集整理的js_long.php,protobuf.js 与 Long.js的使用详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。