Add cursor warp option

This commit is contained in:
2024-11-02 08:00:50 -07:00
parent d66decb7c4
commit 5080f07724
8 changed files with 55 additions and 18 deletions

View File

@ -18,12 +18,11 @@
const std = @import("std");
const mem = std.mem;
/// Validate a glob, returning error.InvalidGlob if it is empty, "**" or has a
/// '*' at any position other than the first and/or last byte.
/// Validate a glob, returning error.InvalidGlob if is "**" or has a '*'
/// at any position other than the first and/or last byte.
pub fn validate(glob: []const u8) error{InvalidGlob}!void {
switch (glob.len) {
0 => return error.InvalidGlob,
1 => {},
0, 1 => {},
2 => if (glob[0] == '*' and glob[1] == '*') return error.InvalidGlob,
else => if (mem.indexOfScalar(u8, glob[1 .. glob.len - 1], '*') != null) {
return error.InvalidGlob;
@ -34,6 +33,7 @@ pub fn validate(glob: []const u8) error{InvalidGlob}!void {
test validate {
const testing = std.testing;
try validate("");
try validate("*");
try validate("a");
try validate("*a");
@ -48,7 +48,6 @@ test validate {
try validate("abc*");
try validate("*abc*");
try testing.expectError(error.InvalidGlob, validate(""));
try testing.expectError(error.InvalidGlob, validate("**"));
try testing.expectError(error.InvalidGlob, validate("***"));
try testing.expectError(error.InvalidGlob, validate("a*c"));
@ -67,7 +66,9 @@ pub fn match(s: []const u8, glob: []const u8) bool {
validate(glob) catch unreachable;
}
if (glob.len == 1) {
if (glob.len == 0) {
return s.len == 0;
} else if (glob.len == 1) {
return glob[0] == '*' or mem.eql(u8, s, glob);
}
@ -89,6 +90,9 @@ test match {
const testing = std.testing;
try testing.expect(match("", "*"));
try testing.expect(match("", ""));
try testing.expect(!match("a", ""));
try testing.expect(!match("", "a"));
try testing.expect(match("a", "*"));
try testing.expect(match("a", "*a*"));
@ -165,8 +169,10 @@ pub fn order(a: []const u8, b: []const u8) std.math.Order {
return .lt;
}
const count_a = @as(u2, @intFromBool(a[0] == '*')) + @intFromBool(a[a.len - 1] == '*');
const count_b = @as(u2, @intFromBool(b[0] == '*')) + @intFromBool(b[b.len - 1] == '*');
const count_a = if (a.len != 0) @as(u2, @intFromBool(a[0] == '*')) +
@intFromBool(a[a.len - 1] == '*') else 0;
const count_b = if (b.len != 0) @as(u2, @intFromBool(b[0] == '*')) +
@intFromBool(b[b.len - 1] == '*') else 0;
if (count_a == 0 and count_b == 0) {
return .eq;
@ -182,6 +188,7 @@ test order {
const testing = std.testing;
const Order = std.math.Order;
try testing.expectEqual(Order.eq, order("", ""));
try testing.expectEqual(Order.eq, order("*", "*"));
try testing.expectEqual(Order.eq, order("*a*", "*b*"));
try testing.expectEqual(Order.eq, order("a*", "*b"));
@ -204,6 +211,7 @@ test order {
"bababab",
"b",
"a",
"",
};
for (descending, 0..) |a, i| {