Skip to content
Browse files

implement and test .ords, &ords()

  • Loading branch information...
1 parent 850e5c6 commit cb9925401dcdf71837d7a61e035e0908bf0c8447 @moritz moritz committed Jul 10, 2011
Showing with 17 additions and 0 deletions.
  1. +4 −0 src/core/Cool.pm
  2. +12 −0 src/core/Str.pm
  3. +1 −0 t/spectest.data
View
4 src/core/Cool.pm
@@ -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 }
@@ -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) }
View
12 src/core/Str.pm
@@ -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))) }
+ }
}
@@ -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))) }
+}
View
1 t/spectest.data
@@ -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

0 comments on commit cb99254

Please sign in to comment.
Something went wrong with that request. Please try again.