| 1 | package Dallycot::Library::Core::Strings; | ||||||||||||||||||||||||||||
| 2 | 9 | our $AUTHORITY = 'cpan:JSMITH'; | |||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Core library of useful string functions | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 9 | ▶ | use Dallycot::Library; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 9 | ▶ | use Digest::MD5; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 9 | ▶ | use Math::BaseCalc; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 9 | ▶ | use experimental qw(switch); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 9 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 9 | ▶ | use Promises qw(deferred); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 9 | ▶ | use Dallycot::Library::Core (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 9 | ▶ | use Dallycot::Library::Core::Streams (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 9 | ns 'http://www.dallycot.net/ns/strings/1.0#'; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 9 | uses 'http://www.dallycot.net/ns/loc/1.0#', | |||||||||||||||||||||||||||
| 27 | 'http://www.dallycot.net/ns/core/1.0#', | ||||||||||||||||||||||||||||
| 28 | 'http://www.dallycot.net/ns/streams/1.0#'; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | #==================================================================== | ||||||||||||||||||||||||||||
| 31 | # | ||||||||||||||||||||||||||||
| 32 | # Basic string functions | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | define | ||||||||||||||||||||||||||||
| 35 | 'string-contains?' => ( | ||||||||||||||||||||||||||||
| 36 | hold => 0, | ||||||||||||||||||||||||||||
| 37 | arity => 2, | ||||||||||||||||||||||||||||
| 38 | options => {} | ||||||||||||||||||||||||||||
| 39 | ), | ||||||||||||||||||||||||||||
| 40 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 41 | 0 | my ( $engine, $options, $string, $patt ) = @_; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | if ( !$string -> isa('Dallycot::Value::String') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 0 | croak 'The first argument of string-contains? must be a string'; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if ( !$patt -> isa('Dallycot::Value::String') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | croak 'The pattern for string-contains? must be a string'; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | return Dallycot::Value::Boolean -> new( | |||||||||||||||||||||||||||
| 51 | index($string -> value, $patt -> value) != -1 | ||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||
| 53 | 9 | }; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | define | ||||||||||||||||||||||||||||
| 56 | 'string-split' => ( | ||||||||||||||||||||||||||||
| 57 | hold => 0, | ||||||||||||||||||||||||||||
| 58 | arity => [1,3], | ||||||||||||||||||||||||||||
| 59 | options => {} | ||||||||||||||||||||||||||||
| 60 | ), | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 62 | 0 | my ( $engine, $options, $string, $patt, $max_count ) = @_; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if ( !$string ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | return Dallycot::Value::EmptyStream->new; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 0 | my @bits; | |||||||||||||||||||||||||||
| 68 | 0 | my $source = $string -> value; | |||||||||||||||||||||||||||
| 69 | 0 | ▶ | if(!$patt) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | @bits = split( /\s+/, $source ); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | else { | ||||||||||||||||||||||||||||
| 73 | 0 | my $count; | |||||||||||||||||||||||||||
| 74 | 0 | ▶ | if($max_count) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | if ( $max_count->isa('Dallycot::Value::Numeric') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | $count = $max_count->value->numify; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | else { | ||||||||||||||||||||||||||||
| 79 | 0 | croak 'Limit for string-split must be numeric'; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 0 | ▶ | if($patt -> isa('Dallycot::Value::String')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if($count) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | @bits = split($patt -> value, $source, $count); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | else { | ||||||||||||||||||||||||||||
| 87 | 0 | @bits = split($patt -> value, $source); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | else { | ||||||||||||||||||||||||||||
| 91 | 0 | croak 'Pattern for string-split must be a string'; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | return Dallycot::Value::Vector->new( | ||||||||||||||||||||||||||||
| 95 | map { | ||||||||||||||||||||||||||||
| 96 | 0 | Dallycot::Value::String->new($_, $string->lang) | |||||||||||||||||||||||||||
| 97 | } @bits | ||||||||||||||||||||||||||||
| 98 | ); | ||||||||||||||||||||||||||||
| 99 | 9 | }; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | define | ||||||||||||||||||||||||||||
| 102 | 'string-take' => ( | ||||||||||||||||||||||||||||
| 103 | hold => 0, | ||||||||||||||||||||||||||||
| 104 | arity => 2, | ||||||||||||||||||||||||||||
| 105 | options => {} | ||||||||||||||||||||||||||||
| 106 | ), | ||||||||||||||||||||||||||||
| 107 | 3 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=29.0
| |||||||||||||||||||||||||||||
| 108 | 3 | my ( $engine, $options, $string, $spec ) = @_; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 3 | ▶ | if ( !$string ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 111 | 0 | my $d = deferred; | |||||||||||||||||||||||||||
| 112 | 0 | $d->resolve( $engine->UNDEFINED ); | |||||||||||||||||||||||||||
| 113 | 0 | return $d->promise; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | elsif ( !$spec ) { | ||||||||||||||||||||||||||||
| 116 | 0 | my $d = deferred; | |||||||||||||||||||||||||||
| 117 | 0 | $d->resolve( $engine->UNDEFINED ); | |||||||||||||||||||||||||||
| 118 | 0 | return $d->promise; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | else { | ||||||||||||||||||||||||||||
| 121 | 3 | ▶ | if ( $spec->isa('Dallycot::Value::Numeric') ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 122 | 1 | my $length = $spec->value->numify; | |||||||||||||||||||||||||||
| 123 | 1 | return $string->take_range( $engine, 1, $length ); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | elsif ( $spec->isa('Dallycot::Value::Vector') ) { | ||||||||||||||||||||||||||||
| 126 | 2 | given ( scalar(@$spec) ) { | |||||||||||||||||||||||||||
| 127 | 2 | when (1) { | |||||||||||||||||||||||||||
| 128 | 1 | ▶ | if ( $spec->[0]->isa('Dallycot::Value::Numeric') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 1 | my $offset = $spec->[0]->value->numify; | |||||||||||||||||||||||||||
| 130 | 1 | return $string->value_at( $engine, $offset ); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | else { | ||||||||||||||||||||||||||||
| 133 | 0 | my $d = deferred; | |||||||||||||||||||||||||||
| 134 | 0 | $d->reject("Offset must be numeric"); | |||||||||||||||||||||||||||
| 135 | 0 | return $d->promise; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 1 | when (2) { | |||||||||||||||||||||||||||
| 139 | 1 | ▶ | if ( $spec->[0]->isa('Dallycot::Value::Numeric') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$spec->[0]->isa("Dallycot::Value::Numeric") and $spec->[1]->isa("Dallycot::Value::Numeric")
MC/DC0%0/2
$spec->[0]->isa("Dallycot::Value::Numeric") and $spec->[1]->isa("Dallycot::Value::Numeric")
$spec->[0]->isa("Dallycot::Value::Numeric")
$spec->[1]->isa("Dallycot::Value::Numeric")
Truth table33%1/3
A and B
A= $spec->[0]->isa("Dallycot::Value::Numeric")B= $spec->[1]->isa("Dallycot::Value::Numeric")
| |||||||||||||||||||||||||||||
| 140 | && $spec->[1]->isa('Dallycot::Value::Numeric') ) | ||||||||||||||||||||||||||||
| 141 | { | ||||||||||||||||||||||||||||
| 142 | 1 | my ( $offset, $length ) | |||||||||||||||||||||||||||
| 143 | = ( $spec->[0]->value->numify, $spec->[1]->value->numify ); | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 1 | return $string->take_range( $engine, $offset, $length ); | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | else { | ||||||||||||||||||||||||||||
| 148 | 0 | my $d = deferred; | |||||||||||||||||||||||||||
| 149 | 0 | $d->reject("string-take requires numeric offsets"); | |||||||||||||||||||||||||||
| 150 | 0 | return $d->promise; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | 0 | default { | |||||||||||||||||||||||||||
| 154 | 0 | my $d = deferred; | |||||||||||||||||||||||||||
| 155 | 0 | $d->reject("string-take requires 1 or 2 numeric elements in an offset vector"); | |||||||||||||||||||||||||||
| 156 | 0 | return $d->promise; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | else { | ||||||||||||||||||||||||||||
| 161 | 0 | my $d = deferred; | |||||||||||||||||||||||||||
| 162 | 0 | $d->reject("Offset must be numeric or a vector of numerics"); | |||||||||||||||||||||||||||
| 163 | 0 | return $d->promise; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | 9 | }; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | define | ||||||||||||||||||||||||||||
| 169 | 'string-drop' => ( | ||||||||||||||||||||||||||||
| 170 | hold => 0, | ||||||||||||||||||||||||||||
| 171 | arity => 2, | ||||||||||||||||||||||||||||
| 172 | options => {}, | ||||||||||||||||||||||||||||
| 173 | ), | ||||||||||||||||||||||||||||
| 174 | 1 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=20.8
| |||||||||||||||||||||||||||||
| 175 | 1 | my ( $engine, $options, $string, $spec ) = @_; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 1 | ▶ | if ( !$string ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 178 | 0 | my $d = deferred; | |||||||||||||||||||||||||||
| 179 | 0 | $d->resolve( $engine->UNDEFINED ); | |||||||||||||||||||||||||||
| 180 | 0 | return $d->promise; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | elsif ( !$spec ) { | ||||||||||||||||||||||||||||
| 183 | 0 | my $d = deferred; | |||||||||||||||||||||||||||
| 184 | 0 | $d->resolve( $engine->UNDEFINED ); | |||||||||||||||||||||||||||
| 185 | 0 | return $d->promise; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | elsif ( $spec->isa('Dallycot::Value::Numeric') ) { | ||||||||||||||||||||||||||||
| 188 | 1 | my $offset = $spec->value->numify; | |||||||||||||||||||||||||||
| 189 | 1 | return $string->drop( $engine, $offset ); | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | else { | ||||||||||||||||||||||||||||
| 192 | 0 | my $d = deferred; | |||||||||||||||||||||||||||
| 193 | 0 | $d->reject("string-drop requires a numeric second argument"); | |||||||||||||||||||||||||||
| 194 | 0 | return $d->promise; | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | 9 | }; | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 9 | define 'string-join' => << 'EOD'; | |||||||||||||||||||||||||||
| 199 | (joiner, string-stream) :> last( | ||||||||||||||||||||||||||||
| 200 | foldl1( | ||||||||||||||||||||||||||||
| 201 | { #1 ::> joiner ::> #2 }/2, | ||||||||||||||||||||||||||||
| 202 | string-stream | ||||||||||||||||||||||||||||
| 203 | ) | ||||||||||||||||||||||||||||
| 204 | ) | ||||||||||||||||||||||||||||
| 205 | EOD | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 9 | define 'ends-with?' => <<'EOD'; | |||||||||||||||||||||||||||
| 208 | (value, substr) :> ( | ||||||||||||||||||||||||||||
| 209 | string-take(value, < length(value) - length(substr) + 1, length(value) > ) = substr | ||||||||||||||||||||||||||||
| 210 | ) | ||||||||||||||||||||||||||||
| 211 | EOD | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 9 | define 'starts-with?' => <<'EOD'; | |||||||||||||||||||||||||||
| 214 | (value, substr) :> ( | ||||||||||||||||||||||||||||
| 215 | string-take(value, < 1, length(substr) > ) = substr | ||||||||||||||||||||||||||||
| 216 | ) | ||||||||||||||||||||||||||||
| 217 | EOD | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | define | ||||||||||||||||||||||||||||
| 220 | 'hash' => ( | ||||||||||||||||||||||||||||
| 221 | hold => 0, | ||||||||||||||||||||||||||||
| 222 | arity => 1, | ||||||||||||||||||||||||||||
| 223 | options => { type => Dallycot::Value::String->new('MD5') } | ||||||||||||||||||||||||||||
| 224 | ), | ||||||||||||||||||||||||||||
| 225 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 226 | 0 | my ( $engine, $options, $string ) = @_; | |||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 0 | my $digest = Digest::MD5::md5_hex( $string->value ); | |||||||||||||||||||||||||||
| 229 | 0 | my $num = Math::BigRat->from_hex("0x$digest"); | |||||||||||||||||||||||||||
| 230 | 0 | my $d = deferred; | |||||||||||||||||||||||||||
| 231 | 0 | $d->resolve( Dallycot::Value::Numeric->new($num) ); | |||||||||||||||||||||||||||
| 232 | 0 | return $d->promise; | |||||||||||||||||||||||||||
| 233 | 9 | }; | |||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | define | ||||||||||||||||||||||||||||
| 236 | 'string-multiply' => ( | ||||||||||||||||||||||||||||
| 237 | hold => 0, | ||||||||||||||||||||||||||||
| 238 | arity => 2, | ||||||||||||||||||||||||||||
| 239 | options => {} | ||||||||||||||||||||||||||||
| 240 | ), | ||||||||||||||||||||||||||||
| 241 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 242 | 0 | my ( $engine, $options, $string, $count ) = @_; | |||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 0 | ▶ | if ( !defined($string) || !$string->isa('Dallycot::Value::String') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $string and $string->isa("Dallycot::Value::String")
MC/DC0%0/2
defined $string and $string->isa("Dallycot::Value::String")
defined $string
$string->isa("Dallycot::Value::String")
Truth table0%0/3
A and B
A= defined $stringB= $string->isa("Dallycot::Value::String")
| |||||||||||||||||||||||||||||
| 245 | 0 | croak 'string-multiple requires a string as its first argument.'; | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | 0 | ▶ | if ( !defined($count) || !$count->isa('Dallycot::Value::Numeric') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $count and $count->isa("Dallycot::Value::Numeric")
MC/DC0%0/2
defined $count and $count->isa("Dallycot::Value::Numeric")
defined $count
$count->isa("Dallycot::Value::Numeric")
Truth table0%0/3
A and B
A= defined $countB= $count->isa("Dallycot::Value::Numeric")
| |||||||||||||||||||||||||||||
| 248 | 0 | croak 'string-multiple requires a numeric second argument.'; | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | 0 | my $base = $string->value; | |||||||||||||||||||||||||||
| 251 | 0 | my $c = $count->value->as_int; | |||||||||||||||||||||||||||
| 252 | 0 | return Dallycot::Value::String->new( $base x $c, $string->lang ); | |||||||||||||||||||||||||||
| 253 | 9 | }; | |||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | define | ||||||||||||||||||||||||||||
| 256 | 'N' => ( | ||||||||||||||||||||||||||||
| 257 | hold => 0, | ||||||||||||||||||||||||||||
| 258 | arity => 1, | ||||||||||||||||||||||||||||
| 259 | options => { accuracy => Dallycot::Value::Numeric->new( Math::BigRat->new(40) ) } | ||||||||||||||||||||||||||||
| 260 | ), | ||||||||||||||||||||||||||||
| 261 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 262 | 0 | my ( $engine, $options, $number ) = @_; | |||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 0 | ▶ | if ( !defined($number) || !$number->isa('Dallycot::Value::Numeric') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $number and $number->isa("Dallycot::Value::Numeric")
MC/DC0%0/2
defined $number and $number->isa("Dallycot::Value::Numeric")
defined $number
$number->isa("Dallycot::Value::Numeric")
Truth table0%0/3
A and B
A= defined $numberB= $number->isa("Dallycot::Value::Numeric")
| |||||||||||||||||||||||||||||
| 265 | 0 | my $d = deferred; | |||||||||||||||||||||||||||
| 266 | 0 | $d->reject("N requires a numeric argument"); | |||||||||||||||||||||||||||
| 267 | 0 | return $d->promise; | |||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 0 | ▶ | if ( !defined( $options->{accuracy} ) || !$options->{accuracy}->isa('Dallycot::Value::Numeric') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $options->{"accuracy"} and $options->{"accuracy"}->isa("Dallycot::Value::Numeric")
MC/DC0%0/2
defined $options->{"accuracy"} and $options->{"accuracy"}->isa("Dallycot::Value::Numeric")
defined $options->{"accuracy"}
$options->{"accuracy"}->isa("Dallycot::Value::Numeric")
Truth table0%0/3
A and B
A= defined $options->{"accuracy"}B= $options->{"accuracy"}->isa("Dallycot::Value::Numeric")
| |||||||||||||||||||||||||||||
| 271 | 0 | my $d = deferred; | |||||||||||||||||||||||||||
| 272 | 0 | $d->reject("N requires a numeric accuracy"); | |||||||||||||||||||||||||||
| 273 | 0 | return $d->promise; | |||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 0 | my $accuracy = $options->{accuracy}->value->as_int; | |||||||||||||||||||||||||||
| 277 | 0 | $number = $number->value->as_float->bround($accuracy); | |||||||||||||||||||||||||||
| 278 | 0 | my $d = deferred; | |||||||||||||||||||||||||||
| 279 | 0 | $d->resolve( Dallycot::Value::String->new( $number->bstr ) ); | |||||||||||||||||||||||||||
| 280 | 0 | return $d->promise; | |||||||||||||||||||||||||||
| 281 | 9 | }; | |||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | define | ||||||||||||||||||||||||||||
| 284 | 'number-string' => ( | ||||||||||||||||||||||||||||
| 285 | hold => 0, | ||||||||||||||||||||||||||||
| 286 | arity => [ 1, 2 ], | ||||||||||||||||||||||||||||
| 287 | options => {} | ||||||||||||||||||||||||||||
| 288 | ), | ||||||||||||||||||||||||||||
| 289 | 1 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=27.6
| |||||||||||||||||||||||||||||
| 290 | 1 | my ( $engine, $options, $number, $base ) = @_; | |||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 1 | ▶ | if ( !defined($number) || !$number->isa('Dallycot::Value::Numeric') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $number and $number->isa("Dallycot::Value::Numeric")
MC/DC0%0/2
defined $number and $number->isa("Dallycot::Value::Numeric")
defined $number
$number->isa("Dallycot::Value::Numeric")
Truth table33%1/3
A and B
A= defined $numberB= $number->isa("Dallycot::Value::Numeric")
| |||||||||||||||||||||||||||||
| 293 | 0 | my $d = deferred; | |||||||||||||||||||||||||||
| 294 | 0 | $d->reject("number-string requires a numeric argument"); | |||||||||||||||||||||||||||
| 295 | 0 | return $d->promise; | |||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 1 | ▶ | $base = defined($base) ? $base->value->numify : 10; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 299 | 1 | my $d = deferred; | |||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | 1 | ▶ | if ( $base < 2 || $base > 64 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$base < 2 or $base > 64
MC/DC0%0/2
$base < 2 or $base > 64
$base < 2
$base > 64
Truth table33%1/3
A or B
A= $base < 2B= $base > 64
| |||||||||||||||||||||||||||||
| 302 | 0 | $d->reject('number-string requires a base between 2 and 64 inclusive'); | |||||||||||||||||||||||||||
| 303 | 0 | return $d->promise; | |||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | 1 | my @regular_digits = ( 0 .. 9, 'a' .. 'z', 'A' .. 'Z', '_' ); | |||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | 1 | my $converter = Math::BaseCalc->new( digits => [ 0, 1 ] ); | |||||||||||||||||||||||||||
| 309 | 1 | ▶ | if ( $base < 64 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | 1 | $converter->digits( [ @regular_digits[ 0 .. $base - 1 ] ] ); | |||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | else { | ||||||||||||||||||||||||||||
| 313 | 0 | $converter->digits( [ 'A' .. 'Z', 'a' .. 'z', 0 .. 9, '+', '/' ] ); | |||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 1 | my $string; | |||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | 1 | my ( $num, $den ) = $number->value->parts; | |||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 1 | ▶ | if ( $number->value->is_int || $den->is_one ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$number->value->is_int or $den->is_one
MC/DC0%0/2
$number->value->is_int or $den->is_one
$number->value->is_int
$den->is_one
Truth table33%1/3
A or B
A= $number->value->is_intB= $den->is_one
| |||||||||||||||||||||||||||||
| 321 | 1 | $string = $converter->to_base($num); | |||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||
| 323 | else { | ||||||||||||||||||||||||||||
| 324 | 0 | $string = $converter->to_base($num) . " / " . $converter->to_base($den); | |||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | 1 | $d->resolve( Dallycot::Value::String->new($string) ); | |||||||||||||||||||||||||||
| 327 | 1 | return $d->promise; | |||||||||||||||||||||||||||
| 328 | 9 | }; | |||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | 9 | 1; | |||||||||||||||||||||||||||