Skip to content

Commit

Permalink
Merge pull request #1706 from multiversx/fix-hex-sc-print
Browse files Browse the repository at this point in the history
Fix hex and binary print for bytes in sc_print!
  • Loading branch information
andrei-marinica authored Jul 15, 2024
2 parents 9b777a8 + ea2b5a0 commit b3505ea
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ path = "src/formatted_message_features.rs"
[dependencies.multiversx-sc]
version = "0.51.1"
path = "../../../framework/base"
features = ["alloc"]

[dev-dependencies.multiversx-sc-scenario]
version = "0.51.1"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[settings]
main = "formatted-message-features"

[contracts.formatted-message-features]
allocator = "static64k"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![no_std]

multiversx_sc::imports!();
use multiversx_sc::imports::*;

#[multiversx_sc::contract]
pub trait FormattedMessageFeatures {
Expand Down Expand Up @@ -72,6 +72,21 @@ pub trait FormattedMessageFeatures {
sc_print!("Printing x: {:c}", x);
}

#[endpoint]
fn print_message_bytes(&self, x: &[u8]) {
sc_print!("Printing x: {}", x,);
}

#[endpoint]
fn print_message_hex_bytes(&self, x: &[u8]) {
sc_print!("Printing x: {:x}", x,);
}

#[endpoint]
fn print_message_binary_bytes(&self, x: &[u8]) {
sc_print!("Printing x: {:b}", x);
}

#[endpoint]
fn format_message_one_part(&self) -> ManagedBuffer {
let message = sc_format!("Test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ fn test_print_ascii() {

fmf.print_message(i32::MIN);
check_printed_and_clear("Printing x: -2147483648");

fmf.print_message_bytes(b"MVX");
check_printed_and_clear("Printing x: MVX");
}

#[test]
Expand All @@ -39,6 +42,9 @@ fn test_print_binary() {

fmf.print_message_binary(u32::MAX);
check_printed_and_clear("Printing x: 11111111111111111111111111111111");

fmf.print_message_binary_bytes(b"MVX");
check_printed_and_clear("Printing x: 010011010101011001011000");
}

#[test]
Expand All @@ -59,6 +65,9 @@ fn test_print_hex() {

fmf.print_message_hex(i32::MIN);
check_printed_and_clear("Printing x: 80000000");

fmf.print_message_hex_bytes(b"MVX");
check_printed_and_clear("Printing x: 4d5658");
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
////////////////////////////////////////////////////

// Init: 1
// Endpoints: 16
// Endpoints: 19
// Async Callback (empty): 1
// Total number of exported functions: 18
// Total number of exported functions: 21

#![no_std]

multiversx_sc_wasm_adapter::allocator!();
multiversx_sc_wasm_adapter::allocator!(static64k);
multiversx_sc_wasm_adapter::panic_handler!();

multiversx_sc_wasm_adapter::endpoints! {
Expand All @@ -28,6 +28,9 @@ multiversx_sc_wasm_adapter::endpoints! {
print_message_hex => print_message_hex
print_message_binary => print_message_binary
print_message_codec => print_message_codec
print_message_bytes => print_message_bytes
print_message_hex_bytes => print_message_hex_bytes
print_message_binary_bytes => print_message_binary_bytes
format_message_one_part => format_message_one_part
format_message_multiple_parts => format_message_multiple_parts
format_message_big_int => format_message_big_int
Expand Down
11 changes: 8 additions & 3 deletions framework/base/src/formatter/formatter_impl_bytes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use super::{FormatByteReceiver, SCBinary, SCDisplay, SCLowerHex};
use super::{
hex_util::{byte_to_binary_digits, encode_bytes_as_hex},
FormatByteReceiver, SCBinary, SCDisplay, SCLowerHex,
};

impl SCDisplay for &[u8] {
fn fmt<F: FormatByteReceiver>(&self, f: &mut F) {
Expand All @@ -8,12 +11,14 @@ impl SCDisplay for &[u8] {

impl SCLowerHex for &[u8] {
fn fmt<F: FormatByteReceiver>(&self, f: &mut F) {
f.append_bytes(self);
f.append_bytes(encode_bytes_as_hex(self).as_bytes());
}
}

impl SCBinary for &[u8] {
fn fmt<F: FormatByteReceiver>(&self, f: &mut F) {
f.append_bytes(self);
for b in self.iter() {
f.append_bytes(&byte_to_binary_digits(*b));
}
}
}

0 comments on commit b3505ea

Please sign in to comment.