authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-05-04 14:01:05 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-05-04 14:01:05 -07:00
log547d1d86fa2eabca61ffe780d3efe71b7dfbd53f
tree0878778db8f0fcb8ba47917a20ef09d1d25496af
parent2d3aee0360eefcf5f5c51887a6b33316f83f7060

add trimPrefixEnsure/trimSuffixEnsure


1 files changed, 10 insertions(+), 0 deletions(-)

src/lib.zig+10
...@@ -46,6 +46,11 @@ pub fn trimPrefix(in: string, prefix: string) string {...@@ -46,6 +46,11 @@ pub fn trimPrefix(in: string, prefix: string) string {
46 return in;46 return in;
47}47}
4848
49pub fn trimPrefixEnsure(in: string, prefix: string) ?string {
50 if (!std.mem.startsWith(u8, in, prefix)) return null;
51 return in[prefix.len..];
52}
53
49pub fn trimSuffix(in: string, suffix: string) string {54pub fn trimSuffix(in: string, suffix: string) string {
50 if (std.mem.endsWith(u8, in, suffix)) {55 if (std.mem.endsWith(u8, in, suffix)) {
51 return in[0 .. in.len - suffix.len];56 return in[0 .. in.len - suffix.len];
...@@ -53,6 +58,11 @@ pub fn trimSuffix(in: string, suffix: string) string {...@@ -53,6 +58,11 @@ pub fn trimSuffix(in: string, suffix: string) string {
53 return in;58 return in;
54}59}
5560
61pub fn trimSuffixEnsure(in: string, suffix: string) ?string {
62 if (!std.mem.endsWith(u8, in, suffix)) return null;
63 return in[0 .. in.len - suffix.len];
64}
65
56pub fn base64EncodeAlloc(alloc: std.mem.Allocator, input: string) !string {66pub fn base64EncodeAlloc(alloc: std.mem.Allocator, input: string) !string {
57 const base64 = std.base64.standard.Encoder;67 const base64 = std.base64.standard.Encoder;
58 var buf = try alloc.alloc(u8, base64.calcSize(input.len));68 var buf = try alloc.alloc(u8, base64.calcSize(input.len));