Class: Baykit::BayServer::Util::StringUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/baykit/bayserver/util/string_util.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.falsesObject (readonly)

Returns the value of attribute falses.



6
7
8
# File 'lib/baykit/bayserver/util/string_util.rb', line 6

def falses
  @falses
end

.truesObject (readonly)

Returns the value of attribute trues.



7
8
9
# File 'lib/baykit/bayserver/util/string_util.rb', line 7

def trues
  @trues
end

Class Method Details

.alloc(len) ⇒ Object



21
22
23
# File 'lib/baykit/bayserver/util/string_util.rb', line 21

def StringUtil.alloc(len)
  String.new("", encoding: Encoding::ASCII_8BIT, capacity: len)
end

.empty?(str) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/baykit/bayserver/util/string_util.rb', line 17

def StringUtil.empty?(str)
  !set?(str)
end

.indent(count) ⇒ Object



37
38
39
# File 'lib/baykit/bayserver/util/string_util.rb', line 37

def StringUtil.indent(count)
  return repeat(" ", count);
end

.parse_bool(val) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/baykit/bayserver/util/string_util.rb', line 41

def StringUtil.parse_bool(val)
  val = val.downcase()
  if @trues.include?(val)
    return true
  elsif @falses.include?(val)
    return false
  else
    BayLog.warn("Invalid boolean value: %s", val)
    return false
  end
end

.parse_size(val) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/baykit/bayserver/util/string_util.rb', line 53

def StringUtil.parse_size(val)
  val = val.downcase
  rate = 1
  if val.end_with?("b")
    val = val[0, val.length - 1]
  end

  if val.end_with?("k")
    val = val[0, val.length - 1]
    rate = 1024
  elsif val.end_with?("m")
    val = val[0, val.length - 1]
    rate = 1024 * 1024
  end

  Integer(val) * rate
end

.realloc(buf, len) ⇒ Object



25
26
27
# File 'lib/baykit/bayserver/util/string_util.rb', line 25

def StringUtil.realloc(buf, len)
  String.new(buf, encoding: Encoding::ASCII_8BIT, capacity: len)
end

.repeat(str, times) ⇒ Object



33
34
35
# File 'lib/baykit/bayserver/util/string_util.rb', line 33

def StringUtil.repeat(str, times)
  return Array.new(times, str).join("")
end

.set?(str) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/baykit/bayserver/util/string_util.rb', line 13

def StringUtil.set?(str)
  str != nil && str.length > 0
end

.to_bytes(buf) ⇒ Object



29
30
31
# File 'lib/baykit/bayserver/util/string_util.rb', line 29

def StringUtil.to_bytes(buf)
  buf.encode(encoding: Encoding::ASCII_8BIT)
end