Method: Converter#rstring2cstr
- Defined in:
- lib/cast_off/compile/namespace/namespace.rb
#rstring2cstr(str, strmax, rs = nil) ⇒ Object
Returns a 2-dimensional array [[str, len], [str, len], … ]
This is needed because Ruby’s String#dump is different from C’s.
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 |
# File 'lib/cast_off/compile/namespace/namespace.rb', line 511 def rstring2cstr str, strmax, rs = nil return [["".inspect, 0]] if str.empty? a = str.each_line rs a = a.to_a a.map! do |b| c = b.each_byte.each_slice strmax c.to_a end a.flatten! 1 a.map! do |bytes| b = bytes.each_slice 80 c = b.map do |d| d.map do |e| '\\x%x' % e #case e # this case statement is optimized #when 0x00 then '\\0' #when 0x07 then '\\a' #when 0x08 then '\\b' #when 0x09 then '\\t' #when 0x0A then '\\n' #when 0x0B then '\\v' #when 0x0C then '\\f' #when 0x0D then '\\r' #when 0x22 then '\\"' #when 0x27 then '\\\'' #when 0x5C then '\\\\' # not \\ #else #case e #when 0x20 ... 0x7F then '%c' % e #else '\\x%x' % e #end #end end end c.map! do |d| "\n " '"' + d.join + '"' end if c.size == 1 c.first.strip! end [ c.join, bytes.size, ] end a end |