From 16e00dd5f26ce342e9562bec08f529d98c23c01c Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 16 Feb 2025 14:23:16 +0100 Subject: Improve code organisation, documentation, and m code improvement MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libterminput_check_utf8_char__.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 libterminput_check_utf8_char__.c (limited to 'libterminput_check_utf8_char__.c') diff --git a/libterminput_check_utf8_char__.c b/libterminput_check_utf8_char__.c new file mode 100644 index 0000000..d2884cf --- /dev/null +++ b/libterminput_check_utf8_char__.c @@ -0,0 +1,36 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +int +libterminput_check_utf8_char__(const char *s, size_t size, size_t *len_out) +{ + size_t i; + *len_out = 0; + if (!size) { + return 0; + } else if ((*s & 0x80) == 0) { + *len_out = 1U; + return 1; + } else if ((*s & 0xE0) == 0xC0) { + *len_out = 2U; + } else if ((*s & 0xF0) == 0xE0) { + *len_out = 3U; + } else if ((*s & 0xF8) == 0xF0) { + *len_out = 4U; + } else if ((*s & 0xFC) == 0xF8) { + *len_out = 5U; + } else if ((*s & 0xFE) == 0xFC) { + *len_out = 6U; + } else { + *len_out = 0U; + return -1; + } + for (i = 1; i < *len_out; i++) { + if (i == size) + return 0; + if ((s[i] & 0xC0) != 0x80) + return -1; + } + return 1; +} -- cgit v1.2.3-70-g09d2