Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 7257

Show
Ignore:
Timestamp:
07/31/07 04:59:10 (1 year ago)
Author:
marcel
Message:

Add support for []= on ActiveSupport::Multibyte::Chars. Closes #9142. [ewan, manfred]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r7217 r7257  
    11*SVN* 
     2 
     3* Add support for []= on ActiveSupport::Multibyte::Chars. Closes #9142. [ewan, manfred] 
    24 
    35* Added Array#extract_options! to encapsulate the pattern of getting an options hash out of a variable number of parameters #8759 [norbert]. 
  • trunk/activesupport/lib/active_support/multibyte/handlers/utf8_handler.rb

    r6044 r7257  
    139139        bidx = str.index(*args) 
    140140        bidx ? (u_unpack(str.slice(0...bidx)).size) : nil 
     141      end 
     142       
     143      # Works just like the indexed replace method on string, except instead of byte offsets you specify 
     144      # character offsets. 
     145      # 
     146      # Example: 
     147      # 
     148      #   s = "MÃŒller" 
     149      #   s.chars[2] = "e" # Replace character with offset 2 
     150      #   s 
     151      #   #=> "MÃŒeler" 
     152      # 
     153      #   s = "MÃŒller" 
     154      #   s.chars[1, 2] = "ö" # Replace 2 characters at character offset 1 
     155      #   s 
     156      #   #=> "Möler" 
     157      def []=(str, *args) 
     158        replace_by = args.pop 
     159        # Indexed replace with regular expressions already works 
     160        return str[*args] = replace_by if args.first.is_a?(Regexp) 
     161        result = u_unpack(str) 
     162        if args[0].is_a?(Fixnum) 
     163          raise IndexError, "index #{args[0]} out of string" if args[0] >= result.length 
     164          min = args[0] 
     165          max = args[1].nil? ? min : (min + args[1] - 1) 
     166          range = Range.new(min, max) 
     167          replace_by = [replace_by].pack('U') if replace_by.is_a?(Fixnum) 
     168        elsif args.first.is_a?(Range) 
     169          raise RangeError, "#{args[0]} out of range" if args[0].min >= result.length 
     170          range = args[0] 
     171        else 
     172          needle = args[0].to_s 
     173          min = index(str, needle) 
     174          max = min + length(needle) - 1 
     175          range = Range.new(min, max) 
     176        end 
     177        result[range] = u_unpack(replace_by) 
     178        str.replace(result.pack('U*')) 
    141179      end 
    142180       
  • trunk/activesupport/test/multibyte_handler_test.rb

    r6021 r7257  
    200200  end 
    201201   
     202  def test_indexed_insert 
     203    s = "Καλη!" 
     204    @handler[s, 2] = "a" 
     205    assert_equal "Καaη!", s 
     206    @handler[s, 2] = "ηη" 
     207    assert_equal "Καηηη!", s 
     208    assert_raises(IndexError) { @handler[s, 10] = 'a' } 
     209    assert_equal "Καηηη!", s 
     210    @handler[s, 2] = 32 
     211    assert_equal "Κα ηη!", s 
     212    @handler[s, 3, 2] = "λλλ" 
     213    assert_equal "Κα λλλ!", s 
     214    @handler[s, 1, 0] = "λ" 
     215    assert_equal "Κλα λλλ!", s 
     216    assert_raises(IndexError) { @handler[s, 10, 4] = 'a' } 
     217    assert_equal "Κλα λλλ!", s 
     218    @handler[s, 4..6] = "ηη" 
     219    assert_equal "Κλα ηη!", s 
     220    assert_raises(RangeError) { @handler[s, 10..12] = 'a' } 
     221    assert_equal "Κλα ηη!", s 
     222    @handler[s, /ηη/] = "λλλ" 
     223    assert_equal "Κλα λλλ!", s 
     224    assert_raises(IndexError) { @handler[s, /ii/] = 'a' } 
     225    assert_equal "Κλα λλλ!", s 
     226    @handler[s, /(λλ)(.)/, 2] = "α" 
     227    assert_equal "Κλα λλα!", s 
     228    assert_raises(IndexError) { @handler[s, /()/, 10] = 'a' } 
     229    assert_equal "Κλα λλα!", s 
     230    @handler[s, "α"] = "η" 
     231    assert_equal "Κλη λλα!", s 
     232    @handler[s, "λλ"] = "ααα" 
     233    assert_equal "Κλη αααα!", s 
     234  end 
     235   
    202236  def test_strip 
    203237    # A unicode aware version of strip should strip all 26 types of whitespace. This includes the NO BREAK SPACE