blob: b5d9f19b10d76898d8b89f32b87e7de0185a7295 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
const std = @import("std");
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;
const GeneralCategories = @import("GeneralCategories");
const LetterCasing = @import("LetterCasing");
const Utf8View = std.unicode.Utf8View;
pub fn escapeXml(writer: anytype, text: []const u8) !void {
for (text) |ch| {
try switch (ch) {
'<' => writer.writeAll("<"),
'>' => writer.writeAll(">"),
'&' => writer.writeAll("&"),
'"' => writer.writeAll("""),
else => writer.writeByte(ch),
};
}
}
var gc_global: ?GeneralCategories = null;
pub fn getGC() !GeneralCategories {
if (gc_global) |gc| {
return gc;
}
gc_global = try GeneralCategories.init(std.heap.page_allocator);
return gc_global.?;
}
var lc_global: ?LetterCasing = null;
pub fn getLetterCasing() !LetterCasing {
if (lc_global) |lc| {
return lc;
}
lc_global = try LetterCasing.init(std.heap.page_allocator);
return lc_global.?;
}
pub inline fn isNull(value: anytype) bool {
return switch (@typeInfo(@TypeOf(value))) {
.null => true,
.optional => value == null,
else => false,
};
}
pub fn isTgWhitespaceStr(str: []const u8) !bool {
const view = try Utf8View.init(str);
const gc = try getGC();
var it = view.iterator();
while (it.nextCodepoint()) |cp| {
if (!isTgWhitespace(gc, cp)) {
return false;
}
}
return true;
}
inline fn isTgWhitespace(gc: GeneralCategories, cp: u21) bool {
return gc.isSeparator(cp) or gc.isControl(cp) or cp == 0x2800 // BRAILLE PATTERN BLANK, telegram treats messages with just this as invalid messages
;
}
|