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...@@ -16,7 +16,7 @@ zigmod aq add 1/nektro/leven
16```16```
1717
18## Usage18## 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
21## Future21## Future
22TODO Unicode support for strings22TODO Unicode support for strings
src/lib.zig+6-1
...@@ -15,7 +15,7 @@ const range = @import("range").range;...@@ -15,7 +15,7 @@ const range = @import("range").range;
15// );15// );
16// }16// }
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 {
19 if (std.mem.eql(T, a, b)) return 0;19 if (std.mem.eql(T, a, b)) return 0;
2020
21 var left = a;21 var left = a;
...@@ -29,6 +29,10 @@ pub fn leven(comptime T: type, alloc: *std.mem.Allocator, a: []const T, b: []con...@@ -29,6 +29,10 @@ pub fn leven(comptime T: type, alloc: *std.mem.Allocator, a: []const T, b: []con
29 var ll = left.len;29 var ll = left.len;
30 var rl = right.len;30 var rl = right.len;
3131
32 if (max != null and rl - ll >= max.?) {
33 return max.?;
34 }
35
32 {36 {
33 const sl = suffixLen(T, a, b);37 const sl = suffixLen(T, a, b);
34 ll -= sl;38 ll -= sl;
...@@ -67,6 +71,7 @@ pub fn leven(comptime T: type, alloc: *std.mem.Allocator, a: []const T, b: []con...@@ -67,6 +71,7 @@ pub fn leven(comptime T: type, alloc: *std.mem.Allocator, a: []const T, b: []con
67 }71 }
68 }72 }
6973
74 if (max != null and result >= max.?) return max.?;
70 return result;75 return result;
71}76}
7277