Skip to content

Commit

Permalink
[mojo-stdlib] Fix base64 encode for non ASCII chars (#2098)
Browse files Browse the repository at this point in the history
This change fixes following bug: #1630.

Signed-off-by: Maxim Zaks <[email protected]>
  • Loading branch information
mzaks authored Apr 3, 2024
1 parent 0e94191 commit 8544d8b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion stdlib/src/base64/base64.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn b64encode(str: String) -> String:
@parameter
@always_inline
fn s(idx: Int) -> Int:
return int(str._buffer[idx])
return int(str._as_ptr().bitcast[DType.uint8]()[idx])

# This algorithm is based on https://arxiv.org/abs/1704.00605
var end = length - (length % 3)
Expand Down
3 changes: 3 additions & 0 deletions stdlib/test/base64/test_base64.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ fn test_b64encode():
# CHECK: SGVsbG8gTW9qbyEhIQ==
print(b64encode("Hello Mojo!!!"))

# CHECK: SGVsbG8g8J+UpSEhIQ==
print(b64encode("Hello 🔥!!!"))

# CHECK: dGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw==
print(b64encode("the quick brown fox jumps over the lazy dog"))

Expand Down

0 comments on commit 8544d8b

Please sign in to comment.