Module: Packcr::Util
- Included in:
- Packcr
- Defined in:
- lib/packcr/util.rb
Instance Method Summary collapse
- #dump_escaped_string(str) ⇒ Object
- #dump_integer_value(value) ⇒ Object
- #escape_character(c) ⇒ Object
- #escape_string(str) ⇒ Object
- #template(path, b, indent: 0, unwrap: false) ⇒ Object
- #unescape_string(str, is_charclass) ⇒ Object
- #unify_indent_spaces(spaces) ⇒ Object
Instance Method Details
#dump_escaped_string(str) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/packcr/util.rb', line 53 def dump_escaped_string(str) if !str $stdout.print "null" return end $stdout.print escape_string(str) end |
#dump_integer_value(value) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/packcr/util.rb', line 45 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 |
# 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 = $&.size next "\\0" * n if $1 next "\\x1b" * n if $2 next "\\\"" * n if $3 next "\\\'" * n if $4 next "\\\\" * n if $5 next $6 if $6 $7.dump[1..-2].downcase end end |
#template(path, b, indent: 0, unwrap: false) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/packcr/util.rb', line 73 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) if unwrap result.gsub!(/\A\{|\}\z/, "") indent -= 4 end result.gsub!(/^(?!$)/, " " * indent) result.gsub!(/^( *?) {0,4}<<<</) { $1 } result 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 } str.gsub!("\"" ) { "\\\"" } end str.gsub!(/\\(.)/) do c = $1 case c when "0" "\\x00" when "'" c else "\\#{c}" end end str.gsub!(/[^\x00-\x7f]/) do "\\x%02x" % $&.ord end str.replace "\"#{str}\"".undump end |
#unify_indent_spaces(spaces) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/packcr/util.rb', line 61 def unify_indent_spaces(spaces) offset = 0 spaces.tr!("\v\f", " ") spaces.gsub!(/\t+/) do chars = $`.length o = 8 * $&.length - (offset + chars) % 8 offset = (7 - chars) % 8 " " * o end spaces end |