Binary/F
From CommonJS Spec Wiki
< Binary
STATUS: PROPOSAL
- Implementations
- [[Implementations/TeaJS|]]
This proposal, based on Binary/E and Binary/F/Wes, defines a single, mutable, fixed-length, numeric byte storage type.
Contents
Specification
The "common/buffer" module exports a "Buffer" type that meets the following criteria.
A Buffer is a mutable, fixed-length, representation of a C unsigned char (byte) array.
Constructor
- A Buffer function must exist.
- Calling and constructing a Buffer both return a new Buffer instance.
- Buffers are instances of Buffer and Object.
- The type of a Buffer is "object".
- Any call or construction of Buffer that does not have a Number, Buffer, Array, or String as its first argument throws a TypeError.
- Buffer(length Number, Number(fill_opt))
- New Buffer of the given length, with the given fill number at all offsets.
- The default filler is 0.
- Buffer(source Buffer, Number(start_opt), Number(stop_opt), Boolean(copy_opt))
- New Buffer that is either a copy or a view of the allocation as the given buffer, from "start" to "stop".
- "start" is 0 if undefined or omitted.
- "stop" is the other buffer's length if undefined or omitted.
- "start" and "stop" may both be negative numbers, in which case they correspond to offsets counting from the length of the buffer.
- "copy" is true if undefined or omitted
- When true, "copy" indicates that the buffer must have its own copy of the allocation.
- When false, "copy" indicates that the buffer must share its allocation with the given buffer.
- Buffer(source Array, Number(start_opt), Number(stop_opt))
- New Buffer that contains the respective values from the given array, from "start" to "stop".
- "start" is 0 if undefined or omitted.
- "stop" is the array's "length" if undefined or omitted.
- The "length" of the new buffer is exactly the length from "start" to "stop".
- "start" and "stop" may both be negative numbers, in which case they correspond to offsets counting from the length of the array.
- Buffer(string String, String(charset))
- Create a buffer from a string, the result being encoded with "charset".
Prototype Properties
- toString() String
- Returns a debug representation like "[Buffer 10]", where 10 is the length this Buffer.
- This signature of "toString" applies if the "arguments.length" is 0.
- toString(String(charset), Number(start_opt), Number(stop_opt)) String
- Decodes this between "start" and "stop" according to the given character set and returns the String from the corresponding unicode points.
- "start" is 0 if undefined or omitted.
- "stop" is the length of this buffer if undefined or omitted.
- "start" and "stop" may be negative numbers, in which case they correspond to an offset counting from this buffer's length.
- Must throw a RangeError if the buffer is malformed, undefined, out of range, or incomplete for the given character set.
- "charset" must be one of "UTF-8", "UTF-16", "ISO-8859-1", "ASCII", "BINARY". Any other "charset" throws a RangeError.
- "ASCII" must properly transcode all 7-bit ASCII code points..
- Note: "ASCII" is not required to properly transcode any code points outside its seven bit domain. "ASCII" may be an alias of "UTF-8" or "ISO-8859-1". For highest performance, "ASCII" may opt to ignore bits outside its domain.
- "BINARY" must properly transcode all 8-bit ISO-8859-1 code points.
- Note: "BINARY" is not required to properly transcode any code points outside its eight bit domain. "BINARY" may be an alias of "ISO-8859-1". For highest performance, "BINARY" may opt to ignore bits outside its domain.
- This signature of "toString" applies if the "arguments.length" is not 0.
- toSource() String
- Returns "require("binary").Buffer([])" for a null buffer or "require("binary").Buffer([0, 1, 2])" for a buffer of length 3 with bytes 0, 1, and 2.
- range(Number(start_opt), Number(stop_opt)) Buffer
- Returns a Buffer that views the given range of the same allocated byte buffer.
- "start" is 0 if undefined or omitted.
- "stop" is the length of this buffer if undefined or omitted.
- "start" and "stop" may be negative numbers, in which case they correspond to an offset counting from this buffer's length.
- The length of "range" is 2.
- slice(Number(start_opt), Number(stop_opt)) Buffer
- Returns a Buffer with a new internal allocation, containing a copy of this buffer from "start" to "stop" offsets.
- "start" is 0 if undefined or omitted.
- "stop" is this buffer's length if undefined or omitted.
- "start" and "stop" may be negative numbers, in which case they correspond to an offset counting from this buffer's length.
- copy(target, Number(targetStart_opt), Number(start_opt)), Number(stop_opt)) Buffer
- Copies the Number values of each byte from this between "start" and "stop" to a "target" Buffer or Array at the "targetStart" offset.
- "stop" is the length of this if undefined or omitted.
- "targetStart", "start", and "stop" are internally coerced to Numbers with the Number constructor.
- "targetStart" may be negative, in which case it corresponds to an offset counting from the length of the target.
- "start" and "stop" may be negative, in which case they correspond to an offset counting from this buffer's length.
- Throws a TypeError if the target is not a Buffer or Array.
- Returns this.
- copyFrom(source, Number(sourceStart_opt), Number(start_opt), Number(stop_opt)) Buffer
- Copies the Number values of each byte from a given Buffer or Array into this from "start" to "stop" from the given "sourceStart" offset.
- "start" is 0 if undefined or omitted.
- "stop" is the length of this buffer if undefined or omitted.
- "sourceStart" may be negative, in which case it corresponds to an offset counting from this buffer's length.
- "start" and "stop" may be negative, in which case they correspond to offsets counting from the length of the source.
- "sourceStart", "start", and "stop" are internally coerced to Numbers with the Number constructor.
- If the source is an Array, all values of that array are coerced to Numbers and masked with 0xFF.
- Throws a TypeError if the target is not a Buffer or Array.
- Returns this.
- read(String(charset), state Object, Number(start_opt), Number(stop_opt)) String
- Decodes as many complete and well formed character codes as possible between "start" and "stop" to a String from the given character set.
- "charset" must be one of "UTF-8", "UTF-16", "ISO-8859-1", "ASCII", "BINARY". Any other "charset" throws a RangeError.
- Assumes that the given byte range begins at the beginning of a code point.
- Stops decoding after the last complete code point byte sequence.
- Returns the decoded String.
- Sets the "bytes" property of the "state" object to the number of bytes transcoded.
- Sets the "characters" property of the "state" object to the number of characters transcoded.
- Sets the "error" property of the "state" object to "malformed" if reading stops at the beginning of a malformed byte sequence.
- The actual stop offset must be either equal to "stop" or at the beginning of the first incomplete code point byte sequence.
- write(source String, String(charset), state Object, Number(start_opt), Number(stop_opt)) Number
- Encodes as much as possible of a String in a given character set into this buffer from "source" to "stop", using the source string, and returns the actual stop index of the source string.
- "charset" must be one of "UTF-8", "UTF-16", "ISO-8859-1", "ASCII", "BINARY". Any other "charset" throws a RangeError.
- "start" is 0 if undefined or omitted and measures bytes.
- "stop" is this buffer's length if undefined or omitted, measured in bytes.
- "start" and "stop" may be negative, in which case they correspond to offsets counting from the length of the source.
- Sets the "characters" property of the "state" object to the number of characters encoded.
- Sets the "bytes" property of the "state" object to the number of bytes written.
- Returns the number of bytes written.
- fill(Number(value), Number(start_opt), Number(stop_opt)) Buffer
- Fills all of this buffer's bytes with the given value as a Number from the "start" offset to the "stop" offset.
- "start" is 0 if undefined or omitted.
- "stop" is the length of this buffer if undefined or omitted.
- "start" and "stop" may be negative, in which case they correspond to offsets counting from the length of this buffer.
- "value" is 0 if undefined or omitted.
- "value" is coerced to Number and masked with 0xFF.
- Returns this.
- Content
- an alias of Number, indicating that Number is the type of what [[Get]] returns.
Internal Properties
- [[Get]] Number
- Returns the Number value of a the byte at the given offset.
- [[Put]] Number
- Sets the byte value at the given offset.
- Throws a RangeError if the index is beyond the current bounds of the buffer.
- Implicitly coerces the value to a Number and masks it with 0xFF.
Instance Properties
- length
- The length in bytes.
- Not [[Configurable]], not [[Enumerable]]. [[ReadOnly]].
Notation
- The notation functionName(argument Type) indicates that the given behavior only occurs when the argument pattern matches the given "Type".
- If the functionName(argument Type) is mentioned for an argument and none of the given forms match the types of the argument, the function must throw a TypeError.
- The notation functionName(argument_opt) indicates that the argument may be undefined or omitted.
- The distinction between an omitted or undefined argument is only detectable by checking the
arguments.length
- The notation function(Type(argument)) implies that any type matches the argument and that it must be internally coerced to the "Type".
- Extra arguments are ignored.
General Requirements
- All of the properties specified on prototypes are not [[Enumerable]] on instances.
- All operations that copy values to a byte in the byte buffer implicitly coerce the value to a Number and bit mask such values with 0xFF.