refactor: split is_not_word_char into char_class and CharClass

This commit is contained in:
CJ van den Berg 2025-11-27 16:13:57 +01:00
parent 1615cd37e8
commit a15ceeb4a7
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -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 = *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; 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 { pub const CharClass = enum {
if (c.len == 0) return true; whitespace,
word,
non_word,
eol,
end,
};
pub fn char_class(c: []const u8) CharClass {
if (c.len == 0) return .end;
return switch (c[0]) { return switch (c[0]) {
' ' => true, '=' => .non_word,
'=' => true, '"' => .non_word,
'"' => true, '\'' => .non_word,
'\'' => true, '/' => .non_word,
'\t' => true, '\\' => .non_word,
'\n' => true, '*' => .non_word,
'/' => true, ':' => .non_word,
'\\' => true, '.' => .non_word,
'*' => true, ',' => .non_word,
':' => true, '(' => .non_word,
'.' => true, ')' => .non_word,
',' => true, '{' => .non_word,
'(' => true, '}' => .non_word,
')' => true, '[' => .non_word,
'{' => true, ']' => .non_word,
'}' => true, ';' => .non_word,
'[' => true, '|' => .non_word,
']' => true, '!' => .non_word,
';' => true, '?' => .non_word,
'|' => true, '&' => .non_word,
'!' => true, '@' => .non_word,
'?' => true, '-' => .non_word,
'&' => true, '<' => .non_word,
'@' => true, '>' => .non_word,
'-' => true, ' ' => .whitespace,
'<' => true, '\t' => .whitespace,
'>' => true, '\n' => .eol,
else => false, 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 { 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 { 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 { 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 { 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 { pub fn is_whitespace_at_cursor(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics) bool {