| 1 | const std = @import("std"); |
| 2 | const extras = @import("./lib.zig"); |
| 3 | |
| 4 | pub fn splitScalarN(comptime T: type, buffer: []const T, delimiter: T, comptime N: usize) ?[N][]const T { |
| 5 | var result: [N][]const T = undefined; |
| 6 | var iter = std.mem.splitScalar(T, buffer, delimiter); |
| 7 | for (&result) |*x| x.* = iter.next() orelse return null; |
| 8 | if (iter.next() != null) return null; |
| 9 | return result; |
| 10 | } |
| 11 | |
| 12 | test { |
| 13 | try std.testing.expectEqualDeep(&[_][]const u8{ "abc", "def" }, &splitScalarN(u8, "abc.def", '.', 2).?); |
| 14 | } |
| 15 | test { |
| 16 | try std.testing.expectEqual(null, splitScalarN(u8, "abcdef", '.', 2)); |
| 17 | } |
| 18 | test { |
| 19 | try std.testing.expectEqual(null, splitScalarN(u8, "ab.cd.ef", '.', 2)); |
| 20 | } |