aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorMichael Koziarski <michael@koziarski.com>2007-08-05 00:48:00 +0000
committerMichael Koziarski <michael@koziarski.com>2007-08-05 00:48:00 +0000
commitfe3e03e7708c576d5b09fb161e68d715796eae67 (patch)
treea6f276261009c7b503ef268190231aeccd3e240f /activesupport
parent4b6411008675dbbfb7da4dfb6ef73c5528c196d1 (diff)
downloadrails-fe3e03e7708c576d5b09fb161e68d715796eae67.tar.gz
rails-fe3e03e7708c576d5b09fb161e68d715796eae67.tar.bz2
rails-fe3e03e7708c576d5b09fb161e68d715796eae67.zip
Add ljust, rjust and center to utf8-handler. Closes #9165 [manfred]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7272 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/multibyte/handlers/utf8_handler.rb66
-rw-r--r--activesupport/test/multibyte_handler_test.rb51
3 files changed, 119 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index bed781d67d..d00be783e1 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Add ljust, rjust and center to utf8-handler. Closes #9165 [manfred]
+
* Fix Time#advance bug when trying to advance a year from leap day. Closes #8655 [gbuesing]
* Add support for []= on ActiveSupport::Multibyte::Chars. Closes #9142. [ewan, manfred]
diff --git a/activesupport/lib/active_support/multibyte/handlers/utf8_handler.rb b/activesupport/lib/active_support/multibyte/handlers/utf8_handler.rb
index 02fc7b3e2b..6ca043ae21 100644
--- a/activesupport/lib/active_support/multibyte/handlers/utf8_handler.rb
+++ b/activesupport/lib/active_support/multibyte/handlers/utf8_handler.rb
@@ -178,6 +178,45 @@ module ActiveSupport::Multibyte::Handlers #:nodoc:
str.replace(result.pack('U*'))
end
+ # Works just like String#rjust, only integer specifies characters instead of bytes.
+ #
+ # Example:
+ #
+ # "¾ cup".chars.rjust(8).to_s
+ # #=> " ¾ cup"
+ #
+ # "¾ cup".chars.rjust(8, " ").to_s # Use non-breaking whitespace
+ # #=> "   ¾ cup"
+ def rjust(str, integer, padstr=' ')
+ justify(str, integer, :right, padstr)
+ end
+
+ # Works just like String#ljust, only integer specifies characters instead of bytes.
+ #
+ # Example:
+ #
+ # "¾ cup".chars.rjust(8).to_s
+ # #=> "¾ cup "
+ #
+ # "¾ cup".chars.rjust(8, " ").to_s # Use non-breaking whitespace
+ # #=> "¾ cup   "
+ def ljust(str, integer, padstr=' ')
+ justify(str, integer, :left, padstr)
+ end
+
+ # Works just like String#center, only integer specifies characters instead of bytes.
+ #
+ # Example:
+ #
+ # "¾ cup".chars.center(8).to_s
+ # #=> " ¾ cup "
+ #
+ # "¾ cup".chars.center(8, " ").to_s # Use non-breaking whitespace
+ # #=> " ¾ cup  "
+ def center(str, integer, padstr=' ')
+ justify(str, integer, :center, padstr)
+ end
+
# Does Unicode-aware rstrip
def rstrip(str)
str.gsub(UNICODE_TRAILERS_PAT, '')
@@ -380,6 +419,33 @@ module ActiveSupport::Multibyte::Handlers #:nodoc:
unpacked.flatten
end
+ # Justifies a string in a certain way. Valid values for <tt>way</tt> are <tt>:right</tt>, <tt>:left</tt> and
+ # <tt>:center</tt>. Is primarily used as a helper method by <tt>rjust</tt>, <tt>ljust</tt> and <tt>center</tt>.
+ def justify(str, integer, way, padstr=' ')
+ raise ArgumentError, "zero width padding" if padstr.length == 0
+ padsize = integer - size(str)
+ padsize = padsize > 0 ? padsize : 0
+ case way
+ when :right
+ str.dup.insert(0, padding(padsize, padstr))
+ when :left
+ str.dup.insert(-1, padding(padsize, padstr))
+ when :center
+ lpad = padding((padsize / 2.0).floor, padstr)
+ rpad = padding((padsize / 2.0).ceil, padstr)
+ str.dup.insert(0, lpad).insert(-1, rpad)
+ end
+ end
+
+ # Generates a padding string of a certain size.
+ def padding(padsize, padstr=' ')
+ if padsize != 0
+ slice(padstr * ((padsize / size(padstr)) + 1), 0, padsize)
+ else
+ ''
+ end
+ end
+
# Convert characters to a different case
def to_case(way, str)
u_unpack(str).map do |codepoint|
diff --git a/activesupport/test/multibyte_handler_test.rb b/activesupport/test/multibyte_handler_test.rb
index e4744def6c..9905cf65d6 100644
--- a/activesupport/test/multibyte_handler_test.rb
+++ b/activesupport/test/multibyte_handler_test.rb
@@ -233,6 +233,57 @@ module UTF8HandlingTest
assert_equal "Κλη αααα!", s
end
+ def test_rjust
+ s = "Καη"
+ assert_raises(ArgumentError) { @handler.rjust(s, 10, '') }
+ assert_raises(ArgumentError) { @handler.rjust(s) }
+ assert_equal "Καη", @handler.rjust(s, -3)
+ assert_equal "Καη", @handler.rjust(s, 0)
+ assert_equal "Καη", @handler.rjust(s, 3)
+ assert_equal " Καη", @handler.rjust(s, 5)
+ assert_equal " Καη", @handler.rjust(s, 7)
+ assert_equal "----Καη", @handler.rjust(s, 7, '-')
+ assert_equal "ααααΚαη", @handler.rjust(s, 7, 'α')
+ assert_equal "abaΚαη", @handler.rjust(s, 6, 'ab')
+ assert_equal "αηαΚαη", @handler.rjust(s, 6, 'αη')
+ end
+
+ def test_ljust
+ s = "Καη"
+ assert_raises(ArgumentError) { @handler.ljust(s, 10, '') }
+ assert_raises(ArgumentError) { @handler.ljust(s) }
+ assert_equal "Καη", @handler.ljust(s, -3)
+ assert_equal "Καη", @handler.ljust(s, 0)
+ assert_equal "Καη", @handler.ljust(s, 3)
+ assert_equal "Καη ", @handler.ljust(s, 5)
+ assert_equal "Καη ", @handler.ljust(s, 7)
+ assert_equal "Καη----", @handler.ljust(s, 7, '-')
+ assert_equal "Καηαααα", @handler.ljust(s, 7, 'α')
+ assert_equal "Καηaba", @handler.ljust(s, 6, 'ab')
+ assert_equal "Καηαηα", @handler.ljust(s, 6, 'αη')
+ end
+
+ def test_center
+ s = "Καη"
+ assert_raises(ArgumentError) { @handler.center(s, 10, '') }
+ assert_raises(ArgumentError) { @handler.center(s) }
+ assert_equal "Καη", @handler.center(s, -3)
+ assert_equal "Καη", @handler.center(s, 0)
+ assert_equal "Καη", @handler.center(s, 3)
+ assert_equal "Καη ", @handler.center(s, 4)
+ assert_equal " Καη ", @handler.center(s, 5)
+ assert_equal " Καη ", @handler.center(s, 6)
+ assert_equal "--Καη--", @handler.center(s, 7, '-')
+ assert_equal "--Καη---", @handler.center(s, 8, '-')
+ assert_equal "ααΚαηαα", @handler.center(s, 7, 'α')
+ assert_equal "ααΚαηααα", @handler.center(s, 8, 'α')
+ assert_equal "aΚαηab", @handler.center(s, 6, 'ab')
+ assert_equal "abΚαηab", @handler.center(s, 7, 'ab')
+ assert_equal "ababΚαηabab", @handler.center(s, 11, 'ab')
+ assert_equal "αΚαηαη", @handler.center(s, 6, 'αη')
+ assert_equal "αηΚαηαη", @handler.center(s, 7, 'αη')
+ end
+
def test_strip
# A unicode aware version of strip should strip all 26 types of whitespace. This includes the NO BREAK SPACE
# aka BOM (byte order mark). The byte order mark has no place in UTF-8 because it's used to detect LE and BE.