Class: ToLua::Helpers

Inherits:
Object show all
Defined in:
lib/to_lua/helpers.rb

Constant Summary collapse

LUA_C_STYLE_ESCAPES =
{
  "\a" => "\\a",
  "\b" => "\\b",
  "\f" => "\\f",
  "\n" => "\\n",
  "\r" => "\\r",
  "\t" => "\\t",
  "\v" => "\\v",
  "\\" => "\\\\",
  "\"" => "\\\"",
  "'"  => "\\'",
  "["  => "\\[",
  "]"  => "\\]"
}.freeze

Class Method Summary collapse

Class Method Details

.encode_string(string) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/to_lua/helpers.rb', line 18

def self.encode_string(string)
  encoded = []

  string.each_char do |char|
    if LUA_C_STYLE_ESCAPES[char]
      encoded << LUA_C_STYLE_ESCAPES[char]
    elsif char =~ /^[^[:print:]]$/
      char.each_byte do |byte|
        encoded << '\\'
        encoded << byte.to_s
      end
    else
      encoded << char
    end
  end

  encoded.join
end