/
Any-str.pm
256 lines (218 loc) Β· 6.89 KB
/
Any-str.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
augment class Any {
our Int multi method bytes() is export {
pir::box__PI(pir::bytelength__IS(self))
}
our Int multi method chars() is export {
pir::length__IS(self);
}
our multi method chomp() is export {
if self ~~ /\x0a$/ {
self.substr(0, self.chars - 1);
} else {
self;
}
}
multi method subst($matcher, Str $replacement, :$g) {
self.split($matcher, :limit($g ?? * !! 2)).join($replacement);
}
multi method comb(Regex $matcher = /./, $limit = *, :$match) {
my $c = 0;
my $l = $limit ~~ ::Whatever ?? Inf !! $limit;
gather while $l > 0 && (my $m = self.match($matcher, :c($c))) {
take $match ?? $m !! ~$m;
$c = $m.to == $c ?? $c + 1 !! $m.to;
--$l;
}
}
multi method split(Regex $matcher, $limit = *, :$all) {
my $c = 0;
my $l = $limit ~~ ::Whatever ?? Inf !! $limit - 1;
if $l >= 0 {
gather {
while $l-- > 0 && (my $m = self.match($matcher, :c($c))) {
take self.substr($c, $m.from - $c);
take $m if $all;
$c = $m.to == $c ?? $c + 1 !! $m.to;
}
take self.substr($c);
}
} else {
Nil;
}
}
multi method split($delimiter, $limit = *) {
my Str $match-string = $delimiter ~~ Str ?? $delimiter !! $delimiter.Str;
my $c = 0;
my $l = $limit ~~ ::Whatever ?? Inf !! $limit - 1;
if $l >= 0 {
gather {
while $l-- > 0 {
if ($match-string eq "") {
last unless $c + 1 < self.chars;
take self.substr($c, 1);
$c++;
} else {
my $m = self.index($match-string, $c);
last if $m.notdef; # CHEAT, but the best I can do for now
take self.substr($c, $m - $c);
$c = $m + $match-string.chars;
}
}
take self.substr($c);
}
} else {
Nil;
}
}
our Str multi method substr($start, $length?) is export {
my $len = $length // self.chars;
if ($len < 0) {
if ($start >= 0) {
$len += self.chars;
}
$len -= $start;
}
pir::substr(self, $start, $len);
}
# S32/Str says that this should always return a StrPos object
our Int multi method index($substring, $pos = 0) is export {
if ($substring.chars == 0) {
my $string_length = self.chars;
return $pos < $string_length ?? $pos !! $string_length;
}
my $result = pir::index__ISSi(self, $substring, $pos);
fail("Substring '$substring' not found in '{self}'") if $result < 0;
return $result;
# also used to be a the following error message, but the condition
# was never checked:
# .tailcall '!FAIL'("Attempt to index from negative position")
}
# S32/Str says that this should always return a StrPos object
# our Int multi method rindex($substring, $pos?) is export {
# if ($substring.chars == 0) {
# my $string_length = self.chars;
# return $pos.defined && $pos < $string_length ?? $pos !! $string_length;
# }
#
# my $result = pir::reverse_index__ISSi(self, $substring, $pos);
# fail("Substring '$substring' not found in '{self}'") if $result < 0;
# return $result;
#
# # also used to be a the following error message, but the condition
# # was never checked:
# # .tailcall '!FAIL'("Attempt to index from negative position")
# }
our Str multi method chop() is export {
self.substr(0, -1)
}
our Str multi method fmt(Str $format = '%s') {
sprintf($format, self)
}
our Str multi method lc() is export {
~(pir::downcase__SS(self))
}
our Str multi method lcfirst() is export {
self gt '' ?? self.substr(0,1).lc ~ self.substr(1) !! ""
}
our multi method match(Regex $pat, :$c = 0, :$g) {
if $g {
my $cont = $c;
gather while my $m = Regex::Cursor.parse(self, :rule($pat), :c($cont)) {
take $m;
if $m.to == $m.from {
$cont = $m.to + 1;
} else {
$cont = $m.to;
}
}
} else {
Regex::Cursor.parse(self, :rule($pat), :c($c));
}
}
our multi method ord() {
given self.chars {
when 0 { fail('Can not take ord of empty string'); }
when 1 { pir::box__PI(pir::ord__IS(self)); }
default {
gather for self.comb {
take pir::box__PI(pir::ord__IS($_))
}
}
}
}
# TODO: Return type should be a Char once that is supported.
our Str multi method p5chop() is export {
my $char = '';
for @.list -> $str is rw {
if $str gt '' {
$char = $str.substr($str.chars - 1, 1);
$str = $str.chop;
}
}
$char
}
multi method eval() {
eval(~self);
}
multi method flip() is export {
(~self).split('').reverse().join;
}
# Not yet spec'd, I expect it will be renamed
multi method trim-leading() is export {
if self ~~ /^\s*:(.*)$/ {
~$/[0];
} else {
self;
}
}
# Not yet spec'd, I expect it will be renamed
multi method trim-trailing() is export {
if self ~~ /^(.*\S)\s*$/ {
~$/[0];
} elsif self ~~ /^\s*$/ {
"";
}
else {
self;
}
}
# TODO: signature not fully specced in S32 yet
multi method trim() is export {
self.trim-leading.trim-trailing;
}
multi method words(Int $limit = *) {
self.comb( / \S+ /, $limit );
}
our Str multi method uc() is export {
~(pir::upcase__SS(self))
}
our Str multi method ucfirst() is export {
self gt '' ?? self.substr(0,1).uc ~ self.substr(1) !! ""
}
}
our multi sub ord($string) {
$string.ord;
}
our proto ord($string) {
$string.ord;
}
our Str proto sub infix:<x>($str, $n) {
$n > 0 ?? ~(pir::repeat__SSI($str, $n)) !! ''
}
our multi sub infix:<cmp>($a, $b) {
if $a eq $b {
0;
} else {
$a lt $b ?? -1 !! 1;
}
}
our multi sub infix:<leg>($a, $b) {
~$a cmp ~$b
}
our multi split ( Str $delimiter, Str $input, Int $limit = * ) {
$input.split($delimiter, $limit);
}
our multi split ( Regex $delimiter, Str $input, Int $limit = * ) {
$input.split($delimiter, $limit);
}
# vim: ft=perl6