0 /*
1 * Copyright (c) 2023 RMF <rawmonk@firemail.cc>
2 *
3 * Permission to use, copy, modify, and distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 */
15 #include <stdlib.h>
16 #include <stdint.h>
17 #include "termbox.h"
18 #include "wcwidth.h"
19 #include "utf8.h"
20
21 int utf8_width(char* ptr, size_t len) {
22 int width = 0;
23 char* max = ptr + len;
24 while (*ptr && ptr < max) {
25 uint32_t c;
26 ptr += tb_utf8_char_to_unicode(&c, ptr);
27 width += mk_wcwidth(c);
28 }
29 return width;
30 }
31
32 int utf8_len(char* ptr, size_t len) {
33 char* max = ptr + len;
34 char* start = ptr;
35 while (*ptr && ptr < max) {
36 ptr += tb_utf8_char_length(*ptr);
37 }
38 return ptr - start;
39 }
40
41 int utf8_last_len(char *ptr, size_t len) {
42 char* max = ptr + len;
43 len = 1;
44 while (*ptr && ptr < max) {
45 len = tb_utf8_char_length(*ptr);
46 ptr += len;
47 }
48 return len;
49 }
50