<commit_before>/*!
 * byte - lib/byte.js
 *
 * Copyright(c) 2013 - 2014
 * MIT Licensed
 *
 * Authors:
 *   fengmk2 <<EMAIL>> (http://fengmk2.github.com)
 *   dead-horse <<EMAIL>> (https://github.com/dead-horse)
 */

"use strict";

/**
 * Module dependencies.
 */

var Long = require('long');
var debug = require('debug')('byte');
var numbers = require('./number');
var utility = require('utility');

var DEFAULT_SIZE = 1024;
var BIG_ENDIAN = 1;
var LITTLE_ENDIAN = 2;
var MAX_INT_31 = Math.pow(2, 31);

function ByteBuffer(options) {
  options = options || {};
  this._order = options.order || BIG_ENDIAN;
  this._size = options.size || DEFAULT_SIZE;
  this._offset = 0;
  var array = options.array;
  if (array) {
    this._bytes = array;
  } else {
    this._bytes = new Buffer(this._size);
  }
}

ByteBuffer.BIG_ENDIAN = BIG_ENDIAN;
ByteBuffer.LITTLE_ENDIAN = LITTLE_ENDIAN;

ByteBuffer.allocate = function (capacity) {
  return new ByteBuffer({size: capacity});
};

ByteBuffer.wrap = function (array, offset, length) {
  if (offset) {
    var end = offset + (length || array.length);
    array = array.slice(offset, end);
  }
  return new ByteBuffer({array: array, size: array.length});
};

ByteBuffer.prototype.order = function (order) {
  this._order = order;
  return this;
};

ByteBuffer.prototype._checkSize = function (afterSize) {
  if (this._size >= afterSize) {
    return;
  }
  var old = this._size;
  this._size = afterSize * 2;
  debug('allocate new Buffer: from %d to %d bytes', old, this._size);
  var bytes = new Buffer(this._size);
  this._bytes.copy(bytes, 0);
  this._bytes = bytes;
};

ByteBuffer.prototype.put = function (src, offset, length) {
  // byte
  // bytes, offset, length
  // index, byte
  var index = this._offset;
  if (Buffer.isBuffer(src)) {
    // bytes, offset, length
    offset = offset || 0;
    length = length || src.length;
    this._offset += length;
    this._checkSize(this._offset);
    src.copy(this._bytes, index, offset, offset + length);
  } else if (typeof src === 'number') {
    if (typeof offset === 'number') {
      // index, byte
      index = src;
      src = offset;
    } else {
      // byte
      index = this._offset;
      this._offset++;
      this._checkSize(this._offset);
    }
    this._bytes[index] = src;
  }
  return this;
};

ByteBuffer.prototype.get = function (index, length) {
  if (typeof index !== 'number') {
    index = this._offset++;
  } else if (typeof length === 'number') {
    // offset, length => Buffer
    return this._fastSlice(index, index + length);
  }
  // return byte
  return this._bytes[index];
};

ByteBuffer.prototype.read = function (size) {
  var index = this._offset;
  this._offset += size;
  return this._bytes.slice(index, this._offset);
};

ByteBuffer.prototype.putChar = function (index, value) {
  // char
  // int, char
  if (value === undefined) {
    // char
    value = index;
    index = this._offset;
    this._offset++;
    this._checkSize(this._offset);
  } else {
    // int, char
  }
  this._bytes[index] = typeof value === 'string' ? value.charCodeAt(0) : value;
  return this;
};

ByteBuffer.prototype.getChar = function (index) {
  var b = this.get(index);
  return String.fromCharCode(b);
};

Object.keys(numbers).forEach(function (type) {
  var putMethod = 'put' + type;
  var getMethod = 'get' + type;
  var handles = numbers[type];
  var size = handles.size;

  ByteBuffer.prototype[putMethod] = function (index, value) {
    var offset = 0;
    if (typeof index === 'number' && typeof value === 'number') {
      //index, value
      offset = index;
    } else {
      //value
      offset = this._offset;
      this._offset += size;
      this._checkSize(this._offset);
      value = index;
    }

    var handle = this._order === BIG_ENDIAN
      ? handles.writeBE
      : handles.writeLE;
    this._bytes[handle](value, offset);
  };

  ByteBuffer.prototype[getMethod] = function (index) {
    if (typeof index !== 'number') {
      index = this._offset;
      this._offset += size;
    }

    var handle = this._order === BIG_ENDIAN
      ? handles.readBE
      : handles.readLE;
    return this._bytes[handle](index);
  };
});

ByteBuffer.prototype._putZero = function (index) {
  this._bytes[index] = 0;
  this._bytes[index + 1] = 0;
  this._bytes[index + 2] = 0;
  this._bytes[index + 3] = 0;<commit_msg>return this on putNumber*<commit_after>/*!
 * byte - lib/byte.js
 *
 * Copyright(c) 2013 - 2014
 * MIT Licensed
 *
 * Authors:
 *   fengmk2 <<EMAIL>> (http://fengmk2.github.com)
 *   dead-horse <<EMAIL>> (https://github.com/dead-horse)
 */

"use strict";

/**
 * Module dependencies.
 */

var Long = require('long');
var debug = require('debug')('byte');
var numbers = require('./number');
var utility = require('utility');

var DEFAULT_SIZE = 1024;
var BIG_ENDIAN = 1;
var LITTLE_ENDIAN = 2;
var MAX_INT_31 = Math.pow(2, 31);

function ByteBuffer(options) {
  options = options || {};
  this._order = options.order || BIG_ENDIAN;
  this._size = options.size || DEFAULT_SIZE;
  this._offset = 0;
  var array = options.array;
  if (array) {
    this._bytes = array;
  } else {
    this._bytes = new Buffer(this._size);
  }
}

ByteBuffer.BIG_ENDIAN = BIG_ENDIAN;
ByteBuffer.LITTLE_ENDIAN = LITTLE_ENDIAN;

ByteBuffer.allocate = function (capacity) {
  return new ByteBuffer({size: capacity});
};

ByteBuffer.wrap = function (array, offset, length) {
  if (offset) {
    var end = offset + (length || array.length);
    array = array.slice(offset, end);
  }
  return new ByteBuffer({array: array, size: array.length});
};

ByteBuffer.prototype.order = function (order) {
  this._order = order;
  return this;
};

ByteBuffer.prototype._checkSize = function (afterSize) {
  if (this._size >= afterSize) {
    return;
  }
  var old = this._size;
  this._size = afterSize * 2;
  debug('allocate new Buffer: from %d to %d bytes', old, this._size);
  var bytes = new Buffer(this._size);
  this._bytes.copy(bytes, 0);
  this._bytes = bytes;
};

ByteBuffer.prototype.put = function (src, offset, length) {
  // byte
  // bytes, offset, length
  // index, byte
  var index = this._offset;
  if (Buffer.isBuffer(src)) {
    // bytes, offset, length
    offset = offset || 0;
    length = length || src.length;
    this._offset += length;
    this._checkSize(this._offset);
    src.copy(this._bytes, index, offset, offset + length);
  } else if (typeof src === 'number') {
    if (typeof offset === 'number') {
      // index, byte
      index = src;
      src = offset;
    } else {
      // byte
      index = this._offset;
      this._offset++;
      this._checkSize(this._offset);
    }
    this._bytes[index] = src;
  }
  return this;
};

ByteBuffer.prototype.get = function (index, length) {
  if (typeof index !== 'number') {
    index = this._offset++;
  } else if (typeof length === 'number') {
    // offset, length => Buffer
    return this._fastSlice(index, index + length);
  }
  // return byte
  return this._bytes[index];
};

ByteBuffer.prototype.read = function (size) {
  var index = this._offset;
  this._offset += size;
  return this._bytes.slice(index, this._offset);
};

ByteBuffer.prototype.putChar = function (index, value) {
  // char
  // int, char
  if (value === undefined) {
    // char
    value = index;
    index = this._offset;
    this._offset++;
    this._checkSize(this._offset);
  } else {
    // int, char
  }
  this._bytes[index] = typeof value === 'string' ? value.charCodeAt(0) : value;
  return this;
};

ByteBuffer.prototype.getChar = function (index) {
  var b = this.get(index);
  return String.fromCharCode(b);
};

Object.keys(numbers).forEach(function (type) {
  var putMethod = 'put' + type;
  var getMethod = 'get' + type;
  var handles = numbers[type];
  var size = handles.size;

  ByteBuffer.prototype[putMethod] = function (index, value) {
    var offset = 0;
    if (typeof index === 'number' && typeof value === 'number') {
      //index, value
      offset = index;
    } else {
      //value
      offset = this._offset;
      this._offset += size;
      this._checkSize(this._offset);
      value = index;
    }

    var handle = this._order === BIG_ENDIAN
      ? handles.writeBE
      : handles.writeLE;
    this._bytes[handle](value, offset);
    return this;
  };

  ByteBuffer.prototype[getMethod] = function (index) {
    if (typeof index !== 'number') {
      index = this._offset;
      this._offset += size;
    }

    var handle = this._order === BIG_ENDIAN
      ? handles.readBE
      : handles.readLE;
    return this._bytes[handle](index);
  };
});

ByteBuffer.prototype._putZero = function (index) {
  this._bytes[index] = 0;
  this._bytes[index + 1] = 0;
  this._bytes[index + 2] = 0;
  this._bytes[index + 3] = 0;
{'fraction_non_alphanumeric': 0.15532866811976598, 'fraction_numerical': 0.007800848915911438, 'mean_word_length': 2.977189781021898, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3890452', 'n_tokens_mistral': 2967, 'n_tokens_neox': 2855, 'n_words': 708}