| ... | @@ -3079,6 +3079,26 @@ test endsWith { | ... | @@ -3079,6 +3079,26 @@ test endsWith { |
| 3079 | try testing.expect(!endsWith(u8, "Bob", "Bo")); | 3079 | try testing.expect(!endsWith(u8, "Bob", "Bo")); |
| 3080 | } | 3080 | } |
| 3081 | | 3081 | |
| | 3082 | /// If `slice` starts with `prefix`, returns the rest of `slice` starting at `prefix.len`. |
| | 3083 | pub fn chompPrefix(comptime T: type, slice: []const T, prefix: []const T) ?[]const T { |
| | 3084 | return if (startsWith(T, slice, prefix)) slice[prefix.len..] else null; |
| | 3085 | } |
| | 3086 | |
| | 3087 | test chompPrefix { |
| | 3088 | try testing.expectEqualStrings("foo", chompPrefix(u8, "--example=foo", "--example=").?); |
| | 3089 | try testing.expectEqual(null, chompPrefix(u8, "--example=foo", "-example=")); |
| | 3090 | } |
| | 3091 | |
| | 3092 | /// If `slice` ends with `suffix`, returns `slice` from beginning to start of `suffix`. |
| | 3093 | pub fn chompSuffix(comptime T: type, slice: []const T, suffix: []const T) ?[]const T { |
| | 3094 | return if (endsWith(T, slice, suffix)) slice[0 .. slice.len - suffix.len] else null; |
| | 3095 | } |
| | 3096 | |
| | 3097 | test chompSuffix { |
| | 3098 | try testing.expectEqualStrings("foo", chompSuffix(u8, "foobar", "bar").?); |
| | 3099 | try testing.expectEqual(null, chompSuffix(u8, "foobar", "baz")); |
| | 3100 | } |
| | 3101 | |
| 3082 | /// Delimiter type for tokenization and splitting operations. | 3102 | /// Delimiter type for tokenization and splitting operations. |
| 3083 | pub const DelimiterType = enum { sequence, any, scalar }; | 3103 | pub const DelimiterType = enum { sequence, any, scalar }; |
| 3084 | | 3104 | |