diff options
| author | 2024-07-20 17:22:25 +0300 | |
|---|---|---|
| committer | 2024-07-20 17:22:25 +0300 | |
| commit | c70ffd095a6de5cd5b872796a0d82a8c5afc1511 (patch) | |
| tree | 56183274b05a294e357bad4d06b523472a1c4a4a /src/types/MessageEntity.zig | |
| download | ukkobot-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.zig | 66 |
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 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | |||
| 3 | const MessageEntity = @This(); | ||
| 4 | const User = @import("User.zig"); | ||
| 5 | const Utf8View = std.unicode.Utf8View; | ||
| 6 | |||
| 7 | pub 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 | |||
| 29 | type: Type, | ||
| 30 | offset: u64, | ||
| 31 | length: u64, | ||
| 32 | url: ?[]const u8 = null, | ||
| 33 | user: ?User = null, | ||
| 34 | language: ?[]const u8 = null, | ||
| 35 | custom_emoji_id: ?[]const u8 = null, | ||
| 36 | |||
| 37 | pub 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 | } | ||