diff options
| author | dam <dam@gudinoff> | 2023-03-16 23:07:00 +0000 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2023-03-16 23:07:00 +0000 |
| commit | a6726bbe9a0217a25dd3007c2b28d1a3ceb30672 (patch) | |
| tree | 5f3a913faf47d689264de0f53d6367bf347d87f7 | |
| parent | 056f874d7df4f243d873f6c045e926ee28988a10 (diff) | |
| download | task-time-tracker-a6726bbe9a0217a25dd3007c2b28d1a3ceb30672.tar.zst task-time-tracker-a6726bbe9a0217a25dd3007c2b28d1a3ceb30672.zip | |
Learning how to work with strings.
| -rw-r--r-- | ttt.jai | 57 |
1 files changed, 43 insertions, 14 deletions
@@ -173,20 +173,23 @@ is_equal_to_any :: (to_compare :string, test_a :string, test_b :string) -> bool return to_compare == test_a || to_compare == test_b; } -/* // Given an UTF8 encoded string, truncate it to length bytes without breaking any UTF8 character. // The string should have capacity for at least length + 1. // The terminating null byte ('\0') is not included in length. // Returns the truncated string length. -size_t truncate_string_utf8(char *string, size_t length) { - assert(string != NULL); +truncate_string_utf8 :: (str: string, length: s64) -> length: s64 { + assert(str.data != null); + assert(str.count >= length); + + data := str.data; + count := str.count; // Find index of first continuation byte. - size_t idx = length; - while (idx > 0 && ((string[idx - 1] & 0xC0) == 0x80)) { - idx--; + idx := length; + while (idx > 0 && ((data[idx - 1] & 0xC0) == 0x80)) { + idx -= 1; } - size_t continuation_bytes = length - idx; + continuation_bytes := length - idx; // If string starts with continuation bytes, it's an invalid UTF8 string. if (idx == 0 && continuation_bytes > 0) { @@ -195,18 +198,19 @@ size_t truncate_string_utf8(char *string, size_t length) { // If length truncates some continuation bytes, remove incomplete UTF8 character. else if (idx > 0 // string is not empty // continuation bytes are not complete - && !(continuation_bytes == 0 && (string[idx - 1] & 0x80) == 0x00) - && !(continuation_bytes == 1 && (string[idx - 1] & 0xE0) == 0xC0) - && !(continuation_bytes == 2 && (string[idx - 1] & 0xF0) == 0xE0) - && !(continuation_bytes == 3 && (string[idx - 1] & 0xF8) == 0xF0) + && !(continuation_bytes == 0 && (data[idx - 1] & 0x80) == 0x00) + && !(continuation_bytes == 1 && (data[idx - 1] & 0xE0) == 0xC0) + && !(continuation_bytes == 2 && (data[idx - 1] & 0xF0) == 0xE0) + && !(continuation_bytes == 3 && (data[idx - 1] & 0xF8) == 0xF0) ) { length -= (continuation_bytes + 1); // Remove '+ 1' start byte. } - - string[length] = '\0'; + + ptr := data + length; + memset(ptr, 0, str.count - length); return length; } - +/* // Returns true when the string is empty or consists of white space characters. bool is_empty_string(const char *string) { for (int idx = 0; string[idx] != '\0'; idx++) { @@ -1274,6 +1278,31 @@ bool read_enter_confirmation(int row, int style, const char *message) { */ main :: () { + + print("---\n"); + + format :: (value: string) { + print("value is '%'\n", value); + } + + format("direct"); + va := "var A"; + format(va); + + sb := "var B"; + vb: [2048] u8; + memcpy(vb.data, sb.data, sb.count); + xb: string = xx sb; + print("> xb.count = %\n> xb.data = %\n", xb.count, xb.data); + format(xx vb); + + print("--- --- ---\n"); + xpto: string = "ç€dam"; + truncate_string_utf8(xpto, 3); + print(">'%'\n", xpto); + + return; + defer free_memory(); |
