summaryrefslogtreecommitdiff
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
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
-rw-r--r--src/main.zig14
-rw-r--r--src/utils.zig27
2 files changed, 14 insertions, 27 deletions
diff --git a/src/main.zig b/src/main.zig
index 2e33cfc..b164284 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -199,10 +199,10 @@ fn onTextMessage(bot: *Bot, msg: types.Message, text: []const u8) !void {
199 }, 199 },
200 }); 200 });
201 } else if (std.ascii.startsWithIgnoreCase(text, "big ")) { 201 } else if (std.ascii.startsWithIgnoreCase(text, "big ")) {
202 const trimmed = try utils.trim(text[4..]); 202 const the_text = text[4..];
203 const cd = try utils.getCD(); 203 if (!try utils.isTgWhitespaceStr(the_text)) {
204 if (trimmed.len > 0) { 204 const cd = try utils.getCD();
205 const uppercased = try cd.toUpperStr(bot.allocator, trimmed); 205 const uppercased = try cd.toUpperStr(bot.allocator, the_text);
206 defer bot.allocator.free(uppercased); 206 defer bot.allocator.free(uppercased);
207 207
208 var output = ArrayList(u8).init(bot.allocator); 208 var output = ArrayList(u8).init(bot.allocator);
@@ -251,11 +251,11 @@ fn onTextMessage(bot: *Bot, msg: types.Message, text: []const u8) !void {
251 }, 251 },
252 }); 252 });
253 } else if (std.ascii.startsWithIgnoreCase(text, "say ")) { 253 } else if (std.ascii.startsWithIgnoreCase(text, "say ")) {
254 const trimmed = try utils.trim(text[4..]); 254 const the_text = text[4..];
255 if (trimmed.len > 0) { 255 if (!try utils.isTgWhitespaceStr(the_text)) {
256 try bot.sendMessage_(.{ 256 try bot.sendMessage_(.{
257 .chat_id = msg.chat.id, 257 .chat_id = msg.chat.id,
258 .text = trimmed, 258 .text = the_text,
259 .reply_parameters = .{ 259 .reply_parameters = .{
260 .message_id = msg.message_id, 260 .message_id = msg.message_id,
261 .chat_id = msg.chat.id, 261 .chat_id = msg.chat.id,
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}