From 353d8b1145db12ffc42e4f6c2148a848ef8bba84 Mon Sep 17 00:00:00 2001 From: dam Date: Sun, 7 Apr 2024 23:23:06 +0100 Subject: Preparing for UTF8 support on read_input_line. --- ttt.jai | 63 +-------------------------------------------------------------- 1 file changed, 1 insertion(+), 62 deletions(-) (limited to 'ttt.jai') diff --git a/ttt.jai b/ttt.jai index 414ab8c..43a972f 100644 --- a/ttt.jai +++ b/ttt.jai @@ -25,6 +25,7 @@ #import "File_Utilities"; #import "String"; #import "Integer_Saturating_Arithmetic"; +#import "UTF8"; TUI :: #import "TUI"(COLOR_BIT_DEPTH=8); VERSION :: "2.0"; // Use only 3 chars (to fit layouts). @@ -189,68 +190,6 @@ count_digits :: (number: s64, base: s64 = 10) -> s64 { return digits; } -Text_Encoding :: enum u8 #specified { - ASCII :: 1; - UTF8 :: 2; -} - -// Truncates the string to the length provided or shorter, in case of UTF8 strings that require so. -// Truncation is done by zeroing the tail of the string in place. -// Returns length of truncated string. -truncate_string :: (str: string, length: s64, $encoding: Text_Encoding = .UTF8) -> length: s64 { - assert(str.data != null, ASSERT_NOT_NULL, "str"); - assert(str.count >= length, "'str.count' should be equal or greater to 'length'."); - - data := str.data; - count := str.count; - - #if encoding == .UTF8 { - // Find index of first continuation byte. - idx := length; - while (idx > 0 && ((data[idx - 1] & 0xC0) == 0x80)) { - idx -= 1; - } - continuation_bytes := length - idx; - - // If string starts with continuation bytes, it's an invalid UTF8 string. - if (idx == 0 && continuation_bytes > 0) { - length = 0; - } - // If length truncates some continuation bytes, remove incomplete UTF8 character. - else if (idx > 0 // string is not empty - // continuation bytes are not complete - && !(continuation_bytes == 0 && (data[idx - 1] & 0x80) == 0x00) - && !(continuation_bytes == 1 && (data[idx - 1] & 0xE0) == 0xC0) - && !(continuation_bytes == 2 && (data[idx - 1] & 0xF0) == 0xE0) - && !(continuation_bytes == 3 && (data[idx - 1] & 0xF8) == 0xF0) - ) { - length -= (continuation_bytes + 1); // Remove start byte, ence '+ 1'. - } - } - - memset(data + length, 0, count - length); - return length; -} - -// Returns true when the string is empty or consists of space characters. -is_empty_string :: (str: string) -> bool { - for 0..str.count-1 { - if str[it] == { - case #char "\0"; #through; - case #char "\t"; #through; // horizontal tab - case #char "\n"; #through; // line feed - case #char "\x0B"; #through; // vertical tabulation - case #char "\x0C"; #through; // form feed - case #char "\r"; #through; // carriage return - case #char " "; - continue; - case; - return false; - } - } - return true; -} - // Prints, on row y and column x, the time using 5 characters centered on space. // Returns the result of a call to mvprintw. print_time :: (y: int, x: int, time: s64, space: int) -> int { -- cgit v1.2.3