Module: Defstr

Defined in:
lib/defstr.rb

Constant Summary collapse

VERSION =
'0.1.1'

Class Method Summary collapse

Class Method Details

.define_string(name, s) ⇒ Object

Generate a C macro that defines the given string as a string constant.



31
32
33
# File 'lib/defstr.rb', line 31

def define_string(name, s)
	result = "#define #{name.upcase} " + string_literal(s)	
end

.string_literal(s) ⇒ Object

Turn the given string to a C string literal that parses to the original string.



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

def string_literal(s)
	result = "\"\\\n"
	s.each_byte do |b|
c = b.chr
if c == "\n"
	result += "\\n\\\n"
elsif c == "\r"
	next
elsif c == "\\"
	result += "\\\\"
elsif c == "\""
	result += "\\\""
else
	result += c
end
	end
	result += "\"\n"
	result
end