Skip to content

Commit

Permalink
[js] Use NFG using ops in a bunch of places
Browse files Browse the repository at this point in the history
  • Loading branch information
pmurias committed Jan 7, 2018
1 parent 4accb64 commit 2ce329c
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/core/Cool.pm
Expand Up @@ -322,8 +322,15 @@ sub split($pat, Cool $target, |c) { $target.split($pat, |c) }

proto sub chars($) is pure {*}
multi sub chars(Cool $x) { $x.Str.chars }

#?if !js
multi sub chars(Str:D $x) { nqp::p6box_i(nqp::chars($x)) }
multi sub chars(str $x --> int) { nqp::chars($x) }
#?endif
#?if js
multi sub chars(Str:D $x) { nqp::p6box_i(nqp::charsnfg($x)) }
multi sub chars(str $x --> int) { nqp::charsnfg($x) }
#?endif

# These probably belong in a separate unicodey file

Expand Down
104 changes: 102 additions & 2 deletions src/core/Str.pm
Expand Up @@ -113,12 +113,22 @@ my class Str does Stringy { # declared in BOOTSTRAP
}

method chomp(Str:D:) {
#?if js
nqp::if(
(nqp::isge_i((my int $chars = nqp::sub_i(nqp::charsnfg($!value),1)),0)
&& nqp::iscclassnfg(nqp::const::CCLASS_NEWLINE,$!value,$chars)),
nqp::p6box_s(nqp::substrnfg($!value,0,$chars)),
self
)
#?endif
#?if !js
nqp::if(
(nqp::isge_i((my int $chars = nqp::sub_i(nqp::chars($!value),1)),0)
&& nqp::iscclass(nqp::const::CCLASS_NEWLINE,$!value,$chars)),
nqp::p6box_s(nqp::substr($!value,0,$chars)),
self
)
#?endif
}

multi method chop(Str:D:) {
Expand Down Expand Up @@ -345,7 +355,12 @@ my class Str does Stringy { # declared in BOOTSTRAP
method !SET-SELF(\string) {
nqp::stmts(
($!str = nqp::unbox_s(string)),
#?if !js
($!chars = nqp::chars($!str)),
#?endif
#?if js
($!chars = nqp::charsnfg($!str)),
#?endif
($!pos = -1),
self
)
Expand All @@ -360,7 +375,12 @@ my class Str does Stringy { # declared in BOOTSTRAP
method pull-one() {
nqp::if(
nqp::islt_i(($!pos = nqp::add_i($!pos,1)),$!chars),
#?if !js
nqp::p6box_s(nqp::substr($!str,$!pos,1)),
#?endif
#?if js
nqp::p6box_s(nqp::substrnfg($!str,$!pos,1)),
#?endif
IterationEnd
)
}
Expand All @@ -383,7 +403,12 @@ my class Str does Stringy { # declared in BOOTSTRAP
method !SET-SELF(\string,\size,\limit,\inf) {
nqp::stmts(
($!str = nqp::unbox_s(string)),
#?if !js
($!chars = nqp::chars($!str)),
#?endif
#?if js
($!chars = nqp::charsnfg($!str)),
#?endif
($!size = 1 max size),
($!pos = -size),
($!max = 1 + floor( ( $!chars - 1 ) / $!size )),
Expand All @@ -400,15 +425,25 @@ my class Str does Stringy { # declared in BOOTSTRAP
}
method pull-one() {
($!todo = $!todo - 1) && ($!pos = $!pos + $!size) < $!chars
#?if !js
?? nqp::p6box_s(nqp::substr($!str, $!pos, $!size))
#?endif
#?if js
?? nqp::p6box_s(nqp::substrnfg($!str, $!pos, $!size))
#?endif
!! IterationEnd
}
method push-all($target --> IterationEnd) {
my int $todo = $!todo;
my int $pos = $!pos;
my int $size = $!size;
my int $chars = $!chars;
#?if !js
$target.push(nqp::p6box_s(nqp::substr($!str, $pos, $size)))
#?endif
#?if js
$target.push(nqp::p6box_s(nqp::substrnfg($!str, $pos, $size)))
#?endif
while ($todo = $todo - 1 ) && ($pos = $pos + $size) < $chars;
$!pos = $!chars;
}
Expand Down Expand Up @@ -2730,7 +2765,12 @@ my class Str does Stringy { # declared in BOOTSTRAP

proto method chars(|) {*}
multi method chars(Str:D: --> Int:D) {
#?if !js
nqp::p6box_i(nqp::chars($!value))
#?endif
#?if js
nqp::p6box_i(nqp::charsnfg($!value))
#?endif
}
multi method chars(Str:U: --> Int:D) {
self.Str; # generate undefined warning
Expand All @@ -2755,7 +2795,12 @@ my class Str does Stringy { # declared in BOOTSTRAP

proto method tc(|) {*}
multi method tc(Str:D:) {
#?if !js
nqp::p6box_s(nqp::tc(nqp::substr($!value,0,1)) ~ nqp::substr($!value,1));
#?endif
#?if js
nqp::p6box_s(nqp::tc(nqp::substrnfg($!value,0,1)) ~ nqp::substrnfg($!value,1));
#?endif
}
multi method tc(Str:U:) {
self.Str
Expand Down Expand Up @@ -2829,11 +2874,21 @@ multi sub infix:<cmp>(str $a, str $b --> Order:D) {
multi sub infix:<===>(Str:D \a, Str:D \b --> Bool:D) {
nqp::p6bool(
nqp::eqaddr(a.WHAT,b.WHAT)
#?if !js
&& nqp::iseq_s(nqp::unbox_s(a), nqp::unbox_s(b))
#?endif
#?if js
&& nqp::iseq_snfg(nqp::unbox_s(a), nqp::unbox_s(b))
#?endif
)
}
multi sub infix:<===>(str $a, str $b --> Bool:D) {
#?if !js
nqp::p6bool(nqp::iseq_s($a, $b))
#?endif
#?if js
nqp::p6bool(nqp::iseq_snfg($a, $b))
#?endif
}

multi sub infix:<leg>(Str:D \a, Str:D \b --> Order:D) {
Expand All @@ -2844,10 +2899,20 @@ multi sub infix:<leg>(str $a, str $b --> Order:D) {
}

multi sub infix:<eq>(Str:D \a, Str:D \b --> Bool:D) {
#?if !js
nqp::p6bool(nqp::iseq_s(nqp::unbox_s(a), nqp::unbox_s(b)))
#?endif
#?if js
nqp::p6bool(nqp::iseq_snfg(nqp::unbox_s(a), nqp::unbox_s(b)))
#?endif
}
multi sub infix:<eq>(str $a, str $b --> Bool:D) {
nqp::p6bool(nqp::iseq_s($a, $b))
#?if !js
nqp::p6bool(nqp::iseq_snfg($a, $b))
#?endif
#?if js
nqp::p6bool(nqp::iseq_snfg($a, $b))
#?endif
}

multi sub infix:<ne>(Str:D \a, Str:D \b --> Bool:D) {
Expand Down Expand Up @@ -3031,27 +3096,52 @@ sub parse-names(Str:D $str) { $str.parse-names }
proto sub substr(|) {*}
multi sub substr(Str:D \what, Int:D \start) {
my str $str = nqp::unbox_s(what);
#?if !js
my int $max = nqp::chars($str);
#?endif
#?if js
my int $max = nqp::charsnfg($str);
#?endif
my int $from = nqp::unbox_i(start);

Rakudo::Internals.SUBSTR-START-OOR($from,$max).fail
if nqp::islt_i($from,0) || nqp::isgt_i($from,$max);

#?if !js
nqp::p6box_s(nqp::substr($str,$from));
#?endif
#?if js
nqp::p6box_s(nqp::substrnfg($str,$from));
#?endif
}
multi sub substr(Str:D \what, Callable:D \start) {
my str $str = nqp::unbox_s(what);
#?if !js
my int $max = nqp::chars($str);
#?endif
#?if js
my int $max = nqp::charsnfg($str);
#?endif
my int $from = nqp::unbox_i((start)(nqp::p6box_i($max)));

Rakudo::Internals.SUBSTR-START-OOR($from,$max).fail
if nqp::islt_i($from,0) || nqp::isgt_i($from,$max);

#?if !js
nqp::p6box_s(nqp::substr($str,$from));
#?endif
#?if js
nqp::p6box_s(nqp::substrnfg($str,$from));
#?endif
}
multi sub substr(Str:D \what, Int:D \start, Int:D \want) {
my str $str = nqp::unbox_s(what);
my int $max = nqp::chars($str);
#?if !js
my int $max = nqp::chars($str);
#?endif
#?if js
my int $max = nqp::charsnfg($str);
#?endif
my int $from = nqp::unbox_i(start);

Rakudo::Internals.SUBSTR-START-OOR($from,$max).fail
Expand All @@ -3061,15 +3151,25 @@ multi sub substr(Str:D \what, Int:D \start, Int:D \want) {
Rakudo::Internals.SUBSTR-CHARS-OOR($chars).fail
if nqp::islt_i($chars,0);

#?if !js
nqp::p6box_s(nqp::substr($str,$from,$chars));
#?endif
#?if js
nqp::p6box_s(nqp::substrnfg($str,$from,$chars));
#?endif
}
multi sub substr(Str() $what, \start, $want?) {

# should really be int, but \ then doesn't work for rw access
my $r := Rakudo::Internals.SUBSTR-SANITY($what, start, $want, my Int $from, my Int $chars);
nqp::istype($r,Failure)
?? $r
#?if !js
!! nqp::p6box_s(nqp::substr(
#?endif
#?if js
!! nqp::p6box_s(nqp::substrnfg(
#?endif
nqp::unbox_s($what),nqp::unbox_i($from),nqp::unbox_i($chars)
))
}
Expand Down
2 changes: 1 addition & 1 deletion tools/build/NQP_REVISION
@@ -1 +1 @@
2017.11-40-g07548c9d3
2017.12-40-g247c7a022

0 comments on commit 2ce329c

Please sign in to comment.