1 # encoding: utf-8
2
3 module ActiveSupport #:nodoc:
4 module Multibyte #:nodoc:
5 if Kernel.const_defined?(:Encoding)
6 # Returns a regular expression that matches valid characters in the current encoding
7 def self.valid_character
8 VALID_CHARACTER[Encoding.default_external.to_s]
9 end
10 else
11 def self.valid_character
12 case $KCODE
13 when 'UTF8'
14 VALID_CHARACTER['UTF-8']
15 when 'SJIS'
16 VALID_CHARACTER['Shift_JIS']
17 end
18 end
19 end
20
21 if 'string'.respond_to?(:valid_encoding?)
22 # Verifies the encoding of a string
23 def self.verify(string)
24 string.valid_encoding?
25 end
26 else
27 def self.verify(string)
28 if expression = valid_character
29 # Splits the string on character boundaries, which are determined based on $KCODE.
30 string.split(//).all? { |c| expression =~ c }
31 else
32 true
33 end
34 end
35 end
36
37 # Verifies the encoding of the string and raises an exception when it's not valid
38 def self.verify!(string)
39 raise EncodingError.new("Found characters with invalid encoding") unless verify(string)
40 end
41
42 if 'string'.respond_to?(:force_encoding)
43 # Removes all invalid characters from the string.
44 #
45 # Note: this method is a no-op in Ruby 1.9
46 def self.clean(string)
47 string
48 end
49 else
50 def self.clean(string)
51 if expression = valid_character
52 # Splits the string on character boundaries, which are determined based on $KCODE.
53 string.split(//).grep(expression).join
54 else
55 string
56 end
57 end
58 end
59 end
60 end