| ... | ... | @@ -3099,6 +3099,44 @@ test cutSuffix { |
| 3099 | 3099 | try testing.expectEqual(null, cutSuffix(u8, "foobar", "baz")); |
| 3100 | 3100 | } |
| 3101 | 3101 | |
| 3102 | /// Returns slice of `haystack` before and after `needle`, or `null` if not |
| 3103 | /// found. |
| 3104 | /// |
| 3105 | /// See also: |
| 3106 | /// * `cutScalar` |
| 3107 | /// * `split` |
| 3108 | /// * `tokenizeAny` |
| 3109 | pub fn cut(comptime T: type, haystack: []const T, needle: []const T) ?struct { []const T, []const T } { |
| 3110 | const index = indexOf(T, haystack, needle) orelse return null; |
| 3111 | return .{ haystack[0..index], haystack[index + needle.len ..] }; |
| 3112 | } |
| 3113 | |
| 3114 | test cut { |
| 3115 | try testing.expectEqual(null, cut(u8, "a b c", "B")); |
| 3116 | const before, const after = cut(u8, "a be c", "be") orelse return error.TestFailed; |
| 3117 | try testing.expectEqualStrings("a ", before); |
| 3118 | try testing.expectEqualStrings(" c", after); |
| 3119 | } |
| 3120 | |
| 3121 | /// Returns slice of `haystack` before and after `needle`, or `null` if not |
| 3122 | /// found. |
| 3123 | /// |
| 3124 | /// See also: |
| 3125 | /// * `cut` |
| 3126 | /// * `splitScalar` |
| 3127 | /// * `tokenizeScalar` |
| 3128 | pub fn cutScalar(comptime T: type, haystack: []const T, needle: T) ?struct { []const T, []const T } { |
| 3129 | const index = indexOfScalar(T, haystack, needle) orelse return null; |
| 3130 | return .{ haystack[0..index], haystack[index + 1 ..] }; |
| 3131 | } |
| 3132 | |
| 3133 | test cutScalar { |
| 3134 | try testing.expectEqual(null, cutScalar(u8, "a b c", 'B')); |
| 3135 | const before, const after = cutScalar(u8, "a b c", 'b') orelse return error.TestFailed; |
| 3136 | try testing.expectEqualStrings("a ", before); |
| 3137 | try testing.expectEqualStrings(" c", after); |
| 3138 | } |
| 3139 | |
| 3102 | 3140 | /// Delimiter type for tokenization and splitting operations. |
| 3103 | 3141 | pub const DelimiterType = enum { sequence, any, scalar }; |
| 3104 | 3142 | |