Make truncate() Unicode-aware

This commit is contained in:
Jacques Distler 2009-12-14 17:41:28 -06:00
parent 2c5e5a0015
commit d3e79ea84a
4 changed files with 41 additions and 9 deletions

View file

@ -2,8 +2,30 @@
class String
# Return the number of unicode characters in a string
#
# :call-seq:
# string.num_chars -> integer
#
# Because Rails 2.3.5's String#mb_chars.length is broken,
# we provide this method.
#--
if "".respond_to?(:force_encoding)
def num_chars
length
end
else
def num_chars
unpack('U*').length
end
end
#++
# A method to allow byte-oriented operations in both Ruby 1.8 and Ruby 1.9
#
# :call-seq:
# string.to_utf_8 -> string (with the encoding set to "ASCII-8BIT")
#
# Under 1.8, this is a NOOP. Under 1.9, it sets the encoding to "ASCII-8BIT"
#--
if "".respond_to?(:force_encoding)
@ -19,6 +41,9 @@ end
#++
# A method to allow string-oriented operations in both Ruby 1.8 and Ruby 1.9
#
# :call-seq:
# string.to_utf_8 -> string (with the encoding set to "UTF-8")
#
# Under 1.8, this is a NOOP. Under 1.9, it sets the encoding to "UTF-8"
#--
if "".respond_to?(:force_encoding)