#include "input.h" #include int utf8_char_count(const char *s, int max_bytes) { int count = 0; for (int i = 0; i < max_bytes && s[i] != '\0'; i++) { if (((unsigned char)s[i] & 0xC0) != 0x80) count++; } return count; } int utf8_byte_offset(const char *s, int char_idx) { int i = 0, count = 0; while (s[i] != '\0' && count < char_idx) { if (((unsigned char)s[i] & 0xC0) != 0x80) count++; if (count <= char_idx) i++; } while (s[i] != '\0' && ((unsigned char)s[i] & 0xC0) == 0x80) i++; return i; } int utf8_prev_char_pos(const char *s, int byte_pos) { if (byte_pos <= 0) return 0; int pos = byte_pos - 1; while (pos > 0 && ((unsigned char)s[pos] & 0xC0) == 0x80) pos--; return pos; } int utf8_next_char_pos(const char *s, int byte_pos) { int len = (int)strlen(s); if (byte_pos >= len) return len; int pos = byte_pos + 1; while (pos < len && ((unsigned char)s[pos] & 0xC0) == 0x80) pos++; return pos; }