utf8.h (1372B)
1 /* This program is licensed under the terms of GNU GPL v3 or (at your option) 2 * any later version. Copyright (C) 2020-2026 Страхиња Радић. 3 * See the file LICENSE for exact copyright and license details. */ 4 5 #include <stdint.h> 6 #include <sys/types.h> 7 8 /* maximum chars for UTF-8 representations of Unicode chars, per Unicode char */ 9 #define UTF8REPMAX 6 10 11 #define XMASK1 0x7F /* b01111111 */ 12 #define XMASK2 0x1F /* b00011111 */ 13 #define XMASK3 0x0F /* b00001111 */ 14 #define XMASK4 0x07 /* b00000111 */ 15 #define XMASK5 0x03 /* b00000011 */ 16 #define XMASK6 0x01 /* b00000001 */ 17 18 #define XMASKR 0x3F /* b00111111 */ 19 20 /* 21 #define BOUND1 0x0*/ 22 #define BOUND2 0x80 23 #define BOUND3 0x800 24 #define BOUND4 0x10000 25 #define BOUND5 0x200000 26 #define BOUND6 0x800000 27 28 #define START1 0x00 /* b0xxxxxxx */ 29 #define START2 0xC0 /* b110xxxxx */ 30 #define START3 0xE0 /* b1110xxxx */ 31 #define START4 0xF0 /* b11110xxx */ 32 #define START5 0xF8 /* b111110xx */ 33 #define START6 0xFC /* b1111110x */ 34 35 #define STARTR 0x80 /* b10xxxxxx */ 36 37 typedef uint8_t u8; 38 typedef uint32_t u32; 39 40 int u8_rune_to_u32(u32* to, const u8* from, size_t* from_delta); 41 int u32_rune_to_u8(u8* to, const u32 from); /* max == 1 */ 42 int u8_to_u32(u32* to, const u8* from, const ssize_t max, size_t* from_delta); 43 int u32_to_u8(u8* to, const u32* from, const ssize_t max); 44 size_t u32_strlen(const u32* s, const ssize_t max);