···101101}
102102103103/// matches when the utf8 encoding of the codepoint and relevant mods matches the
104104-/// text of the key. This function will consume Shift and Caps Lock when matching
104104+/// text of the key. This function will consume Shift and Caps Lock when matching.
105105+///
106106+/// Lifetime: `Key.text`, when set, points into the parser's per-event scratch
107107+/// buffer and is only valid until the next event is decoded. Callers that
108108+/// retain a `Key` past that point — for example, queueing events to another
109109+/// thread — MUST copy the slice before doing so; otherwise both this routine
110110+/// and any direct read of `text` race with the parser overwriting its buffer.
105111pub fn matchText(self: Key, cp: u21, mods: Modifiers) bool {
106106- // return early if we have no text
107107- if (self.text == null) return false;
112112+ const text = self.text orelse return false;
113113+ if (text.len == 0) return false;
108114109115 var self_mods = self.mods;
110116 self_mods.num_lock = false;
···126132127133 var buf: [4]u8 = undefined;
128134 const n = std.unicode.utf8Encode(_cp, &buf) catch return false;
129129- return std.mem.eql(u8, self.text.?, buf[0..n]) and std.meta.eql(self_mods, arg_mods);
135135+ return std.mem.eql(u8, text, buf[0..n]) and std.meta.eql(self_mods, arg_mods);
130136}
131137132138// The key must exactly match the codepoint and modifiers. caps_lock and