Module: Packcr::Util

Included in:
Packcr
Defined in:
lib/packcr/util.rb

Instance Method Summary collapse

Instance Method Details

#dump_escaped_string(str) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/packcr/util.rb', line 54

def dump_escaped_string(str)
  if !str
    $stdout.print "null"
    return
  end
  $stdout.print escape_string(str)
end

#dump_integer_value(value) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/packcr/util.rb', line 46

def dump_integer_value(value)
  if value.nil?
    $stdout.print "void"
  else
    $stdout.print value
  end
end

#escape_character(c) ⇒ Object



27
28
29
# File 'lib/packcr/util.rb', line 27

def escape_character(c)
  escape_string(c.chr)
end

#escape_string(str) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/packcr/util.rb', line 31

def escape_string(str)
  str = str.b
  str.gsub(/(\0+)|(\e+)|("+)|('+)|(\\+)|((?:(?![\0\e"'\\])[ -~])+)|([\x01-\x1a\x1c-\x1f\x7f-\xff]+)/n) do
    n = ::Regexp.last_match(0).size
    next "\\0"   * n if ::Regexp.last_match(1)
    next "\\x1b" * n if ::Regexp.last_match(2)
    next "\\\""  * n if ::Regexp.last_match(3)
    next "\\'" * n if ::Regexp.last_match(4)
    next "\\\\" * n if ::Regexp.last_match(5)
    next ::Regexp.last_match(6) if ::Regexp.last_match(6)

    ::Regexp.last_match(7).dump[1..-2].downcase
  end
end

#format_code(result, indent: 0, unwrap: false) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/packcr/util.rb', line 82

def format_code(result, indent: 0, unwrap: false)
  if unwrap
    result.gsub!(/\A\{|\}\z/, "")
    indent -= 4
  end
  result.gsub!(/^(?!$)/, " " * indent)
  result.gsub!(/^( *?) {0,4}<<<</) { ::Regexp.last_match(1) }
  result
end

#template(path, b, indent: 0, unwrap: false) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/packcr/util.rb', line 74

def template(path, b, indent: 0, unwrap: false)
  template_path = File.join(File.dirname(__FILE__), "templates", path)
  erb = ERB.new(File.read(template_path), trim_mode: "%-")
  erb.filename = template_path
  result = erb.result(b)
  format_code(result, indent: indent, unwrap: unwrap)
end

#unescape_string(str, is_charclass) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/packcr/util.rb', line 5

def unescape_string(str, is_charclass)
  if is_charclass
    str.gsub!("\\" * 2) { "\\" * 4 }
  end
  str.gsub!("\"") { "\\\"" }
  str.gsub!(/\\(.)/) do
    c = ::Regexp.last_match(1)
    case c
    when "0"
      "\\x00"
    when "'"
      c
    else
      "\\#{c}"
    end
  end
  str.gsub!(/[^\x00-\x7f]/) do
    format("\\x%02x", ::Regexp.last_match(0).ord)
  end
  str.replace "\"#{str}\"".undump
end

#unify_indent_spaces(spaces) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/packcr/util.rb', line 62

def unify_indent_spaces(spaces)
  offset = 0
  spaces.tr!("\v\f", " ")
  spaces.gsub!(/\t+/) do
    chars = ::Regexp.last_match.pre_match.length
    o = (8 * ::Regexp.last_match(0).length) - ((offset + chars) % 8)
    offset = (7 - chars) % 8
    " " * o
  end
  spaces
end