Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
implement and test .ords, &ords()
  • Loading branch information
moritz committed Jul 10, 2011
1 parent 850e5c6 commit cb99254
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/core/Cool.pm
Expand Up @@ -80,6 +80,8 @@ my class Cool {
# TODO: fail() instead of returning Str
$result < 0 ?? Str !! $result;
}

method ords(Cool:D:) { self.Str.ords }
}

sub chop($s) { $s.chop }
Expand All @@ -94,3 +96,5 @@ sub substr($s,$pos,$chars?) { $s.substr($pos,$chars) }
sub uc($s) { $s.uc }
sub ucfirst($s) { $s.ucfirst }

proto sub ords(|$) { * }
multi sub ords(Cool $s) { ords($s.Stringy) }
12 changes: 12 additions & 0 deletions src/core/Str.pm
Expand Up @@ -142,6 +142,12 @@ my class Str does Stringy {
method comb() {
(^self.chars).map({self.substr($_, 1) });
}

method ords(Str:D:) {
my Int $c = self.chars;
my str $ns = nqp::unbox_s(self);
(^$c).map: { nqp::p6box_i(nqp::ord(nqp::substr($ns, $_, 1))) }
}
}


Expand Down Expand Up @@ -207,3 +213,9 @@ multi infix:<~^>(Str \$a, Str \$b) {
multi prefix:<~^>(Str \$a) {
fail "prefix:<~^> NYI"; # XXX
}

multi sub ords(Str $s) {
my Int $c = $s.chars;
my str $ns = nqp::unbox_s($s);
(^$c).map: { nqp::p6box_i(nqp::ord(nqp::substr($ns, $_, 1))) }
}
1 change: 1 addition & 0 deletions t/spectest.data
Expand Up @@ -542,6 +542,7 @@ S32-str/flip.t
S32-str/lc.t # icu
S32-str/lcfirst.t # icu
# S32-str/lines.t # err: Method 'lines' not found for invocant of class 'Str'
S32-str/ords.t
# S32-str/p5chomp.t # err: Could not find sub &p5chomp
# S32-str/p5chop.t # err: Could not find sub &p5chop
# S32-str/pack.t # err: Could not find sub &pack
Expand Down

0 comments on commit cb99254

Please sign in to comment.