Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

adds native str ops and return type annotations

  • Loading branch information...
commit b1d83dbd8a08fc82aaec1833fe42dad5e15f203f 1 parent 5e1b9a8
kboga authored August 04, 2012

Showing 1 changed file with 59 additions and 29 deletions. Show diff stats Hide diff stats

  1. 88  src/core/Str.pm
88  src/core/Str.pm
@@ -908,95 +908,125 @@ my class Str does Stringy {
908 908
         }).join;
909 909
     }
910 910
 
911  
-    method codes(Str:D:) {
912  
-        nqp::p6box_i(nqp::chars(nqp::unbox_s(self)));
913  
-
  911
+    method codes(Str:D:) returns Int:D {
  912
+        nqp::p6box_i(nqp::chars(nqp::unbox_s(self)))
914 913
     }
915 914
 
916  
-    method tclc(Str:D:) {
  915
+    method tclc(Str:D:) returns Str:D {
917 916
         nqp::p6box_s(nqp::tclc(nqp::unbox_s(self)))
918 917
     }
919 918
 
920  
-    method path(Str:D:) {
921  
-        IO::Path.new(self);
  919
+    method path(Str:D:) returns IO::Path:D {
  920
+        IO::Path.new(self)
922 921
     }
923 922
 }
924 923
 
925 924
 
926  
-multi prefix:<~>(Str:D \$a) { $a }
  925
+multi prefix:<~>(Str:D \$a) returns Str:D { $a }
  926
+multi prefix:<~>(str $a)    returns str   { $a }
927 927
 
928  
-multi infix:<~>(Str:D \$a, Str:D \$b) {
  928
+multi infix:<~>(Str:D \$a, Str:D \$b) returns Str:D {
929 929
     nqp::p6box_s(nqp::concat_s(nqp::unbox_s($a), nqp::unbox_s($b)))
930 930
 }
  931
+multi infix:<~>(str $a, str $b) returns str { nqp::concat_s($a, $b) }
931 932
 
932  
-multi infix:<x>(Str:D $s, Int:D $repetition) {
933  
-    $repetition <= 0
  933
+multi infix:<x>(Str:D $s, Int:D $repetition) returns Str:D {
  934
+    $repetition < 0
934 935
         ?? ''
935 936
         !!  nqp::p6box_s(nqp::x(nqp::unbox_s($s), nqp::unbox_i($repetition)))
936 937
 }
  938
+multi infix:<x>(str $s, int $repetition) returns str {
  939
+    nqp::if(nqp::islt_i($repetition, 0), '', nqp::x($s, $repetition))
  940
+}
937 941
 
938  
-multi infix:<cmp>(Str:D \$a, Str:D \$b) {
  942
+multi infix:<cmp>(Str:D \$a, Str:D \$b) returns Order:D {
939 943
     Order.(nqp::p6box_i(nqp::cmp_s(nqp::unbox_s($a), nqp::unbox_s($b))))
940 944
 }
  945
+multi infix:<cmp>(str $a, str $b) returns Order:D {
  946
+    Order.(nqp::p6box_i(nqp::cmp_s($a, $b)))
  947
+}
941 948
 
942  
-multi infix:<===>(Str:D \$a, Str:D \$b) {
  949
+multi infix:<===>(Str:D \$a, Str:D \$b) returns Bool:D {
943 950
     nqp::p6bool(nqp::iseq_s(nqp::unbox_s($a), nqp::unbox_s($b)))
944 951
 }
  952
+multi infix:<===>(str $a, str $b) returns Bool:D {
  953
+    nqp::p6bool(nqp::iseq_s($a, $b))
  954
+}
945 955
 
946  
-multi infix:<leg>(Str:D \$a, Str:D \$b) {
  956
+multi infix:<leg>(Str:D \$a, Str:D \$b) returns Order:D {
947 957
     Order.(nqp::p6box_i(nqp::cmp_s(nqp::unbox_s($a), nqp::unbox_s($b))))
948 958
 }
  959
+multi infix:<leg>(str $a, str $b) returns Order:D {
  960
+    Order.(nqp::p6box_i(nqp::cmp_s($a, $b)))
  961
+}
949 962
 
950  
-multi infix:<eq>(Str:D \$a, Str:D \$b) {
  963
+multi infix:<eq>(Str:D \$a, Str:D \$b) returns Bool:D {
951 964
     nqp::p6bool(nqp::iseq_s(nqp::unbox_s($a), nqp::unbox_s($b)))
952 965
 }
  966
+multi infix:<eq>(str $a, str $b) returns Bool:D {
  967
+    nqp::p6bool(nqp::iseq_s($a, $b))
  968
+}
953 969
 
954  
-multi infix:<lt>(Str:D \$a, Str:D \$b) {
  970
+multi infix:<lt>(Str:D \$a, Str:D \$b) returns Bool:D {
955 971
     nqp::p6bool(nqp::islt_s(nqp::unbox_s($a), nqp::unbox_s($b)))
956 972
 }
  973
+multi infix:<lt>(str $a, str $b) returns Bool:D {
  974
+    nqp::p6bool(nqp::islt_s($a, $b))
  975
+}
957 976
 
958  
-multi infix:<le>(Str:D \$a, Str:D \$b) {
  977
+multi infix:<le>(Str:D \$a, Str:D \$b) returns Bool:D {
959 978
     nqp::p6bool(nqp::isle_s(nqp::unbox_s($a), nqp::unbox_s($b)))
960 979
 }
  980
+multi infix:<le>(str $a, str $b) returns Bool:D {
  981
+    nqp::p6bool(nqp::isle_s($a, $b))
  982
+}
961 983
 
962  
-multi infix:<gt>(Str:D \$a, Str:D \$b) {
  984
+multi infix:<gt>(Str:D \$a, Str:D \$b) returns Bool:D {
963 985
     nqp::p6bool(nqp::isgt_s(nqp::unbox_s($a), nqp::unbox_s($b)))
964 986
 }
  987
+multi infix:<gt>(str $a, str $b) returns Bool:D {
  988
+    nqp::p6bool(nqp::isgt_s($a, $b))
  989
+}
965 990
 
966  
-multi infix:<ge>(Str:D \$a, Str:D \$b) {
  991
+multi infix:<ge>(Str:D \$a, Str:D \$b) returns Bool:D {
967 992
     nqp::p6bool(nqp::isge_s(nqp::unbox_s($a), nqp::unbox_s($b)))
968 993
 }
  994
+multi infix:<le>(str $a, str $b) returns Bool:D {
  995
+    nqp::p6bool(nqp::isle_s($a, $b))
  996
+}
969 997
 
970  
-
971  
-multi infix:<~|>(Str:D \$a, Str:D \$b) {
  998
+multi infix:<~|>(Str:D \$a, Str:D \$b) returns Str:D {
972 999
     nqp::p6box_s(nqp::bitor_s(nqp::unbox_s($a), nqp::unbox_s($b)))
973 1000
 }
  1001
+multi infix:<~|>(str $a, str $b) returns str { nqp::bitor_s($a, $b) }
974 1002
 
975  
-multi infix:<~&>(Str:D \$a, Str:D \$b) {
  1003
+multi infix:<~&>(Str:D \$a, Str:D \$b) returns Str:D {
976 1004
     nqp::p6box_s(nqp::bitand_s(nqp::unbox_s($a), nqp::unbox_s($b)))
977 1005
 }
  1006
+multi infix:<~&>(str $a, str $b) returns str { nqp::bitand_s($a, $b) }
978 1007
 
979  
-multi infix:<~^>(Str:D \$a, Str:D \$b) {
  1008
+multi infix:<~^>(Str:D \$a, Str:D \$b) returns Str:D {
980 1009
     nqp::p6box_s(nqp::bitxor_s(nqp::unbox_s($a), nqp::unbox_s($b)))
981 1010
 }
  1011
+multi infix:<~^>(str $a, str $b) returns str { nqp::bitxor_s($a, $b) }
982 1012
 
983 1013
 multi prefix:<~^>(Str \$a) {
984 1014
     fail "prefix:<~^> NYI";   # XXX
985 1015
 }
986 1016
 
987  
-multi sub ords(Str $s) {
  1017
+multi sub ords(Str $s) returns List:D {
988 1018
     my Int $c  = $s.chars;
989 1019
     my str $ns = nqp::unbox_s($s);
990 1020
     (^$c).map: { nqp::p6box_i(nqp::ord(nqp::substr($ns, $_, 1))) }
991 1021
 }
992 1022
 
993 1023
 # TODO: Cool  variants
994  
-sub trim         (Str:D $s) { $s.trim }
995  
-sub trim-leading (Str:D $s) { $s.trim-leading }
996  
-sub trim-trailing(Str:D $s) { $s.trim-trailing }
  1024
+sub trim         (Str:D $s) returns Str:D { $s.trim }
  1025
+sub trim-leading (Str:D $s) returns Str:D { $s.trim-leading }
  1026
+sub trim-trailing(Str:D $s) returns Str:D { $s.trim-trailing }
997 1027
 
998 1028
 # the opposite of Real.base, used for :16($hex_str)
999  
-sub unbase(Int:D $base, Str:D $str) {
  1029
+sub unbase(Int:D $base, Str:D $str) returns Numeric:D {
1000 1030
     my Str $prefix = $str.substr(0, 2);
1001 1031
     if    $base <= 10 && $prefix eq any(<0x 0d 0o 0b>)
1002 1032
        or $base <= 24 && $prefix eq any <0o 0x>
@@ -1008,7 +1038,7 @@ sub unbase(Int:D $base, Str:D $str) {
1008 1038
     }
1009 1039
 }
1010 1040
 
1011  
-sub chrs(*@c) {
  1041
+sub chrs(*@c) returns Str:D {
1012 1042
     @c.map({.chr}).join('');
1013 1043
 }
1014 1044
 
@@ -1024,6 +1054,6 @@ sub substr-rw($s is rw, $from = 0, $chars = $s.chars - $from) {
1024 1054
     );
1025 1055
 }
1026 1056
 
1027  
-multi sub tclc(Str:D $s) {
  1057
+multi sub tclc(Str:D $s) returns Str:D {
1028 1058
     nqp::p6box_s(nqp::tclc(nqp::unbox_s($s)));
1029 1059
 }

0 notes on commit b1d83db

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