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

jnbt / java-properties / 4254520611

pending completion
4254520611

push

github

GitHub
Merge pull request #21 from jnbt/update-ruby

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

100.0
/lib/java-properties/parsing/parser.rb
1
# coding: utf-8
2
module JavaProperties
7✔
3
  module Parsing
7✔
4
    # This module allows parsing of a properties file content string
5
    # into a {Properties} object
6
    #
7
    # @example
8
    #   Parser.parse("item=something \u05d4") => {:item => "something ה"}
9
    #
10
    module Parser
7✔
11

12
      # Symbol which separates key from value after normalization
13
      # @return [String]
14
      KEY_VALUE_MARKER = '='
7✔
15
      
16
      # Symbol which escapes a KEY_VALUE_MARKER in the key name
17
      # @return [String]
18
      KEY_ESCAPE = '\\'
7✔
19

20
      # Marker for a line which only consists of an key w/o value
21
      # @return [Regexp]
22
      KEY_ONLY_MARKER  = /^(\S+)$/
7✔
23

24
      # Parses a string into a {Properties} object
25
      # @param text [String]
26
      # @return [Properties]
27
      def self.parse(text)
7✔
28
        properties = Properties.new
28✔
29
        Normalizer.normalize!(text)
28✔
30
        text.each_line do |line|
28✔
31
          key, value = extract_key_and_value(line.chomp)
126✔
32
          append_to_properties(properties, key, value)
126✔
33
        end
34
        properties
28✔
35
      end
36

37
      private 
7✔
38

39
      def self.extract_key_and_value(line)
7✔
40
        # A line must be handled char by char to handled escaped '=' chars in the key name
41
        key          = StringIO.new
126✔
42
        value        = StringIO.new
126✔
43
        key_complete = false
126✔
44
        last_token   = ''
126✔
45
        line.each_char do |char|
126✔
46
          if !key_complete && char == KEY_VALUE_MARKER && last_token != KEY_ESCAPE
2,394✔
47
            key_complete = true
119✔
48
          else
49
            (key_complete ? value : key) << char
2,275✔
50
          end
51
          last_token = char
2,394✔
52
        end
53
        [key.string, value.string]
126✔
54
      end
55

56
      def self.append_to_properties(properties, key, value)
7✔
57
        unless key.nil? && value.nil?
126✔
58
          properties[Encoding.decode!(key).to_sym] = Encoding.decode!(value, Encoding::SKIP_SEPARATORS)
126✔
59
        end
60
      end
61

62
    end
63
  end
64
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