aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDylan Araps <dylan.araps@gmail.com>2026-03-01 21:32:28 +0200
committerDylan Araps <dylan.araps@gmail.com>2026-03-01 21:32:28 +0200
commit8ed17a9a7a9ac21c7685815cf7679e0d4e9fe614 (patch)
tree862adfd2fe9aa13e924bb1de5c8fe4e03165674d /lib
parent0dbc6f7a2ab11488363b78d0c325e81867b26457 (diff)
dfm: truncate entry but keep end
Works with utf8 too. Before: Kamisama.Mou.Sukoshi.Dake.EP01.1080p.FOD.WEB-DL.AAC Kamisama.Mou.Sukoshi.Dake.EP01.1080p.FOD.WEB-DL.AAC After: Kamisama.Mou.Sukoshi.Dake.EP01.1080p.FOD....tar.mkv Kamisama.Mou.Sukoshi.Dake.EP01.1080p.FOD....tar.srt
Diffstat (limited to 'lib')
-rw-r--r--lib/utf8.h33
1 files changed, 10 insertions, 23 deletions
diff --git a/lib/utf8.h b/lib/utf8.h
index 0104661..bc73670 100644
--- a/lib/utf8.h
+++ b/lib/utf8.h
@@ -33,7 +33,7 @@ utf8_expected(u8 b)
return L[b >> 3];
}
-static inline int
+static inline usize
utf8_width(u32 c)
{
if (c == 0) return 0;
@@ -151,33 +151,20 @@ utf8_cols(const void *s, usize l, usize *lw)
}
static inline usize
-utf8_trunc_narrow(const char *s, usize l, usize c)
-{
- const unsigned char *p = (const unsigned char *)s;
- const unsigned char *e = p + l;
- for (usize i = 0; p < e && i < c; i++) {
- unsigned char b = *p++;
- if (!(b & 0x80)) continue;
- for (; p < e && ((*p & 0xC0) == 0x80); p++);
- }
- return (usize)(p - (const unsigned char *)s);
-}
-
-static inline usize
-utf8_trunc_wide(const char *s, usize l, usize c)
+utf8_trunc(const char *s, usize l, usize c, usize *oc)
{
const unsigned char *p = (const unsigned char *)s;
const unsigned char *e = p + l;
- for (usize i = 0; p < e && i < c; ) {
+ usize co = 0;
+ while (p < e) {
u32 cp;
- const unsigned char *n = (const unsigned char *)utf8_decode((void *)p, &cp);
- usize a = (usize)(n - p);
- if (!a) a = 1;
- int w = utf8_width(cp);
- if (i + w > c) break;
- i += w;
- p += a;
+ const unsigned char *n = utf8_decode((void *)p, &cp);
+ usize w = utf8_width(cp);
+ if (w > c - co) break;
+ co += w;
+ p = n;
}
+ *oc = co;
return (usize)(p - (const unsigned char *)s);
}