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)


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

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

.render_json_string(s) ⇒ Object

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



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

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



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

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

.whitespace?(c) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
# File 'lib/hocon/impl/config_impl_util.rb', line 71

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