aboutsummaryrefslogtreecommitdiff
path: root/modules/UTF8.jai
diff options
context:
space:
mode:
Diffstat (limited to 'modules/UTF8.jai')
-rw-r--r--modules/UTF8.jai19
1 files changed, 10 insertions, 9 deletions
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;
}