/
Blob.pod
85 lines (53 loc) 路 2.43 KB
/
Blob.pod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
=begin pod
=TITLE role Blob
=SUBTITLE Common interface for binary large objects
role Blob[::T = uint8] does Positional[T] does Stringy { }
The C<Blob> role is an immutable interface to binary types, and offers a
list-like interface to lists of integers, typically unsigned integers.
=head1 Methods
=head2 method new
method new(*@codes)
Creates a C<Blob> from a list of integers.
=head2 method Bool
multi method Bool(Blob:D:)
Returns C<False> if and only if the buffer is empty.
=head2 method elems
multi method elems(Blob:D:) returns Int:D
Returns the number of elements of the buffer.
=head2 method decode
multi method decode(Blob:D: Str:D $encoding = 'UTF-8') returns Str:D
Applies an encoding to turn the blob into a L<Str|/type/Str>.
=head2 method subbuf
method subbuf(Int $from, Int $len = self.elems) returns Blob:D
Extracts a part of the invocant buffer, starting from the index with
elements C<$from>, and taking C<$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); # Blob:0x<03 04 05 06>
=head2 method unpack
method unpack(Blob:D: $template) returns List:D
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
C<*> (which typically stands for "use up the rest of the Blob here"), or a
positive integer (without a C<+>).
Whitespace between template units are ignored.
So valid templates are for example C<"A4 C n*"> or C<"A*">.
The following letters are recognized:
=begin table
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" (big-endian) byte order into a single integer
N Extracts four elements and combines them in "network" (big-endian) 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
=end table
=end pod