xonecas.io
79113
Ma

mini-coding-agent / files

mini-coding-agent / src / text.zig

3.7 KB
1const std = @import("std");
2
3const binary_sniff = 8192;
4
5pub fn isBinary(bytes: []const u8) bool {
6    return std.mem.indexOfScalar(u8, bytes[0..@min(bytes.len, binary_sniff)], 0) != null;
7}
8
9pub fn countLines(content: []const u8) usize {
10    if (content.len == 0) return 0;
11    return std.mem.count(u8, content, "\n") + @intFromBool(content[content.len - 1] != '\n');
12}
13
14pub fn plural(a: std.mem.Allocator, n: usize, one: []const u8, many: []const u8) []const u8 {
15    return std.fmt.allocPrint(a, "{d} {s}", .{ n, if (n == 1) one else many }) catch many;
16}
17
18pub fn join(a: std.mem.Allocator, parts: []const []const u8, sep: []const u8) ![]const u8 {
19    var out: std.ArrayList(u8) = .empty;
20    for (parts, 0..) |part, i| {
21        if (i > 0) try out.appendSlice(a, sep);
22        try out.appendSlice(a, part);
23    }
24    return out.items;
25}
26
27fn utf8SeqLen(p: []const u8) ?usize {
28    if (p.len == 0) return null;
29    const c = p[0];
30    if (c >= 0xC2 and c <= 0xDF) {
31        if (p.len >= 2 and p[1] & 0xC0 == 0x80) return 2;
32    } else if (c == 0xE0) {
33        if (p.len >= 3 and p[1] >= 0xA0 and p[1] <= 0xBF and p[2] & 0xC0 == 0x80) return 3;
34    } else if (c >= 0xE1 and c <= 0xEC) {
35        if (p.len >= 3 and p[1] & 0xC0 == 0x80 and p[2] & 0xC0 == 0x80) return 3;
36    } else if (c == 0xED) {
37        if (p.len >= 3 and p[1] >= 0x80 and p[1] <= 0x9F and p[2] & 0xC0 == 0x80) return 3;
38    } else if (c >= 0xEE and c <= 0xEF) {
39        if (p.len >= 3 and p[1] & 0xC0 == 0x80 and p[2] & 0xC0 == 0x80) return 3;
40    } else if (c == 0xF0) {
41        if (p.len >= 4 and p[1] >= 0x90 and p[1] <= 0xBF and p[2] & 0xC0 == 0x80 and p[3] & 0xC0 == 0x80) return 4;
42    } else if (c >= 0xF1 and c <= 0xF3) {
43        if (p.len >= 4 and p[1] & 0xC0 == 0x80 and p[2] & 0xC0 == 0x80 and p[3] & 0xC0 == 0x80) return 4;
44    } else if (c == 0xF4) {
45        if (p.len >= 4 and p[1] >= 0x80 and p[1] <= 0x8F and p[2] & 0xC0 == 0x80 and p[3] & 0xC0 == 0x80) return 4;
46    }
47    return null;
48}
49
50pub const Cp = struct { cp: u21, len: usize };
51
52pub fn decodeAt(bytes: []const u8, i: usize) ?Cp {
53    if (i >= bytes.len) return null;
54    const b = bytes[i];
55    const n = std.unicode.utf8ByteSequenceLength(b) catch return .{ .cp = b, .len = 1 };
56    if (i + n > bytes.len) return .{ .cp = b, .len = 1 };
57    return .{ .cp = std.unicode.utf8Decode(bytes[i .. i + n]) catch b, .len = n };
58}
59
60pub fn cpLen(bytes: []const u8) usize {
61    var i: usize = 0;
62    var n: usize = 0;
63    while (decodeAt(bytes, i)) |d| {
64        i += d.len;
65        n += 1;
66    }
67    return n;
68}
69
70pub fn utf8Clean(a: std.mem.Allocator, text: []const u8) ![]const u8 {
71    if (std.unicode.utf8ValidateSlice(text)) return text;
72    var out: std.ArrayList(u8) = .empty;
73    var i: usize = 0;
74    while (i < text.len) {
75        if (text[i] < 0x80) {
76            try out.append(a, text[i]);
77            i += 1;
78        } else if (utf8SeqLen(text[i..])) |len| {
79            try out.appendSlice(a, text[i .. i + len]);
80            i += len;
81        } else {
82            try out.appendSlice(a, "\u{FFFD}");
83            i += 1;
84        }
85    }
86    return out.items;
87}
88
89pub fn slugify(a: std.mem.Allocator, text: []const u8) []const u8 {
90    var out: std.ArrayList(u8) = .empty;
91    var pending_dash = false;
92    for (text) |c| {
93        if (out.items.len >= 40) break;
94        const lower = std.ascii.toLower(c);
95        if (std.ascii.isAlphanumeric(lower)) {
96            if (pending_dash and out.items.len > 0 and out.items.len < 40) out.append(a, '-') catch {};
97            pending_dash = false;
98            out.append(a, lower) catch {};
99        } else {
100            pending_dash = true;
101        }
102    }
103    if (out.items.len == 0) return a.dupe(u8, "session") catch "session";
104    return out.items;
105}