diff options
Diffstat (limited to 'identifiers.c')
-rw-r--r-- | identifiers.c | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/identifiers.c b/identifiers.c index 73bc413..370ad7d 100644 --- a/identifiers.c +++ b/identifiers.c @@ -136,13 +136,39 @@ static void fprint_ident_reduced_charset(FILE *out, Identifier id) { for (const char *s = id->str; is_ident(*s); ++s) { int c = (unsigned char)(*s); if (c > 127) { - fprintf(out, "x__%x", c); + fprintf(out, "x__%02x", c); } else { putc(*s, out); } } } +static char *ident_to_str_reduced_charset(Identifier id) { + assert(id); + size_t nchars = 0; + for (const char *s = id->str; is_ident(*s); ++s) { + int c = (unsigned char)(*s); + if (c > 127) + nchars += 5; + else + ++nchars; + } + char *ret = err_malloc(nchars+1); + char *p = ret; + for (const char *s = id->str; is_ident(*s); ++s) { + int c = (unsigned char)(*s); + if (c > 127) + sprintf(p, "x__%02x", c); + else + *p = (char)c; + ++p; + } + *p = 0; + assert(p == ret + nchars); + return ret; +} + + /* NULL = no such identifier. returns identifier "foo" for both "foo\0" and "foo+92384324..." */ static Identifier ident_get(Identifiers *ids, char *s) { size_t len = ident_str_len(s); |