最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

node.js中的buffer.slice方法使用說(shuō)明

 更新時(shí)間:2014年12月10日 10:01:17   投稿:junjie  
這篇文章主要介紹了node.js中的buffer.slice方法使用說(shuō)明,本文介紹了buffer.slice的方法說(shuō)明、語(yǔ)法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下

方法說(shuō)明:

返回一個(gè)新的buffer對(duì)象,這個(gè)新buffer和老buffer公用一個(gè)內(nèi)存。

但是被start和end索引偏移縮減了。(比如,一個(gè)buffer里有1到10個(gè)字節(jié),我們只想要4-8個(gè)字節(jié),就可以用這個(gè)函數(shù)buf.slice(4,8),因?yàn)樗麄児灿靡粋€(gè)內(nèi)存,所以不會(huì)消耗內(nèi)存,)

因?yàn)楣灿脙?nèi)存,所以修改新的buffer后,老buffer的內(nèi)容同樣也會(huì)被修改。

語(yǔ)法:

復(fù)制代碼 代碼如下:

buffer.slice([start], [end])

接收參數(shù):

start      開(kāi)始位置,默認(rèn)

end      結(jié)束位置,默認(rèn)為buffer長(zhǎng)度

例子:

用ASCII碼字母表創(chuàng)建一個(gè)buffer,用一下slice函數(shù),然后修改原buffer中的一個(gè)字節(jié)。

復(fù)制代碼 代碼如下:

var buf1 = new Buffer(26);
for (var i = 0 ; i < 26 ; i++) {
  buf1[i] = i + 97; // 97 is ASCII a
}
var buf2 = buf1.slice(0, 3);
console.log(buf2.toString('ascii', 0, buf2.length));
buf1[0] = 33;
console.log(buf2.toString('ascii', 0, buf2.length));
// abc
// !bc

源碼:

復(fù)制代碼 代碼如下:

// TODO(trevnorris): currently works like Array.prototype.slice(), which
// doesn't follow the new standard for throwing on out of range indexes.
Buffer.prototype.slice = function(start, end) {
  var len = this.length;
  start = ~~start;
  end = util.isUndefined(end) ? len : ~~end;
  if (start < 0) {
    start += len;
    if (start < 0)
      start = 0;
  } else if (start > len) {
    start = len;
  }
  if (end < 0) {
    end += len;
    if (end < 0)
      end = 0;
  } else if (end > len) {
    end = len;
  }
  if (end < start)
    end = start;
  var buf = new NativeBuffer();
  sliceOnto(this, buf, start, end);
  buf.length = end - start;
  if (buf.length > 0)
    buf.parent = util.isUndefined(this.parent) ? this : this.parent;
  return buf;
};

相關(guān)文章

最新評(píng)論

景泰县| 会理县| 涪陵区| 德格县| 海丰县| 阳江市| 化州市| 孟州市| 锡林郭勒盟| 丰县| 大姚县| 抚远县| 洞口县| 天峨县| 司法| 沽源县| 义乌市| 北流市| 仲巴县| 利辛县| 武川县| 泽州县| 麻阳| 宁城县| 尉氏县| 南靖县| 利川市| 呼玛县| 龙胜| 麻城市| 九江县| 通许县| 平乡县| 乌鲁木齐县| 临漳县| 田阳县| 郎溪县| 盐津县| 雷波县| 抚远县| 永登县|