Sidef/Types/Range/RangeString.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 132 CC 10 SCAR 17.7
CC
10
Cov
0%
new
12.0
call
12.0
from
6.0
CRAP 5.8
1 package Sidef::Types::Range::RangeString;
2  
3 use utf8;
4 use 5.016;
5  
6 use parent qw(
7 Sidef::Types::Range::Range
8 );
9  
10 use overload q{""} => sub {
11 my ($self) = @_;
12 "RangeStr(" . join(', ', $self->{from}->chr->dump, $self->{to}->chr->dump, "$self->{step}") . ")";
13 };
14  
15 use Sidef::Types::Bool::Bool;
16 use Sidef::Types::Number::Number;
17  
18 # This expects numbers
19 sub new {
20 my (undef, $from, $to, $step) = @_;
21  
22 if (not defined $from) {
23 $from = Sidef::Types::Number::Number::ZERO;
24 $to = Sidef::Types::Number::Number::MONE;
25 }
26  
27 if (not defined $to) {
28 $to = $from;
29 $from = Sidef::Types::Number::Number::_set_int(CORE::ord("a"));
30 }
31  
32 bless {
33 from => $from,
34 to => $to,
35 step => $step // Sidef::Types::Number::Number::ONE,
36 },
37 __PACKAGE__;
38 }
39  
40 # This expects characters
41 sub call {
42 my (undef, $from, $to, $step) = @_;
43  
44 if (defined $from) {
45 $from = $from->ord;
46 }
47 else {
48 $from = Sidef::Types::Number::Number::ZERO;
49 }
50  
51 if (defined $to) {
52 $to = $to->ord;
53 }
54 else {
55 $to = $from;
56 $from = Sidef::Types::Number::Number::_set_int(CORE::ord("a"));
57 }
58  
59 bless {
60 from => $from,
61 to => $to,
62 step => $step // Sidef::Types::Number::Number::ONE,
63 },
64 __PACKAGE__;
65 }
66  
67 sub from {
68 my ($self, $from) = @_;
69 defined($from) ? $self->SUPER::from($from->ord) : $self->{from}->chr;
70 }
71  
72 sub to {
73 my ($self, $to) = @_;
74 defined($to) ? $self->SUPER::to($to->ord) : $self->{to}->chr;
75 }
76  
77 sub min {
78 $_[0]->SUPER::min->chr;
79 }
80  
81 sub max {
82 $_[0]->SUPER::max->chr;
83 }
84  
85 sub contains {
86 my ($self, $value) = @_;
87 $self->SUPER::contains($value->ord);
88 }
89  
90 *contain = \&contains;
91 *include = \&contains;
92 *includes = \&contains;
93  
94 sub iter {
95 my ($self) = @_;
96  
97 my $step = $self->{step};
98 my $from = $self->{from};
99 my $to = $self->{to};
100  
101 my $asc = ($self->{_asc} //= !!$step->is_pos);
102 my $i = $from;
103  
104 Sidef::Types::Block::Block->new(
105 code => sub {
106 ($asc ? $i->le($to) : $i->ge($to)) || return undef;
107 my $value = $i;
108 $i = $i->add($step);
109 $value->chr;
110 },
111 );
112 }
113  
114 sub pick {
115 my ($self, $num) = @_;
116  
117 defined($num)
118 ? $self->SUPER::pick($num)->map_operator('chr')
119 : $self->SUPER::pick->chr;
120 }
121  
122 sub rand {
123 my ($self, $num) = @_;
124 defined($num)
125 ? $self->SUPER::rand($num)->map_operator('chr')
126 : $self->SUPER::rand->chr;
127 }
128  
129 *sample = \&rand;
130  
131 sub dump {
132 my ($self) = @_;
133 Sidef::Types::String::String->new("$self");
134 }
135  
136 *to_s = \&dump;
137 *to_str = \&dump;
138  
139 1