authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2020-02-07 14:17:40+11:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-06 18:49:10-05:00
log119ac13eda7fe95b6c1139b4bd20e05928abe427
tree854e48803baff1397abab4e8b8fba91f374c0cc6
parentfa46bcb36864e6616ce4449965063f3b8720f8e1
signaturelock-open Commit is signed but in an unrecognized format.

std: add .startsWith and .endsWith to std.ArrayList


2 files changed, 12 insertions(+), 1 deletions(-)

lib/std/array_list.zig+11
...@@ -248,6 +248,17 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {...@@ -248,6 +248,17 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
248 if (self.len == 0) return null;248 if (self.len == 0) return null;
249 return self.pop();249 return self.pop();
250 }250 }
251
252 pub fn startsWith(self: Self, m: []const T) bool {
253 if (self.len < m.len) return false;
254 return mem.eql(T, self.items[0..m.len], m);
255 }
256
257 pub fn endsWith(self: Self, m: []const T) bool {
258 if (self.len < m.len) return false;
259 const start = self.len - m.len;
260 return mem.eql(T, self.items[start..self.len], m);
261 }
251 };262 };
252}263}
253264
lib/std/buffer.zig+1-1
...@@ -133,7 +133,7 @@ pub const Buffer = struct {...@@ -133,7 +133,7 @@ pub const Buffer = struct {
133133
134 pub fn startsWith(self: Buffer, m: []const u8) bool {134 pub fn startsWith(self: Buffer, m: []const u8) bool {
135 if (self.len() < m.len) return false;135 if (self.len() < m.len) return false;
136 return mem.eql(u8, self.list.items[0..m.len], m);136 return self.list.startsWith(m);
137 }137 }
138138
139 pub fn endsWith(self: Buffer, m: []const u8) bool {139 pub fn endsWith(self: Buffer, m: []const u8) bool {