| ... | ... | @@ -304,7 +304,7 @@ pub const base64 = struct { |
| 304 | 304 | return (lt(x, 26) & (x +% 'A')) | |
| 305 | 305 | (ge(x, 26) & lt(x, 52) & (x +% 'a' -% 26)) | |
| 306 | 306 | (ge(x, 52) & lt(x, 62) & (x +% '0' -% 52)) | |
| 307 | | (eq(x, 62) & '+') | (eq(x, 63) & if (urlsafe) '_' else '/'); |
| 307 | (eq(x, 62) & if (urlsafe) '-' else '+') | (eq(x, 63) & if (urlsafe) '_' else '/'); |
| 308 | 308 | } |
| 309 | 309 | |
| 310 | 310 | fn byteFromChar(c: u8, comptime urlsafe: bool) u8 { |
| ... | ... | @@ -312,7 +312,7 @@ pub const base64 = struct { |
| 312 | 312 | (ge(c, 'A') & le(c, 'Z') & (c -% 'A')) | |
| 313 | 313 | (ge(c, 'a') & le(c, 'z') & (c -% 'a' +% 26)) | |
| 314 | 314 | (ge(c, '0') & le(c, '9') & (c -% '0' +% 52)) | |
| 315 | | (eq(c, '+') & 62) | (eq(c, if (urlsafe) '_' else '/') & 63); |
| 315 | (eq(c, if (urlsafe) '-' else '+') & 62) | (eq(c, if (urlsafe) '_' else '/') & 63); |
| 316 | 316 | return x | (eq(x, 0) & ~eq(c, 'A')); |
| 317 | 317 | } |
| 318 | 318 | |
| ... | ... | @@ -453,6 +453,16 @@ test "hex with ignored chars" { |
| 453 | 453 | try testing.expectEqualSlices(u8, &expected, bin); |
| 454 | 454 | } |
| 455 | 455 | |
| 456 | test "base64 urlsafe" { |
| 457 | const input = [_]u8{ 0xfb, 0xff }; |
| 458 | var enc_buf: [4]u8 = undefined; |
| 459 | var dec_buf: [2]u8 = undefined; |
| 460 | const encoded = try base64.encode(&enc_buf, &input, .urlsafe); |
| 461 | try testing.expectEqualSlices(u8, "-_8=", encoded); |
| 462 | const decoded = try base64.decode(&dec_buf, encoded, .urlsafe); |
| 463 | try testing.expectEqualSlices(u8, &input, decoded); |
| 464 | } |
| 465 | |
| 456 | 466 | test "base64 with ignored chars" { |
| 457 | 467 | const encoded = "dGVzdCBi\r\nYXNlNjQ=\n"; |
| 458 | 468 | const expected = "test base64"; |