diff options
Diffstat (limited to 'modules/UTF8.jai')
| -rw-r--r-- | modules/UTF8.jai | 40 |
1 files changed, 24 insertions, 16 deletions
diff --git a/modules/UTF8.jai b/modules/UTF8.jai index b583809..205e57d 100644 --- a/modules/UTF8.jai +++ b/modules/UTF8.jai @@ -21,15 +21,15 @@ count_character_bytes :: inline (leading_byte: u8) -> int { return 1; } -// Truncates the string to the provided length and zeroes the discarded bytes. -// Returns the length of truncated string or -1 if string has no data. -truncate :: (str: *string, length: int) -> length: int { - if str.data == null then return -1; +// Returns the string (using same str.data) truncated to the provided length. +truncate :: (str: string, length: int) -> string { - if str.count < length then length = str.count; + if str.data == null then return ""; + + if str.count < length then return .{str.count, str.data}; - data := str.data; - count := str.count; + data := str.data; + count := str.count; // Find index of first continuation byte. idx := length; @@ -50,12 +50,10 @@ truncate :: (str: *string, length: int) -> length: int { && !(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'. + length -= (continuation_bytes + 1); // Remove start byte, hence '+ 1'. } - - memset(data + length, 0, count - length); - str.count = length; - return length; + + return .{length, str.data}; } // Returns true when the string is empty or consists of space characters. @@ -78,13 +76,23 @@ is_empty :: (str: string) -> bool { } // Counts the number of characters. -count_characters :: (str: string) -> int { +count_characters :: (str: string, $is_null_terminated := false) -> int { characters := 0; idx := 0; - while idx < str.count { - idx += count_character_bytes(str[idx]); - characters += 1; + + #if is_null_terminated { + while idx < str.count && str[idx] != 0 { + idx += count_character_bytes(str[idx]); + characters += 1; + } + } + else { + while idx < str.count { + idx += count_character_bytes(str[idx]); + characters += 1; + } } + return characters; } |
