1
1
module ActiveSupport #:nodoc:
2
- module Multibyte #:nodoc:
3
- # Returns a regular expression that matches valid characters in the current encoding
4
- def self . valid_character
5
- case $KCODE
6
- when 'UTF8'
7
- VALID_CHARACTER [ 'UTF-8' ]
8
- when 'SJIS'
9
- VALID_CHARACTER [ 'Shift_JIS' ]
10
- end
11
- end
2
+ module Multibyte #:nodoc:
3
+ # Returns a regular expression that matches valid characters in the current encoding
4
+ def self . valid_character
5
+ case $KCODE
6
+ when 'UTF8'
7
+ VALID_CHARACTER [ 'UTF-8' ]
8
+ when 'SJIS'
9
+ VALID_CHARACTER [ 'Shift_JIS' ]
10
+ end
11
+ end
12
12
13
- # Verifies the encoding of a string
14
- def self . verify ( string )
15
- if expression = valid_character
16
- for c in string . split ( // )
17
- return false unless valid_character . match ( c )
18
- end
19
- end
20
- true
21
- end
13
+ if 'string' . respond_to? ( :valid_encoding? )
14
+ # Verifies the encoding of a string
15
+ def self . verify ( string )
16
+ string . valid_encoding?
17
+ end
18
+ else
19
+ def self . verify ( string )
20
+ if expression = valid_character
21
+ for c in string . split ( // )
22
+ return false unless expression . match ( c )
23
+ end
24
+ end
25
+ true
26
+ end
27
+ end
22
28
23
- # Verifies the encoding of the string and raises an exception when it's not valid
24
- def self . verify! ( string )
25
- raise ActiveSupport ::Multibyte ::Handlers ::EncodingError . new ( "Found characters with invalid encoding" ) unless verify ( string )
26
- end
29
+ # Verifies the encoding of the string and raises an exception when it's not valid
30
+ def self . verify! ( string )
31
+ raise ActiveSupport ::Multibyte ::Handlers ::EncodingError . new ( "Found characters with invalid encoding" ) unless verify ( string )
32
+ end
27
33
28
- # Removes all invalid characters from the string
29
- def self . clean ( string )
30
- if expression = valid_character
31
- stripped = [ ] ; for c in string . split ( // )
32
- stripped << c if valid_character . match ( c )
33
- end ; stripped . join
34
- else
35
- string
36
- end
37
- end
38
- end
39
- end
34
+ if 'string' . respond_to? ( :force_encoding )
35
+ # Removes all invalid characters from the string.
36
+ #
37
+ # Note: this method is a no-op in Ruby 1.9
38
+ def self . clean ( string )
39
+ string
40
+ end
41
+ else
42
+ def self . clean ( string )
43
+ if expression = valid_character
44
+ stripped = [ ] ; for c in string . split ( // )
45
+ stripped << c if expression . match ( c )
46
+ end ; stripped . join
47
+ else
48
+ string
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
0 commit comments