From 8ed17a9a7a9ac21c7685815cf7679e0d4e9fe614 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sun, 1 Mar 2026 21:32:28 +0200 Subject: 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 --- lib/utf8.h | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) (limited to 'lib') 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); } -- cgit v1.2.3