Skip to content
Permalink
Browse files

Streamline unival

- move logic to Int, as the basic logic operates on an Int
- rewrite logic using state to logic using nqp::ifnull
- add Str.unival method
- adapt Cool.unival method accordingly
- makes unival() more than 2x as fast
  • Loading branch information...
lizmat committed Jan 5, 2019
1 parent a5b9fc2 commit 08ae9bfcff78d60673c799f1932d1db55e9799a2
Showing with 44 additions and 11 deletions.
  1. +5 −11 src/core/Cool.pm6
  2. +37 −0 src/core/Int.pm6
  3. +2 −0 src/core/Str.pm6
@@ -103,7 +103,7 @@ my class Cool { # declared in BOOTSTRAP

method uniname() { uniname(self) }
method uninames() { uninames(self) }
method unival() { unival(self) }
method unival(Cool:D:) { self.Int.unival }
method univals() { univals(self) }
method uniprop(|c) { uniprop(self, |c) }
method uniprop-int(|c) { uniprop-int(self, |c) }
@@ -502,7 +502,7 @@ multi sub uniprop(Int:D $code, Stringy:D $propname) {
nqp::getuniname($code),
nqp::if(
nqp::iseq_s($pref, 'nv'),
unival($code),
$code.unival,
nqp::if(
nqp::iseq_s($pref, 'bmg'),
nqp::stmts(
@@ -548,17 +548,11 @@ multi sub uniprops(Str:D $str, Stringy:D $propname = "General_Category") {
}

proto sub unival($, *%) {*}
multi sub unival(Str:D $str) { $str ?? unival($str.ord) !! Nil }
multi sub unival(Int:D $code) {
state $nuprop = nqp::unipropcode("Numeric_Value_Numerator");
state $deprop = nqp::unipropcode("Numeric_Value_Denominator");
my $nu = nqp::getuniprop_str($code, $nuprop);
my $de = nqp::getuniprop_str($code, $deprop);
!$de || $de eq '1' ?? $nu.Int !! $nu / $de;
}
multi sub unival(Str:D $str) { $str ?? $str.ord.unival !! Nil }
multi sub unival(Int:D $code) { $code.unival }

proto sub univals($, *%) {*}
multi sub univals(Str:D $str) { $str.ords.map: { unival($_) } }
multi sub univals(Str:D $str) { $str.ords.map: { .unival } }

proto sub unimatch($, |) {*}
multi sub unimatch(Str:D $str, |c) { $str ?? unimatch($str.ord, |c) !! Nil }
@@ -235,6 +235,43 @@ my class Int does Real { # declared in BOOTSTRAP
}
}
}

my $nuprop := nqp::null;
my $deprop := nqp::null;
method unival(Int:D:) {
my str $de = nqp::getuniprop_str(
self,
nqp::ifnull(
$deprop,
$deprop := nqp::unipropcode("Numeric_Value_Denominator")
)
);
nqp::if(
nqp::chars($de),
nqp::if( # some string to work with
nqp::iseq_s($de,"NaN"),
NaN, # no value found
nqp::stmts( # value for denominator
(my str $nu = nqp::getuniprop_str(
self,
nqp::ifnull(
$nuprop,
$nuprop := nqp::unipropcode("Numeric_Value_Numerator")
)
)),
nqp::if(
nqp::iseq_s($de,"1"),
nqp::atpos(nqp::radix(10,$nu,0,0),0), # just the numerator
Rat.new( # spotted a Rat
nqp::atpos(nqp::radix(10,$nu,0,0),0),
nqp::atpos(nqp::radix(10,$de,0,0),0)
)
)
)
),
Nil # no string, so no value
)
}
}

multi sub prefix:<++>(Int:D $a is rw --> Int:D) {
@@ -2301,6 +2301,8 @@ my class Str does Stringy { # declared in BOOTSTRAP
method NFKD() { X::NYI.new(:feature<NFKD>).throw }
#?endif

method unival(Str:D:) { self ?? self.ord.unival !! Nil }

method wordcase(Str:D: :&filter = &tclc, Mu :$where = True --> Str:D) {
self.subst(:g, / [<:L> \w* ] +% <['\-]> /, -> $m { # ' highlighting
my Str $s = $m.Str;

0 comments on commit 08ae9bf

Please sign in to comment.
You can’t perform that action at this time.