summaryrefslogtreecommitdiff
path: root/src/utils.zig
diff options
context:
space:
mode:
authorGravatar Uko Kokņevičs2024-08-31 07:02:07 +0300
committerGravatar Uko Kokņevičs2024-08-31 07:02:07 +0300
commitcbbad1f5666950ab9aa0751aeee73403b1c2cd83 (patch)
treee55be845984ed87585a8fccb2d4e4d934431d7a3 /src/utils.zig
parentthank you Q&A, now theres proper unicode support n shit (diff)
downloadukkobot-cbbad1f5666950ab9aa0751aeee73403b1c2cd83.tar.gz
ukkobot-cbbad1f5666950ab9aa0751aeee73403b1c2cd83.tar.xz
ukkobot-cbbad1f5666950ab9aa0751aeee73403b1c2cd83.zip
replace trimming with just checking if the string is whitespace
Diffstat (limited to '')
-rw-r--r--src/utils.zig27
1 files changed, 7 insertions, 20 deletions
diff --git a/src/utils.zig b/src/utils.zig
index 631e464..b739f6f 100644
--- a/src/utils.zig
+++ b/src/utils.zig
@@ -46,34 +46,21 @@ pub inline fn isNull(value: anytype) bool {
46 }; 46 };
47} 47}
48 48
49pub fn trim(str: []const u8) ![]const u8 { 49pub fn isTgWhitespaceStr(str: []const u8) !bool {
50 const view = try Utf8View.init(str); 50 const view = try Utf8View.init(str);
51 const gcd = try getGCD(); 51 const gcd = try getGCD();
52 52
53 var it = view.iterator(); 53 var it = view.iterator();
54 var idx: usize = 0;
55 const first = while (it.nextCodepoint()) |cp| {
56 if (!isTrimmable(gcd, cp)) {
57 break idx;
58 }
59 idx = it.i;
60 } else {
61 return "";
62 };
63
64 idx = it.i;
65
66 var last = first;
67 while (it.nextCodepoint()) |cp| { 54 while (it.nextCodepoint()) |cp| {
68 if (!isTrimmable(gcd, cp)) { 55 if (!isTgWhitespace(gcd, cp)) {
69 last = idx + (std.unicode.utf8CodepointSequenceLength(cp) catch unreachable) - 1; 56 return false;
70 } 57 }
71 idx = it.i;
72 } 58 }
73 59
74 return str[first .. last + 1]; 60 return true;
75} 61}
76 62
77inline fn isTrimmable(gcd: GenCatData, cp: u21) bool { 63inline fn isTgWhitespace(gcd: GenCatData, cp: u21) bool {
78 return gcd.isSeparator(cp) or gcd.isControl(cp); 64 return gcd.isSeparator(cp) or gcd.isControl(cp) or cp == 0x2800 // BRAILLE PATTERN BLANK, telegram treats messages with just this as invalid messages
65 ;
79} 66}