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)
| 208 | |
| 209 | |
| 210 | def 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 | |
| 239 | def to_c_symbol(filename, used): |
no test coverage detected