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

Ticket #9165: add_rjust_ljust_and_center_to_utf8_handler.diff

File add_rjust_ljust_and_center_to_utf8_handler.diff, 5.8 kB (added by manfred, 1 year ago)
  • activesupport/test/multibyte_handler_test.rb

    old new  
    233233    assert_equal "Κλη αααα!", s 
    234234  end 
    235235   
     236  def test_rjust 
     237    s = "Καη" 
     238    assert_raises(ArgumentError) { @handler.rjust(s, 10, '') } 
     239    assert_raises(ArgumentError) { @handler.rjust(s) } 
     240    assert_equal "Καη", @handler.rjust(s, -3) 
     241    assert_equal "Καη", @handler.rjust(s, 0) 
     242    assert_equal "Καη", @handler.rjust(s, 3) 
     243    assert_equal "  Καη", @handler.rjust(s, 5) 
     244    assert_equal "    Καη", @handler.rjust(s, 7) 
     245    assert_equal "----Καη", @handler.rjust(s, 7, '-') 
     246    assert_equal "ααααΚαη", @handler.rjust(s, 7, 'α') 
     247    assert_equal "abaΚαη", @handler.rjust(s, 6, 'ab') 
     248    assert_equal "αηαΚαη", @handler.rjust(s, 6, 'αη') 
     249  end 
     250   
     251  def test_ljust 
     252    s = "Καη" 
     253    assert_raises(ArgumentError) { @handler.ljust(s, 10, '') } 
     254    assert_raises(ArgumentError) { @handler.ljust(s) } 
     255    assert_equal "Καη", @handler.ljust(s, -3) 
     256    assert_equal "Καη", @handler.ljust(s, 0) 
     257    assert_equal "Καη", @handler.ljust(s, 3) 
     258    assert_equal "Καη  ", @handler.ljust(s, 5) 
     259    assert_equal "Καη    ", @handler.ljust(s, 7) 
     260    assert_equal "Καη----", @handler.ljust(s, 7, '-') 
     261    assert_equal "Καηαααα", @handler.ljust(s, 7, 'α') 
     262    assert_equal "Καηaba", @handler.ljust(s, 6, 'ab') 
     263    assert_equal "Καηαηα", @handler.ljust(s, 6, 'αη') 
     264  end 
     265   
     266  def test_center 
     267    s = "Καη" 
     268    assert_raises(ArgumentError) { @handler.center(s, 10, '') } 
     269    assert_raises(ArgumentError) { @handler.center(s) } 
     270    assert_equal "Καη", @handler.center(s, -3) 
     271    assert_equal "Καη", @handler.center(s, 0) 
     272    assert_equal "Καη", @handler.center(s, 3) 
     273    assert_equal "Καη ", @handler.center(s, 4) 
     274    assert_equal " Καη ", @handler.center(s, 5) 
     275    assert_equal " Καη  ", @handler.center(s, 6) 
     276    assert_equal "--Καη--", @handler.center(s, 7, '-') 
     277    assert_equal "--Καη---", @handler.center(s, 8, '-') 
     278    assert_equal "ααΚαηαα", @handler.center(s, 7, 'α') 
     279    assert_equal "ααΚαηααα", @handler.center(s, 8, 'α') 
     280    assert_equal "aΚαηab", @handler.center(s, 6, 'ab') 
     281    assert_equal "abΚαηab", @handler.center(s, 7, 'ab') 
     282    assert_equal "ababΚαηabab", @handler.center(s, 11, 'ab') 
     283    assert_equal "αΚαηαη", @handler.center(s, 6, 'αη') 
     284    assert_equal "αηΚαηαη", @handler.center(s, 7, 'αη') 
     285  end 
     286   
    236287  def test_strip 
    237288    # A unicode aware version of strip should strip all 26 types of whitespace. This includes the NO BREAK SPACE 
    238289    # aka BOM (byte order mark). The byte order mark has no place in UTF-8 because it's used to detect LE and BE. 
  • activesupport/lib/active_support/multibyte/handlers/utf8_handler.rb

    old new  
    178178        str.replace(result.pack('U*')) 
    179179      end 
    180180       
     181      # Works just like String#rjust, only integer specifies characters instead of bytes. 
     182      # 
     183      # Example: 
     184      # 
     185      #   "Ÿ cup".chars.rjust(8).to_s 
     186      #   #=> "   ÂŸ cup" 
     187      # 
     188      #   "Ÿ cup".chars.rjust(8, " ").to_s # Use non-breaking whitespace 
     189      #   #=> "   Ÿ cup" 
     190      def rjust(str, integer, padstr=' ') 
     191        justify(str, integer, :right, padstr) 
     192      end 
     193       
     194      # Works just like String#ljust, only integer specifies characters instead of bytes. 
     195      # 
     196      # Example: 
     197      # 
     198      #   "Ÿ cup".chars.rjust(8).to_s 
     199      #   #=> "Ÿ cup   " 
     200      # 
     201      #   "Ÿ cup".chars.rjust(8, " ").to_s # Use non-breaking whitespace 
     202      #   #=> "Ÿ cup   " 
     203      def ljust(str, integer, padstr=' ') 
     204        justify(str, integer, :left, padstr) 
     205      end 
     206       
     207      # Works just like String#center, only integer specifies characters instead of bytes. 
     208      # 
     209      # Example: 
     210      # 
     211      #   "Ÿ cup".chars.center(8).to_s 
     212      #   #=> " Ÿ cup  " 
     213      # 
     214      #   "Ÿ cup".chars.center(8, " ").to_s # Use non-breaking whitespace 
     215      #   #=> " Ÿ cup  " 
     216      def center(str, integer, padstr=' ') 
     217        justify(str, integer, :center, padstr) 
     218      end 
     219       
    181220      # Does Unicode-aware rstrip 
    182221      def rstrip(str) 
    183222        str.gsub(UNICODE_TRAILERS_PAT, '') 
     
    380419        unpacked.flatten 
    381420      end 
    382421       
     422      # Justifies a string in a certain way. Valid values for <tt>way</tt> are <tt>:right</tt>, <tt>:left</tt> and 
     423      # <tt>:center</tt>. Is primarily used as a helper method by <tt>rjust</tt>, <tt>ljust</tt> and <tt>center</tt>. 
     424      def justify(str, integer, way, padstr=' ') 
     425        raise ArgumentError, "zero width padding" if padstr.length == 0 
     426        padsize = integer - size(str) 
     427        padsize = padsize > 0 ? padsize : 0 
     428        case way 
     429        when :right 
     430          str.dup.insert(0, padding(padsize, padstr)) 
     431        when :left 
     432          str.dup.insert(-1, padding(padsize, padstr)) 
     433        when :center 
     434          lpad = padding((padsize / 2.0).floor, padstr) 
     435          rpad = padding((padsize / 2.0).ceil, padstr) 
     436          str.dup.insert(0, lpad).insert(-1, rpad) 
     437        end 
     438      end 
     439       
     440      # Generates a padding string of a certain size. 
     441      def padding(padsize, padstr=' ') 
     442        if padsize != 0 
     443          slice(padstr * ((padsize / size(padstr)) + 1), 0, padsize) 
     444        else 
     445          '' 
     446        end 
     447      end 
     448       
    383449      # Convert characters to a different case 
    384450      def to_case(way, str) 
    385451        u_unpack(str).map do |codepoint|