[::T = uint8] does Blob[T] is repr('VMArray') is array_type(T)
A Buf
does the role of a mutable sequence of (usually unsigned) integers.
my = Buf.new(1, 2, 3);[1] = 42;
However, it's a parameterized type, and you can instantiate with several integer types:
my = Buf[int32].new(3, -3, 0xff32, -44);say ; # OUTPUT: «Buf[int32]:0x<03 -3 FF32 -2C>»
By default, Buf
uses 8-bit unsigned integers, that is, it is equivalent to Buf[uint8]. Some other types of Buf
s which are used often get their own class name.
buf8 | Buf[uint8] |
buf16 | Buf[uint16] |
buf32 | Buf[uint32] |
buf64 | Buf[uint64] |
You can use these in pretty much the same way you would with Buf
:
my = buf8.new(3, 6, 254);say ; # OUTPUT: «Buf[uint8]:0x<03 06 fe>»
Plus there are some object methods, like encode
that might return a buf8
in some cases where it is the best representation for a particular encoding.
Methods
method subbuf-rw
method subbuf-rw( = 0, = self.elems - ) is rw
A mutable version of subbuf
that returns a Proxy functioning as a writable reference to a part of a buffer. Its first argument, $from
specifies the index in the buffer from which a substitution should occur, and its last argument, $elems
specifies how many elements are to be replaced.
For example, to replace one element at index 3 with two elements, 100
and 101
:
my Buf .= new(0..5);.subbuf-rw(3,1) = Buf.new(100, 101);say .raku; # OUTPUT: «Buf.new(0,1,2,100,101,4,5)»
In the case the $elems
argument is not specified, the substitution happens at the specified index $from
removing all trailing elements:
my Buf .= new(0..5);.subbuf-rw(3) = Buf.new(200);say .raku; # OUTPUT: «Buf.new(0,1,2,200)»
In the case the $from
argument is not specified, the substitution happens from the very beginning of the buffer:
my Buf .= new(0..5);.subbuf-rw = Buf.new(123, 123);say .raku; # OUTPUT: «Buf.new(123, 123)»
routine subbuf-rw
multi sub subbuf-rw(Buf \b) is rwmulti sub subbuf-rw(Buf \b, Int() ) is rwmulti sub subbuf-rw(Buf \b, , ) is rw
Returns a writable reference to a part of a buffer. Invokes the subbuf-rw
method on the specified Buf
:
my Buf .= new(1,2,3);subbuf-rw(,2,1) = Buf.new(42);say .raku; # OUTPUT: «Buf.new(1,2,42)»
method reallocate
method reallocate(Buf: Int )
Change the number of elements of the Buf
, returning the changed Buf
. The size of Buf
will be adapted depending on the number of $elems
specified: if it is smaller than the actual size of the Buf
the resulting Buf
will be shrunk down, otherwise it will be enlarged to fit the number of $elems
. In the case the Buf
is enlarged, newly created items will be assigned a Virtual Machine specific null value, therefore you should not rely upon their value since it could be inconsistent across different virtual machines.
my Buf .= new(^10);.reallocate(5);say .raku; # OUTPUT: «Buf.new(0,1,2,3,4)»= Buf.new( 1..3 );.reallocate( 10 );.raku.say; # OUTPUT: «Buf.new(1,2,3,0,0,0,0,0,0,0)»
method list
multi method list(Buf:)
Returns a List
of integers.
say Buf.new(122,105,112,205).list; # OUTPUT: «(122 105 112 205)»
method push
method push( )
Adds elements at the end of the buffer
my = 1,1, * + * … ∞;my = Buf.new( [^5] );.push( [5] );say .raku; # OUTPUT: «Buf.new(1,1,2,3,5,8)»
method pop
method pop()
Extracts the last element of the buffer
say .pop(); # OUTPUT: «8»say .raku; # OUTPUT: «Buf.new(1,1,2,3,5)»
method append
method append( )
Appends at the end of the buffer
.append( [5..10] );say .raku; # OUTPUT: «Buf.new(1,1,2,3,5,8,13,21,34,55,89)»
method prepend
method prepend( )
Adds elements at the beginning of the buffer
.prepend( 0 );say .raku; # OUTPUT: «Buf.new(0,1,1,2,3,5,8,13,21,34,55,89)»
method shift
method shift()
Takes out the first element of the buffer
.shift();say .raku; # OUTPUT: «Buf.new(1,1,2,3,5,8,13,21,34,55,89)»
method unshift
method unshift()
Adds elements at the beginning of the buffer
.unshift( 0 );say .raku; # OUTPUT: «Buf.new(0,1,1,2,3,5,8,13,21,34,55,89)»
method splice
method splice( Buf: = 0, ?, * --> Buf)
Substitutes elements of the buffer by other elements
.splice: 0, 3, <3 2 1>;say .raku; # OUTPUT: «Buf.new(3,2,1,2,3,5,8,13,21,34,55,89)»
Methods on buf8 only (6.d, 2018.12 and later)
These methods are available on the buf8
type only. They allow low level access to writing bytes to the underlying data and in different ways with regards to type (integer or floating point (num)), size (8, 16, 32, 64 or 128 bits), signed or unsigned (for integer values) and endianness (native, little and big endianness). These methods always return Nil
.
Endianness must be indicated by using values of the Endian enum as the third parameter to these methods. If no endianness is specified, NativeEndian
will be assumed. Other values are LittleEndian
and BigEndian
.
The buffer will be automatically resized to support any bytes being written if it is not large enough yet.
method write-uint8
method write-uint8(buf8: uint , uint8 , = NativeEndian --> Nil)
Writes an unsigned 8-bit integer value at the given position. The $endian
parameter has no meaning, but is available for consistency.
method write-int8
method write-int8(buf8: uint , int8 , = NativeEndian --> Nil)
Writes a signed 8-bit integer value at the given position. The $endian
parameter has no meaning, but is available for consistency.
method write-uint16
method write-uint16(buf8: uint , uint16 , = NativeEndian --> Nil)
Writes an unsigned 16-bit integer value at the given position with the given endianness.
method write-int16
method write-int16(buf8: uint , int16 , = NativeEndian --> Nil)
Writes a signed 16-bit integer value at the given position with the given endianness.
method write-uint32
method write-uint32(buf8: uint , uint32 , = NativeEndian --> Nil)
Writes an unsigned 32-bit integer value at the given position with the given endianness.
method write-int32
method write-int32(buf8: uint , int32 , = NativeEndian --> Nil)
Writes a signed 32-bit integer value at the given position with the given endianness.
method write-uint64
method write-uint64(buf8: uint , uint64 , = NativeEndian --> Nil)
Writes an unsigned 64-bit integer value at the given position with the given endianness.
method write-int64
method write-int64(buf8: uint , Int , = NativeEndian --> Nil)
Writes a signed 64-bit integer value at the given position with the given endianness.
method write-uint128
method write-uint128(buf8: uint , UInt , = NativeEndian --> Nil)
Writes an unsigned 128-bit integer value at the given position with the given endianness.
method write-int128
method write-int128(buf8: uint , Int , = NativeEndian --> Nil)
Writes a signed 128-bit integer value at the given position with the given endianness.
method write-num32
method write-num32(buf8: uint , num32 , = NativeEndian --> Nil)
Writes a native num32
IEEE floating point value at the given position with the given endianness.
method write-num64
method write-num64(buf8: uint , num64 , = NativeEndian --> Nil)
Writes a native num64
IEEE floating point value at the given position with the given endianness.
Methods on buf8 only (6.d, 2019.03 and later)
method write-ubits
method write-ubits(buf8: uint , uint , UInt --> Nil)
Writes an unsigned integer value to the bits from the given bit offset and given number of bits. The endianness of the bits is assumed to be BigEndian
. Always returns Nil.
method write-bits
method write-bits(buf8: uint , uint , Int --> Nil)
Writes a signed integer value for the bits from the given bit offset and given number of bits. The endianness of the bits is assumed to be BigEndian
. Always returns Nil.
Methods on buf8 only (6.d, 2019.10 and later)
These methods are available on the buf8
type only. They allow low level access to writing bytes to the underlying data and in different ways with regards to type (integer or floating point (num)), size (8, 16, 32, 64 or 128 bits), signed or unsigned (for integer values) and endianness (native, little and big endianness).
These methods can also be called on the buf8
type object, in which case a new buf8
object will be returned. Otherwise, the invocant will be returned to allow for easier chaining of operations on the buf8
object. The buffer will be automatically resized to support any bytes being written if it is not large enough yet.
Endianness must be indicated by using values of the Endian enum as the third parameter to these methods. If no endianness is specified, NativeEndian
will be assumed. Other values are LittleEndian
and BigEndian
.
method write-uint8
method write-uint8(buf8: uint , uint8 , = NativeEndian --> buf8)
Writes an unsigned 8-bit integer value at the given position. The $endian
parameter has no meaning, but is available for consistency.
method write-int8
method write-int8(buf8: uint , int8 , = NativeEndian --> buf8)
Writes a signed 8-bit integer value at the given position. The $endian
parameter has no meaning, but is available for consistency.
method write-uint16
method write-uint16(buf8: uint , uint16 , = NativeEndian --> buf8)
Writes an unsigned 16-bit integer value at the given position with the given endianness.
method write-int16
method write-int16(buf8: uint , int16 , = NativeEndian --> buf8)
Writes a signed 16-bit integer value at the given position with the given endianness.
method write-uint32
method write-uint32(buf8: uint , uint32 , = NativeEndian --> buf8)
Writes an unsigned 32-bit integer value at the given position with the given endianness.
method write-int32
method write-int32(buf8: uint , int32 , = NativeEndian --> buf8)
Writes a signed 32-bit integer value at the given position with the given endianness.
method write-uint64
method write-uint64(buf8: uint , uint64 , = NativeEndian --> buf8)
Writes an unsigned 64-bit integer value at the given position with the given endianness.
method write-int64
method write-int64(buf8: uint , Int , = NativeEndian --> buf8)
Writes a signed 64-bit integer value at the given position with the given endianness.
method write-uint128
method write-uint128(buf8: uint , UInt , = NativeEndian --> buf8)
Writes an unsigned 128-bit integer value at the given position with the given endianness.
method write-int128
method write-int128(buf8: uint , Int , = NativeEndian --> buf8)
Writes a signed 128-bit integer value at the given position with the given endianness.
method write-num32
method write-num32(buf8: uint , num32 , = NativeEndian --> buf8)
Writes a native num32
IEEE floating point value at the given position with the given endianness.
method write-num64
method write-num64(buf8: uint , num64 , = NativeEndian --> buf8)
Writes a native num64
IEEE floating point value at the given position with the given endianness.
method write-ubits
method write-ubits(buf8: uint , uint , UInt --> buf8)
Writes an unsigned integer value to the bits from the given bit offset and given number of bits. The endianness of the bits is assumed to be BigEndian
.
method write-bits
method write-bits(buf8: uint , uint , Int --> buf8)
Writes a signed integer value for the bits from the given bit offset and given number of bits. The endianness of the bits is assumed to be BigEndian
.