1#! /your/favourite/path/to/ruby 2# -*- mode: ruby; coding: utf-8; indent-tabs-mode: nil; ruby-indent-level: 2 -*- 3# -*- frozen_string_literal: true; -*- 4# -*- warn_indent: true; -*- 5# 6# Copyright (c) 2017 Urabe, Shyouhei. All rights reserved. 7# 8# This file is a part of the programming language Ruby. Permission is hereby 9# granted, to either redistribute and/or modify this file, provided that the 10# conditions mentioned in the file COPYING are met. Consult the file for 11# details. 12 13require 'securerandom' 14 15module RubyVM::CEscape 16 module_function 17 18 # generate comment, with escaps. 19 def commentify str 20 return "/* #{str.strip.b.gsub '*/', '*\\/'} */" 21 end 22 23 # Mimic gensym of CL. 24 def gensym prefix = 'gensym_' 25 return as_tr_cpp "#{prefix}#{SecureRandom.uuid}" 26 end 27 28 # Mimic AS_TR_CPP() of autoconf. 29 def as_tr_cpp name 30 q = name.b 31 q.gsub! %r/[^a-zA-Z0-9_]/m, '_' 32 q.gsub! %r/_+/, '_' 33 return q 34 end 35 36 # Section 6.10.4 of ISO/IEC 9899:1999 specifies that the file name used for 37 # #line directive shall be a "character string literal". So this is needed. 38 # 39 # I'm not sure how many chars are allowed here, though. The standard 40 # specifies 4095 chars at most, after string concatenation (section 5.2.4.1). 41 # But it is easy to have a path that is longer than that. 42 # 43 # Here we ignore the standard. Just create single string literal of any 44 # needed length. 45 def rstring2cstr str 46 # I believe this is the fastest implementation done in pure-ruby. 47 # Constants cached, gsub skips block evaluation, string literal optimized. 48 buf = str.b 49 buf.gsub! %r/./n, RString2CStr 50 return %'"#{buf}"' 51 end 52 53 RString2CStr = { 54 "\x00"=> "\\0", "\x01"=> "\\x1", "\x02"=> "\\x2", "\x03"=> "\\x3", 55 "\x04"=> "\\x4", "\x05"=> "\\x5", "\x06"=> "\\x6", "\a"=> "\\a", 56 "\b"=> "\\b", "\t"=> "\\t", "\n"=> "\\n", "\v"=> "\\v", 57 "\f"=> "\\f", "\r"=> "\\r", "\x0E"=> "\\xe", "\x0F"=> "\\xf", 58 "\x10"=>"\\x10", "\x11"=>"\\x11", "\x12"=>"\\x12", "\x13"=>"\\x13", 59 "\x14"=>"\\x14", "\x15"=>"\\x15", "\x16"=>"\\x16", "\x17"=>"\\x17", 60 "\x18"=>"\\x18", "\x19"=>"\\x19", "\x1A"=>"\\x1a", "\e"=>"\\x1b", 61 "\x1C"=>"\\x1c", "\x1D"=>"\\x1d", "\x1E"=>"\\x1e", "\x1F"=>"\\x1f", 62 " "=> " ", "!"=> "!", "\""=> "\\\"", "#"=> "#", 63 "$"=> "$", "%"=> "%", "&"=> "&", "'"=> "\\'", 64 "("=> "(", ")"=> ")", "*"=> "*", "+"=> "+", 65 ","=> ",", "-"=> "-", "."=> ".", "/"=> "/", 66 "0"=> "0", "1"=> "1", "2"=> "2", "3"=> "3", 67 "4"=> "4", "5"=> "5", "6"=> "6", "7"=> "7", 68 "8"=> "8", "9"=> "9", ":"=> ":", ";"=> ";", 69 "<"=> "<", "="=> "=", ">"=> ">", "?"=> "?", 70 "@"=> "@", "A"=> "A", "B"=> "B", "C"=> "C", 71 "D"=> "D", "E"=> "E", "F"=> "F", "G"=> "G", 72 "H"=> "H", "I"=> "I", "J"=> "J", "K"=> "K", 73 "L"=> "L", "M"=> "M", "N"=> "N", "O"=> "O", 74 "P"=> "P", "Q"=> "Q", "R"=> "R", "S"=> "S", 75 "T"=> "T", "U"=> "U", "V"=> "V", "W"=> "W", 76 "X"=> "X", "Y"=> "Y", "Z"=> "Z", "["=> "[", 77 "\\"=> "\\\\", "]"=> "]", "^"=> "^", "_"=> "_", 78 "`"=> "`", "a"=> "a", "b"=> "b", "c"=> "c", 79 "d"=> "d", "e"=> "e", "f"=> "f", "g"=> "g", 80 "h"=> "h", "i"=> "i", "j"=> "j", "k"=> "k", 81 "l"=> "l", "m"=> "m", "n"=> "n", "o"=> "o", 82 "p"=> "p", "q"=> "q", "r"=> "r", "s"=> "s", 83 "t"=> "t", "u"=> "u", "v"=> "v", "w"=> "w", 84 "x"=> "x", "y"=> "y", "z"=> "z", "{"=> "{", 85 "|"=> "|", "}"=> "}", "~"=> "~", "\x7F"=>"\\x7f", 86 "\x80"=>"\\x80", "\x81"=>"\\x81", "\x82"=>"\\x82", "\x83"=>"\\x83", 87 "\x84"=>"\\x84", "\x85"=>"\\x85", "\x86"=>"\\x86", "\x87"=>"\\x87", 88 "\x88"=>"\\x88", "\x89"=>"\\x89", "\x8A"=>"\\x8a", "\x8B"=>"\\x8b", 89 "\x8C"=>"\\x8c", "\x8D"=>"\\x8d", "\x8E"=>"\\x8e", "\x8F"=>"\\x8f", 90 "\x90"=>"\\x90", "\x91"=>"\\x91", "\x92"=>"\\x92", "\x93"=>"\\x93", 91 "\x94"=>"\\x94", "\x95"=>"\\x95", "\x96"=>"\\x96", "\x97"=>"\\x97", 92 "\x98"=>"\\x98", "\x99"=>"\\x99", "\x9A"=>"\\x9a", "\x9B"=>"\\x9b", 93 "\x9C"=>"\\x9c", "\x9D"=>"\\x9d", "\x9E"=>"\\x9e", "\x9F"=>"\\x9f", 94 "\xA0"=>"\\xa0", "\xA1"=>"\\xa1", "\xA2"=>"\\xa2", "\xA3"=>"\\xa3", 95 "\xA4"=>"\\xa4", "\xA5"=>"\\xa5", "\xA6"=>"\\xa6", "\xA7"=>"\\xa7", 96 "\xA8"=>"\\xa8", "\xA9"=>"\\xa9", "\xAA"=>"\\xaa", "\xAB"=>"\\xab", 97 "\xAC"=>"\\xac", "\xAD"=>"\\xad", "\xAE"=>"\\xae", "\xAF"=>"\\xaf", 98 "\xB0"=>"\\xb0", "\xB1"=>"\\xb1", "\xB2"=>"\\xb2", "\xB3"=>"\\xb3", 99 "\xB4"=>"\\xb4", "\xB5"=>"\\xb5", "\xB6"=>"\\xb6", "\xB7"=>"\\xb7", 100 "\xB8"=>"\\xb8", "\xB9"=>"\\xb9", "\xBA"=>"\\xba", "\xBB"=>"\\xbb", 101 "\xBC"=>"\\xbc", "\xBD"=>"\\xbd", "\xBE"=>"\\xbe", "\xBF"=>"\\xbf", 102 "\xC0"=>"\\xc0", "\xC1"=>"\\xc1", "\xC2"=>"\\xc2", "\xC3"=>"\\xc3", 103 "\xC4"=>"\\xc4", "\xC5"=>"\\xc5", "\xC6"=>"\\xc6", "\xC7"=>"\\xc7", 104 "\xC8"=>"\\xc8", "\xC9"=>"\\xc9", "\xCA"=>"\\xca", "\xCB"=>"\\xcb", 105 "\xCC"=>"\\xcc", "\xCD"=>"\\xcd", "\xCE"=>"\\xce", "\xCF"=>"\\xcf", 106 "\xD0"=>"\\xd0", "\xD1"=>"\\xd1", "\xD2"=>"\\xd2", "\xD3"=>"\\xd3", 107 "\xD4"=>"\\xd4", "\xD5"=>"\\xd5", "\xD6"=>"\\xd6", "\xD7"=>"\\xd7", 108 "\xD8"=>"\\xd8", "\xD9"=>"\\xd9", "\xDA"=>"\\xda", "\xDB"=>"\\xdb", 109 "\xDC"=>"\\xdc", "\xDD"=>"\\xdd", "\xDE"=>"\\xde", "\xDF"=>"\\xdf", 110 "\xE0"=>"\\xe0", "\xE1"=>"\\xe1", "\xE2"=>"\\xe2", "\xE3"=>"\\xe3", 111 "\xE4"=>"\\xe4", "\xE5"=>"\\xe5", "\xE6"=>"\\xe6", "\xE7"=>"\\xe7", 112 "\xE8"=>"\\xe8", "\xE9"=>"\\xe9", "\xEA"=>"\\xea", "\xEB"=>"\\xeb", 113 "\xEC"=>"\\xec", "\xED"=>"\\xed", "\xEE"=>"\\xee", "\xEF"=>"\\xef", 114 "\xF0"=>"\\xf0", "\xF1"=>"\\xf1", "\xF2"=>"\\xf2", "\xF3"=>"\\xf3", 115 "\xF4"=>"\\xf4", "\xF5"=>"\\xf5", "\xF6"=>"\\xf6", "\xF7"=>"\\xf7", 116 "\xF8"=>"\\xf8", "\xF9"=>"\\xf9", "\xFA"=>"\\xfa", "\xFB"=>"\\xfb", 117 "\xFC"=>"\\xfc", "\xFD"=>"\\xfd", "\xFE"=>"\\xfe", "\xFF"=>"\\xff", 118 }.freeze 119 private_constant :RString2CStr 120end 121 122unless defined? ''.b 123 class String 124 def b 125 return dup.force_encoding 'binary' 126 end 127 end 128end 129