| 1 | const std = @import("std"); |
| 2 | const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); |
| 4 | |
| 5 | pub fn sliceTo(comptime T: type, haystack: []const T, needle: T) []const T { |
| 6 | if (std.mem.indexOfScalar(T, haystack, needle)) |index| { |
| 7 | return haystack[0..index]; |
| 8 | } |
| 9 | return haystack; |
| 10 | } |
| 11 | |
| 12 | test { |
| 13 | try std.testing.expect(std.mem.eql(u8, sliceTo(u8, "abcdefgh", 'd'), "abc")); |
| 14 | } |
| 15 | test { |
| 16 | try std.testing.expect(std.mem.eql(u8, sliceTo(u8, "abcdefgh", 'r'), "abcdefgh")); |
| 17 | } |
| 18 | test { |
| 19 | try std.testing.expect(std.mem.eql(u8, sliceTo(u8, "abcdefgh", 'a'), "")); |
| 20 | } |