From cac15643cf5c62dddf672bfaf239b42c1e108d19 Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Fri, 6 Jan 2012 12:06:26 -0300 Subject: Implement Chars#swapcase. --- activesupport/lib/active_support/multibyte/chars.rb | 8 ++++++++ activesupport/lib/active_support/multibyte/unicode.rb | 8 ++++++++ 2 files changed, 16 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 99b974e4a7..d69bfefc23 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -118,6 +118,14 @@ module ActiveSupport #:nodoc: chars Unicode.downcase(@wrapped_string) end + # Converts characters in the string to the opposite case. + # + # Example: + # 'El Cañón".mb_chars.swapcase.to_s # => "eL cAÑÓN" + def swapcase + chars Unicode.swapcase(@wrapped_string) + end + # Converts the first character to uppercase and the remainder to lowercase. # # Example: diff --git a/activesupport/lib/active_support/multibyte/unicode.rb b/activesupport/lib/active_support/multibyte/unicode.rb index 94fb0a48aa..a0a8f3c97e 100644 --- a/activesupport/lib/active_support/multibyte/unicode.rb +++ b/activesupport/lib/active_support/multibyte/unicode.rb @@ -293,9 +293,17 @@ module ActiveSupport apply_mapping string, :uppercase_mapping end + def swapcase(string) + apply_mapping string, :swapcase_mapping + end + # Holds data about a codepoint in the Unicode database class Codepoint attr_accessor :code, :combining_class, :decomp_type, :decomp_mapping, :uppercase_mapping, :lowercase_mapping + + def swapcase_mapping + uppercase_mapping > 0 ? uppercase_mapping : lowercase_mapping + end end # Holds static data from the Unicode database -- cgit v1.2.3