Skip to content

Commit

Permalink
move methods from Cool.pm to Str.pm: <codes chars uc lc tc tclc ord f…
Browse files Browse the repository at this point in the history
…lip>

Boost performance on all of these functions by ~25% when performed
on a Str object; roughly unchanged performance on other Cool types.
  • Loading branch information
Brent Laabs committed Apr 20, 2015
1 parent 4ba4aab commit a96118d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 20 deletions.
25 changes: 11 additions & 14 deletions src/core/Cool.pm
Expand Up @@ -68,11 +68,11 @@ my class Cool { # declared in BOOTSTRAP

## string methods

method chars() {
nqp::p6box_i(nqp::chars(nqp::unbox_s(self.Str)));
method chars() returns Int:D {
self.Str.chars
}
method codes() {
nqp::p6box_i(nqp::chars(nqp::unbox_s(self.Str)));
self.Str.codes
}

method fmt($format = '%s') {
Expand All @@ -89,20 +89,19 @@ my class Cool { # declared in BOOTSTRAP
method substr-rw(\SELF: $from, $length?) { substr-rw(SELF,$from,$length) }

method uc() {
nqp::p6box_s(nqp::uc(nqp::unbox_s(self.Str)))
self.Str.uc
}

method lc() {
nqp::p6box_s(nqp::lc(nqp::unbox_s(self.Str)))
self.Str.lc
}

method tc() {
my $u := nqp::unbox_s(self.Str);
nqp::p6box_s(nqp::uc(nqp::substr($u,0,1)) ~ nqp::substr($u,1));
self.Str.tc
}

method tclc() {
nqp::p6box_s(nqp::tclc(nqp::unbox_s(self.Str)))
self.Str.tclc
}

method wordcase() { self.Str.wordcase }
Expand All @@ -125,20 +124,19 @@ my class Cool { # declared in BOOTSTRAP
}

method ord(--> Int) {
my $s := self.Str;
$s.chars
?? nqp::p6box_i(nqp::ord(nqp::unbox_s($s)))
!! Int;
self.Str.ord
}
method chr() {
self.Int.chr;
}
method chrs(Cool:D:) {
self>>.chr.join;
}
method ords(Cool:D:) { self.Str.ords }


method flip() {
nqp::p6box_s(nqp::flip(nqp::unbox_s(self.Str)))
self.Str.flip
}
method trans(*@a) { self.Str.trans(@a) }

Expand Down Expand Up @@ -222,7 +220,6 @@ my class Cool { # declared in BOOTSTRAP
$result;
}

method ords(Cool:D:) { self.Str.ords }
proto method split(|) {*}
multi method split(Regex $pat, $limit = Inf, :$all) {
self.Stringy.split($pat, $limit, :$all);
Expand Down
68 changes: 62 additions & 6 deletions src/core/Str.pm
Expand Up @@ -1107,7 +1107,7 @@ my class Str does Stringy { # declared in BOOTSTRAP

has str $.unsubstituted_text;
has str $.substituted_text;

submethod BUILD(:$!source, :$!squash, :$!complement) { }

method add_substitution($key, $value) {
Expand Down Expand Up @@ -1443,15 +1443,20 @@ my class Str does Stringy { # declared in BOOTSTRAP
}).join;
}

method codes(Str:D:) returns Int:D {
nqp::p6box_i(nqp::chars(nqp::unbox_s(self)))
}

method path(Str:D:) returns IO::Path:D {
DEPRECATED('IO', |<2014.11 2015.11>);
IO::Path.new(self)
}

proto method codes(|) { * }
multi method codes(Str:D:) returns Int:D {
nqp::p6box_i(nqp::chars(nqp::unbox_s(self)))
}
multi method codes(Str:U:) returns Int:D {
self.Str; # generate undefined warning
0
}

proto method chars(|) { * }
multi method chars(Str:D:) returns Int:D {
nqp::p6box_i(nqp::chars($!value))
Expand All @@ -1461,6 +1466,57 @@ my class Str does Stringy { # declared in BOOTSTRAP
0
}

proto method uc(|) { * }
multi method uc(Str:D:) {
nqp::p6box_s(nqp::uc($!value));
}
multi method uc(Str:U:) {
self.Str;
}

proto method lc(|) { * }
multi method lc(Str:D:) {
nqp::p6box_s(nqp::lc($!value));
}
multi method lc(Str:U:) {
self.Str;
}

proto method tc(|) { * }
multi method tc(Str:D:) {
nqp::p6box_s(nqp::uc(nqp::substr($!value,0,1)) ~ nqp::substr($!value,1));
}
multi method tc(Str:U:) {
self.Str
}

proto method tclc(|) { * }
multi method tclc(Str:D:) {
nqp::p6box_s(nqp::tclc($!value))
}
multi method tclc(Str:U:) {
self.Str
}

proto method flip(|) { * }
multi method flip(Str:D:) {
nqp::p6box_s(nqp::flip($!value))
}
multi method flip(Str:U:) {
self.Str
}

proto method ord(|) { * }
multi method ord(Str:D:) returns Int {
nqp::chars($!value)
?? nqp::p6box_i(nqp::ord($!value))
!! Int;
}
multi method ord(Str:U:) {
self.Str;
Int
}

}


Expand Down Expand Up @@ -1713,7 +1769,7 @@ multi sub substr(Str() $what, \start, $want?) {

sub substr-rw(\what, \start, $want?) is rw {
my $Str := nqp::istype(what,Str) ?? what !! what.Str;

# should really be int, but \ then doesn't work for rw access
my $r := SUBSTR-SANITY($Str, start, $want, my Int $from, my Int $chars);
$r.defined
Expand Down

0 comments on commit a96118d

Please sign in to comment.