File: utils.rb

package info (click to toggle)
ruby-mail 2.8.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,704 kB
  • sloc: ruby: 73,709; makefile: 3
file content (44 lines) | stat: -rw-r--r-- 1,144 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# encoding: utf-8
# frozen_string_literal: true

module Mail #:nodoc:
  module Multibyte #:nodoc:
    # Returns a regular expression that matches valid characters in the current encoding
    def self.valid_character
      VALID_CHARACTER[Encoding.default_external.to_s]
    end

    # Returns true if string has valid utf-8 encoding
    def self.is_utf8?(string)
      case string.encoding
      when Encoding::UTF_8
        verify(string)
      when Encoding::ASCII_8BIT, Encoding::US_ASCII
        verify(to_utf8(string))
      else
        false
      end
    end

    # Verifies the encoding of a string
    def self.verify(string)
      string.valid_encoding?
    end

    # Verifies the encoding of the string and raises an exception when it's not valid
    def self.verify!(string)
      raise EncodingError.new("Found characters with invalid encoding") unless verify(string)
    end

    # Removes all invalid characters from the string.
    #
    # Note: this method is a no-op in Ruby 1.9
    def self.clean(string)
      string
    end

    def self.to_utf8(string)
      string.dup.force_encoding(Encoding::UTF_8)
    end
  end
end