slweb

Једноставни генератор статичких веб страна
git clone https://git.sr.ht/~strahinja/slweb
Дневник | Датотеке | Референце | ПРОЧИТАЈМЕ | ЛИЦЕНЦА

чување 3d589309bbbdeac4dfd4f9543b11702921161a5b
родитељ d60045ffe57af389d18074dba9dbb97c8cb98b1c
Аутор: Страхиња Радић <sr@strahinja.org>
Датум:   Sun, 14 Jul 2024 21:32:23 +0200

utf8.[ch]: Synchronize changes from table

Diffstat:
Mutf8.c | 96++++++++++++++++++++++++++++++++++++++-----------------------------------------
Mutf8.h | 51+++++++++++++++++++++++++++------------------------
измењених датотека: 2, додавања: 73(+), брисања: 74(-)

diff --git a/utf8.c b/utf8.c @@ -20,40 +20,36 @@ * (2^1 = 2 chars) */ -const int -utf_length_table[256] = { - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 32 */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 64 */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 96 */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 128 */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 160 */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 192 */ - 2, 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 4, + 5, 5, 5, 5, 6, 6, 1, 1, /* 256 */ }; -const int -extract_masks[6] = { - XMASK1, XMASK2, XMASK3, XMASK4, XMASK5, XMASK6 -}; +const int extract_masks[6] = {XMASK1, XMASK2, XMASK3, XMASK4, XMASK5, XMASK6}; int -u8_char_to_u32(u32* to, const u8 *from, size_t* from_delta) +u8_rune_to_u32(u32* to, const u8* from, size_t* from_delta) { const u8* pfrom = from; if (!from) return 1; *from_delta = 0; - int len = utf_length_table[*from]; - *to = *from & extract_masks[len-1]; + int len = utf_length_table[*from]; + *to = *from & extract_masks[len - 1]; for (int i = 1; i < len; i++) { pfrom = from + i; @@ -67,43 +63,43 @@ u8_char_to_u32(u32* to, const u8 *from, size_t* from_delta) return 0; } -size_t -u32_char_to_u8(u8 *to, const u32 from) +int +u32_rune_to_u8(u8* to, const u32 from) { - size_t len = 0; - u8 start = 0; u32 cfrom = from; + u8 start = 0; + int len = 0; if (from >= BOUND6) { start = START6; - len = 6; + len = 6; } else if (from >= BOUND5) { start = START5; - len = 5; + len = 5; } else if (from >= BOUND4) { start = START4; - len = 4; + len = 4; } else if (from >= BOUND3) { start = START3; - len = 3; + len = 3; } else if (from >= BOUND2) { start = START2; - len = 2; + len = 2; } else { start = START1; - len = 1; + len = 1; } - for (int i = len-1; i > 0; i--) + for (int i = len - 1; i > 0; i--) { to[i] = STARTR | (cfrom & XMASKR); cfrom >>= 6; @@ -113,17 +109,17 @@ u32_char_to_u8(u8 *to, const u32 from) } int -u8_to_u32(u32 *to, const u8 *from, size_t* from_delta) +u8_to_u32(u32* to, const u8* from, const ssize_t max, size_t* from_delta) { const u8* pfrom = from; - u32* pto = to; + u32* pto = to; if (!from) return 1; size_t delta = 0; - *from_delta = 0; - while (*pfrom) + *from_delta = 0; + while (pfrom - from < max && *pfrom) { - int result = u8_char_to_u32(pto, pfrom, &delta); + int result = u8_rune_to_u32(pto, pfrom, &delta); if (result) return result; pto++; @@ -134,17 +130,17 @@ u8_to_u32(u32 *to, const u8 *from, size_t* from_delta) return 0; } -size_t -u32_to_u8(u8 *to, const u32* from) +int +u32_to_u8(u8* to, const u32* from, const ssize_t max) { - u8* pto = to; const u32* pfrom = from; - size_t len = 0; + u8* pto = to; + int len = 0; if (!from) return 0; - while (*pfrom) + while (pfrom - from < max && *pfrom) { - size_t delta = u32_char_to_u8(pto, *pfrom); + size_t delta = u32_rune_to_u8(pto, *pfrom); pto += delta; len += delta; pfrom++; @@ -152,11 +148,11 @@ u32_to_u8(u8 *to, const u32* from) return len; } -size_t -u32_strlen(const u32* s) +size_t +u32_strlen(const u32* s, const ssize_t max) { const u32* ps = s; - while (ps && *ps) + while (ps && ps - s < max && *ps) ps++; return ps - s; } diff --git a/utf8.h b/utf8.h @@ -5,37 +5,40 @@ #include <stdint.h> #include <sys/types.h> -#define XMASK1 0x7F /* b01111111 */ -#define XMASK2 0x1F /* b00011111 */ -#define XMASK3 0x0F /* b00001111 */ -#define XMASK4 0x07 /* b00000111 */ -#define XMASK5 0x03 /* b00000011 */ -#define XMASK6 0x01 /* b00000001 */ +/* maximum chars for UTF-8 representations of Unicode chars, per Unicode char */ +#define UTF8REPMAX 6 -#define XMASKR 0x3F /* b00111111 */ +#define XMASK1 0x7F /* b01111111 */ +#define XMASK2 0x1F /* b00011111 */ +#define XMASK3 0x0F /* b00001111 */ +#define XMASK4 0x07 /* b00000111 */ +#define XMASK5 0x03 /* b00000011 */ +#define XMASK6 0x01 /* b00000001 */ + +#define XMASKR 0x3F /* b00111111 */ /* #define BOUND1 0x0*/ -#define BOUND2 0x80 -#define BOUND3 0x800 -#define BOUND4 0x10000 -#define BOUND5 0x200000 -#define BOUND6 0x800000 +#define BOUND2 0x80 +#define BOUND3 0x800 +#define BOUND4 0x10000 +#define BOUND5 0x200000 +#define BOUND6 0x800000 -#define START1 0x00 /* b0xxxxxxx */ -#define START2 0xC0 /* b110xxxxx */ -#define START3 0xE0 /* b1110xxxx */ -#define START4 0xF0 /* b11110xxx */ -#define START5 0xF8 /* b111110xx */ -#define START6 0xFC /* b1111110x */ +#define START1 0x00 /* b0xxxxxxx */ +#define START2 0xC0 /* b110xxxxx */ +#define START3 0xE0 /* b1110xxxx */ +#define START4 0xF0 /* b11110xxx */ +#define START5 0xF8 /* b111110xx */ +#define START6 0xFC /* b1111110x */ -#define STARTR 0x80 /* b10xxxxxx */ +#define STARTR 0x80 /* b10xxxxxx */ typedef uint8_t u8; typedef uint32_t u32; -int u8_char_to_u32(u32* to, const u8* from, size_t* from_delta); -size_t u32_char_to_u8(u8* to, const u32 from); -int u8_to_u32(u32* to, const u8* from, size_t* from_delta); -size_t u32_to_u8(u8* to, const u32* from); -size_t u32_strlen(const u32* s); +int u8_rune_to_u32(u32* to, const u8* from, size_t* from_delta); +int u32_rune_to_u8(u8* to, const u32 from); /* max == 1 */ +int u8_to_u32(u32* to, const u8* from, const ssize_t max, size_t* from_delta); +int u32_to_u8(u8* to, const u32* from, const ssize_t max); +size_t u32_strlen(const u32* s, const ssize_t max);