diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 5e34ca3..58f2f89 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -2220,42 +2220,54 @@ pub const Editor = struct { const cursel_operator_mut = *const fn (self: *Self, root: Buffer.Root, cursel: *CurSel, allocator: Allocator) error{Stop}!Buffer.Root; const cursel_operator_mut_arg = *const fn (self: *Self, root: Buffer.Root, cursel: *CurSel, allocator: Allocator, ctx: Context) error{Stop}!Buffer.Root; - pub fn is_not_word_char(c: []const u8) bool { - if (c.len == 0) return true; + pub const CharClass = enum { + whitespace, + word, + non_word, + eol, + end, + }; + + pub fn char_class(c: []const u8) CharClass { + if (c.len == 0) return .end; return switch (c[0]) { - ' ' => true, - '=' => true, - '"' => true, - '\'' => true, - '\t' => true, - '\n' => true, - '/' => true, - '\\' => true, - '*' => true, - ':' => true, - '.' => true, - ',' => true, - '(' => true, - ')' => true, - '{' => true, - '}' => true, - '[' => true, - ']' => true, - ';' => true, - '|' => true, - '!' => true, - '?' => true, - '&' => true, - '@' => true, - '-' => true, - '<' => true, - '>' => true, - else => false, + '=' => .non_word, + '"' => .non_word, + '\'' => .non_word, + '/' => .non_word, + '\\' => .non_word, + '*' => .non_word, + ':' => .non_word, + '.' => .non_word, + ',' => .non_word, + '(' => .non_word, + ')' => .non_word, + '{' => .non_word, + '}' => .non_word, + '[' => .non_word, + ']' => .non_word, + ';' => .non_word, + '|' => .non_word, + '!' => .non_word, + '?' => .non_word, + '&' => .non_word, + '@' => .non_word, + '-' => .non_word, + '<' => .non_word, + '>' => .non_word, + ' ' => .whitespace, + '\t' => .whitespace, + '\n' => .eol, + else => .word, }; } + pub fn is_not_word_char(c: []const u8) bool { + return char_class(c) != .word; + } + pub fn is_word_char(c: []const u8) bool { - return !is_not_word_char(c); + return char_class(c) == .word; } fn is_word_char_at_cursor(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics) bool { @@ -2279,11 +2291,17 @@ pub const Editor = struct { } pub fn is_whitespace(c: []const u8) bool { - return (c.len == 0) or (c[0] == ' ') or (c[0] == '\t'); + return switch (char_class(c)) { + .whitespace, .end => true, + else => false, + }; } pub fn is_whitespace_or_eol(c: []const u8) bool { - return is_whitespace(c) or c[0] == '\n'; + return switch (char_class(c)) { + .whitespace, .end, .eol => true, + else => false, + }; } pub fn is_whitespace_at_cursor(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics) bool {