role Blob
Immutable buffer for binary data ('Binary Large OBject')
1 | Methods |
1.1 | method new |
1.2 | method Bool |
1.3 | method Capture |
1.4 | method elems |
1.5 | method bytes |
1.6 | method chars |
1.7 | method Str |
1.8 | method Stringy |
1.9 | method decode |
1.10 | method list |
1.11 | method gist |
1.12 | method subbuf |
1.13 | method allocate |
1.14 | method unpack |
1.15 | sub pack |
1.16 | method reverse |
2 | Methods on blob8 only (6.d, 2018.12 and later) |
2.1 | method read-uint8 |
2.2 | method read-int8 |
2.3 | method read-uint16 |
2.4 | method read-int16 |
2.5 | method read-uint32 |
2.6 | method read-int32 |
2.7 | method read-uint64 |
2.8 | method read-int64 |
2.9 | method read-uint128 |
2.10 | method read-int128 |
2.11 | method read-num32 |
2.12 | method read-num64 |
3 | Methods on blob8 only (6.d, 2019.03 and later) |
3.1 | method read-ubits |
3.2 | method read-bits |
4 | Type Graph |
5 | Routines supplied by role Positional |
5.1 | (Positional) method of |
5.2 | (Positional) method elems |
5.3 | (Positional) method AT-POS |
5.4 | (Positional) method EXISTS-POS |
5.5 | (Positional) method STORE |
[::T = uint8] does Positional[T] does Stringy
The Blob
role is an immutable interface to binary types, and offers a list-like interface to lists of integers, typically unsigned integers.
Methods
method new
Defined as:
multi method new(Blob:)multi method new(Blob: Blob )multi method new(Blob: int )multi method new(Blob: )multi method new(Blob: *)
Creates an empty Blob
, or a new Blob
from another Blob
, or from a list of integers or values (which will have to be coerced into integers):
my = Blob.new([1, 2, 3]);say Blob.new(<1 2 3>); # OUTPUT: «Blob:0x<01 02 03>»
method Bool
Defined as:
multi method Bool(Blob:)
Returns False
if and only if the buffer is empty.
my = Blob.new();say .Bool; # OUTPUT: «False»= Blob.new([1, 2, 3]);say .Bool; # OUTPUT: «True»
method Capture
Defined as:
method Capture(Blob)
Equivalent to calling .List.Capture
on the invocant.
method elems
Defined as:
multi method elems(Blob:)multi method elems(Blob: --> 1)
Returns the number of elements of the buffer.
my = Blob.new([1, 2, 3]);say .elems; # OUTPUT: «3»
It will also return 1 on the class object.
method bytes
Defined as:
method bytes(Blob: --> Int)
Returns the number of bytes used by the elements in the buffer.
say Blob.new([1, 2, 3]).bytes; # OUTPUT: «3»say blob16.new([1, 2, 3]).bytes; # OUTPUT: «6»say blob64.new([1, 2, 3]).bytes; # OUTPUT: «24»
method chars
Defined as:
method chars(Blob:)
Throws X::Buf::AsStr
with chars
as payload.
method Str
Defined as:
multi method Str(Blob:)
Throws X::Buf::AsStr
with Str
as payload. In order to convert to a Str
you need to use .decode
.
method Stringy
Defined as:
multi method Stringy(Blob:)
Throws X::Buf::AsStr
with Stringy
as payload.
method decode
Defined as:
multi method decode(Blob: = self.encoding // "utf-8")
multi method decode(Blob: , Str :!,Bool : = False)
multi method decode(Blob: , Bool : = False)
Applies an encoding to turn the blob into a Str; the encoding will be UTF-8 by default.
my Blob = "string".encode('utf-8');say .decode('utf-8'); # OUTPUT: «string»
On malformed utf-8 .decode
will throw X::AdHoc. To handle sloppy utf-8 use utf8-c8
.
method list
Defined as:
multi method list(Blob:)
Returns the list of codepoints:
say "zipi".encode("ascii").list; # OUTPUT: «(122 105 112 105)»
method gist
Defined as:
method gist(Blob: --> Str)
Returns the string containing the "gist" of the Blob, listing up to the first 100 elements, separated by space, appending an ellipsis if the Blob has more than 100 elements.
put Blob.new(1, 2, 3).gist; # OUTPUT: «Blob:0x<01 02 03>»put Blob.new(1..2000).gist;# OUTPUT:# Blob:0x<01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15# 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C# 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43# 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A# 5B 5C 5D 5E 5F 60 61 62 63 64 ...>
method subbuf
Defined as:
multi method subbuf(Int , Int = self.elems --> Blob)multi method subbuf(Range --> Blob)
Extracts a part of the invocant buffer, starting from the index with elements $from
, and taking $len
elements (or less if the buffer is shorter), and creates a new buffer as the result.
say Blob.new(1..10).subbuf(2, 4); # OUTPUT: «Blob:0x<03 04 05 06>»say Blob.new(1..10).subbuf(*-2); # OUTPUT: «Blob:0x<09 0a>»say Blob.new(1..10).subbuf(*-5,2); # OUTPUT: «Blob:0x<06 07>»
For convenience, also allows a Range
to be specified to indicate which part of the invocant buffer you would like:
say Blob.new(1..10).subbuf(2..5); # OUTPUT: «Blob:0x<03 04 05 06>»
method allocate
Defined as:
multi method allocate(Blob: Int )multi method allocate(Blob: Int , int )multi method allocate(Blob: Int , Int \value)multi method allocate(Blob: Int , Mu )multi method allocate(Blob: Int , int )multi method allocate(Blob: Int , Blob )multi method allocate(Blob: Int , )
Returns a newly created Blob
object with the given number of elements. Optionally takes a second argument that indicates the pattern with which to fill the Blob
: this can be a single (possibly native) integer value, or any Iterable
that generates integer values, including another Blob
. The pattern will be repeated if not enough values are given to fill the entire Blob
.
my Blob = Blob.allocate(10,0);.say; # OUTPUT: «Blob:0x<00 00 00 00 00 00 00 00 00 00>»
If the pattern is a general Mu
value, it will fail.
method unpack
This method is considered experimental, in order to use it you will need to do:
use experimental :pack;
Defined as:
multi method unpack(Blob: Str )multi method unpack(Blob: )multi sub unpack(Blob \blob, Str )multi sub unpack(Blob \blob, )
Extracts features from the blob according to the template string, and returns them as a list.
The template string consists of zero or more units that begin with an ASCII letter, and are optionally followed by a quantifier. The quantifier can be *
(which typically stands for "use up the rest of the Blob here"), or a positive integer (without a +
).
Whitespace between template units is ignored.
Examples of valid templates include "A4 C n*"
and "A*"
.
The following letters are recognized:
Letter | Meaning |
---|---|
A | Extract a string, where each element of the Blob maps to a codepoint |
a | Same as A |
C | Extract an element from the blob as an integer |
H | Extracts a hex string |
L | Extracts four elements and returns them as a single unsigned integer |
n | Extracts two elements and combines them in "network" (BigEndian) byte order into a single integer |
N | Extracts four elements and combines them in "network" (BigEndian) byte order into a single integer |
S | Extracts two elements and returns them as a single unsigned integer |
v | Same as S |
V | Same as L |
x | Drop an element from the blob (that is, ignore it) |
Z | Same as A |
Example:
use experimental :pack;say Blob.new(1..10).unpack("C*");# OUTPUT: «(1 2 3 4 5 6 7 8 9 10)»
sub pack
This subroutine is considered experimental, in order to use it you will need to do:
use experimental :pack;
multi sub pack(Str , *)multi sub pack(, *)
Packs the given items according to the template and returns a buffer containing the packed bytes.
The template string consists of zero or more units that begin with an ASCII letter, and are optionally followed by a quantifier. For details, see unpack.
method reverse
Defined as:
method reverse(Blob: --> Blob)
Returns a Blob with all elements in reversed order.
say Blob.new([1, 2, 3]).reverse; # OUTPUT: «Blob:0x<03 02 01>»say blob16.new([2]).reverse; # OUTPUT: «Blob[uint16]:0x<02>»say buf32.new([16, 32]).reverse; # OUTPUT: «Buf[uint32]:0x<20 10>»
Methods on blob8 only (6.d, 2018.12 and later)
These methods are available on the blob8 (and buf8
) types only. They allow low level access to reading bytes from the underlying data and interpreting them 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). The returned values are always expanded to a 64 bit native value where possible, and to a (big) integer value if that is not possible.
Endianness must be indicated by using values of the Endian enum as the second parameter to these methods. If no endianness is specified, NativeEndian
will be assumed. Other values are LittleEndian
and BigEndian
.
method read-uint8
Defined as:
method read-uint8(blob8: uint , = NativeEndian --> uint)
Returns an unsigned native integer value for the byte at the given position. The $endian
parameter has no meaning, but is available for consistency.
method read-int8
Defined as:
method read-int8(blob8: uint , = NativeEndian --> int)
Returns a native int
value for the byte at the given position. The $endian
parameter has no meaning, but is available for consistency.
method read-uint16
Defined as:
method read-uint16(blob8: uint , = NativeEndian --> uint)
Returns a native uint
value for the two bytes starting at the given position.
method read-int16
Defined as:
method read-int16(blob8: uint , = NativeEndian --> int)
Returns a native int
value for the two bytes starting at the given position.
method read-uint32
Defined as:
method read-uint32(blob8: uint , = NativeEndian --> uint)
Returns a native uint
value for the four bytes starting at the given position.
method read-int32
Defined as:
method read-int32(blob8: uint , = NativeEndian --> int)
Returns a native int
value for the four bytes starting at the given position.
method read-uint64
Defined as:
method read-uint64(blob8: uint , = NativeEndian --> UInt)
Returns an unsigned integer value for the eight bytes starting at the given position.
method read-int64
Defined as:
method read-int64(blob8: uint , = NativeEndian --> int)
Returns a native int
value for the eight bytes starting at the given position.
method read-uint128
Defined as:
method read-uint128(blob8: uint , = NativeEndian --> UInt)
Returns an unsigned integer value for the sixteen bytes starting at the given position.
method read-int128
Defined as:
method read-int128(blob8: uint , = NativeEndian --> Int)
Returns an integer value for the sixteen bytes starting at the given position.
method read-num32
Defined as:
method read-num32(blob8: uint , = NativeEndian --> int)
Returns a native num
value for the four bytes starting at the given position.
method read-num64
Defined as:
method read-num64(blob8: uint , = NativeEndian --> int)
Returns a native num
value for the eight bytes starting at the given position.
Methods on blob8 only (6.d, 2019.03 and later)
method read-ubits
Defined as:
method read-ubits(blob8: uint , uint --> UInt)
Returns an unsigned 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
.
method read-bits
Defined as:
method read-bits(blob8: uint , uint --> Int)
Returns 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
.
Type Graph
Routines supplied by role Positional
Blob does role Positional, which provides the following routines:
(Positional) method of
method of()
Returns the type constraint for elements of the positional container. Defaults to Mu.
(Positional) method elems
method elems()
Should return the number of available elements in the instantiated object.
(Positional) method AT-POS
method AT-POS(\position)
Should return the value / container at the given position.
(Positional) method EXISTS-POS
method EXISTS-POS(\position)
Should return a Bool
indicating whether the given position actually has a value.
(Positional) method STORE
method STORE(\values, :)
This method should only be supplied if you want to support the:
my is Foo = 1,2,3;
syntax for binding your implementation of the Positional
role.
Should accept the values to (re-)initialize the object with. The optional named parameter will contain a True
value when the method is called on the object for the first time. Should return the invocant.