diff options
Diffstat (limited to 'src/utils.zig')
| -rw-r--r-- | src/utils.zig | 27 |
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 | ||
| 49 | pub fn trim(str: []const u8) ![]const u8 { | 49 | pub 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 | ||
| 77 | inline fn isTrimmable(gcd: GenCatData, cp: u21) bool { | 63 | inline 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 | } |