From b72b71fd51e633cb7a63d9f1883266b3d4acac11 Mon Sep 17 00:00:00 2001 From: Olly Betts Date: Thu, 21 Nov 2024 08:38:41 +1300 Subject: [PATCH] Factor out duplicate code for writing 4 hex digits --- compiler/generator.c | 8 ++++++++ compiler/generator_csharp.c | 17 ++--------------- compiler/generator_go.c | 17 ++--------------- compiler/generator_java.c | 17 ++--------------- compiler/generator_js.c | 17 ++--------------- compiler/generator_python.c | 17 ++--------------- compiler/generator_rust.c | 19 +++---------------- compiler/header.h | 1 + 8 files changed, 22 insertions(+), 91 deletions(-) diff --git a/compiler/generator.c b/compiler/generator.c index 5c78e304..bc67f34f 100644 --- a/compiler/generator.c +++ b/compiler/generator.c @@ -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 */ diff --git a/compiler/generator_csharp.c b/compiler/generator_csharp.c index 151f85e3..7e53f480 100644 --- a/compiler/generator_csharp.c +++ b/compiler/generator_csharp.c @@ -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; @@ -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, "\""); diff --git a/compiler/generator_go.c b/compiler/generator_go.c index 0da71689..5268fda3 100644 --- a/compiler/generator_go.c +++ b/compiler/generator_go.c @@ -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; @@ -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, "\""); diff --git a/compiler/generator_java.c b/compiler/generator_java.c index a0940d7c..c1aa6fd3 100644 --- a/compiler/generator_java.c +++ b/compiler/generator_java.c @@ -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; @@ -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, "\""); diff --git a/compiler/generator_js.c b/compiler/generator_js.c index d5ed27b6..a8159956 100644 --- a/compiler/generator_js.c +++ b/compiler/generator_js.c @@ -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; @@ -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, "\""); diff --git a/compiler/generator_python.c b/compiler/generator_python.c index b4b0755b..b7f87272 100644 --- a/compiler/generator_python.c +++ b/compiler/generator_python.c @@ -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; @@ -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, "\""); diff --git a/compiler/generator_rust.c b/compiler/generator_rust.c index bdd63285..924d1830 100644 --- a/compiler/generator_rust.c +++ b/compiler/generator_rust.c @@ -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; @@ -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, "\""); diff --git a/compiler/header.h b/compiler/header.h index 3bc845ce..88efcfc9 100644 --- a/compiler/header.h +++ b/compiler/header.h @@ -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);