this repo has no description
3
fork

Configure Feed

Select the types of activity you want to include in your feed.

irc: use faster case folding

Do casefold matching by finding differences instead of turning each
string into lowercase

+29 -24
+29 -24
src/irc.zig
··· 674 674 } 675 675 676 676 pub fn getOrCreateChannel(self: *Client, name: []const u8) !*Channel { 677 - const name_lower = try self.alloc.alloc(u8, name.len); 678 - defer self.alloc.free(name_lower); 679 - try caseMap(name_lower, name, .rfc1459); 680 677 for (self.channels.items) |*channel| { 681 - const chan_lower = try self.alloc.alloc(u8, channel.name.len); 682 - defer self.alloc.free(chan_lower); 683 - try caseMap(chan_lower, channel.name, .rfc1459); 684 - if (std.mem.eql(u8, name_lower, chan_lower)) return channel; 678 + if (caseFold(name, channel.name)) return channel; 685 679 } 686 680 const channel: Channel = .{ 687 681 .name = try self.alloc.dupe(u8, name), ··· 775 769 rfc1459_strict, 776 770 }; 777 771 778 - pub fn caseMap(out: []u8, str: []const u8, algo: CaseMapAlgo) !void { 779 - assert(out.len == str.len); 772 + pub fn caseMap(char: u8, algo: CaseMapAlgo) u8 { 780 773 switch (algo) { 781 774 .ascii => { 782 - for (str, 0..) |b, i| { 783 - switch (b) { 784 - 'A'...'Z' => out[i] = b + 0x20, 785 - else => out[i] = b, 786 - } 775 + switch (char) { 776 + 'A'...'Z' => return char + 0x20, 777 + else => return char, 787 778 } 788 779 }, 789 780 .rfc1459 => { 790 - for (str, 0..) |b, i| { 791 - switch (b) { 792 - 'A'...'^' => out[i] = b + 0x20, 793 - else => out[i] = b, 794 - } 781 + switch (char) { 782 + 'A'...'^' => return char + 0x20, 783 + else => return char, 795 784 } 796 785 }, 797 786 .rfc1459_strict => { 798 - for (str, 0..) |b, i| { 799 - switch (b) { 800 - 'A'...']' => out[i] = b + 0x20, 801 - else => out[i] = b, 802 - } 787 + switch (char) { 788 + 'A'...']' => return char + 0x20, 789 + else => return char, 803 790 } 804 791 }, 805 792 } 806 793 } 794 + 795 + pub fn caseFold(a: []const u8, b: []const u8) bool { 796 + if (a.len != b.len) return false; 797 + var i: usize = 0; 798 + while (i < a.len) { 799 + const diff = std.mem.indexOfDiff(u8, a[i..], b[i..]) orelse return true; 800 + const a_diff = caseMap(a[diff], .rfc1459); 801 + const b_diff = caseMap(b[diff], .rfc1459); 802 + if (a_diff != b_diff) return false; 803 + i += diff + 1; 804 + } 805 + return true; 806 + } 807 + 808 + test "caseFold" { 809 + try testing.expect(caseFold("a", "A")); 810 + try testing.expect(caseFold("aBcDeFgH", "abcdefgh")); 811 + }