From bdaf3348de0ea99525c685f9acfd7abb523f59c5 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 3 Feb 2009 18:41:06 -0800 Subject: Ruby 1.9: force ascii encoding for comparison with utf8 regexp --- activesupport/lib/active_support/multibyte/chars.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activesupport/lib/active_support/multibyte') diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index a00b165222..62b6d798ef 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -617,6 +617,8 @@ module ActiveSupport #:nodoc: # Replaces all ISO-8859-1 or CP1252 characters by their UTF-8 equivalent resulting in a valid UTF-8 string. def tidy_bytes(string) string.split(//u).map do |c| + c.force_encoding(Encoding::ASCII) if c.respond_to?(:force_encoding) + if !UTF8_PAT.match(c) n = c.unpack('C')[0] n < 128 ? n.chr : -- cgit v1.2.3 From 74387884819e8b6e16c3f67388610b8d1192cb4d Mon Sep 17 00:00:00 2001 From: George Ogata Date: Tue, 30 Dec 2008 14:16:15 +0100 Subject: Make Chars#slice! behave more like String#slice! [#1243 state:resolved] - Chars#slice! now returns the slice instead of itself - Chars#slice! now removes the slice from itself Signed-off-by: Pratik Naik --- activesupport/lib/active_support/multibyte/chars.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'activesupport/lib/active_support/multibyte') diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index 62b6d798ef..60f082bcc1 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -344,7 +344,19 @@ module ActiveSupport #:nodoc: end alias_method :[], :slice - # Converts first character in the string to Unicode value + # Like String#slice!, except instead of byte offsets you specify character offsets. + # + # Example: + # s = 'こんにちは' + # s.mb_chars.slice!(2..3).to_s #=> "にち" + # s #=> "こんは" + def slice!(*args) + slice = self[*args] + self[*args] = '' + slice + end + + # Returns the codepoint of the first character in the string. # # Example: # 'こんにちは'.mb_chars.ord #=> 12371 @@ -432,7 +444,7 @@ module ActiveSupport #:nodoc: chars(self.class.tidy_bytes(@wrapped_string)) end - %w(lstrip rstrip strip reverse upcase downcase slice tidy_bytes capitalize).each do |method| + %w(lstrip rstrip strip reverse upcase downcase tidy_bytes capitalize).each do |method| define_method("#{method}!") do |*args| unless args.nil? @wrapped_string = send(method, *args).to_s -- cgit v1.2.3