diff --git a/kitty/charsets.c b/kitty/charsets.c index 0a7cf84f2..c8c8ad6e5 100644 --- a/kitty/charsets.c +++ b/kitty/charsets.c @@ -5,9 +5,13 @@ * Distributed under terms of the GPL3 license. */ -#include "data-types.h" // Taken from consolemap.c in the linux vt driver sourcecode +#include +#include +#define UTF8_ACCEPT 0 +#define UTF8_REJECT 1 + static uint32_t charset_translations[5][256] = { /* 8-bit Latin-1 mapped to Unicode -- trivial mapping */ { @@ -238,6 +242,26 @@ decode_utf8(uint32_t* state, uint32_t* codep, uint8_t byte) { return *state; } +size_t +decode_utf8_string(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; + size_t i, d; + for (i = 0, d = 0; i < sz; i++) { + switch(decode_utf8(&state, &codep, src[i])) { + case UTF8_ACCEPT: + dest[d++] = codep; + break; + case UTF8_REJECT: + state = UTF8_ACCEPT; + if (prev != UTF8_ACCEPT && i > 0) i--; + break; + } + prev = state; + } + return d; +} + unsigned int encode_utf8(uint32_t ch, char* dest) { if (ch < 0x80) { diff --git a/kitty/charsets.h b/kitty/charsets.h new file mode 100644 index 000000000..b3b144881 --- /dev/null +++ b/kitty/charsets.h @@ -0,0 +1,14 @@ +/* + * Copyright (C) 2019 Kovid Goyal + * + * Distributed under terms of the GPL3 license. + */ + +#pragma once + +#include +#include + +uint32_t decode_utf8(uint32_t*, uint32_t*, uint8_t byte); +size_t decode_utf8_string(char *src, size_t sz, uint32_t *dest); +unsigned int encode_utf8(uint32_t ch, char* dest); diff --git a/kitty/child-monitor.c b/kitty/child-monitor.c index 09f488857..1d64c4a1b 100644 --- a/kitty/child-monitor.c +++ b/kitty/child-monitor.c @@ -9,6 +9,7 @@ #include "threading.h" #include "screen.h" #include "fonts.h" +#include "charsets.h" #include #include #include diff --git a/kitty/data-types.h b/kitty/data-types.h index 9b540b5a0..27697f6ec 100644 --- a/kitty/data-types.h +++ b/kitty/data-types.h @@ -268,8 +268,6 @@ ColorProfile* alloc_color_profile(); PyObject* create_256_color_table(); PyObject* parse_bytes_dump(PyObject UNUSED *, PyObject *); PyObject* parse_bytes(PyObject UNUSED *, PyObject *); -uint32_t decode_utf8(uint32_t*, uint32_t*, uint8_t byte); -unsigned int encode_utf8(uint32_t ch, char* dest); void cursor_reset(Cursor*); Cursor* cursor_copy(Cursor*); void cursor_copy_to(Cursor *src, Cursor *dest); diff --git a/kitty/line.c b/kitty/line.c index 64978c0fe..45446849a 100644 --- a/kitty/line.c +++ b/kitty/line.c @@ -8,6 +8,7 @@ #include "data-types.h" #include "unicode-data.h" #include "lineops.h" +#include "charsets.h" extern PyTypeObject Cursor_Type; diff --git a/kitty/mouse.c b/kitty/mouse.c index ca32f9fc7..de26c2dfb 100644 --- a/kitty/mouse.c +++ b/kitty/mouse.c @@ -8,6 +8,7 @@ #include "state.h" #include "screen.h" #include "lineops.h" +#include "charsets.h" #include #include #include "glfw-wrapper.h" diff --git a/kitty/parser.c b/kitty/parser.c index 580792aaa..22f164fbb 100644 --- a/kitty/parser.c +++ b/kitty/parser.c @@ -8,6 +8,7 @@ #include "control-codes.h" #include "screen.h" #include "graphics.h" +#include "charsets.h" #include extern PyTypeObject Screen_Type; diff --git a/kitty/screen.c b/kitty/screen.c index b77147e0f..3e8bbc357 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -23,6 +23,7 @@ #include "modes.h" #include "wcwidth-std.h" #include "control-codes.h" +#include "charsets.h" static const ScreenModes empty_modes = {0, .mDECAWM=true, .mDECTCEM=true, .mDECARM=true}; static Selection EMPTY_SELECTION = {0};