From 5fb5ced0cba3306e0f39e45eb299fd0ea32cdd4b Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Thu, 5 Jan 2012 11:39:45 -0300 Subject: Remove overidden slice and slice! methods. --- .../lib/active_support/multibyte/chars.rb | 30 +--------------------- 1 file changed, 1 insertion(+), 29 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index dcc176e93f..266cfc2eda 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -150,34 +150,6 @@ module ActiveSupport #:nodoc: chars(Unicode.g_unpack(@wrapped_string).reverse.flatten.pack('U*')) end - # Implements Unicode-aware slice with codepoints. Slicing on one point returns the codepoints for that - # character. - # - # Example: - # 'こんにちは'.mb_chars.slice(2..3).to_s # => "にち" - def slice(*args) - if args.size > 2 - raise ArgumentError, "wrong number of arguments (#{args.size} for 1)" # Do as if we were native - elsif (args.size == 2 && !(args.first.is_a?(Numeric) || args.first.is_a?(Regexp))) - raise TypeError, "cannot convert #{args.first.class} into Integer" # Do as if we were native - elsif (args.size == 2 && !args[1].is_a?(Numeric)) - raise TypeError, "cannot convert #{args[1].class} into Integer" # Do as if we were native - elsif args[0].kind_of? Range - cps = Unicode.u_unpack(@wrapped_string).slice(*args) - result = cps.nil? ? nil : cps.pack('U*') - elsif args[0].kind_of? Regexp - result = @wrapped_string.slice(*args) - elsif args.size == 1 && args[0].kind_of?(Numeric) - character = Unicode.u_unpack(@wrapped_string)[args[0]] - result = character && [character].pack('U') - else - cps = Unicode.u_unpack(@wrapped_string).slice(*args) - result = cps && cps.pack('U*') - end - result && chars(result) - end - alias_method :[], :slice - # Limit the byte size of the string to a number of bytes without breaking characters. Usable # when the storage for a string is limited for some reason. # @@ -265,7 +237,7 @@ module ActiveSupport #:nodoc: chars(Unicode.tidy_bytes(@wrapped_string, force)) end - %w(capitalize downcase lstrip reverse rstrip slice strip tidy_bytes upcase).each do |method| + %w(capitalize downcase lstrip reverse rstrip strip tidy_bytes upcase).each do |method| # Only define a corresponding bang method for methods defined in the proxy; On 1.9 the proxy will # exclude lstrip!, rstrip! and strip! because they are already work as expected on multibyte strings. if public_method_defined?(method) -- cgit v1.2.3 From 4387f9724273ee477ff60876b360d0abcee0e344 Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Thu, 5 Jan 2012 11:40:01 -0300 Subject: Override #slice! to ensure proper return value. The default pass-through to `method_missing` makes `#slice!` return `self` rather than the string that was sliced off. --- activesupport/lib/active_support/multibyte/chars.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index 266cfc2eda..9ca977012c 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -142,6 +142,10 @@ module ActiveSupport #:nodoc: end end + def slice!(*args) + chars(@wrapped_string.slice!(*args)) + end + # Reverses all characters in the string. # # Example: -- cgit v1.2.3 From c4b522d3c89208b554780e9c49747d07dbe3a4e5 Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Thu, 5 Jan 2012 11:51:10 -0300 Subject: Make return value from bang methods match Ruby docs The docs for the String class indicate that methods like `rstrip!` and others should return nil when they do not have an effect on the string. --- activesupport/lib/active_support/multibyte/chars.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index 9ca977012c..027aadb029 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -47,8 +47,8 @@ module ActiveSupport #:nodoc: # Forward all undefined methods to the wrapped string. def method_missing(method, *args, &block) if method.to_s =~ /!$/ - @wrapped_string.__send__(method, *args, &block) - self + result = @wrapped_string.__send__(method, *args, &block) + self if result else result = @wrapped_string.__send__(method, *args, &block) result.kind_of?(String) ? chars(result) : result -- cgit v1.2.3 From 7301aa2e0d3b16b96fcbffcd79ee9bbf6bc65c57 Mon Sep 17 00:00:00 2001 From: Sergey Nartimov Date: Thu, 5 Jan 2012 18:25:27 +0300 Subject: refactor AS::Multibyte::Chars --- .../lib/active_support/multibyte/chars.rb | 44 +++------------------- 1 file changed, 5 insertions(+), 39 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index dcc176e93f..1a9c2c7ef6 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -150,34 +150,6 @@ module ActiveSupport #:nodoc: chars(Unicode.g_unpack(@wrapped_string).reverse.flatten.pack('U*')) end - # Implements Unicode-aware slice with codepoints. Slicing on one point returns the codepoints for that - # character. - # - # Example: - # 'こんにちは'.mb_chars.slice(2..3).to_s # => "にち" - def slice(*args) - if args.size > 2 - raise ArgumentError, "wrong number of arguments (#{args.size} for 1)" # Do as if we were native - elsif (args.size == 2 && !(args.first.is_a?(Numeric) || args.first.is_a?(Regexp))) - raise TypeError, "cannot convert #{args.first.class} into Integer" # Do as if we were native - elsif (args.size == 2 && !args[1].is_a?(Numeric)) - raise TypeError, "cannot convert #{args[1].class} into Integer" # Do as if we were native - elsif args[0].kind_of? Range - cps = Unicode.u_unpack(@wrapped_string).slice(*args) - result = cps.nil? ? nil : cps.pack('U*') - elsif args[0].kind_of? Regexp - result = @wrapped_string.slice(*args) - elsif args.size == 1 && args[0].kind_of?(Numeric) - character = Unicode.u_unpack(@wrapped_string)[args[0]] - result = character && [character].pack('U') - else - cps = Unicode.u_unpack(@wrapped_string).slice(*args) - result = cps && cps.pack('U*') - end - result && chars(result) - end - alias_method :[], :slice - # Limit the byte size of the string to a number of bytes without breaking characters. Usable # when the storage for a string is limited for some reason. # @@ -265,14 +237,10 @@ module ActiveSupport #:nodoc: chars(Unicode.tidy_bytes(@wrapped_string, force)) end - %w(capitalize downcase lstrip reverse rstrip slice strip tidy_bytes upcase).each do |method| - # Only define a corresponding bang method for methods defined in the proxy; On 1.9 the proxy will - # exclude lstrip!, rstrip! and strip! because they are already work as expected on multibyte strings. - if public_method_defined?(method) - define_method("#{method}!") do |*args| - @wrapped_string = send(args.nil? ? method : method, *args).to_s - self - end + %w(capitalize downcase reverse slice tidy_bytes upcase).each do |method| + define_method("#{method}!") do |*args| + @wrapped_string = send(method, *args).to_s + self end end @@ -282,10 +250,8 @@ module ActiveSupport #:nodoc: return nil if byte_offset.nil? return 0 if @wrapped_string == '' - @wrapped_string = @wrapped_string.dup.force_encoding(Encoding::ASCII_8BIT) - begin - @wrapped_string[0...byte_offset].unpack('U*').length + @wrapped_string.byteslice(0...byte_offset).unpack('U*').length rescue ArgumentError byte_offset -= 1 retry -- cgit v1.2.3 From 60bbdf7d83a5cc1ee5be053d438fc56172b1ea84 Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Thu, 5 Jan 2012 15:43:06 -0300 Subject: Just delegate a few methods directly to @wrapped_string --- .../lib/active_support/multibyte/chars.rb | 23 +++------------------- 1 file changed, 3 insertions(+), 20 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index fbc469ae12..90544a2e64 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -1,6 +1,7 @@ # encoding: utf-8 require 'active_support/core_ext/string/access' require 'active_support/core_ext/string/behavior' +require 'active_support/core_ext/module/delegation' module ActiveSupport #:nodoc: module Multibyte #:nodoc: @@ -38,6 +39,8 @@ module ActiveSupport #:nodoc: alias to_s wrapped_string alias to_str wrapped_string + delegate :<=>, :=~, :acts_like_string?, :to => :wrapped_string + # Creates a new Chars instance by wrapping _string_. def initialize(string) @wrapped_string = string @@ -61,11 +64,6 @@ module ActiveSupport #:nodoc: super || @wrapped_string.respond_to?(method, include_private) end - # Enable more predictable duck-typing on String-like classes. See Object#acts_like?. - def acts_like_string? - true - end - # Returns +true+ when the proxy class can handle the string. Returns +false+ otherwise. def self.consumes?(string) # Unpack is a little bit faster than regular expressions. @@ -77,21 +75,6 @@ module ActiveSupport #:nodoc: include Comparable - # Returns -1, 0, or 1, depending on whether the Chars object is to be sorted before, - # equal or after the object on the right side of the operation. It accepts any object - # that implements +to_s+: - # - # 'é'.mb_chars <=> 'ü'.mb_chars # => -1 - # - # See String#<=> for more details. - def <=>(other) - @wrapped_string <=> other.to_s - end - - def =~(other) - @wrapped_string =~ other - end - # Works just like String#split, with the exception that the items in the resulting list are Chars # instances instead of String. This makes chaining methods easier. # -- cgit v1.2.3 From 4b5a3d7367ecdfcff1c2ffe1b299a9865d66ab7e Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Thu, 5 Jan 2012 16:14:39 -0300 Subject: Remove useless parens --- activesupport/lib/active_support/multibyte/unicode.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/multibyte/unicode.rb b/activesupport/lib/active_support/multibyte/unicode.rb index 754ca9290b..0197d86adc 100644 --- a/activesupport/lib/active_support/multibyte/unicode.rb +++ b/activesupport/lib/active_support/multibyte/unicode.rb @@ -123,7 +123,7 @@ module ActiveSupport # Example: # Unicode.g_pack(Unicode.g_unpack('क्षि')) # => 'क्षि' def g_pack(unpacked) - (unpacked.flatten).pack('U*') + unpacked.flatten.pack('U*') end # Re-order codepoints so the string becomes canonical. -- cgit v1.2.3 From d2a4acdbe3027e447c888558bb36d1aef2bfccf9 Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Thu, 5 Jan 2012 16:17:12 -0300 Subject: Update to Unicode 6.0 --- .../lib/active_support/multibyte/unicode.rb | 2 +- .../lib/active_support/values/unicode_tables.dat | Bin 813343 -> 877274 bytes 2 files changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/multibyte/unicode.rb b/activesupport/lib/active_support/multibyte/unicode.rb index 0197d86adc..b35e7b9b80 100644 --- a/activesupport/lib/active_support/multibyte/unicode.rb +++ b/activesupport/lib/active_support/multibyte/unicode.rb @@ -10,7 +10,7 @@ module ActiveSupport NORMALIZATION_FORMS = [:c, :kc, :d, :kd] # The Unicode version that is supported by the implementation - UNICODE_VERSION = '5.2.0' + UNICODE_VERSION = '6.0.0' # The default normalization used for operations that require normalization. It can be set to any of the # normalizations in NORMALIZATION_FORMS. diff --git a/activesupport/lib/active_support/values/unicode_tables.dat b/activesupport/lib/active_support/values/unicode_tables.dat index 4fe0268cca..7edc4663e8 100644 Binary files a/activesupport/lib/active_support/values/unicode_tables.dat and b/activesupport/lib/active_support/values/unicode_tables.dat differ -- cgit v1.2.3 From 46579340c0847c433c0f9c3f46c79cd4521d9234 Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Thu, 5 Jan 2012 16:21:59 -0300 Subject: Assume Encoding support --- activesupport/lib/active_support/multibyte/chars.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index 90544a2e64..1505d3e604 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -66,11 +66,7 @@ module ActiveSupport #:nodoc: # Returns +true+ when the proxy class can handle the string. Returns +false+ otherwise. def self.consumes?(string) - # Unpack is a little bit faster than regular expressions. - string.unpack('U*') - true - rescue ArgumentError - false + string.encoding == Encoding::UTF_8 end include Comparable -- cgit v1.2.3 From b81bef531c9ebe08d39a4859b01824b390866675 Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Thu, 5 Jan 2012 16:26:24 -0300 Subject: Just use Ruby's String#[]= --- .../lib/active_support/multibyte/chars.rb | 41 ---------------------- 1 file changed, 41 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index 1505d3e604..8cd3cfe455 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -80,47 +80,6 @@ module ActiveSupport #:nodoc: @wrapped_string.split(*args).map { |i| i.mb_chars } end - # Like String#[]=, except instead of byte offsets you specify character offsets. - # - # Example: - # - # s = "Müller" - # s.mb_chars[2] = "e" # Replace character with offset 2 - # s - # # => "Müeler" - # - # s = "Müller" - # s.mb_chars[1, 2] = "ö" # Replace 2 characters at character offset 1 - # s - # # => "Möler" - def []=(*args) - replace_by = args.pop - # Indexed replace with regular expressions already works - if args.first.is_a?(Regexp) - @wrapped_string[*args] = replace_by - else - result = Unicode.u_unpack(@wrapped_string) - case args.first - when Fixnum - raise IndexError, "index #{args[0]} out of string" if args[0] >= result.length - min = args[0] - max = args[1].nil? ? min : (min + args[1] - 1) - range = Range.new(min, max) - replace_by = [replace_by].pack('U') if replace_by.is_a?(Fixnum) - when Range - raise RangeError, "#{args[0]} out of range" if args[0].min >= result.length - range = args[0] - else - needle = args[0].to_s - min = index(needle) - max = min + Unicode.u_unpack(needle).length - 1 - range = Range.new(min, max) - end - result[range] = Unicode.u_unpack(replace_by) - @wrapped_string.replace(result.pack('U*')) - end - end - def slice!(*args) chars(@wrapped_string.slice!(*args)) end -- cgit v1.2.3 From 262af66d0578b748fb878e2b0d36eb40af722efc Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Thu, 5 Jan 2012 16:36:09 -0300 Subject: Document method definition --- activesupport/lib/active_support/multibyte/chars.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index 8cd3cfe455..ba2ada2c1a 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -80,6 +80,8 @@ module ActiveSupport #:nodoc: @wrapped_string.split(*args).map { |i| i.mb_chars } end + # Works like like String#slice!, but returns an instance of Chars, or nil if the string was not + # modified. def slice!(*args) chars(@wrapped_string.slice!(*args)) end -- cgit v1.2.3 From c973161c388dbf4e175c59fd8939b794c5f662cb Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Thu, 5 Jan 2012 16:36:21 -0300 Subject: Remove unused code. --- .../lib/active_support/multibyte/chars.rb | 25 ---------------------- 1 file changed, 25 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index ba2ada2c1a..fc0650dbbe 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -202,31 +202,6 @@ module ActiveSupport #:nodoc: end end - def justify(integer, way, padstr=' ') #:nodoc: - raise ArgumentError, "zero width padding" if padstr.length == 0 - padsize = integer - size - padsize = padsize > 0 ? padsize : 0 - case way - when :right - result = @wrapped_string.dup.insert(0, padding(padsize, padstr)) - when :left - result = @wrapped_string.dup.insert(-1, padding(padsize, padstr)) - when :center - lpad = padding((padsize / 2.0).floor, padstr) - rpad = padding((padsize / 2.0).ceil, padstr) - result = @wrapped_string.dup.insert(0, lpad).insert(-1, rpad) - end - chars(result) - end - - def padding(padsize, padstr=' ') #:nodoc: - if padsize != 0 - chars(padstr * ((padsize / Unicode.u_unpack(padstr).size) + 1)).slice(0, padsize) - else - '' - end - end - def chars(string) #:nodoc: self.class.new(string) end -- cgit v1.2.3 From 9ea34ad3875243b9dd57a7fbd9ff6fa9c55872ac Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Thu, 5 Jan 2012 16:43:28 -0300 Subject: Remove "_codepoints" from compose/decompose --- activesupport/lib/active_support/multibyte/chars.rb | 4 ++-- activesupport/lib/active_support/multibyte/unicode.rb | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index fc0650dbbe..1389cb50c9 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -153,7 +153,7 @@ module ActiveSupport #:nodoc: # 'é'.length # => 2 # 'é'.mb_chars.decompose.to_s.length # => 3 def decompose - chars(Unicode.decompose_codepoints(:canonical, Unicode.u_unpack(@wrapped_string)).pack('U*')) + chars(Unicode.decompose(:canonical, Unicode.u_unpack(@wrapped_string)).pack('U*')) end # Performs composition on all the characters. @@ -162,7 +162,7 @@ module ActiveSupport #:nodoc: # 'é'.length # => 3 # 'é'.mb_chars.compose.to_s.length # => 2 def compose - chars(Unicode.compose_codepoints(Unicode.u_unpack(@wrapped_string)).pack('U*')) + chars(Unicode.compose(Unicode.u_unpack(@wrapped_string)).pack('U*')) end # Returns the number of grapheme clusters in the string. diff --git a/activesupport/lib/active_support/multibyte/unicode.rb b/activesupport/lib/active_support/multibyte/unicode.rb index b35e7b9b80..c689c14631 100644 --- a/activesupport/lib/active_support/multibyte/unicode.rb +++ b/activesupport/lib/active_support/multibyte/unicode.rb @@ -143,7 +143,7 @@ module ActiveSupport end # Decompose composed characters to the decomposed form. - def decompose_codepoints(type, codepoints) + def decompose(type, codepoints) codepoints.inject([]) do |decomposed, cp| # if it's a hangul syllable starter character if HANGUL_SBASE <= cp and cp < HANGUL_SLAST @@ -156,7 +156,7 @@ module ActiveSupport decomposed.concat ncp # if the codepoint is decomposable in with the current decomposition type elsif (ncp = database.codepoints[cp].decomp_mapping) and (!database.codepoints[cp].decomp_type || type == :compatability) - decomposed.concat decompose_codepoints(type, ncp.dup) + decomposed.concat decompose(type, ncp.dup) else decomposed << cp end @@ -164,7 +164,7 @@ module ActiveSupport end # Compose decomposed characters to the composed form. - def compose_codepoints(codepoints) + def compose(codepoints) pos = 0 eoa = codepoints.length - 1 starter_pos = 0 @@ -286,13 +286,13 @@ module ActiveSupport codepoints = u_unpack(string) case form when :d - reorder_characters(decompose_codepoints(:canonical, codepoints)) + reorder_characters(decompose(:canonical, codepoints)) when :c - compose_codepoints(reorder_characters(decompose_codepoints(:canonical, codepoints))) + compose(reorder_characters(decompose(:canonical, codepoints))) when :kd - reorder_characters(decompose_codepoints(:compatability, codepoints)) + reorder_characters(decompose(:compatability, codepoints)) when :kc - compose_codepoints(reorder_characters(decompose_codepoints(:compatability, codepoints))) + compose(reorder_characters(decompose(:compatability, codepoints))) else raise ArgumentError, "#{form} is not a valid normalization variant", caller end.pack('U*') -- cgit v1.2.3 From 51648a6fee31c9642d3ce8899a1c718e1604f4bc Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Thu, 5 Jan 2012 17:02:14 -0300 Subject: Remove multibyte utils This is neither a public API, nor used internally, so let's remove it. --- activesupport/lib/active_support/multibyte.rb | 21 +---------------- .../lib/active_support/multibyte/utils.rb | 27 ---------------------- 2 files changed, 1 insertion(+), 47 deletions(-) delete mode 100644 activesupport/lib/active_support/multibyte/utils.rb (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/multibyte.rb b/activesupport/lib/active_support/multibyte.rb index 57e8e24bf4..cabe073616 100644 --- a/activesupport/lib/active_support/multibyte.rb +++ b/activesupport/lib/active_support/multibyte.rb @@ -21,24 +21,5 @@ module ActiveSupport #:nodoc: def self.proxy_class @proxy_class ||= ActiveSupport::Multibyte::Chars end - - # Regular expressions that describe valid byte sequences for a character - VALID_CHARACTER = { - # Borrowed from the Kconv library by Shinji KONO - (also as seen on the W3C site) - 'UTF-8' => /\A(?: - [\x00-\x7f] | - [\xc2-\xdf] [\x80-\xbf] | - \xe0 [\xa0-\xbf] [\x80-\xbf] | - [\xe1-\xef] [\x80-\xbf] [\x80-\xbf] | - \xf0 [\x90-\xbf] [\x80-\xbf] [\x80-\xbf] | - [\xf1-\xf3] [\x80-\xbf] [\x80-\xbf] [\x80-\xbf] | - \xf4 [\x80-\x8f] [\x80-\xbf] [\x80-\xbf])\z /xn, - # Quick check for valid Shift-JIS characters, disregards the odd-even pairing - 'Shift_JIS' => /\A(?: - [\x00-\x7e\xa1-\xdf] | - [\x81-\x9f\xe0-\xef] [\x40-\x7e\x80-\x9e\x9f-\xfc])\z /xn - } end -end - -require 'active_support/multibyte/utils' \ No newline at end of file +end \ No newline at end of file diff --git a/activesupport/lib/active_support/multibyte/utils.rb b/activesupport/lib/active_support/multibyte/utils.rb deleted file mode 100644 index bd6d4bad41..0000000000 --- a/activesupport/lib/active_support/multibyte/utils.rb +++ /dev/null @@ -1,27 +0,0 @@ -# encoding: utf-8 - -module ActiveSupport #:nodoc: - module Multibyte #:nodoc: - # Returns a regular expression that matches valid characters in the current encoding - def self.valid_character - VALID_CHARACTER[Encoding.default_external.to_s] - end - - # Verifies the encoding of a string - def self.verify(string) - string.valid_encoding? - end - - # Verifies the encoding of the string and raises an exception when it's not valid - def self.verify!(string) - raise EncodingError.new("Found characters with invalid encoding") unless verify(string) - end - - # Removes all invalid characters from the string. - # - # Note: this method is a no-op in Ruby 1.9 - def self.clean(string) - string - end - end -end -- cgit v1.2.3 From 3fe7ca1dbea75ae83cd2eb868ba3f8518c0849a4 Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Thu, 5 Jan 2012 17:08:27 -0300 Subject: Replace Unicode.u_unpack with String#codepoints --- activesupport/lib/active_support/multibyte.rb | 1 - activesupport/lib/active_support/multibyte/chars.rb | 4 ++-- .../lib/active_support/multibyte/exceptions.rb | 8 -------- activesupport/lib/active_support/multibyte/unicode.rb | 19 +++---------------- 4 files changed, 5 insertions(+), 27 deletions(-) delete mode 100644 activesupport/lib/active_support/multibyte/exceptions.rb (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/multibyte.rb b/activesupport/lib/active_support/multibyte.rb index cabe073616..fc15af17db 100644 --- a/activesupport/lib/active_support/multibyte.rb +++ b/activesupport/lib/active_support/multibyte.rb @@ -3,7 +3,6 @@ require 'active_support/core_ext/module/attribute_accessors' module ActiveSupport #:nodoc: module Multibyte - autoload :EncodingError, 'active_support/multibyte/exceptions' autoload :Chars, 'active_support/multibyte/chars' autoload :Unicode, 'active_support/multibyte/unicode' diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index 1389cb50c9..c0796320b2 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -153,7 +153,7 @@ module ActiveSupport #:nodoc: # 'é'.length # => 2 # 'é'.mb_chars.decompose.to_s.length # => 3 def decompose - chars(Unicode.decompose(:canonical, Unicode.u_unpack(@wrapped_string)).pack('U*')) + chars(Unicode.decompose(:canonical, @wrapped_string.codepoints.to_a).pack('U*')) end # Performs composition on all the characters. @@ -162,7 +162,7 @@ module ActiveSupport #:nodoc: # 'é'.length # => 3 # 'é'.mb_chars.compose.to_s.length # => 2 def compose - chars(Unicode.compose(Unicode.u_unpack(@wrapped_string)).pack('U*')) + chars(Unicode.compose(@wrapped_string.codepoints.to_a).pack('U*')) end # Returns the number of grapheme clusters in the string. diff --git a/activesupport/lib/active_support/multibyte/exceptions.rb b/activesupport/lib/active_support/multibyte/exceptions.rb deleted file mode 100644 index 62066e3c71..0000000000 --- a/activesupport/lib/active_support/multibyte/exceptions.rb +++ /dev/null @@ -1,8 +0,0 @@ -# encoding: utf-8 - -module ActiveSupport #:nodoc: - module Multibyte #:nodoc: - # Raised when a problem with the encoding was found. - class EncodingError < StandardError; end - end -end \ No newline at end of file diff --git a/activesupport/lib/active_support/multibyte/unicode.rb b/activesupport/lib/active_support/multibyte/unicode.rb index c689c14631..e258e2e48e 100644 --- a/activesupport/lib/active_support/multibyte/unicode.rb +++ b/activesupport/lib/active_support/multibyte/unicode.rb @@ -61,19 +61,6 @@ module ActiveSupport TRAILERS_PAT = /(#{codepoints_to_pattern(LEADERS_AND_TRAILERS)})+\Z/u LEADERS_PAT = /\A(#{codepoints_to_pattern(LEADERS_AND_TRAILERS)})+/u - # Unpack the string at codepoints boundaries. Raises an EncodingError when the encoding of the string isn't - # valid UTF-8. - # - # Example: - # Unicode.u_unpack('Café') # => [67, 97, 102, 233] - def u_unpack(string) - begin - string.unpack 'U*' - rescue ArgumentError - raise EncodingError, 'malformed UTF-8 character' - end - end - # Detect whether the codepoint is in a certain character class. Returns +true+ when it's in the specified # character class and +false+ otherwise. Valid character classes are: :cr, :lf, :l, # :v, :lv, :lvt and :t. @@ -89,7 +76,7 @@ module ActiveSupport # Unicode.g_unpack('क्षि') # => [[2325, 2381], [2359], [2367]] # Unicode.g_unpack('Café') # => [[67], [97], [102], [233]] def g_unpack(string) - codepoints = u_unpack(string) + codepoints = string.codepoints.to_a unpacked = [] pos = 0 marker = 0 @@ -283,7 +270,7 @@ module ActiveSupport def normalize(string, form=nil) form ||= @default_normalization_form # See http://www.unicode.org/reports/tr15, Table 1 - codepoints = u_unpack(string) + codepoints = string.codepoints.to_a case form when :d reorder_characters(decompose(:canonical, codepoints)) @@ -299,7 +286,7 @@ module ActiveSupport end def apply_mapping(string, mapping) #:nodoc: - u_unpack(string).map do |codepoint| + string.each_codepoint.map do |codepoint| cp = database.codepoints[codepoint] if cp and (ncp = cp.send(mapping)) and ncp > 0 ncp -- cgit v1.2.3 From d2455bd8a56eeb41c346d8a0b49f4ba7475f2471 Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Thu, 5 Jan 2012 17:15:21 -0300 Subject: Remove unnecessary requires/encoding comment --- activesupport/lib/active_support/multibyte.rb | 3 --- 1 file changed, 3 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/multibyte.rb b/activesupport/lib/active_support/multibyte.rb index fc15af17db..5efe13c537 100644 --- a/activesupport/lib/active_support/multibyte.rb +++ b/activesupport/lib/active_support/multibyte.rb @@ -1,6 +1,3 @@ -# encoding: utf-8 -require 'active_support/core_ext/module/attribute_accessors' - module ActiveSupport #:nodoc: module Multibyte autoload :Chars, 'active_support/multibyte/chars' -- cgit v1.2.3 From a8a8dc4a691577c2d029be92c32f593d0fd7db75 Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Thu, 5 Jan 2012 17:15:36 -0300 Subject: Move include to top of class for clarity --- activesupport/lib/active_support/multibyte/chars.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index c0796320b2..b85c5e8bd1 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -35,6 +35,7 @@ module ActiveSupport #:nodoc: # # ActiveSupport::Multibyte.proxy_class = CharsForUTF32 class Chars + include Comparable attr_reader :wrapped_string alias to_s wrapped_string alias to_str wrapped_string @@ -69,8 +70,6 @@ module ActiveSupport #:nodoc: string.encoding == Encoding::UTF_8 end - include Comparable - # Works just like String#split, with the exception that the items in the resulting list are Chars # instances instead of String. This makes chaining methods easier. # -- cgit v1.2.3 From 4ac056c7f46fa6961699402d0bf296dac6a02384 Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Thu, 5 Jan 2012 17:16:35 -0300 Subject: Use more descriptive method names --- activesupport/lib/active_support/multibyte/chars.rb | 4 ++-- activesupport/lib/active_support/multibyte/unicode.rb | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index b85c5e8bd1..a6256d0c47 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -90,7 +90,7 @@ module ActiveSupport #:nodoc: # Example: # 'Café'.mb_chars.reverse.to_s # => 'éfaC' def reverse - chars(Unicode.g_unpack(@wrapped_string).reverse.flatten.pack('U*')) + chars(Unicode.unpack_graphemes(@wrapped_string).reverse.flatten.pack('U*')) end # Limit the byte size of the string to a number of bytes without breaking characters. Usable @@ -170,7 +170,7 @@ module ActiveSupport #:nodoc: # 'क्षि'.mb_chars.length # => 4 # 'क्षि'.mb_chars.g_length # => 3 def g_length - Unicode.g_unpack(@wrapped_string).length + Unicode.unpack_graphemes(@wrapped_string).length end # Replaces all ISO-8859-1 or CP1252 characters by their UTF-8 equivalent resulting in a valid UTF-8 string. diff --git a/activesupport/lib/active_support/multibyte/unicode.rb b/activesupport/lib/active_support/multibyte/unicode.rb index e258e2e48e..10e3a24e7f 100644 --- a/activesupport/lib/active_support/multibyte/unicode.rb +++ b/activesupport/lib/active_support/multibyte/unicode.rb @@ -73,9 +73,9 @@ module ActiveSupport # Unpack the string at grapheme boundaries. Returns a list of character lists. # # Example: - # Unicode.g_unpack('क्षि') # => [[2325, 2381], [2359], [2367]] - # Unicode.g_unpack('Café') # => [[67], [97], [102], [233]] - def g_unpack(string) + # Unicode.unpack_graphemes('क्षि') # => [[2325, 2381], [2359], [2367]] + # Unicode.unpack_graphemes('Café') # => [[67], [97], [102], [233]] + def unpack_graphemes(string) codepoints = string.codepoints.to_a unpacked = [] pos = 0 @@ -105,11 +105,11 @@ module ActiveSupport unpacked end - # Reverse operation of g_unpack. + # Reverse operation of unpack_graphemes. # # Example: - # Unicode.g_pack(Unicode.g_unpack('क्षि')) # => 'क्षि' - def g_pack(unpacked) + # Unicode.pack_graphemes(Unicode.unpack_graphemes('क्षि')) # => 'क्षि' + def pack_graphemes(unpacked) unpacked.flatten.pack('U*') end -- cgit v1.2.3 From db6eb1930d5e31d08e2cc613ad0570d0d1bb0730 Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Thu, 5 Jan 2012 17:20:15 -0300 Subject: Use friendlier method names for upcasing/downcasing --- .../lib/active_support/multibyte/chars.rb | 6 ++--- .../lib/active_support/multibyte/unicode.rb | 26 ++++++++++++++-------- 2 files changed, 20 insertions(+), 12 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index a6256d0c47..eb7f99dd71 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -107,7 +107,7 @@ module ActiveSupport #:nodoc: # Example: # 'Laurent, où sont les tests ?'.mb_chars.upcase.to_s # => "LAURENT, OÙ SONT LES TESTS ?" def upcase - chars(Unicode.apply_mapping @wrapped_string, :uppercase_mapping) + chars Unicode.upcase(@wrapped_string) end # Convert characters in the string to lowercase. @@ -115,7 +115,7 @@ module ActiveSupport #:nodoc: # Example: # 'VĚDA A VÝZKUM'.mb_chars.downcase.to_s # => "věda a výzkum" def downcase - chars(Unicode.apply_mapping @wrapped_string, :lowercase_mapping) + chars Unicode.downcase(@wrapped_string) end # Converts the first character to uppercase and the remainder to lowercase. @@ -132,7 +132,7 @@ module ActiveSupport #:nodoc: # "ÉL QUE SE ENTERÓ".mb_chars.titleize # => "Él Que Se Enteró" # "日本語".mb_chars.titleize # => "日本語" def titleize - chars(downcase.to_s.gsub(/\b('?[\S])/u) { Unicode.apply_mapping $1, :uppercase_mapping }) + chars(downcase.to_s.gsub(/\b('?[\S])/u) { Unicode.upcase($1)}) end alias_method :titlecase, :titleize diff --git a/activesupport/lib/active_support/multibyte/unicode.rb b/activesupport/lib/active_support/multibyte/unicode.rb index 10e3a24e7f..94fb0a48aa 100644 --- a/activesupport/lib/active_support/multibyte/unicode.rb +++ b/activesupport/lib/active_support/multibyte/unicode.rb @@ -285,15 +285,12 @@ module ActiveSupport end.pack('U*') end - def apply_mapping(string, mapping) #:nodoc: - string.each_codepoint.map do |codepoint| - cp = database.codepoints[codepoint] - if cp and (ncp = cp.send(mapping)) and ncp > 0 - ncp - else - codepoint - end - end.pack('U*') + def downcase(string) + apply_mapping string, :lowercase_mapping + end + + def upcase(string) + apply_mapping string, :uppercase_mapping end # Holds data about a codepoint in the Unicode database @@ -361,6 +358,17 @@ module ActiveSupport private + def apply_mapping(string, mapping) #:nodoc: + string.each_codepoint.map do |codepoint| + cp = database.codepoints[codepoint] + if cp and (ncp = cp.send(mapping)) and ncp > 0 + ncp + else + codepoint + end + end.pack('U*') + end + def tidy_byte(byte) if byte < 160 [database.cp1252[byte] || byte].pack("U").unpack("C*") -- cgit v1.2.3 From 16bee7618c328ecd790db366221639661912c477 Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Thu, 5 Jan 2012 17:25:40 -0300 Subject: Use friendlier method name --- activesupport/lib/active_support/multibyte/chars.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index eb7f99dd71..99b974e4a7 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -168,8 +168,8 @@ module ActiveSupport #:nodoc: # # Example: # 'क्षि'.mb_chars.length # => 4 - # 'क्षि'.mb_chars.g_length # => 3 - def g_length + # 'क्षि'.mb_chars.grapheme_length # => 3 + def grapheme_length Unicode.unpack_graphemes(@wrapped_string).length end -- cgit v1.2.3