summaryrefslogtreecommitdiff
path: root/src/types/MessageEntity.zig
diff options
context:
space:
mode:
authorGravatar Uko Kokņevičs2024-07-20 17:22:25 +0300
committerGravatar Uko Kokņevičs2024-07-20 17:22:25 +0300
commitc70ffd095a6de5cd5b872796a0d82a8c5afc1511 (patch)
tree56183274b05a294e357bad4d06b523472a1c4a4a /src/types/MessageEntity.zig
downloadukkobot-c70ffd095a6de5cd5b872796a0d82a8c5afc1511.tar.gz
ukkobot-c70ffd095a6de5cd5b872796a0d82a8c5afc1511.tar.xz
ukkobot-c70ffd095a6de5cd5b872796a0d82a8c5afc1511.zip
Initial commit
Diffstat (limited to 'src/types/MessageEntity.zig')
-rw-r--r--src/types/MessageEntity.zig66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/types/MessageEntity.zig b/src/types/MessageEntity.zig
new file mode 100644
index 0000000..1c44d20
--- /dev/null
+++ b/src/types/MessageEntity.zig
@@ -0,0 +1,66 @@
1const std = @import("std");
2
3const MessageEntity = @This();
4const User = @import("User.zig");
5const Utf8View = std.unicode.Utf8View;
6
7pub const Type = enum {
8 mention,
9 hashtag,
10 cashtag,
11 bot_command,
12 url,
13 email,
14 phone_number,
15 bold,
16 italic,
17 underline,
18 strikethrough,
19 spoiler,
20 blockquote,
21 expandable_blockquote,
22 code,
23 pre,
24 text_link,
25 text_mention,
26 custom_emoji,
27};
28
29type: Type,
30offset: u64,
31length: u64,
32url: ?[]const u8 = null,
33user: ?User = null,
34language: ?[]const u8 = null,
35custom_emoji_id: ?[]const u8 = null,
36
37pub fn extract(self: MessageEntity, src: []const u8) ![]const u8 {
38 if (self.length == 0) {
39 return "";
40 }
41
42 var utf8 = (try Utf8View.init(src)).iterator();
43 var i: usize = 0;
44
45 const start = if (i >= self.offset)
46 utf8.i
47 else blk: {
48 while (utf8.nextCodepoint()) |cp| {
49 i += std.unicode.utf16CodepointSequenceLength(cp) catch unreachable;
50 if (i >= self.offset) {
51 break :blk utf8.i;
52 }
53 }
54 return "";
55 };
56
57 i = 0;
58 while (utf8.nextCodepoint()) |cp| {
59 i += std.unicode.utf16CodepointSequenceLength(cp) catch unreachable;
60 if (i >= self.length) {
61 return src[start..utf8.i];
62 }
63 }
64
65 return src[start..];
66}