/
experimental.pm6
199 lines (184 loc) Β· 6.91 KB
/
experimental.pm6
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use nqp;
package EXPORT::cached {
multi sub trait_mod:<is>(Routine $r, :$cached!) {
my %cache;
$r.wrap(-> |c {
my $key := c.gist;
%cache.EXISTS-KEY($key)
?? %cache{$key}
!! (%cache{$key} := callsame);
});
}
multi sub trait_mod:<is>(Method $m, :$cached!) {
X::NYI.new(:feature("'is cached' on methods")).throw;
}
OUR::{'&trait_mod:<is>'} := &trait_mod:<is>;
}
package EXPORT::macros {
OUR::<EXPERIMENTAL-MACROS> := True;
}
package EXPORT::smallnatives {
our native int1 is repr('P6int') is Int is nativesize( 1) { }
our native int2 is repr('P6int') is Int is nativesize( 2) { }
our native int4 is repr('P6int') is Int is nativesize( 4) { }
our native uint1 is repr('P6int') is Int is nativesize( 1) is unsigned { }
our native bit is repr('P6int') is Int is nativesize( 1) is unsigned { }
our native uint2 is repr('P6int') is Int is nativesize( 2) is unsigned { }
our native uint4 is repr('P6int') is Int is nativesize( 4) is unsigned { }
}
package EXPORT::pack {
proto sub pack($, |) {*}
multi sub pack(Str $template, *@items) {
pack($template.comb(/<[a..zA..Z]>[\d+|'*']?/), @items)
}
multi sub pack(@template, *@items) {
my @bytes;
for @template -> $unit {
my $directive = substr($unit,0,1);
my $amount = substr($unit,1);
given $directive {
when 'A' {
my $ascii = shift @items // '';
my $data = $ascii.ords.cache;
if $amount eq '*' {
$amount = $data.elems;
}
if $amount eq '' {
$amount = 1;
}
for (@$data, 0x20 xx *).flat[^$amount] -> $byte {
X::Buf::Pack::NonASCII.new(:char($byte.chr)).throw if $byte > 0x7f;
@bytes.push: $byte;
}
}
when 'a' {
my $data = shift @items // Buf.new;
$data.=encode if nqp::istype($data,Str);
if $amount eq '*' {
$amount = $data.elems;
}
if $amount eq '' {
$amount = 1;
}
for (@$data, 0 xx *).flat[^$amount] -> $byte {
@bytes.push: $byte;
}
}
when 'H' {
my $hexstring = shift @items // '';
if $hexstring.chars % 2 {
$hexstring ~= '0';
}
@bytes.append: map { :16($_) }, $hexstring.comb(/../);
}
when 'x' {
if $amount eq '*' {
$amount = 0;
}
elsif $amount eq '' {
$amount = 1;
}
@bytes.append: 0x00 xx $amount;
}
when 'C' {
my $number = shift(@items);
@bytes.push: $number % 0x100;
}
when 'S' | 'v' {
my $number = shift(@items);
@bytes.append: ($number, $number +> 0x08) >>%>> 0x100;
}
when 'L' | 'V' {
my $number = shift(@items);
@bytes.append: ($number, $number +> 0x08,
$number +> 0x10, $number +> 0x18) >>%>> 0x100;
}
when 'n' {
my $number = shift(@items);
@bytes.append: ($number +> 0x08, $number) >>%>> 0x100;
}
when 'N' {
my $number = shift(@items);
@bytes.append: ($number +> 0x18, $number +> 0x10,
$number +> 0x08, $number) >>%>> 0x100;
}
X::Buf::Pack.new(:$directive).throw;
}
}
return Buf.new(@bytes);
}
proto sub unpack(|) {*}
multi sub unpack(Blob:D \blob, Str:D $template) {
unpack(blob, $template.comb(/<[a..zA..Z]>[\d+|'*']?/))
}
multi sub unpack(Blob:D \blob, @template) {
my @bytes = blob.list;
my @fields;
for @template -> $unit {
my $directive = substr($unit,0,1);
my $amount = substr($unit,1);
my $pa = $amount eq '' ?? 1 !!
$amount eq '*' ?? @bytes.elems !! +$amount;
given $directive {
when 'a' | 'A' | 'Z' {
@fields.push: @bytes.splice(0, $pa).map(&chr).join;
}
when 'H' {
my str $hexstring = '';
for ^$pa {
my $byte = shift @bytes;
$hexstring ~= ($byte +> 4).fmt('%x')
~ ($byte % 16).fmt('%x');
}
@fields.push($hexstring);
}
when 'x' {
splice @bytes, 0, $pa;
}
when 'C' {
@fields.append: @bytes.splice(0, $pa);
}
when 'S' | 'v' {
for ^$pa {
last if @bytes.elems < 2;
@fields.append: shift(@bytes)
+ (shift(@bytes) +< 0x08);
}
}
when 'L' | 'V' {
for ^$pa {
last if @bytes.elems < 4;
@fields.append: shift(@bytes)
+ (shift(@bytes) +< 0x08)
+ (shift(@bytes) +< 0x10)
+ (shift(@bytes) +< 0x18);
}
}
when 'n' {
for ^$pa {
last if @bytes.elems < 2;
@fields.append: (shift(@bytes) +< 0x08)
+ shift(@bytes);
}
}
when 'N' {
for ^$pa {
last if @bytes.elems < 4;
@fields.append: (shift(@bytes) +< 0x18)
+ (shift(@bytes) +< 0x10)
+ (shift(@bytes) +< 0x08)
+ shift(@bytes);
}
}
X::Buf::Pack.new(:$directive).throw;
}
}
return |@fields;
}
BEGIN OUR::<EXPERIMENTAL-PACK> := &unpack; # BEGIN is needed somehow
OUR::{'&pack'} := &pack;
}
package EXPORT::collation {
OUR::<EXPERIMENTAL-COLLATION> := True;
}
# vim: ft=perl6 expandtab sw=4