Skip to content

Instantly share code, notes, and snippets.

@ciniglio
Created December 5, 2012 18:35
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 ciniglio/4218251 to your computer and use it in GitHub Desktop.
Save ciniglio/4218251 to your computer and use it in GitHub Desktop.
Added multi-byte support for string#chop
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment