From ee844db84596e8ba8e0b84f770d66b6e6fabb028 Mon Sep 17 00:00:00 2001 From: david Date: Tue, 6 Oct 2026 17:50:28 -0300 Subject: fixed utf-8 characters returning the wrong width with strlen. added a bunch of stuff --- src/helper.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/helper.c (limited to 'src/helper.c') diff --git a/src/helper.c b/src/helper.c new file mode 100644 index 0000000..2749daa --- /dev/null +++ b/src/helper.c @@ -0,0 +1,78 @@ +#include "../defs/all.h" +#include "../defs/widechar.h" + +/* this reads one utf81 character from the start of bytes + it puts the character's number in *codepoint and returns how many bytes it used (its always 1 or more) + invalid input it stores U+FFFD (�) and returns 1, so the caller always moves forward. */ +static int utf8_decode(const unsigned char *bytes, unsigned long *codepoint){ + unsigned long value = bytes[0]; + unsigned long minallowed = 0; //rejects overlong encodings + int sequence_length; + int byte_index; + + if(value < 0x80){ + *codepoint = value; + return 1; + } else if(value >= 0xC2 && value <= 0xDF){ + sequence_length = 2; + value &= 0x1F; + minallowed = 0x80; + } else if(value >= 0xE0 && value <= 0xEF){ + sequence_length = 3; + value &= 0x0F; + minallowed = 0x800; + } else if(value >= 0xF0 && value <= 0xF4){ + sequence_length = 4; + value &= 0x07; + minallowed = 0x10000; + } else { + *codepoint = 0xFFFD; + return 1; + } + + for(byte_index = 1; byte_index < sequence_length; byte_index++){ + if((bytes[byte_index] & 0xC0) != 0x80){ + *codepoint = 0xFFFD; + return 1; + } + value = (value << 6) | (bytes[byte_index] & 0x3F); + } + + if(value < minallowed || value > 0x10FFFF || (value >= 0xD800 && value <= 0xDFFF)){ + *codepoint = 0xFFFD; + return 1; + } + + *codepoint = value; + return sequence_length; +} + +/* this turns the library's return value into a column count so 0 1 or 2 */ +static int codepoint_width(unsigned long codepoint){ + int width = widechar_wcwidth(codepoint); + if(width > 0) + return width; + + switch(width){ + case widechar_ambiguous: + case widechar_private_use: + return 1; + case widechar_widened_in_9: + return 2; + default: + return 0; + } +} + +/* how many terminal columns a string takes up */ +int displaywidth(const char *text){ + const unsigned char *current_byte = (const unsigned char *)text; + int total_width = 0; + + while(*current_byte){ + unsigned long codepoint; + current_byte += utf8_decode(current_byte, &codepoint); + total_width += codepoint_width(codepoint); + } + return total_width; +} -- cgit v1.3-2-g0d8e