чување a9e96234bfc5a104b1328b28f047b990cc67736b
родитељ 390ab9999d0add9add0b66ebbd50e9740eab2f59
Аутор: Страхиња Радић <sr@strahinja.org>
Датум: Tue, 10 Mar 2026 12:47:19 +0100
utf8.[ch]: Update from sled
Diffstat:
| M | utf8.c | | | 54 | ++++++++++++++++++++++++++++-------------------------- |
| M | utf8.h | | | 10 | +++++----- |
измењених датотека: 2, додавања: 33(+), брисања: 31(-)
diff --git a/utf8.c b/utf8.c
@@ -20,29 +20,31 @@
* (2^1 = 2 chars)
*/
+/* clang-format off */
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, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
- 2, 2, 2, 2, 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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)
@@ -64,7 +66,7 @@ u8_char_to_u32(u32* to, const u8* from, size_t* from_delta)
}
int
-u32_char_to_u8(u8* to, const u32 from)
+u32_rune_to_u8(u8* to, const u32 from)
{
u32 cfrom = from;
u8 start = 0;
@@ -109,7 +111,7 @@ 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;
@@ -117,9 +119,9 @@ u8_to_u32(u32* to, const u8* from, size_t* from_delta)
return 1;
size_t delta = 0;
*from_delta = 0;
- while (*pfrom)
+ 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++;
@@ -131,16 +133,16 @@ u8_to_u32(u32* to, const u8* from, size_t* from_delta)
}
int
-u32_to_u8(u8* to, const u32* from)
+u32_to_u8(u8* to, const u32* from, const ssize_t max)
{
const u32* pfrom = from;
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++;
@@ -149,10 +151,10 @@ u32_to_u8(u8* to, const u32* from)
}
size_t
-u32_strlen(const u32* s)
+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
@@ -34,8 +34,8 @@
typedef uint8_t u8;
typedef uint32_t u32;
-int u8_char_to_u32(u32* to, const u8* from, size_t* from_delta);
-int u32_char_to_u8(u8* to, const u32 from);
-int u8_to_u32(u32* to, const u8* from, size_t* from_delta);
-int 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);