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 233 233 assert_equal "Îλη αααα!", s 234 234 end 235 235 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 236 287 def test_strip 237 288 # A unicode aware version of strip should strip all 26 types of whitespace. This includes the NO BREAK SPACE 238 289 # 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 178 178 str.replace(result.pack('U*')) 179 179 end 180 180 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 181 220 # Does Unicode-aware rstrip 182 221 def rstrip(str) 183 222 str.gsub(UNICODE_TRAILERS_PAT, '') … … 380 419 unpacked.flatten 381 420 end 382 421 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 383 449 # Convert characters to a different case 384 450 def to_case(way, str) 385 451 u_unpack(str).map do |codepoint|