• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jnbt / java-properties / 4254512104

pending completion
4254512104

Pull #21

github

GitHub
Merge bd6a0ab8d into c409d01fa
Pull Request #21: Test on Ruby 3.1 and 3.2

334 of 339 relevant lines covered (98.53%)

134.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.84
/lib/java-properties/encoding/unicode.rb
1
module JavaProperties
7✔
2
  module Encoding
7✔
3
    # Module to encode and decode unicode chars
4
    # This code is highly influced by Florian Frank's JSON gem
5
    # @see https://github.com/flori/json/
6
    module Unicode
7✔
7

8
      # @private
9
      MAP = {
2✔
10
        "\x0" => '\u0000',
5✔
11
        "\x1" => '\u0001',
12
        "\x2" => '\u0002',
13
        "\x3" => '\u0003',
14
        "\x4" => '\u0004',
15
        "\x5" => '\u0005',
16
        "\x6" => '\u0006',
17
        "\x7" => '\u0007',
18
        "\xb" => '\u000b',
19
        "\xe" => '\u000e',
20
        "\xf" => '\u000f',
21
        "\x10" => '\u0010',
22
        "\x11" => '\u0011',
23
        "\x12" => '\u0012',
24
        "\x13" => '\u0013',
25
        "\x14" => '\u0014',
26
        "\x15" => '\u0015',
27
        "\x16" => '\u0016',
28
        "\x17" => '\u0017',
29
        "\x18" => '\u0018',
30
        "\x19" => '\u0019',
31
        "\x1a" => '\u001a',
32
        "\x1b" => '\u001b',
33
        "\x1c" => '\u001c',
34
        "\x1d" => '\u001d',
35
        "\x1e" => '\u001e',
36
        "\x1f" => '\u001f',
37
      }
38

39
      # @private
40
      EMPTY_8BIT_STRING = ''
7✔
41
      EMPTY_8BIT_STRING.force_encoding(::Encoding::ASCII_8BIT)
7✔
42

43
      # Decodes all unicode chars from escape sequences in place
44
      # @param text [String]
45
      # @return [String] The encoded text for chaining
46
      def self.decode!(text)
7✔
47
        string = text.dup
301✔
48
        string = string.gsub(%r((?:\\[uU](?:[A-Fa-f\d]{4}))+)) do |c|
301✔
49
          c.downcase!
119✔
50
          bytes = EMPTY_8BIT_STRING.dup
119✔
51
          i = 0
119✔
52
          while c[6 * i] == ?\\ && c[6 * i + 1] == ?u
119✔
53
            bytes << c[6 * i + 2, 2].to_i(16) << c[6 * i + 4, 2].to_i(16)
147✔
54
            i += 1
147✔
55
          end
56
          bytes.encode("utf-8", "utf-16be")
119✔
57
        end
58
        string.force_encoding(::Encoding::UTF_8)
301✔
59

60
        text.replace string
301✔
61
        text
301✔
62
      end
63

64
      # Decodes all unicode chars into escape sequences in place
65
      # @param text [String]
66
      # @return [String] The decoded text for chaining
67
      def self.encode!(text)
7✔
68
        string = text.dup
588✔
69
        string.force_encoding(::Encoding::ASCII_8BIT)
588✔
70
        string.gsub!(/["\\\x0-\x1f]/n) { |c| MAP[c] || c }
889✔
71
        string.gsub!(/(
420✔
72
          (?:
73
           [\xc2-\xdf][\x80-\xbf]    |
74
           [\xe0-\xef][\x80-\xbf]{2} |
75
           [\xf0-\xf4][\x80-\xbf]{3}
76
          )+ |
77
          [\x80-\xc1\xf5-\xff]       # invalid
78
        )/nx) { |c|
168✔
79
          c.size == 1 and raise "Invalid utf8 byte: '#{c}'"
182✔
80
          s = c.encode("utf-16be", "utf-8").unpack('H*')[0]
182✔
81
          s.force_encoding(::Encoding::ASCII_8BIT)
182✔
82
          s.gsub!(/.{4}/n, '\\\\u\&')
182✔
83
          s.force_encoding(::Encoding::UTF_8)
182✔
84
        }
85
        string.force_encoding(::Encoding::UTF_8)
588✔
86
        text.replace string
588✔
87
        text
588✔
88
      end
89

90
      private
7✔
91

92
      def self.unicode(code)
7✔
93
        code.chr(::Encoding::UTF_8)
×
94
      end
95

96
      def self.hex(codepoint)
7✔
97
        hex  = codepoint.to_s(16)
×
98
        size = [4, hex.size].max
×
99
        target_size = size.even? ? size : size+1
×
100
        hex.rjust(target_size, '0')
×
101
      end
102

103
    end
104
  end
105
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc