Skip to content

Instantly share code, notes, and snippets.

@ShimmerFairy
Created July 5, 2011 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShimmerFairy/1065930 to your computer and use it in GitHub Desktop.
Save ShimmerFairy/1065930 to your computer and use it in GitHub Desktop.
chomp fix patch
From 6361d029bb12914182663dfceab759ca023131f3 Mon Sep 17 00:00:00 2001
From: lue <rnddim@gmail.com>
Date: Tue, 5 Jul 2011 13:55:10 -0700
Subject: [PATCH] Fixed chomp to work on strings with less than 2 characters.
Also removed item from LHF.markdown
---
LHF.markdown | 3 ---
src/core/Str.pm | 5 +++--
2 files changed, 3 insertions(+), 5 deletions(-)
diff --git a/LHF.markdown b/LHF.markdown
index 675d3ec..916330b 100644
--- a/LHF.markdown
+++ b/LHF.markdown
@@ -8,9 +8,6 @@ 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.
-
## Basic IO
the stat calls should be pretty straight-forward to port from master
diff --git a/src/core/Str.pm b/src/core/Str.pm
index 6c0ed68..4369e61 100644
--- a/src/core/Str.pm
+++ b/src/core/Str.pm
@@ -19,8 +19,9 @@ my class Str {
multi method perl(Str:D:) { "'" ~ self ~ "'" }
method chomp() {
- my $n_idx = self.chars - 1;
- my $rn_idx = $n_idx - 1;
+ my $n_idx;
+ my $rn_idx;
+ (self.chars < 2) ?? ($n_idx = 0 ; $rn_idx = 0) !! ($n_idx = self.chars - 1 ; $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;
--
1.6.3.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment