Skip to content

Commit

Permalink
Merge pull request #7 from alberic89/has-resize
Browse files Browse the repository at this point in the history
feat: add an `hasSize` function
  • Loading branch information
fjebaker authored Aug 15, 2024
2 parents 2c5d606 + af63859 commit 6204327
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
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));
// 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

0 comments on commit 6204327

Please sign in to comment.