From 032902146a6ff8a194901ba91cd4d6b00b9eb411 Mon Sep 17 00:00:00 2001 From: Jason Cheow Date: Wed, 26 Nov 2008 22:16:28 +0800 Subject: [PATCH] Add ActiveSupport::Multibyte::Chars#ord method so that it returns correct Unicode value instead of falling back on String#ord in CoreExtensions, which is not multibyte compatible [#1483 state:committed] Signed-off-by: Jeremy Kemper --- activesupport/CHANGELOG | 7 ++++++- activesupport/lib/active_support/multibyte/chars.rb | 8 ++++++++ activesupport/test/multibyte_chars_test.rb | 4 ++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 1cd5e584662d..b6c60d535d08 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,4 +1,9 @@ -*2.2 (November 21st, 2008)* +*2.2.3 (next release)* + +* Multibyte: add multibyte-safe Chars#ord rather than falling back to String#ord. #1483 [Jason Cheow] + + +*2.2.2 (November 21st, 2008)* * TimeZone offset tests: use current_period, to ensure TimeZone#utc_offset is up-to-date [Geoff Buesing] diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index be9c6d3567f4..a00b165222b3 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -344,6 +344,14 @@ def slice(*args) end alias_method :[], :slice + # Converts first character in the string to Unicode value + # + # Example: + # 'こんにちは'.mb_chars.ord #=> 12371 + def ord + self.class.u_unpack(@wrapped_string)[0] + end + # Convert characters in the string to uppercase. # # Example: diff --git a/activesupport/test/multibyte_chars_test.rb b/activesupport/test/multibyte_chars_test.rb index ca2af9b9864f..067c46183795 100644 --- a/activesupport/test/multibyte_chars_test.rb +++ b/activesupport/test/multibyte_chars_test.rb @@ -397,6 +397,10 @@ def test_slice_should_throw_exceptions_on_invalid_arguments assert_raise(ArgumentError) { @chars.slice(1, 1, 1) } end + def test_ord_should_return_unicode_value_for_first_character + assert_equal 12371, @chars.ord + end + def test_upcase_should_upcase_ascii_characters assert_equal '', ''.mb_chars.upcase assert_equal 'ABC', 'aBc'.mb_chars.upcase