public
Created

Added multi-byte support for string#chop

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
From 2f5fa63bf565c7dcc6f3ff4808eab337f1240dff Mon Sep 17 00:00:00 2001
From: Alejandro Ciniglio <mail@alejandrociniglio.com>
Date: Wed, 5 Dec 2012 12:08:45 -0500
Subject: [PATCH] Added multi-byte support for String#chop
 
Added support for string19
Added spec to test under 1.9
---
kernel/common/string19.rb | 11 ++++++++++-
spec/ruby/core/string/chop_spec.rb | 5 +++++
2 files changed, 15 insertions(+), 1 deletion(-)
 
diff --git a/kernel/common/string19.rb b/kernel/common/string19.rb
index 9e54523..c20b41d 100644
--- a/kernel/common/string19.rb
+++ b/kernel/common/string19.rb
@@ -487,7 +487,16 @@ class String
@data[@num_bytes-1] == 10 and @data[@num_bytes-2] == 13
self.num_bytes -= 2
else
- self.num_bytes -= 1
+ if @data[@num_bytes-1] & 128 == 0
+ self.num_bytes -= 1
+ elsif @data[@num_bytes-2] & (128|64) == (128|64)
+ self.num_bytes -= 2
+ elsif @data[@num_bytes-3] & (128|64|32) == (128|64|32)
+ self.num_bytes -= 3
+ elsif @data[@num_bytes-4] & (128|64|32|16) == (128|64|32|16)
+ self.num_bytes -= 4
+ end
+
end
self
diff --git a/spec/ruby/core/string/chop_spec.rb b/spec/ruby/core/string/chop_spec.rb
index fb18eef..fa91c0d 100644
--- a/spec/ruby/core/string/chop_spec.rb
+++ b/spec/ruby/core/string/chop_spec.rb
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes.rb', __FILE__)
@@ -46,6 +47,10 @@ describe "String#chop" do
"hello".untrust.chop.untrusted?.should == true
"".untrust.chop.untrusted?.should == true
end
+
+ it "works with multi-byte characters" do
+ "日本語".chop.should == "日本"
+ end
end
it "returns subclass instances when called on a subclass" do
--
1.8.0.1

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.