this repo has no description
13
fork

Configure Feed

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

docs(key): document Key.text parser-scoped lifetime

authored by

Omkar Bhad and committed by
Tim Culverhouse
0c9d3cb7 7111c304

+10 -4
+10 -4
src/Key.zig
··· 101 101 } 102 102 103 103 /// matches when the utf8 encoding of the codepoint and relevant mods matches the 104 - /// text of the key. This function will consume Shift and Caps Lock when matching 104 + /// text of the key. This function will consume Shift and Caps Lock when matching. 105 + /// 106 + /// Lifetime: `Key.text`, when set, points into the parser's per-event scratch 107 + /// buffer and is only valid until the next event is decoded. Callers that 108 + /// retain a `Key` past that point — for example, queueing events to another 109 + /// thread — MUST copy the slice before doing so; otherwise both this routine 110 + /// and any direct read of `text` race with the parser overwriting its buffer. 105 111 pub fn matchText(self: Key, cp: u21, mods: Modifiers) bool { 106 - // return early if we have no text 107 - if (self.text == null) return false; 112 + const text = self.text orelse return false; 113 + if (text.len == 0) return false; 108 114 109 115 var self_mods = self.mods; 110 116 self_mods.num_lock = false; ··· 126 132 127 133 var buf: [4]u8 = undefined; 128 134 const n = std.unicode.utf8Encode(_cp, &buf) catch return false; 129 - return std.mem.eql(u8, self.text.?, buf[0..n]) and std.meta.eql(self_mods, arg_mods); 135 + return std.mem.eql(u8, text, buf[0..n]) and std.meta.eql(self_mods, arg_mods); 130 136 } 131 137 132 138 // The key must exactly match the codepoint and modifiers. caps_lock and