Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add an hasSize function #7

Merged
merged 5 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ pub fn AlgorithmType(
self.max_needle = max_needle;
}

// Check if buffers have sufficient memory for a given haystack and
// needle length.
pub fn hasSize(self: *const Self, max_haystack: usize, max_needle: usize) bool {
return (max_haystack <= self.max_haystack) and (max_needle <= self.max_needle);
}

/// Compute matching score
pub fn score(
self: *Self,
Expand Down Expand Up @@ -253,7 +259,7 @@ pub fn AlgorithmType(
};

std.debug.assert(haystack.len <= self.maximumHaystackLen());
std.debug.assert(needle.len < self.maximumNeedleLen());
std.debug.assert(needle.len <= self.maximumNeedleLen());

const rows = needle.len;
const cols = haystack.len;
Expand Down Expand Up @@ -649,6 +655,12 @@ pub const Ascii = struct {
pub fn resize(self: *Ascii, max_haystack: usize, max_needle: usize) !void {
try self.alg.resize(max_haystack, max_needle);
}

// Check if buffers have sufficient memory for a given haystack and
// needle length.
pub fn hasSize(self: *const Ascii, max_haystack: usize, max_needle: usize) bool {
return self.alg.hasSize(max_haystack, max_needle);
}
};

fn doTestScore(alg: *Ascii, haystack: []const u8, needle: []const u8, comptime score: i32) !void {
Expand Down Expand Up @@ -849,3 +861,42 @@ test "traceback" {
try doTestTraceback(&alg, "A" ++ "a" ** 20 ++ "B", "AB", &.{ 0, 21 });
try doTestTraceback(&alg, "./src/main.zig", "main", &.{ 6, 7, 8, 9 });
}

test "resize" {
const o = Ascii.Scores{};
var alg = try Ascii.init(
std.testing.allocator,
16,
4,
.{},
);
defer alg.deinit();

try std.testing.expect(alg.hasSize(2, 2));
try alg.resize(2, 2);
try std.testing.expect(alg.hasSize(2, 2));
try doTestScore(&alg, "ab", "ab", o.score_match * 2 +
(o.bonus_head * o.bonus_first_character_multiplier) +
o.bonus_consecutive);

try std.testing.expect(!alg.hasSize(8, 2));
try alg.resize(8, 2);
try std.testing.expect(alg.hasSize(8, 2));
alberic89 marked this conversation as resolved.
Show resolved Hide resolved
// larger sizes should fail
try std.testing.expect(!alg.hasSize(8, 3));
// smaller sizes should be fine
try std.testing.expect(alg.hasSize(2, 1));
try doTestScore(&alg, "ab" ** 4, "ab", o.score_match * 2 +
(o.bonus_head * o.bonus_first_character_multiplier) +
o.bonus_consecutive);

try std.testing.expect(alg.hasSize(3, 2));
try alg.resize("abc".len, "ab".len);
try std.testing.expect(alg.hasSize(3, 2));
try doTestScore(&alg, "abc", "ab", o.score_match * 2 +
(o.bonus_head * o.bonus_first_character_multiplier) +
o.bonus_consecutive);

// normally works
try alg.resize(0, 0);
}
6 changes: 6 additions & 0 deletions src/unicode.zig
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ pub const Unicode = struct {
pub fn resize(self: *Unicode, max_haystack: usize, max_needle: usize) !void {
try self.alg.resize(max_haystack, max_needle);
}

// Check if buffers have sufficient memory for a given haystack and
// needle length.
pub fn hasSize(self: *const Unicode, max_haystack: usize, max_needle: usize) bool {
return self.alg.hasSize(max_haystack, max_needle);
}
};

fn doTestScoreUnicode(
Expand Down