Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 56 lines (53 sloc) 1.996 kb
f28bd95 @jeremy Fix dependencies revealed by testing in isolation
jeremy authored
1 require 'active_support/multibyte'
22f75d5 @Manfred Simplify ActiveSupport::Multibyte and make it run on Ruby 1.9.
Manfred authored
2
83a89b8 @jeremy Convert String conversions, filters, starts/ends_with, and multibyte …
jeremy authored
3 class String
7ab4775 @josevalim Initial pass at removing dead 1.8.x code from Active Support.
josevalim authored
4 # == Multibyte proxy
5 #
6 # +mb_chars+ is a multibyte safe proxy for string methods.
7 #
27b79f4 @steveklabnik Fix documentation for String#mb_chars.
steveklabnik authored
8 # It creates and returns an instance of the ActiveSupport::Multibyte::Chars class which
7ab4775 @josevalim Initial pass at removing dead 1.8.x code from Active Support.
josevalim authored
9 # encapsulates the original string. A Unicode safe version of all the String methods are defined on this proxy
10 # class. If the proxy class doesn't respond to a certain method, it's forwarded to the encapsulated string.
11 #
12 # name = 'Claus Müller'
13 # name.reverse # => "rell??M sualC"
14 # name.length # => 13
15 #
16 # name.mb_chars.reverse.to_s # => "rellüM sualC"
17 # name.mb_chars.length # => 12
18 #
19 # == Method chaining
20 #
21 # All the methods on the Chars proxy which normally return a string will return a Chars object. This allows
22 # method chaining on the result of any of these methods.
23 #
24 # name.mb_chars.reverse.length # => 12
25 #
26 # == Interoperability and configuration
27 #
28 # The Chars object tries to be as interchangeable with String objects as possible: sorting and comparing between
29 # String and Char work like expected. The bang! methods change the internal string representation in the Chars
30 # object. Interoperability problems can be resolved easily with a +to_s+ call.
31 #
32 # For more information about the methods defined on the Chars proxy see ActiveSupport::Multibyte::Chars. For
33 # information about how to change the default Multibyte behavior see ActiveSupport::Multibyte.
34 def mb_chars
bd85260 @steveklabnik Simplify String#mb_chars and stop proxying #class
steveklabnik authored
35 ActiveSupport::Multibyte.proxy_class.new(self)
7ab4775 @josevalim Initial pass at removing dead 1.8.x code from Active Support.
josevalim authored
36 end
f3abc8a @norman Use multibyte proxy class on 1.9, refactor Unicode.
norman authored
37
69db8dc @wazery Doc fixes [ci skip]
wazery authored
38 # Returns +true+ if string has utf_8 encoding.
dde6ee9 @davydovanton [skip ci] Add documentation for String#is_utf8? method
davydovanton authored
39 #
40 # utf_8_str = "some string".encode "UTF-8"
41 # iso_str = "some string".encode "ISO-8859-1"
42 #
43 # utf_8_str.is_utf8? # => true
44 # iso_str.is_utf8? # => false
7ab4775 @josevalim Initial pass at removing dead 1.8.x code from Active Support.
josevalim authored
45 def is_utf8?
46 case encoding
47 when Encoding::UTF_8
48 valid_encoding?
49 when Encoding::ASCII_8BIT, Encoding::US_ASCII
50 dup.force_encoding(Encoding::UTF_8).valid_encoding?
51 else
52 false
f3abc8a @norman Use multibyte proxy class on 1.9, refactor Unicode.
norman authored
53 end
22f75d5 @Manfred Simplify ActiveSupport::Multibyte and make it run on Ruby 1.9.
Manfred authored
54 end
55 end
Something went wrong with that request. Please try again.