/
Builtin.pm
44 lines (37 loc) · 1.19 KB
/
Builtin.pm
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
my class Encoding::Encoder::Builtin does Encoding::Encoder {
has str $!encoding;
has Blob $!type;
method new(Str $encoding, Blob:U $type) {
nqp::create(self)!setup($encoding, $type)
}
method !setup($encoding, $type) {
$!encoding = $encoding;
$!type := nqp::can($type.HOW, 'pun') ?? $type.^pun !! $type.WHAT;
self
}
method encode-chars(str $str --> Blob:D) {
nqp::encode($str, $!encoding, nqp::create($!type))
}
}
my class Encoding::Encoder::Builtin::Replacement does Encoding::Encoder {
has str $!encoding;
has Blob $!type;
has str $!replacement;
method new(Str $encoding, Blob:U $type, Str $replacement) {
nqp::create(self)!setup($encoding, $type, $replacement)
}
method !setup($encoding, $type, $replacement) {
$!encoding = $encoding;
$!type := nqp::can($type.HOW, 'pun') ?? $type.^pun !! $type.WHAT;
$!replacement = $replacement;
self
}
method encode-chars(str $str --> Blob:D) {
#?if moar
nqp::encoderep($str, $!encoding, $!replacement, nqp::create($!type))
#?endif
#?if !moar
X::NYI.new(feature => 'encoding with replacement').throw
#?endif
}
}