diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2025-05-28 21:54:58 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2025-05-28 21:54:58 -0700 |
commit | 0ee7c2b049f3e2764f9c27dcb4f849617ae52d73 (patch) | |
tree | ddaade5532be0b10e755d9978a76701593ab8125 | |
parent | 31921a308074679a6fec36aa06a75df154f6bd21 (diff) | |
download | txr-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.c | 25 | ||||
-rw-r--r-- | stream.c | 15 | ||||
-rw-r--r-- | utf8.c | 6 | ||||
-rw-r--r-- | utf8.h | 6 |
4 files changed, 21 insertions, 31 deletions
@@ -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; } @@ -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, @@ -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; @@ -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 *); |