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
Just a few std.mem changes #944
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please squash your changes
std/mem.zig
Outdated
/// Remove values from the beginning and end of a slice. | ||
pub fn trim(comptime T: type, slice: []const T, values_to_strip: []const T) []const T { | ||
var begin: usize = 0; | ||
pub fn trim(comptime T: type, slice: []const T, values_to_strip: []const T, side: Side) []const T { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be trim
, trimLeft
, and trimRight
, instead of an enum parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair
std/mem.zig
Outdated
} | ||
|
||
pub fn indexOfScalarPos(comptime T: type, slice: []const T, start_index: usize, value: T) ?usize { | ||
pub fn indexOfScalarPos(comptime T: type, slice: []const T, start_index: usize, value: T, highest: bool) ?usize { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be indexOfScalarPos
and lastIndexOfScalarPos
instead of a boolean parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair, will do
std/mem.zig
Outdated
return i; | ||
if (slice[i] == value) { | ||
out = i; | ||
if (!highest) break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lastIndexOfScalarPos
should use a better algorithm than this, searching from the end.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes as I said in my comment I didn't want to remove readability for that speed bonus but if you want them as separate functions I'll implement the superior algorithm :).
std/mem.zig
Outdated
} | ||
|
||
pub fn indexOfAnyPos(comptime T: type, slice: []const T, start_index: usize, values: []const T) ?usize { | ||
pub fn indexOfAnyPos(comptime T: type, slice: []const T, start_index: usize, values: []const T, highest: bool) ?usize { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indexOfAnyPos
and lastIndexOfAnyPos
without the bool.
std/mem.zig
Outdated
return i; | ||
if (slice[i] == value) { | ||
out = i; | ||
if (!highest) break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lastIndexOfAnyPos
should have a better algorithm than this. at worst searching from the end, at best reverse boyer-moore or something like that.
std/mem.zig
Outdated
} | ||
|
||
pub fn indexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize { | ||
return indexOfPos(T, haystack, 0, needle); | ||
return indexOfPos(T, haystack, 0, needle, false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see above comments
std/mem.zig
Outdated
return i; | ||
if (eql(T, haystack[i .. i + needle.len], needle)) { | ||
out = i; | ||
if (!highest) break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see above comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I'll just reply once or twice as to not repeat myself. I'll clean this up :).
@@ -355,6 +382,10 @@ pub fn startsWith(comptime T: type, haystack: []const T, needle: []const T) bool | |||
return if (needle.len > haystack.len) false else eql(T, haystack[0 .. needle.len], needle); | |||
} | |||
|
|||
pub fn endsWith(comptime T: type, haystack: []const T, needle: []const T) bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add a test for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure :). I'll add a test for starts with too.
…dsWith, added tests for indexOf, implemented commands and starts/endswith
@andrewrk Squashed commits and made requested changes. Also I think the fail is not due to anything I did? It says it is a timer assert failure despite there being no link? I'll merge in master and redo it. == Edit == |
I fixed some bugs and improved the API, and avoided the merge commit |
Much smaller PR