File Coverage

blib/lib/Spp/Builtin.pm
Criterion Covered Total %
statement 75 159 47.1
branch 17 64 26.5
condition 3 3 100.0
subroutine 24 43 55.8
pod 0 38 0.0
total 119 307 38.7


line stmt bran cond sub pod time code
1             package Spp::Builtin;
2              
3 2     2   26 use 5.012;
  2         6  
4 2     2   11 no warnings "experimental";
  2         2  
  2         57  
5              
6 2     2   8 use Exporter;
  2         2  
  2         237  
7             our @ISA = qw(Exporter);
8             our @EXPORT = qw(End In Out True False Qstr Qint
9             is_estr is_qint is_int is_str is_array is_true
10             is_false is_bool is_atom is_atoms
11             error read_file write_file to_json from_json
12             first tail rest len trim subarray uuid cutlast
13             is_space is_upper is_lower is_digit is_xdigit
14             is_alpha is_words is_hspace is_vspace
15             clean_ast clean_atom start_with end_with to_end
16             see_ast);
17              
18 2     2   815 use JSON::XS qw(encode_json decode_json);
  2         11153  
  2         173  
19              
20             use constant {
21 2         3092 End => chr(0),
22             In => chr(1),
23             Out => chr(2),
24             True => chr(3),
25             False => chr(4),
26             Qstr => chr(5),
27             Qint => chr(6),
28 2     2   15 };
  2         2  
29              
30             sub is_estr {
31 0     0 0 0 my $estr = shift;
32 0         0 return first($estr) eq In;
33             }
34              
35             sub is_qint {
36 0     0 0 0 my $estr = shift;
37 0         0 return first($estr) eq Qint;
38             }
39              
40             sub is_int {
41 0     0 0 0 my $int = shift;
42 0         0 return ($int ^ $int) eq '0';
43             }
44              
45             sub is_str {
46 696     696 0 1017 my $x = shift;
47 696         2062 return (ref($x) eq ref(''));
48             }
49              
50             sub is_array {
51 93     93 0 127 my $x = shift;
52 93         330 return (ref($x) eq ref([]));
53             }
54              
55             sub is_true {
56 186     186 0 259 my $atom = shift;
57 186         534 return $atom eq True;
58             }
59              
60             sub is_false {
61 1369     1369 0 1702 my $atom = shift;
62 1369         3894 return $atom eq False;
63             }
64              
65             sub is_bool {
66 397     397 0 476 my $atom = shift;
67 397 100       515 return 1 if is_false($atom);
68 23 50       43 return 1 if is_true($atom);
69 23         59 return 0;
70             }
71              
72             sub is_atom {
73 66     66 0 93 my $x = shift;
74 66   100     102 return (is_array($x) and is_str($x->[0]));
75             }
76              
77             sub is_atoms {
78 0     0 0 0 my $pairs = shift;
79 0 0       0 return 0 if !is_array($pairs);
80 0         0 for my $pair (@{$pairs}) {
  0         0  
81 0 0       0 return 0 if !is_atom($pair);
82             }
83 0         0 return 1;
84             }
85              
86 0     0 0 0 sub error { say @_; exit() }
  0         0  
87              
88             sub read_file {
89 0     0 0 0 my $file = shift;
90 0 0       0 error("file: $file not exists") if not (-e $file);
91 0         0 local $/;
92 0 0       0 open my ($fh), '<:encoding(UTF-8)', $file or die $!;
93 0         0 return <$fh>;
94             }
95              
96             sub write_file {
97 0     0 0 0 my ($file, $str) = @_;
98 0 0       0 open my ($fh), '>:encoding(UTF-8)', $file or die $!;
99 0         0 print {$fh} $str;
  0         0  
100 0         0 say "write file: $file ok!";
101 0         0 return $file;
102             }
103              
104             sub to_json {
105 3     3 0 7 my $data = shift;
106 3 50       7 if (is_str($data)) {
107 0         0 my $json_str = encode_json([$data]);
108 0         0 return substr($json_str, 1, -1);
109             }
110 3         41 return encode_json($data);
111             }
112              
113 3     3 0 412 sub from_json { return decode_json(shift) }
114              
115             sub first {
116 2     2 0 9 my $data = shift;
117 2 50       6 if (is_str($data)) { return substr($data, 0, 1) }
  2         10  
118 0 0       0 if (is_array($data)) { return $data->[0] }
  0         0  
119 0         0 error("could not first No Str/array");
120             }
121              
122             sub tail {
123 2     2 0 4 my $data = shift;
124 2 50       4 if (is_str($data)) { return substr($data, -1) }
  2         10  
125 0 0       0 if (is_array($data)) { return $data->[-1] }
  0         0  
126 0         0 error("Could not tail not Str/array");
127             }
128              
129             sub rest {
130 3     3 0 7 my $data = shift;
131 3 50       20 return substr($data, 1) if is_str($data);
132 3 50       9 if (is_array($data)) {
133 3         5 my @array = @{$data};
  3         11  
134 3         16 return [splice(@array, 1)];
135             }
136 0         0 error("rest only could do str or array");
137             }
138              
139             sub len {
140 24     24 0 39 my $data = shift;
141 24 50       42 return scalar(@{$data}) if is_array($data);
  24         88  
142 0 0       0 return length($data) if is_str($data);
143             }
144              
145             sub trim {
146 0     0 0 0 my $str = shift;
147 0 0       0 if (is_str($str)) {
148 0         0 $str =~ s/^\s+|\s+$//g;
149 0         0 return $str;
150             }
151 0         0 error("trim only make string");
152             }
153              
154             sub subarray {
155 0     0 0 0 my ($array, $from, $to) = @_;
156 0         0 my @array = @{$array};
  0         0  
157 0 0       0 if (is_array($array)) {
158 0 0       0 if ($to > 0) {
159 0         0 my $len = $to - $from + 1;
160 0         0 my $sub_array = [splice @array, $from, $len];
161 0         0 return $sub_array;
162             }
163 0 0       0 if (defined $to) {
164 0         0 return [splice @array, $from, $to];
165             }
166 0         0 return [splice @array, $from];
167             }
168 0         0 error("subarray only could process array");
169             }
170              
171 0     0 0 0 sub uuid { return scalar(rand()) }
172              
173             sub cutlast {
174 0     0 0 0 my $str = shift;
175 0 0       0 if (is_str($str)) {
176 0         0 return substr($str, 0, -1);
177             }
178 0 0       0 if (is_array($str)) {
179 0         0 my @array = @{$str};
  0         0  
180 0         0 return [splice @array, 0, -1];
181             }
182             }
183              
184             sub is_space {
185 34     34 0 53 my $r = shift;
186 34         179 return $r ~~ ["\n", "\t", "\r", ' '];
187             }
188              
189             sub is_upper {
190 2     2 0 5 my $r = shift;
191 2         27 return $r ~~ ['A' .. 'Z'];
192             }
193              
194             sub is_lower {
195 0     0 0 0 my $r = shift;
196 0         0 return $r ~~ ['a' .. 'z'];
197             }
198              
199             sub is_digit {
200 29     29 0 50 my $r = shift;
201 29         206 return $r ~~ ['0' .. '9'];
202             }
203              
204             sub is_xdigit {
205 0     0 0 0 my $char = shift;
206 0 0       0 return 1 if is_digit($char);
207 0 0       0 return 1 if $char ~~ ['a' .. 'f'];
208 0 0       0 return 1 if $char ~~ ['A' .. 'F'];
209 0         0 return 0;
210             }
211              
212             sub is_alpha {
213 40     40 0 81 my $r = shift;
214 40         686 return $r ~~ ['a' .. 'z', 'A' .. 'Z', '_'];
215             }
216              
217             sub is_words {
218 0     0 0 0 my $r = shift;
219 0 0       0 return 1 if is_digit($r);
220 0 0       0 return 1 if is_alpha($r);
221 0         0 return 0;
222             }
223              
224             sub is_hspace {
225 0     0 0 0 my $r = shift;
226 0         0 return $r ~~ [' ', "\t"];
227             }
228              
229             sub is_vspace {
230 0     0 0 0 my $r = shift;
231 0         0 return $r ~~ ["\r", "\n"];
232             }
233              
234             sub clean_ast {
235 5     5 0 10 my $ast = shift;
236 5 50       14 return clean_atom($ast) if is_atom($ast);
237 5         13 my $clean_atoms = [];
238 5         7 for my $atom (@{$ast}) {
  5         12  
239 8         14 push @{$clean_atoms}, clean_atom($atom);
  8         22  
240             }
241 5         19 return $clean_atoms;
242             }
243              
244             sub clean_atom {
245 13     13 0 20 my $atom = shift;
246 13         18 my ($name, $value) = @{$atom};
  13         28  
247 13 100       29 if (is_str($value)) { return [$name, $value] }
  6         24  
248 7 50       13 if (len($value) == 0) {
249 0         0 return [$name, $value]
250             }
251 7 100       15 if (is_atom($value)) {
252 5         16 return [$name, clean_atom($value)];
253             }
254 2         9 return [$name, clean_ast($value)];
255             }
256              
257             sub start_with {
258 219     219 0 323 my ($str, $start) = @_;
259 219 100       546 return 1 if index($str, $start) == 0;
260 108         213 return 0;
261             }
262              
263             sub end_with {
264 0     0 0   my ($str, $end) = @_;
265 0           my $len = length($end);
266 0           return substr($str, -$len) eq $end;
267             }
268              
269             sub to_end {
270 0     0 0   my $str = shift;
271 0           my $index = index($str, "\n");
272 0           return substr($str, 0, $index);
273             }
274              
275             sub see_ast {
276 0     0 0   my $ast = shift;
277 0           return to_json(clean_ast($ast));
278             }
279              
280             1;