/
Uni.pm6
157 lines (134 loc) Β· 4.35 KB
/
Uni.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
my class NFC is repr('VMArray') is array_type(uint32) { ... }
my class NFD is repr('VMArray') is array_type(uint32) { ... }
my class NFKC is repr('VMArray') is array_type(uint32) { ... }
my class NFKD is repr('VMArray') is array_type(uint32) { ... }
my class Uni does Positional[uint32] does Stringy is repr('VMArray') is array_type(uint32) {
method new(*@codes) {
my $uni := nqp::create(self);
my int $n = @codes.elems;
loop (my int $i = 0; $i < $n; $i = $i + 1) {
nqp::bindpos_i($uni, $i, @codes.AT-POS($i));
}
$uni
}
my class UniList does PredictiveIterator {
has $!uni;
has int $!els;
has int $!i;
method !SET-SELF(\uni) {
$!uni := uni;
$!i = -1;
$!els = nqp::elems(uni);
self
}
method new (\uni) { nqp::create(self)!SET-SELF: uni }
method pull-one {
nqp::if(
nqp::islt_i(($!i = nqp::add_i($!i, 1)), $!els),
nqp::atpos_i($!uni, $!i),
IterationEnd
)
}
method skip-one {
nqp::islt_i(($!i = nqp::add_i($!i, 1)), $!els)
}
method push-all($target --> IterationEnd) {
nqp::stmts(
(my $uni := $!uni), # lexicals faster than attrs
(my int $els = $!els),
(my int $i = $!i),
nqp::while(
nqp::islt_i(($i = nqp::add_i($i, 1)), $els),
$target.push: nqp::atpos_i($uni, $i)
),
($!i = $i)
)
}
method count-only(--> Int:D) {
nqp::p6box_i($!els - $!i - nqp::islt_i($!i,$!els))
}
}
method list(Uni:D:) { Seq.new(UniList.new(self)) }
method Uni(Uni:D:) {
self
}
method NFC(Uni:D:) {
nqp::normalizecodes(self, nqp::const::NORMALIZE_NFC, nqp::create(NFC))
}
method NFD(Uni:D:) {
nqp::normalizecodes(self, nqp::const::NORMALIZE_NFD, nqp::create(NFD))
}
method NFKC(Uni:D:) {
nqp::normalizecodes(self, nqp::const::NORMALIZE_NFKC, nqp::create(NFKC))
}
method NFKD(Uni:D:) {
nqp::normalizecodes(self, nqp::const::NORMALIZE_NFKD, nqp::create(NFKD))
}
multi method Str(Uni:D:) {
nqp::strfromcodes(self)
}
multi method Bool(Uni:D:) {
nqp::hllbool(nqp::elems(self));
}
method codes(Uni:D:) { nqp::elems(self) }
method elems(Uni:D:) { nqp::elems(self) }
method Numeric(Uni:D:) { nqp::elems(self) }
method Int(Uni:D:) { nqp::elems(self) }
multi method EXISTS-POS(Uni:D: int \pos) {
nqp::hllbool(
nqp::islt_i(pos,nqp::elems(self)) && nqp::isge_i(pos,0)
);
}
multi method EXISTS-POS(Uni:D: Int:D \pos) {
pos < nqp::elems(self) && pos >= 0;
}
multi method AT-POS(Uni:D: int \pos) {
nqp::isge_i(pos,nqp::elems(self)) || nqp::islt_i(pos,0)
?? Failure.new(X::OutOfRange.new(
:what($*INDEX // 'Index'),
:got(pos),
:range("0..{nqp::elems(self)-1}")))
!! nqp::atpos_i(self, pos)
}
multi method AT-POS(Uni:D: Int:D \pos) {
my int $pos = nqp::unbox_i(pos);
nqp::isge_i($pos,nqp::elems(self)) || nqp::islt_i($pos,0)
?? Failure.new(X::OutOfRange.new(
:what($*INDEX // 'Index'),
:got(pos),
:range("0..{nqp::elems(self)-1}")))
!! nqp::atpos_i(self,$pos)
}
multi method gist(Uni:D:) {
self.^name ~ ':0x<' ~ self.list.fmt('%04x', ' ') ~ '>'
}
multi method perl(Uni:D:) {
'Uni.new(' ~ self.list.fmt('0x%04x', ', ') ~ ')' ~
(self.WHAT === Uni ?? '' !! '.' ~ self.^name);
}
}
my class NFD is Uni {
method new(|) {
die "Cannot create an NFD directly"; # XXX typed, better message
}
method NFD() { self }
}
my class NFC is Uni {
method new(|) {
die "Cannot create an NFC directly"; # XXX typed, better message
}
method NFC() { self }
}
my class NFKD is Uni {
method new(|) {
die "Cannot create an NFKD directly"; # XXX typed, better message
}
method NFKD() { self }
}
my class NFKC is Uni {
method NFKC() { self }
method new(|) {
die "Cannot create an NFKC directly"; # XXX typed, better message
}
}
# vim: ft=perl6 expandtab sw=4