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
- #find_trailing_blanks(str) ⇒ Object
- #is_identifier_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
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
28 29 30 |
# File 'lib/packcr/util.rb', line 28 def escape_character(c) escape_string(c.chr) end |
#escape_string(str) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/packcr/util.rb', line 32 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 |
#find_trailing_blanks(str) ⇒ Object
62 63 64 |
# File 'lib/packcr/util.rb', line 62 def find_trailing_blanks(str) str =~ /[ \v\f\t\n\r]*\z/ end |
#is_identifier_string(str) ⇒ Object
3 4 5 |
# File 'lib/packcr/util.rb', line 3 def is_identifier_string(str) str.match?(/\A(?!\d)\w+\z/) end |
#template(path, b, indent: 0, unwrap: false) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/packcr/util.rb', line 78 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
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/packcr/util.rb', line 7 def unescape_string(str, is_charclass) if is_charclass str.gsub!("\\" * 2) { "\\" * 4 } 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
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/packcr/util.rb', line 66 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 |