diff options
| author | dam <dam@gudinoff> | 2024-05-08 16:20:07 +0100 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2024-05-08 16:20:07 +0100 |
| commit | 2dd22e4847b7bc239f129c5ed2e7d0bdff38006e (patch) | |
| tree | 4e3b49a120ca99acc0708fb0a3cefddc659829c4 | |
| parent | 89157d00fc4110055d5816cd1897e4def120bcaf (diff) | |
| download | task-time-tracker-2dd22e4847b7bc239f129c5ed2e7d0bdff38006e.tar.zst task-time-tracker-2dd22e4847b7bc239f129c5ed2e7d0bdff38006e.zip | |
Improved code readability.
| -rw-r--r-- | modules/TUI/module.jai | 5 | ||||
| -rw-r--r-- | modules/UTF8.jai | 19 |
2 files changed, 14 insertions, 10 deletions
diff --git a/modules/TUI/module.jai b/modules/TUI/module.jai index 612f93e..112c666 100644 --- a/modules/TUI/module.jai +++ b/modules/TUI/module.jai @@ -560,9 +560,12 @@ read_input_line :: (count_limit: int, is_visible: bool = true) -> string, Key { case; if is_escape_code(key) continue; - buff_idx := get_byte_idx(str, idx); key_str := to_string(key,, allocator = temporary_allocator); + // Get the buffer index to insert the next character. + buff_idx, success := get_byte_index(str, idx); + if success == false then buff_idx = str.count; + // Make sure we have space to append the new character at the end (in case we're trying to do it). if buff_idx > count_limit - key_str.count then continue; diff --git a/modules/UTF8.jai b/modules/UTF8.jai index 205e57d..72d3d75 100644 --- a/modules/UTF8.jai +++ b/modules/UTF8.jai @@ -97,8 +97,11 @@ count_characters :: (str: string, $is_null_terminated := false) -> int { } // Deletes character by it's index, and moves tail data to take its place. -delete_character :: (str: *string, character_idx: int) { - buffer_idx := get_byte_idx(str.*, character_idx); +delete_character :: (str: *string, character_idx: int) -> success := true { + buffer_idx := get_byte_index(str.*, character_idx); + + if buffer_idx < 0 return false; + bytes_to_delete := count_character_bytes(str.data[buffer_idx]); for buffer_idx..str.count-1-bytes_to_delete { @@ -109,19 +112,17 @@ delete_character :: (str: *string, character_idx: int) { } str.count -= bytes_to_delete; + return; } // Searches for the given character index and returns its byte index on the string. -get_byte_idx :: (str: string, character_idx: int) -> buffer_idx: int, success: bool { - if character_idx < 0 then return -1, false; - if character_idx > str.count then return -2, false; - if character_idx == 0 then return 0, true; - +get_byte_index :: (str: string, character_index: int) -> buffer_index: int, success := true { buff_idx := 0; char_idx := 0; - while buff_idx < str.count && char_idx != character_idx { + while buff_idx < str.count { + if char_idx == character_index return buff_idx; buff_idx += count_character_bytes(str[buff_idx]); char_idx += 1; } - return buff_idx, char_idx == character_idx; + return -1, false; } |
