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 1 commit
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
14 changes: 14 additions & 0 deletions src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,15 @@ pub fn AlgorithmType(
self.max_needle = max_needle;
}

// Check is there is enough memory allocated
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Check is there is enough memory allocated
// Check if buffers have sufficient memory for a given haystack and
// needle length.

pub fn hasSize(self: *Self, max_haystack: usize, max_needle: usize) bool {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn hasSize(self: *Self, max_haystack: usize, max_needle: usize) bool {
pub fn hasSize(self: *const Self, max_haystack: usize, max_needle: usize) bool {

This won't and should not mutate memory, so we mark the pointer as const.

if (self.max_haystack < max_haystack or self.max_needle < max_needle) {
return false;
} else {
return true;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (self.max_haystack < max_haystack or self.max_needle < max_needle) {
return false;
} else {
return true;
}
return (max_haystack <= self.max_haystack) and (max_needle <= self.max_needle);

It's easier just to check the positive case.

}

/// Compute matching score
pub fn score(
self: *Self,
Expand Down Expand Up @@ -649,6 +658,11 @@ 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 is there is enough memory allocated
pub fn hasSize(self: *Ascii, max_haystack: usize, max_needle: usize) bool {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Check is there is enough memory allocated
pub fn hasSize(self: *Ascii, max_haystack: usize, max_needle: usize) bool {
// 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
5 changes: 5 additions & 0 deletions src/unicode.zig
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ 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 is there is enough memory allocated
pub fn hasSize(self: *Unicode, max_haystack: usize, max_needle: usize) bool {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Check is there is enough memory allocated
pub fn hasSize(self: *Unicode, max_haystack: usize, max_needle: usize) bool {
// 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