From 8544d8bbc7a0b28917e56cde559a15e243a0466b Mon Sep 17 00:00:00 2001 From: Maxim Zaks Date: Wed, 3 Apr 2024 06:21:52 +0200 Subject: [PATCH] [mojo-stdlib] Fix base64 encode for non ASCII chars (#2098) This change fixes following bug: https://github.com/modularml/mojo/issues/1630. Signed-off-by: Maxim Zaks --- stdlib/src/base64/base64.mojo | 2 +- stdlib/test/base64/test_base64.mojo | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/stdlib/src/base64/base64.mojo b/stdlib/src/base64/base64.mojo index 9fbd0c933e..fe02bbb26b 100644 --- a/stdlib/src/base64/base64.mojo +++ b/stdlib/src/base64/base64.mojo @@ -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) diff --git a/stdlib/test/base64/test_base64.mojo b/stdlib/test/base64/test_base64.mojo index 310a606892..934a87e57c 100644 --- a/stdlib/test/base64/test_base64.mojo +++ b/stdlib/test/base64/test_base64.mojo @@ -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"))