authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-07-27 23:48:27 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-07-27 23:48:27 -07:00
log8e9f827117ab1418578b692a2325754cc3ee692d
tree5782999915ec938d8f186acec819431a378d7720
parent2c64271ce793bcb904ad2f93eeefb4840cbac012

add optional max parameter to short circuit long string detection


2 files changed, 7 insertions(+), 2 deletions(-)

README.md+1-1
......@@ -16,7 +16,7 @@ zigmod aq add 1/nektro/leven
1616```
1717
1818## Usage
19`pub fn leven(comptime T: type, alloc: *std.mem.Allocator, a: []const T, b: []const T) !usize`
19`pub fn leven(comptime T: type, alloc: *std.mem.Allocator, a: []const T, b: []const T, max: ?usize) !usize`
2020
2121## Future
2222TODO Unicode support for strings
src/lib.zig+6-1
......@@ -15,7 +15,7 @@ const range = @import("range").range;
1515// );
1616// }
1717
18pub fn leven(comptime T: type, alloc: *std.mem.Allocator, a: []const T, b: []const T) !usize {
18pub fn leven(comptime T: type, alloc: *std.mem.Allocator, a: []const T, b: []const T, max: ?usize) !usize {
1919 if (std.mem.eql(T, a, b)) return 0;
2020
2121 var left = a;
......@@ -29,6 +29,10 @@ pub fn leven(comptime T: type, alloc: *std.mem.Allocator, a: []const T, b: []con
2929 var ll = left.len;
3030 var rl = right.len;
3131
32 if (max != null and rl - ll >= max.?) {
33 return max.?;
34 }
35
3236 {
3337 const sl = suffixLen(T, a, b);
3438 ll -= sl;
......@@ -67,6 +71,7 @@ pub fn leven(comptime T: type, alloc: *std.mem.Allocator, a: []const T, b: []con
6771 }
6872 }
6973
74 if (max != null and result >= max.?) return max.?;
7075 return result;
7176}
7277