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

This commit is contained in:
daurnimator 2020-02-07 14:17:40 +11:00 committed by Andrew Kelley
parent fa46bcb368
commit 119ac13eda
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
2 changed files with 12 additions and 1 deletions

View File

@ -248,6 +248,17 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
if (self.len == 0) return null;
return self.pop();
}
pub fn startsWith(self: Self, m: []const T) bool {
if (self.len < m.len) return false;
return mem.eql(T, self.items[0..m.len], m);
}
pub fn endsWith(self: Self, m: []const T) bool {
if (self.len < m.len) return false;
const start = self.len - m.len;
return mem.eql(T, self.items[start..self.len], m);
}
};
}

View File

@ -133,7 +133,7 @@ pub const Buffer = struct {
pub fn startsWith(self: Buffer, m: []const u8) bool {
if (self.len() < m.len) return false;
return mem.eql(u8, self.list.items[0..m.len], m);
return self.list.startsWith(m);
}
pub fn endsWith(self: Buffer, m: []const u8) bool {