Skip to content

Commit

Permalink
Factor out duplicate code for writing 4 hex digits
Browse files Browse the repository at this point in the history
  • Loading branch information
ojwb committed Nov 20, 2024
1 parent a10c9c4 commit b72b71f
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 91 deletions.
8 changes: 8 additions & 0 deletions compiler/generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ static void write_hexdigit(struct generator * g, int i) {
str_append_ch(g->outbuf, "0123456789ABCDEF"[i & 0xF]); /* hexchar */
}

extern void write_hex4(struct generator * g, int ch) {
int i;
for (i = 0; i < 4; ++i) {
write_hexdigit(g, ch);
ch >>= 4;
}
}

static void write_hex(struct generator * g, int i) {
if (i >> 4) write_hex(g, i >> 4);
write_hexdigit(g, i); /* hex integer */
Expand Down
17 changes: 2 additions & 15 deletions compiler/generator_csharp.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,6 @@ static void write_varref(struct generator * g, struct name * p) {
write_varname(g, p);
}

static void write_hexdigit(struct generator * g, int n) {

write_char(g, n < 10 ? n + '0' : n - 10 + 'A');
}

static void write_hex(struct generator * g, int ch) {

write_string(g, "\\u");
{
int i;
for (i = 12; i >= 0; i -= 4) write_hexdigit(g, ch >> i & 0xf);
}
}

static void write_literal_string(struct generator * g, symbol * p) {

int i;
Expand All @@ -67,7 +53,8 @@ static void write_literal_string(struct generator * g, symbol * p) {
if (ch == '\"' || ch == '\\') write_string(g, "\\");
write_char(g, ch);
} else {
write_hex(g, ch);
write_string(g, "\\u");
write_hex4(g, ch);
}
}
write_string(g, "\"");
Expand Down
17 changes: 2 additions & 15 deletions compiler/generator_go.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,6 @@ static void write_varref(struct generator * g, struct name * p) {
write_varname(g, p);
}

static void write_hexdigit(struct generator * g, int n) {

write_char(g, n < 10 ? n + '0' : n - 10 + 'A');
}

static void write_hex(struct generator * g, int ch) {

write_string(g, "\\u");
{
int i;
for (i = 12; i >= 0; i -= 4) write_hexdigit(g, ch >> i & 0xf);
}
}

static void write_literal_string(struct generator * g, symbol * p) {

int i = 0;
Expand All @@ -78,7 +64,8 @@ static void write_literal_string(struct generator * g, symbol * p) {
if (ch == '\"' || ch == '\\') write_string(g, "\\");
write_char(g, ch);
} else {
write_hex(g, ch);
write_string(g, "\\u");
write_hex4(g, ch);
}
}
write_string(g, "\"");
Expand Down
17 changes: 2 additions & 15 deletions compiler/generator_java.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,6 @@ static void write_varref(struct generator * g, struct name * p) {
write_varname(g, p);
}

static void write_hexdigit(struct generator * g, int n) {

write_char(g, n < 10 ? n + '0' : n - 10 + 'A');
}

static void write_hex(struct generator * g, int ch) {

write_string(g, "\\u");
{
int i;
for (i = 12; i >= 0; i -= 4) write_hexdigit(g, ch >> i & 0xf);
}
}

static void write_literal_string(struct generator * g, symbol * p) {

int i;
Expand All @@ -67,7 +53,8 @@ static void write_literal_string(struct generator * g, symbol * p) {
if (ch == '\"' || ch == '\\') write_string(g, "\\");
write_char(g, ch);
} else {
write_hex(g, ch);
write_string(g, "\\u");
write_hex4(g, ch);
}
}
write_string(g, "\"");
Expand Down
17 changes: 2 additions & 15 deletions compiler/generator_js.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,6 @@ static void write_varref(struct generator * g, struct name * p) {
write_varname(g, p);
}

static void write_hexdigit(struct generator * g, int n) {

write_char(g, n < 10 ? n + '0' : n - 10 + 'A');
}

static void write_hex(struct generator * g, int ch) {

write_string(g, "\\u");
{
int i;
for (i = 12; i >= 0; i -= 4) write_hexdigit(g, ch >> i & 0xf);
}
}

static void write_literal_string(struct generator * g, symbol * p) {

int i;
Expand All @@ -65,7 +51,8 @@ static void write_literal_string(struct generator * g, symbol * p) {
if (ch == '\"' || ch == '\\') write_string(g, "\\");
write_char(g, ch);
} else {
write_hex(g, ch);
write_string(g, "\\u");
write_hex4(g, ch);
}
}
write_string(g, "\"");
Expand Down
17 changes: 2 additions & 15 deletions compiler/generator_python.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,6 @@ static void write_varref(struct generator * g, struct name * p) {
write_varname(g, p);
}

static void write_hexdigit(struct generator * g, int n) {

write_char(g, n < 10 ? n + '0' : n - 10 + 'A');
}

static void write_hex(struct generator * g, int ch) {

write_string(g, "\\u");
{
int i;
for (i = 12; i >= 0; i -= 4) write_hexdigit(g, ch >> i & 0xf);
}
}

static void write_literal_string(struct generator * g, symbol * p) {

int i;
Expand All @@ -77,7 +63,8 @@ static void write_literal_string(struct generator * g, symbol * p) {
if (ch == '\"' || ch == '\\') write_string(g, "\\");
write_char(g, ch);
} else {
write_hex(g, ch);
write_string(g, "\\u");
write_hex4(g, ch);
}
}
write_string(g, "\"");
Expand Down
19 changes: 3 additions & 16 deletions compiler/generator_rust.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,6 @@ static void write_varref(struct generator * g, struct name * p) {
write_varname(g, p);
}

static void write_hexdigit(struct generator * g, int n) {

write_char(g, n < 10 ? n + '0' : n - 10 + 'A');
}

static void write_hex(struct generator * g, int ch) {

write_string(g, "\\u{");
{
int i;
for (i = 12; i >= 0; i -= 4) write_hexdigit(g, ch >> i & 0xf);
}
write_string(g, "}");
}

static void write_literal_string(struct generator * g, symbol * p) {

int i = 0;
Expand All @@ -73,7 +58,9 @@ static void write_literal_string(struct generator * g, symbol * p) {
if (ch == '\"' || ch == '\\') write_string(g, "\\");
write_char(g, ch);
} else {
write_hex(g, ch);
write_string(g, "\\u{");
write_hex4(g, ch);
write_string(g, "}");
}
}
write_string(g, "\"");
Expand Down
1 change: 1 addition & 0 deletions compiler/header.h
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ extern void write_char(struct generator * g, int ch);
extern void write_newline(struct generator * g);
extern void write_string(struct generator * g, const char * s);
extern void write_int(struct generator * g, int i);
extern void write_hex4(struct generator * g, int ch);
extern void write_symbol(struct generator * g, symbol s);
extern void write_s(struct generator * g, const byte * b);
extern void write_str(struct generator * g, struct str * str);
Expand Down

0 comments on commit b72b71f

Please sign in to comment.