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]
11
def self.valid_character
14
VALID_CHARACTER['UTF-8']
16
VALID_CHARACTER['Shift_JIS']
21
if 'string'.respond_to?(:valid_encoding?)
22
# Verifies the encoding of a string
23
def self.verify(string)
24
string.valid_encoding?
27
def self.verify(string)
28
if expression = valid_character
29
for c in string.split(//)
30
return false unless expression.match(c)
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)
42
if 'string'.respond_to?(:force_encoding)
43
# Removes all invalid characters from the string.
45
# Note: this method is a no-op in Ruby 1.9
46
def self.clean(string)
50
def self.clean(string)
51
if expression = valid_character
52
stripped = []; for c in string.split(//)
53
stripped << c if expression.match(c)