Skip to content

Commit

Permalink
Save upto 2 allocations per Date/DateTime.yyyy-mm-dd
Browse files Browse the repository at this point in the history
- and dd-mm-yyyy and mm-dd-yyyy friends
- 1 Int allocation if month > 9
- 1 Int allocation if day > 9

For some reason, *in the core setting*, having a native int attribute
as part of a nqp::push_s (which should just convert it to a native
string), gives strange errors.  The workaround was to use nqp::to_str_I
on a nqp::getattr_i, which caused the Int allocation.  Oddly enough
using an native int attribute as part of a nqp::concat just works, so
use that together with an nqp::x in which the repeat factor is determined
by the result of the condition < 10 (which yields either 1 or 0).
  • Loading branch information
lizmat committed Mar 15, 2020
1 parent 70ac9b7 commit f9ecf6e
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions src/core.c/Dateish.pm6
Expand Up @@ -138,26 +138,22 @@ my role Dateish {
?? self!year-Str
!! nqp::tostr_I(nqp::getattr_i(self,$?CLASS,'$!year'))
);
nqp::push_s($parts, $!month < 10
?? nqp::concat('0',$!month)
!! nqp::tostr_I(nqp::getattr_i(self,$?CLASS,'$!month'))
nqp::push_s($parts,
nqp::concat(nqp::x('0',nqp::islt_i($!month,10)),$!month)
);
nqp::push_s($parts, $!day < 10
?? nqp::concat('0',$!day)
!! nqp::tostr_I(nqp::getattr_i(self,$?CLASS,'$!day'))
nqp::push_s($parts,
nqp::concat(nqp::x('0',nqp::islt_i($!day,10)),$!day)
);
nqp::join($sep,$parts)
}

method dd-mm-yyyy(str $sep = "-" --> Str:D) {
my $parts := nqp::list_s;
nqp::push_s($parts, $!day < 10
?? nqp::concat('0',$!day)
!! nqp::tostr_I(nqp::getattr_i(self,$?CLASS,'$!day'))
nqp::push_s($parts,
nqp::concat(nqp::x('0',nqp::islt_i($!day,10)),$!day)
);
nqp::push_s($parts, $!month < 10
?? nqp::concat('0',$!month)
!! nqp::tostr_I(nqp::getattr_i(self,$?CLASS,'$!month'))
nqp::push_s($parts,
nqp::concat(nqp::x('0',nqp::islt_i($!month,10)),$!month)
);
nqp::push_s($parts, $!year < 1000 || $!year > 9999
?? self!year-Str
Expand All @@ -168,13 +164,11 @@ my role Dateish {

method mm-dd-yyyy(str $sep = "-" --> Str:D) {
my $parts := nqp::list_s;
nqp::push_s($parts, $!month < 10
?? nqp::concat('0',$!month)
!! nqp::tostr_I(nqp::getattr_i(self,$?CLASS,'$!month'))
nqp::push_s($parts,
nqp::concat(nqp::x('0',nqp::islt_i($!month,10)),$!month)
);
nqp::push_s($parts, $!day < 10
?? nqp::concat('0',$!day)
!! nqp::tostr_I(nqp::getattr_i(self,$?CLASS,'$!day'))
nqp::push_s($parts,
nqp::concat(nqp::x('0',nqp::islt_i($!day,10)),$!day)
);
nqp::push_s($parts, $!year < 1000 || $!year > 9999
?? self!year-Str
Expand Down

0 comments on commit f9ecf6e

Please sign in to comment.