/
Any-str.pm
202 lines (176 loc) Β· 5.42 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
class Any is also {
our Int multi method bytes() is export {
Q:PIR {
$S0 = self
$I0 = bytelength $S0
%r = box $I0
}
}
our Str multi method capitalize() is export {
self.lc.subst(/\w+/, { .ucfirst }, :global)
}
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 {
Q:PIR {
$S0 = self
downcase $S0
%r = box $S0
}
}
our Str multi method lcfirst() is export {
self gt '' ?? self.substr(0,1).lc ~ self.substr(1) !! ""
}
our Int multi method ord() is export {
Q:PIR {
$S0 = self
$I0 = ord $S0
%r = box $I0
}
}
our Int multi method p5chomp() is export {
my $num = 0;
for @.list -> $str is rw {
if $str ~~ /\x0a$/ {
$str = $str.substr(0, $str.chars - 1);
$num++;
}
}
$num;
}
# 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
}
our Str multi method samecase(Str $pattern) is export {
my @pattern = $pattern.split('');
[~] gather {
my $p = "";
for (~self).split('') -> $s {
$p = @pattern.shift if @pattern;
given $p {
when /<upper>/ { take $s.uc }
when /<lower>/ { take $s.lc }
default { take $s }
}
}
}
}
our List multi method split(Code $delimiter, $limit = *) {
my $s = ~self;
my $l = $limit ~~ Whatever ?? Inf !! $limit;
my $keep = '';
return gather {
while $l > 1 && $s ~~ $delimiter {
take $keep ~ $s.substr(0, $/.from);
if $/.from == $/.to {
$keep = $s.substr($/.to, 1);
$s.=substr($/.to + 1);
} else {
$keep = '';
$s.=substr($/.to)
}
$l--;
}
take $keep ~ $s if $l > 0;
}
}
multi method flip() is export {
(~self).split('').reverse().join;
}
# TODO: substitute with '$delimiter as Str' once coercion is implemented
our List multi method split($delimiter, $limit = *) {
my Int $prev = 0;
my $l = $limit ~~ Whatever ?? Inf !! $limit;
my $s = ~self;
if $delimiter eq '' {
return gather {
take $s.substr($_, 1) for 0 .. ($s.chars - 1 min $l - 2);
if $l <= $s.chars {
take $s.substr($l - 1 );
};
}
}
return gather {
my $pos = 0;
while $l > 1
&& $pos < $s.chars
&& defined ($pos = $s.index($delimiter, $prev)) {
take $s.substr($prev, $pos - $prev);
$prev = [max] 1 + $prev, $pos + (~$delimiter).chars;
$l--;
}
take $s.substr($prev) if $l > 0;
}
}
our List multi method comb (Code $matcher = /./, $limit = *) {
my $l = $limit ~~ Whatever ?? Inf !! $limit;
# currently we use a copy of self and destroy it piece by piece.
# the preferred way of doing it is using self, not destroying it,
# and use the :pos modifier to the regex. That way the offsets into
# self will be right
my $s = ~self;
return gather {
while $l > 0 && $s ~~ $matcher {
# if we have captures, return the actual match object
take @($/) || %($/) ?? $/.clone !! ~$/;
$l--;
$s.=substr([max] 1, $/.to);
}
}
}
our List multi method words($limit = *) {
self.comb(/\S+/, $limit);
}
# TODO: signature not fully specced in S32 yet
our Str multi method trim() is export {
(~self).subst(/(^\s+)|(\s+$)/, "", :g)
}
our Str multi method uc() is export {
Q:PIR {
$S0 = self
upcase $S0
%r = box $S0
}
}
our Str multi method ucfirst() is export {
self gt '' ?? self.substr(0,1).uc ~ self.substr(1) !! ""
}
}
multi sub split($delimiter, $target, $limit = *) {
$target.split($delimiter, $limit);
}
# TODO: '$filename as Str' once support for that is in place
multi sub lines(Str $filename,
:$bin = False,
:$enc = 'Unicode',
:$nl = "\n",
:$chomp = True) {
my $filehandle = open($filename, :r);
return lines($filehandle, :$bin, :$enc, :$nl, :$chomp);
}
sub unpack($template, $target) {
$template.trans(/\s+/ => '') ~~ / ((<[Ax]>)(\d+))* /
or return (); # unknown syntax
my $pos = 0;
return gather for $0.values -> $chunk {
my ($operation, $count) = $chunk.[0, 1];
given $chunk.[0] {
when 'A' { take $target.substr($pos, $count); }
when 'x' { } # just skip
}
$pos += $count;
}
}
# vim: ft=perl6