Class: Hocon::Impl::ConfigImplUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/hocon/impl/config_impl_util.rb

Class Method Summary collapse

Class Method Details

.equals_handling_nil?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/hocon/impl/config_impl_util.rb', line 7

def self.equals_handling_nil?(a, b)
  # This method probably doesn't make any sense in ruby... not sure
  if a.nil? && !b.nil?
    false
  elsif !a.nil? && b.nil?
    false
  # in ruby, the == and .equal? are the opposite of what they are in Java
  elsif a.equal?(b)
    true
  else
    a == b
  end
end

.join_path(*elements) ⇒ Object



73
74
75
# File 'lib/hocon/impl/config_impl_util.rb', line 73

def self.join_path(*elements)
  Hocon::Impl::Path.from_string_list(elements).render
end

.render_json_string(s) ⇒ Object

This is public ONLY for use by the “config” package, DO NOT USE this ABI may change.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hocon/impl/config_impl_util.rb', line 25

def self.render_json_string(s)
  sb = StringIO.new
  sb << '"'
  s.chars.each do |c|
    case c
      when '"' then sb << "\\\""
      when "\\" then sb << "\\\\"
      when "\n" then sb << "\\n"
      when "\b" then sb << "\\b"
      when "\f" then sb << "\\f"
      when "\r" then sb << "\\r"
      when "\t" then sb << "\\t"
      else
        if c =~ /[[:cntrl:]]/
          sb << ("\\u%04x" % c)
        else
          sb << c
        end
    end
  end
  sb << '"'
  sb.string
end

.render_string_unquoted_if_possible(s) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/hocon/impl/config_impl_util.rb', line 49

def self.render_string_unquoted_if_possible(s)
  # this can quote unnecessarily as long as it never fails to quote when
  # necessary
  if s.length == 0
    return render_json_string(s)
  end

  # if it starts with a hyphen or number, we have to quote
  # to ensure we end up with a string and not a number
  first = s.chars.first
  if (first =~ /[[:digit:]]/) || (first == '-')
    return render_json_string(s)
  end

  # only unquote if it's pure alphanumeric
  s.chars.each do |c|
    unless (c =~ /[[:alnum:]]/) || (c == '-')
      return render_json_string(s)
    end
  end

  s
end

.split_path(path) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/hocon/impl/config_impl_util.rb', line 77

def self.split_path(path)
  p = Hocon::Impl::Path.new_path(path)
  elements = []

  until p.nil?
    elements << p.first
    p = p.remainder
  end

  elements
end

.unicode_trim(s) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/hocon/impl/config_impl_util.rb', line 97

def self.unicode_trim(s)
  # this implementation is *not* a port of the java code. Ruby can strip
  # unicode whitespace much easier than Java can, and relies on a lot of
  # Java functions that don't really have straight equivalents in Ruby.
  s.gsub(/[:space]/, ' ')
  s.strip
end

.whitespace?(c) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
# File 'lib/hocon/impl/config_impl_util.rb', line 89

def self.whitespace?(c)
  # this implementation is *not* a port of the java code, because it relied on
  # the method java.lang.Character#isWhitespace.  This is probably
  # insanely slow (running a regex against every single character in the
  # file).
  c =~ /[[:space:]]/
end