summaryrefslogtreecommitdiffstatshomepage
path: root/py/emitcpy.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-01-25 13:51:19 +0000
committerDamien George <damien.p.george@gmail.com>2014-01-25 13:51:19 +0000
commitb829b5caecd1ba4fbc30e03978776d2c868dd67b (patch)
treeb3135377105920ff382d7c7ebc2117655b4d00da /py/emitcpy.c
parente0722ee9d94c0e812a474e349ded5147f6b869f6 (diff)
downloadmicropython-b829b5caecd1ba4fbc30e03978776d2c868dd67b.tar.gz
micropython-b829b5caecd1ba4fbc30e03978776d2c868dd67b.zip
Implement mp_parse_node_free; print properly repr(string).
Diffstat (limited to 'py/emitcpy.c')
-rw-r--r--py/emitcpy.c31
1 files changed, 14 insertions, 17 deletions
diff --git a/py/emitcpy.c b/py/emitcpy.c
index de2a5784db..71861c918d 100644
--- a/py/emitcpy.c
+++ b/py/emitcpy.c
@@ -192,29 +192,26 @@ static void print_quoted_str(qstr qstr, bool bytes) {
if (bytes) {
printf("b");
}
- bool quote_single = false;
+ int quote_char = '\'';
if (has_single_quote && !has_double_quote) {
- printf("\"");
- } else {
- quote_single = true;
- printf("'");
+ quote_char = '"';
}
- for (int i = 0; i < len; i++) {
- if (str[i] == '\n') {
- printf("\\n");
- } else if (str[i] == '\\') {
+ printf("%c", quote_char);
+ for (const char *s = str, *top = str + len; s < top; s++) {
+ if (*s == quote_char) {
+ printf("\\%c", quote_char);
+ } else if (*s == '\\') {
printf("\\\\");
- } else if (str[i] == '\'' && quote_single) {
- printf("\\'");
+ } else if (32 <= *s && *s <= 126) {
+ printf("%c", *s);
+ } else if (*s == '\n') {
+ printf("\\n");
+ // TODO add more escape codes here
} else {
- printf("%c", str[i]);
+ printf("\\x%02x", (*s) & 0xff);
}
}
- if (has_single_quote && !has_double_quote) {
- printf("\"");
- } else {
- printf("'");
- }
+ printf("%c", quote_char);
}
static void emit_cpy_load_const_str(emit_t *emit, qstr qstr, bool bytes) {