ByteString.prototype.split = function(delimiters, options) { var options = options || {}, count = options.count === undefined ? -1 : options.count, includeDelimiter = options.includeDelimiter || false; // standardize delimiters into an array of ByteStrings: if (!Array.isArray(delimiters)) delimiters = [delimiters]; delimiters = delimiters.map(function(delimiter) { if (typeof delimiter === "number") delimiter = [delimiter]; return new ByteString(delimiter); }); var components = [], startOffset = this._offset, currentOffset = this._offset; // loop until there's no more bytes to consume bytes_loop : while (currentOffset < this._offset + this._length) { // try each delimiter until we find a match delimiters_loop : for (var i = 0; i < delimiters.length; i++) { var d = delimiters[i]; for (var j = 0; j < d._length; j++) { // reached the end of the bytes, OR bytes not equal if (currentOffset + j > this._offset + this._length || B_GET(this._bytes, currentOffset + j) !== B_GET(d._bytes, d._offset + j)) { continue delimiters_loop; } } // push the part before the delimiter components.push(new ByteString(this._bytes, startOffset, currentOffset - startOffset)); // optionally push the delimiter if (includeDelimiter) components.push(new ByteString(this._bytes, currentOffset, d._length)) // reset the offsets startOffset = currentOffset = currentOffset + d._length; continue bytes_loop; } // if there was no match, increment currentOffset to try the next one currentOffset++; } // push the remaining part, if any if (currentOffset > startOffset) components.push(new ByteString(this._bytes, startOffset, currentOffset - startOffset)); return components; };
ByteArray.prototype.reverse = function() { // "limit" is halfway, rounded down. "top" is the last index. var limit = Math.floor(this._length/2) + this._offset, top = this._length - 1; // swap each pair of bytes, up to the halfway point for (var i = this._offset; i < limit; i++) { var tmp = B_GET(this._bytes, i); B_SET(this._bytes, i, B_GET(this._bytes, top - i)); B_SET(this._bytes, top - i, tmp); } return this; };
Binary.prototype.get = function(offset) { if (offset < 0 || offset >= this._length) return NaN; //var b = this._bytes[this._offset + offset]; //return (b >= 0) ? b : -1 * ((b ^ 0xFF) + 1); return B_GET(this._bytes, this._offset + offset) };
ByteArray.prototype.pop = function() { if (this._length === 0) return undefined; this._length--; return B_GET(this._bytes, this._offset + this._length); };
ByteArray.prototype.shift = function() { if (this._length === 0) return undefined; this._length--; this._offset++; return B_GET(this._bytes, this._offset - 1); };