From 0372242d12d8b669ecc3d3d8bc5050898ac355c7 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 31 Mar 2021 15:19:27 +0530 Subject: [PATCH] Use an enum for UTF8 decoder state --- kittens/choose/choose-data-types.h | 2 -- kitty/charsets.c | 11 +++++------ kitty/data-types.h | 4 ++-- kitty/history.c | 5 +++-- kitty/key_encoding.c | 4 ++-- kitty/screen.c | 2 +- kitty/screen.h | 3 ++- 7 files changed, 15 insertions(+), 16 deletions(-) diff --git a/kittens/choose/choose-data-types.h b/kittens/choose/choose-data-types.h index d6634e261..65da19799 100644 --- a/kittens/choose/choose-data-types.h +++ b/kittens/choose/choose-data-types.h @@ -30,8 +30,6 @@ typedef uint8_t len_t; typedef uint32_t text_t; #define LEN_MAX UINT8_MAX -#define UTF8_ACCEPT 0 -#define UTF8_REJECT 1 #define IS_LOWERCASE(x) (x) >= 'a' && (x) <= 'z' #define IS_UPPERCASE(x) (x) >= 'A' && (x) <= 'Z' #define LOWERCASE(x) ((IS_UPPERCASE(x)) ? (x) + 32 : (x)) diff --git a/kitty/charsets.c b/kitty/charsets.c index 5a2a3a19a..422efad22 100644 --- a/kitty/charsets.c +++ b/kitty/charsets.c @@ -7,10 +7,8 @@ // Taken from consolemap.c in the linux vt driver sourcecode -#include -#include -#define UTF8_ACCEPT 0 -#define UTF8_REJECT 1 +#include "data-types.h" + static uint32_t charset_translations[5][256] = { /* 8-bit Latin-1 mapped to Unicode -- trivial mapping */ @@ -231,7 +229,7 @@ static const uint8_t utf8_data[] = { }; uint32_t -decode_utf8(uint32_t* state, uint32_t* codep, uint8_t byte) { +decode_utf8(UTF8State* state, uint32_t* codep, uint8_t byte) { uint32_t type = utf8_data[byte]; *codep = (*state != UTF8_ACCEPT) ? @@ -245,7 +243,8 @@ decode_utf8(uint32_t* state, uint32_t* codep, uint8_t byte) { size_t decode_utf8_string(const char *src, size_t sz, uint32_t *dest) { // dest must be a zeroed array of size at least sz - uint32_t codep = 0, state = 0, prev = UTF8_ACCEPT; + uint32_t codep = 0; + UTF8State state = 0, prev = UTF8_ACCEPT; size_t i, d; for (i = 0, d = 0; i < sz; i++) { switch(decode_utf8(&state, &codep, src[i])) { diff --git a/kitty/data-types.h b/kitty/data-types.h index 3d1dedd2c..f6cb45be9 100644 --- a/kitty/data-types.h +++ b/kitty/data-types.h @@ -80,8 +80,6 @@ typedef enum { TILING, SCALED, MIRRORED } BackgroundImageLayout; #define ATTRS_MASK_FOR_SGR (ATTRS_MASK_WITHOUT_MARK | ATTRS_MASK_WITHOUT_WIDTH) #define MARK_MASK 3 #define COL_MASK 0xFFFFFFFF -#define UTF8_ACCEPT 0 -#define UTF8_REJECT 1 #define DECORATION_FG_CODE 58 #define CHAR_IS_BLANK(ch) ((ch) == 32 || (ch) == 0) #define CONTINUED_MASK 1 @@ -153,6 +151,8 @@ typedef enum { TILING, SCALED, MIRRORED } BackgroundImageLayout; #endif +typedef enum UTF8State { UTF8_ACCEPT = 0, UTF8_REJECT = 1} UTF8State; + typedef struct { uint32_t left, top, right, bottom; } Region; diff --git a/kitty/history.c b/kitty/history.c index bba7bb863..70899c00c 100644 --- a/kitty/history.c +++ b/kitty/history.c @@ -196,7 +196,8 @@ static inline bool pagerhist_ensure_start_is_valid_utf8(PagerHistoryBuf *ph) { uint8_t scratch[8]; size_t num = ringbuf_memcpy_from(scratch, ph->ringbuf, arraysz(scratch)); - uint32_t state = UTF8_ACCEPT, codep; + uint32_t codep; + UTF8State state = UTF8_ACCEPT; size_t count = 0; size_t last_reject_at = 0; while (count < num) { @@ -333,7 +334,7 @@ get_line(HistoryBuf *self, index_type y, Line *l) { init_line(self, index_of(sel static inline char_type pagerhist_remove_char(PagerHistoryBuf *ph, unsigned *count, uint8_t record[8]) { - uint32_t codep, state = UTF8_ACCEPT; + uint32_t codep; UTF8State state = UTF8_ACCEPT; *count = 0; size_t num = ringbuf_bytes_used(ph->ringbuf); while (num--) { diff --git a/kitty/key_encoding.c b/kitty/key_encoding.c index b379d7fae..c138bcc6c 100644 --- a/kitty/key_encoding.c +++ b/kitty/key_encoding.c @@ -90,7 +90,7 @@ serialize(const EncodingData *data, char *output, const char csi_trailer) { } if (third_field_not_empty) { const char *p = data->text; - uint32_t codep, state = UTF8_ACCEPT; + uint32_t codep; UTF8State state = UTF8_ACCEPT; bool first = true; while(*p) { if (decode_utf8(&state, &codep, *p) == UTF8_ACCEPT) { @@ -397,7 +397,7 @@ encode_key(const KeyEvent *ev, char *output) { static inline bool startswith_ascii_control_char(const char *p) { if (!p || !*p) return true; - uint32_t codep, state = UTF8_ACCEPT; + uint32_t codep; UTF8State state = UTF8_ACCEPT; while(*p) { if (decode_utf8(&state, &codep, *p) == UTF8_ACCEPT) { return codep < 32 || codep == 127; diff --git a/kitty/screen.c b/kitty/screen.c index 5ffedf9ac..52b77d00b 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -622,7 +622,7 @@ screen_draw_overlay_text(Screen *self, const char *utf8_text) { self->overlay_line.ynum = self->cursor->y; self->overlay_line.xstart = self->cursor->x; self->overlay_line.xnum = 0; - uint32_t codepoint = 0, state = UTF8_ACCEPT; + uint32_t codepoint = 0; UTF8State state = UTF8_ACCEPT; bool orig_line_wrap_mode = self->modes.mDECAWM; self->modes.mDECAWM = false; self->cursor->reverse ^= true; diff --git a/kitty/screen.h b/kitty/screen.h index 8f6b62e6d..8b42855f6 100644 --- a/kitty/screen.h +++ b/kitty/screen.h @@ -83,7 +83,8 @@ typedef struct { CellPixelSize cell_size; OverlayLine overlay_line; id_type window_id; - uint32_t utf8_state, utf8_codepoint, *g0_charset, *g1_charset, *g_charset; + uint32_t utf8_codepoint, *g0_charset, *g1_charset, *g_charset; + UTF8State utf8_state; unsigned int current_charset; Selections selections, url_ranges; struct {