File: encoding_helper.rb

package info (click to toggle)
ruby-pdf-reader 2.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,512 kB
  • sloc: ruby: 11,959; sh: 46; makefile: 11
file content (48 lines) | stat: -rw-r--r-- 1,082 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
45
46
47
48
# typed: false
# coding: utf-8

module EncodingHelper
  def binary_string(str)
    str.dup.force_encoding("binary")
  end

  # On M17N aware VMs, recursively checks strings and containers with strings
  # to ensure everything is UTF-8 encoded
  #
  def check_utf8(obj)
    case obj
    when Array
      obj.each { |item| check_utf8(item) }
    when Hash
      obj.each { |key, value|
        check_utf8(key)
        check_utf8(value)
      }
    when String
      expect(obj.encoding).to eq Encoding.find("utf-8")
      expect(obj.valid_encoding?).to eq true
    else
      return
    end
  end

  # On M17N aware VMs, recursively checks strings and containers with strings
  # to ensure everything is Binary encoded
  #
  def check_binary(obj)
    case obj
    when Array
      obj.each { |item| check_binary(item) }
    when Hash
      obj.each { |key, value|
        check_binary(key)
        check_binary(value)
      }
    when String
      expect(obj.encoding).to eq Encoding.find("binary")
      expect(obj.valid_encoding?).to eq true
    else
      return
    end
  end
end