aboutsummaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
Diffstat (limited to 'modules')
-rw-r--r--modules/TUI/module.jai2
-rw-r--r--modules/UTF8.jai40
2 files changed, 25 insertions, 17 deletions
diff --git a/modules/TUI/module.jai b/modules/TUI/module.jai
index 7c3a71d..a5db3bf 100644
--- a/modules/TUI/module.jai
+++ b/modules/TUI/module.jai
@@ -572,7 +572,7 @@ read_input_line :: (count_limit: int, is_visible: bool = true) -> string, Key {
idx += 1;
// Truncate string to avoid incomplete utf8 codes on the string tail.
- truncate(*str, count_limit);
+ str.count = truncate(str, count_limit).count;
}
}
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;
}