<commit_before>/**
 * Copyright 2014 Mozilla Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

module Shumway.ArrayUtilities {
  import notImplemented = Shumway.Debug.notImplemented;

  import utf8decode = Shumway.StringUtilities.utf8decode;
  import utf8encode = Shumway.StringUtilities.utf8encode;
  import clamp = Shumway.NumberUtilities.clamp;
  import swap16 = Shumway.IntegerUtilities.swap16;
  import swap32 = Shumway.IntegerUtilities.swap32;
  import floatToInt32 = Shumway.IntegerUtilities.floatToInt32;
  import int32ToFloat = Shumway.IntegerUtilities.int32ToFloat;

  function throwEOFError() {
    notImplemented("throwEOFError");
    // Runtime.throwErrorFromVM(AVM2.currentDomain(), "flash.errors.EOFError", "End of file was encountered.");
  }

  function throwRangeError() {
    notImplemented("throwRangeError");
    // var error = Errors.ParamRangeError;
    // Runtime.throwErrorFromVM("RangeError", getErrorMessage(error.code), error.code);
  }

  function throwCompressedDataError() {
    notImplemented("throwCompressedDataError");
//    var error = Errors.CompressedDataError;
//    Runtime.throwErrorFromVM("CompressedDataError", getErrorMessage(error.code), error.code);
  }

  function checkRange(x: number, min: number, max: number) {
    if (x !== clamp(x, min, max)) {
      throwRangeError();
    }
  }

  function asCoerceString(x): string {
    if (typeof x === "string") {
      return x;
    } else if (x == undefined) {
      return null;
    }
    return x + '';
  }

  export interface IDataInput {
    readBytes: (bytes: DataBuffer, offset?: number /*uint*/, length?: number /*uint*/) => void;
    readBoolean: () => boolean;
    readByte: () => number /*int*/;
    readUnsignedByte: () => number /*uint*/;
    readShort: () => number /*int*/;
    readUnsignedShort: () => number /*uint*/;
    readInt: () => number /*int*/;
    readUnsignedInt: () => number /*uint*/;
    readFloat: () => number;
    readDouble: () => number;
    readMultiByte: (length: number /*uint*/, charSet: string) => string;
    readUTF: () => string;
    readUTFBytes: (length: number /*uint*/) => string;
    bytesAvailable: number /*uint*/;
    objectEncoding: number /*uint*/;
    endian: string;
  }

  export interface IDataOutput {
    writeBytes: (bytes: DataBuffer, offset?: number /*uint*/, length?: number /*uint*/) => void;
    writeBoolean: (value: boolean) => void;
    writeByte: (value: number /*int*/) => void;
    writeShort: (value: number /*int*/) => void;
    writeInt: (value: number /*int*/) => void;
    writeUnsignedInt: (value: number /*uint*/) => void;
    writeFloat: (value: number) => void;
    writeDouble: (value: number) => void;
    writeMultiByte: (value: string, charSet: string) => void;
    writeUTF: (value: string) => void;
    writeUTFBytes: (value: string) => void;
    objectEncoding: number /*uint*/;
    endian: string;
  }

  export class DataBuffer implements IDataInput, IDataOutput {
    private static _nativeLittleEndian = new Int8Array(new Int32Array([1]).buffer)[0] === 1;

    /* The initial size of the backing, in bytes. Doubled every OOM. */
    private static INITIAL_SIZE = 128;

    private _buffer: ArrayBuffer;
    private _length: number;
    private _position: number;
    private _littleEndian: boolean;
    private _objectEncoding: number;
    private _i8View: Int8Array;
    private _u8View: Uint8Array;
    private _i32View: Int32Array;
    private _dataView: DataView;

    private _bitBuffer: number;
    private _bitLength: number;

    constructor() {
      this._buffer = new ArrayBuffer(DataBuffer.INITIAL_SIZE);
      this._length = 0;
      this._position = 0;
      this._cacheViews();
      this._littleEndian = false; // AS3 is bigEndian by default.
      this._bitBuffer = 0;
      this._bitLength = 0;
    }

    static FromArrayBuffer(buffer: ArrayBuffer, length: number = -1) : DataBuffer {
      var dataBuffer: DataBuffer = Object.create(DataBuffer.prototype);
      dataBuffer._buffer = buffer;
      dataBuffer._length = length === -1 ? buffer.byteLength : length;
      dataBuffer._position = 0;
      dataBuffer._cacheViews();
      dataBuffer._littleEndian = false; // AS3 is bigEndian by default.
      dataBuffer._bitBuffer = 0;<commit_msg>Enable explicitly setting a DataBuffer's initial size<commit_after>/**
 * Copyright 2014 Mozilla Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

module Shumway.ArrayUtilities {
  import notImplemented = Shumway.Debug.notImplemented;

  import utf8decode = Shumway.StringUtilities.utf8decode;
  import utf8encode = Shumway.StringUtilities.utf8encode;
  import clamp = Shumway.NumberUtilities.clamp;
  import swap16 = Shumway.IntegerUtilities.swap16;
  import swap32 = Shumway.IntegerUtilities.swap32;
  import floatToInt32 = Shumway.IntegerUtilities.floatToInt32;
  import int32ToFloat = Shumway.IntegerUtilities.int32ToFloat;

  function throwEOFError() {
    notImplemented("throwEOFError");
    // Runtime.throwErrorFromVM(AVM2.currentDomain(), "flash.errors.EOFError", "End of file was encountered.");
  }

  function throwRangeError() {
    notImplemented("throwRangeError");
    // var error = Errors.ParamRangeError;
    // Runtime.throwErrorFromVM("RangeError", getErrorMessage(error.code), error.code);
  }

  function throwCompressedDataError() {
    notImplemented("throwCompressedDataError");
//    var error = Errors.CompressedDataError;
//    Runtime.throwErrorFromVM("CompressedDataError", getErrorMessage(error.code), error.code);
  }

  function checkRange(x: number, min: number, max: number) {
    if (x !== clamp(x, min, max)) {
      throwRangeError();
    }
  }

  function asCoerceString(x): string {
    if (typeof x === "string") {
      return x;
    } else if (x == undefined) {
      return null;
    }
    return x + '';
  }

  export interface IDataInput {
    readBytes: (bytes: DataBuffer, offset?: number /*uint*/, length?: number /*uint*/) => void;
    readBoolean: () => boolean;
    readByte: () => number /*int*/;
    readUnsignedByte: () => number /*uint*/;
    readShort: () => number /*int*/;
    readUnsignedShort: () => number /*uint*/;
    readInt: () => number /*int*/;
    readUnsignedInt: () => number /*uint*/;
    readFloat: () => number;
    readDouble: () => number;
    readMultiByte: (length: number /*uint*/, charSet: string) => string;
    readUTF: () => string;
    readUTFBytes: (length: number /*uint*/) => string;
    bytesAvailable: number /*uint*/;
    objectEncoding: number /*uint*/;
    endian: string;
  }

  export interface IDataOutput {
    writeBytes: (bytes: DataBuffer, offset?: number /*uint*/, length?: number /*uint*/) => void;
    writeBoolean: (value: boolean) => void;
    writeByte: (value: number /*int*/) => void;
    writeShort: (value: number /*int*/) => void;
    writeInt: (value: number /*int*/) => void;
    writeUnsignedInt: (value: number /*uint*/) => void;
    writeFloat: (value: number) => void;
    writeDouble: (value: number) => void;
    writeMultiByte: (value: string, charSet: string) => void;
    writeUTF: (value: string) => void;
    writeUTFBytes: (value: string) => void;
    objectEncoding: number /*uint*/;
    endian: string;
  }

  export class DataBuffer implements IDataInput, IDataOutput {
    private static _nativeLittleEndian = new Int8Array(new Int32Array([1]).buffer)[0] === 1;

    /* The initial size of the backing, in bytes. Doubled every OOM. */
    private static INITIAL_SIZE = 128;

    private _buffer: ArrayBuffer;
    private _length: number;
    private _position: number;
    private _littleEndian: boolean;
    private _objectEncoding: number;
    private _i8View: Int8Array;
    private _u8View: Uint8Array;
    private _i32View: Int32Array;
    private _dataView: DataView;

    private _bitBuffer: number;
    private _bitLength: number;

    constructor(initialSize: number = DataBuffer.INITIAL_SIZE) {
      this._buffer = new ArrayBuffer(initialSize);
      this._length = 0;
      this._position = 0;
      this._cacheViews();
      this._littleEndian = false; // AS3 is bigEndian by default.
      this._bitBuffer = 0;
      this._bitLength = 0;
    }

    static FromArrayBuffer(buffer: ArrayBuffer, length: number = -1) : DataBuffer {
      var dataBuffer: DataBuffer = Object.create(DataBuffer.prototype);
      dataBuffer._buffer = buffer;
      dataBuffer._length = length === -1 ? buffer.byteLength : length;
      dataBuffer._position = 0;
      dataBuffer._cacheViews();
      dataBuffer._littleEndian = false; // AS3 is bigEndian by default.
      dataBuffer._bitBuffer = 0;
{'fraction_non_alphanumeric': 0.1250390421655388, 'fraction_numerical': 0.01166059344091619, 'mean_word_length': 3.535410764872521, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '761281', 'n_tokens_mistral': 2898, 'n_tokens_neox': 2815, 'n_words': 809}