summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2025-05-28 21:54:58 -0700
committerKaz Kylheku <kaz@kylheku.com>2025-05-28 21:54:58 -0700
commit0ee7c2b049f3e2764f9c27dcb4f849617ae52d73 (patch)
treeddaade5532be0b10e755d9978a76701593ab8125
parent31921a308074679a6fec36aa06a75df154f6bd21 (diff)
downloadtxr-0ee7c2b049f3e2764f9c27dcb4f849617ae52d73.tar.gz
txr-0ee7c2b049f3e2764f9c27dcb4f849617ae52d73.tar.bz2
txr-0ee7c2b049f3e2764f9c27dcb4f849617ae52d73.zip
utf8: move duplicated code from parser and stream.
* stream.c (struct byte_input_ungetch): Removed. (byte_in_unget_char_callback): Removed. (byte_in_unget_char): Use struct utf8_tiny_buf instead of struct byte_input_ungetch, and use utf8_tiny_buf_putc instead of byte_in_unget_char_callback. * parser.c (struct shadow_ungetch): Removed. (shadow_unget_char_callback): Removed. (shadow_unget_char): Use struct utf8_tiny_buf instead of struct shadow_ungetch, and use utf8_tiny_buf_putc instead of shadow_unget_char_callback. * utf8.c (utf8_tiny_buf_putc): New function, identical to shadow_unget_char_callback and byte_in_unget_char_callback. * utf8.h (struct utf8_tiny_buf): New struct type, identical to removed struct shadow_ungetch and struct byte_input_ungetch. (utf8_tiny_buf_putc): Declared.
-rw-r--r--parser.c25
-rw-r--r--stream.c15
-rw-r--r--utf8.c6
-rw-r--r--utf8.h6
4 files changed, 21 insertions, 31 deletions
diff --git a/parser.c b/parser.c
index cc910d83..79be046f 100644
--- a/parser.c
+++ b/parser.c
@@ -98,11 +98,6 @@ static struct shadow_ops_map {
struct strm_ops *from, *to;
} shadow_tab[SHADOW_TAB_SIZE];
-struct shadow_ungetch {
- unsigned char buf[8];
- unsigned char *ptr;
-};
-
static void yy_tok_mark(struct yy_token *tok)
{
gc_conservative_mark(tok->yy_lval.val);
@@ -2180,31 +2175,25 @@ static val shadow_get_char(val stream)
return (wch != WEOF) ? chr(wch) : nil;
}
-static int shadow_unget_char_callback(int ch, mem_t *ctx)
-{
- struct shadow_ungetch *su = coerce(struct shadow_ungetch *, ctx);
- return (su->ptr > su->buf) ? *--su->ptr = ch, 1 : 0;
-}
-
static val shadow_unget_char(val stream, val ch)
{
struct strm_base *s = coerce(struct strm_base *, stream->co.handle);
struct shadow_context *sc = coerce(struct shadow_context *, s->shadow_obj);
- struct shadow_ungetch su;
- unsigned char *bend = su.buf + sizeof su.buf;
+ struct utf8_tiny_buf bu;
+ unsigned char *bend = bu.buf + sizeof bu.buf;
- su.ptr = bend;
+ bu.ptr = bend;
- (void) utf8_encode(c_chr(ch), shadow_unget_char_callback, coerce(mem_t *, &su));
+ (void) utf8_encode(c_chr(ch), utf8_tiny_buf_putc, coerce(mem_t *, &bu));
- if (convert(size_t, bend - su.ptr) > sc->index)
+ if (convert(size_t, bend - bu.ptr) > sc->index)
uw_throwf(file_error_s,
lit("unget-char: cannot push past beginning of byte stream"),
nao);
- while (su.ptr < bend)
- sc->buf[--sc->index] = *su.ptr++;
+ while (bu.ptr < bend)
+ sc->buf[--sc->index] = *bu.ptr++;
return ch;
}
diff --git a/stream.c b/stream.c
index e58ead71..182d5696 100644
--- a/stream.c
+++ b/stream.c
@@ -2162,11 +2162,6 @@ struct byte_input {
size_t index;
};
-struct byte_input_ungetch {
- unsigned char buf[8];
- unsigned char *ptr;
-};
-
static void byte_in_stream_destroy(val stream)
{
struct byte_input *bi = coerce(struct byte_input *, stream->co.handle);
@@ -2196,21 +2191,15 @@ static val byte_in_get_char(val stream)
return (wch != WEOF) ? chr(wch) : nil;
}
-static int byte_in_unget_char_callback(int ch, mem_t *ctx)
-{
- struct byte_input_ungetch *bu = coerce(struct byte_input_ungetch *, ctx);
- return (bu->ptr > bu->buf) ? *--bu->ptr = ch, 1 : 0;
-}
-
static val byte_in_unget_char(val stream, val ch)
{
struct byte_input *bi = coerce(struct byte_input *, stream->co.handle);
- struct byte_input_ungetch bu;
+ struct utf8_tiny_buf bu;
unsigned char *bend = bu.buf + sizeof bu.buf;
bu.ptr = bend;
- (void) utf8_encode(c_chr(ch), byte_in_unget_char_callback, coerce(mem_t *, &bu));
+ (void) utf8_encode(c_chr(ch), utf8_tiny_buf_putc, coerce(mem_t *, &bu));
if (convert(size_t, bend - bu.ptr) > bi->index)
uw_throwf(file_error_s,
diff --git a/utf8.c b/utf8.c
index 0bdb70a8..4ce7742d 100644
--- a/utf8.c
+++ b/utf8.c
@@ -275,6 +275,12 @@ int utf8_encode(wchar_t wch, int (*put)(int ch, mem_t *ctx), mem_t *ctx)
num(wch), nao);
}
+int utf8_tiny_buf_putc(int ch, mem_t *ctx)
+{
+ struct utf8_tiny_buf *bu = coerce(struct utf8_tiny_buf *, ctx);
+ return (bu->ptr > bu->buf) ? *--bu->ptr = ch, 1 : 0;
+}
+
void utf8_decoder_init(utf8_decoder_t *ud)
{
ud->state = utf8_init;
diff --git a/utf8.h b/utf8.h
index b0985613..69d7e13d 100644
--- a/utf8.h
+++ b/utf8.h
@@ -49,7 +49,13 @@ typedef struct utf8_decoder {
#define utf8_decoder_initializer { utf8_init, 0, 0, 0, 0, 0, 0, { 0 } }
+struct utf8_tiny_buf {
+ unsigned char buf[8];
+ unsigned char *ptr;
+};
+
int utf8_encode(wchar_t, int (*put)(int ch, mem_t *ctx), mem_t *ctx);
+int utf8_tiny_buf_putc(int ch, mem_t *ctx);
void utf8_decoder_init(utf8_decoder_t *);
wint_t utf8_decode(utf8_decoder_t *,int (*get)(mem_t *ctx), mem_t *ctx);
int utf8_getc(utf8_decoder_t *);