MCPcopy Create free account
hub / github.com/emscripten-core/emscripten / to_asm_string

Function to_asm_string

tools/file_packager.py:210–236  ·  view source on GitHub ↗

Convert a python string to string suitable for using in `.asciz` assembly directive. The result will be an UTF-8 encoded string in the data section.

(string)

Source from the content-addressed store, hash-verified

208
209
210def to_asm_string(string):
211 """Convert a python string to string suitable for using in `.asciz` assembly directive.
212
213 The result will be an UTF-8 encoded string in the data section.
214 """
215 # See MCAsmStreamer::PrintQuotedString in llvm/lib/MC/MCAsmStreamer.cpp
216 # And isPrint in llvm/include/llvm/ADT/StringExtras.h
217
218 def is_print(c):
219 return c >= 0x20 and c <= 0x7E
220
221 def escape(c):
222 if is_print(c):
223 return chr(c)
224 escape_chars = {
225 '\b': '\\b',
226 '\f': '\\f',
227 '\n': '\\n',
228 '\r': '\\r',
229 '\t': '\\t',
230 }
231 if c in escape_chars:
232 return escape_chars[c]
233 # Encode all other chars as three octal digits(!)
234 return '\\%s%s%s' % (oct(c >> 6), oct(c >> 3), oct(c >> 0))
235
236 return ''.join(escape(c) for c in string.encode('utf-8'))
237
238
239def to_c_symbol(filename, used):

Callers 1

generate_object_fileFunction · 0.85

Calls 3

encodeMethod · 0.80
escapeFunction · 0.70
joinMethod · 0.45

Tested by

no test coverage detected