~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/activesupport/lib/active_support/multibyte/utils.rb

  • Committer: Michael Forrest
  • Date: 2010-10-15 16:28:50 UTC
  • Revision ID: michael.forrest@canonical.com-20101015162850-tj2vchanv0kr0dun
refrozeĀ gems

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
          for c in string.split(//)
 
30
            return false unless expression.match(c)
 
31
          end
 
32
        end
 
33
        true
 
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
          stripped = []; for c in string.split(//)
 
53
            stripped << c if expression.match(c)
 
54
          end; stripped.join
 
55
        else
 
56
          string
 
57
        end
 
58
      end
 
59
    end
 
60
  end
 
61
end