diff options
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 15 |
1 files changed, 7 insertions, 8 deletions
@@ -114,20 +114,19 @@ bool inline static is_equal_to_any(const char *to_compare, const char *test_a, c } // Given an UTF8 encoded string, truncate it to length without breaking any UTF8 character. -// The string should have capacity for at least length number of items. -// The terminating null byte ('\0') is included in length. -// TODO null byte should not be part of the length. +// The string should have capacity for at least length + 1. +// The terminating null byte ('\0') is not included in length. // Returns the amount of items discarded. size_t truncate_string_utf8(char *string, size_t length) { // Check for special cases where no truncation is required. - if (length == 0 || string[length-1] == '\0') { + if (length == 0 || string[length] == '\0') { return 0; } // Search for a non-UTF8-sequence-item so we can truncate the string. - size_t idx = length - 1; - while(idx > 0 && ((string[idx] & 0xC0) == 0x80)) { // TODO Only needs to back up 3 items... because of UTF8 being limited to 4bytes + size_t idx = length; + while(idx > 0 && ((string[idx] & 0xC0) == 0x80)) { idx--; } string[idx] = '\0'; @@ -657,8 +656,8 @@ bool import_from_csv(database_st *db, const char *path) { continue; } size_t name_length = (name_delimiter - csv_buffer) + 1; - if (name_length > MAX_TASK_NAME) { - name_length = MAX_TASK_NAME; + if (name_length >= MAX_TASK_NAME) { + name_length = MAX_TASK_NAME - 1; } // Prepare new task. |
