Skip to content
Browse files

fix chomp

  • Loading branch information...
1 parent 4336ba9 commit 6d53c0a7587b1e801b641583df59813cd7bf6766 @moritz moritz committed
Showing with 14 additions and 7 deletions.
  1. +6 −2 LHF.markdown
  2. +8 −5 src/core/Str.pm
View
8 LHF.markdown
@@ -8,8 +8,12 @@ Note - do not just copy stuff from the master setting! Often things
need to be done differently in the "nom" branch, to take advantage of
new possible performance.
-## chomp
-Fix Str.chomp to work with strings of less than 2 characters.
+## Implement minmax operator
+Make S03-operators/minmax.t pass
+
+## More Smart-matching
+See S03-smartmatch tests, especially array-array.t, hash-hash.t and
+so forth.
## Implement minmax operator
Make S03-operators/minmax.t pass
View
13 src/core/Str.pm
@@ -16,11 +16,14 @@ my class Str does Stringy {
multi method ACCEPTS(Str:D: $other) { $other eq self }
method chomp() {
- my $n_idx = self.chars - 1;
- my $rn_idx = $n_idx - 1;
- self.substr($rn_idx) eq "\r\n" ?? self.substr(0, $rn_idx) !!
- self.substr($n_idx) eq "\n" ?? self.substr(0, $n_idx) !!
- self;
+ my Int $chars = self.chars;
+ return '' if $chars == 0;
+ my Str $last = nqp::p6box_s(nqp::substr(nqp::unbox_s(self), nqp::unbox_i($chars - 1)));
+ my Int $to_remove = 0;
+ $to_remove = 1 if $last eq "\n" || $last eq "\r";
+ $to_remove = 2 if $chars > 1
+ && nqp::p6box_s(nqp::substr(nqp::unbox_s(self), nqp::unbox_i($chars - 2))) eq "\r\n";
+ nqp::p6box_s(pir::chopn__Ssi(nqp::unbox_s(self),nqp::unbox_i($to_remove)))
}
method chop() {

0 comments on commit 6d53c0a

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