Create urlDecode function for redirects

This commit is contained in:
mitteneer 2025-06-18 02:10:51 -04:00
parent 36873053bc
commit 0b07947b8a

View file

@ -33,3 +33,20 @@ pub fn scrobbleToRow(allocator: std.mem.Allocator, scrobble: Data.Scrobble) !Dat
.date = try dateFmt(allocator, scrobble.date),
};
}
pub fn urlDecode(allocator: std.mem.Allocator, str: []const u8) ![]const u8 {
var decoded = std.ArrayList(u8).init(allocator);
var i: usize = 0;
while (i < str.len) : (i += 1) {
const v = str[i];
if (v == '%') {
if (i + 2 < str.len) {
const hex = str[i + 1 .. i + 3];
const char = try std.fmt.parseInt(u8, hex, 16);
try decoded.append(char);
i += 2;
} else return error.InvalidInput;
} else try decoded.append(v);
}
return decoded.toOwnedSlice();
}