|
1 | 1 | use crate as conversions; // trick for CairoDeserialize macro
|
2 | 2 | use crate::serde::deserialize::{BufferReadError, BufferReadResult, BufferReader};
|
3 | 3 | use crate::{serde::serialize::SerializeToFeltVec, string::TryFromHexStr};
|
4 |
| -use cairo_lang_runner::short_string::as_cairo_short_string_ex; |
5 | 4 | use cairo_lang_utils::byte_array::{BYTE_ARRAY_MAGIC, BYTES_IN_WORD};
|
6 | 5 | use cairo_serde_macros::{CairoDeserialize, CairoSerialize};
|
| 6 | +use conversions::felt::ToShortString; |
7 | 7 | use starknet_types_core::felt::Felt;
|
8 | 8 | use std::fmt;
|
9 | 9 |
|
@@ -56,15 +56,88 @@ impl ByteArray {
|
56 | 56 |
|
57 | 57 | impl fmt::Display for ByteArray {
|
58 | 58 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
59 |
| - let full_words_string = self |
| 59 | + let words: String = self |
60 | 60 | .words
|
61 | 61 | .iter()
|
62 |
| - .map(|word| as_cairo_short_string_ex(word, BYTES_IN_WORD).unwrap()) |
63 |
| - .collect::<String>(); |
| 62 | + .map(|word| { |
| 63 | + let word: String = word.to_short_string().map_err(|_| fmt::Error)?; |
| 64 | + if word.len() != BYTES_IN_WORD { |
| 65 | + return Err(fmt::Error)?; |
| 66 | + } |
| 67 | + Ok(word) |
| 68 | + }) |
| 69 | + .collect::<Result<Vec<String>, fmt::Error>>()? |
| 70 | + .join(""); |
| 71 | + let pending_word = self |
| 72 | + .pending_word |
| 73 | + .to_short_string() |
| 74 | + .map_err(|_| fmt::Error)?; |
64 | 75 |
|
65 |
| - let pending_word_string = |
66 |
| - as_cairo_short_string_ex(&self.pending_word, self.pending_word_len).unwrap(); |
| 76 | + write!(f, "{words}{pending_word}") |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +#[cfg(test)] |
| 81 | +mod tests { |
| 82 | + use super::*; |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn test_fmt_empty() { |
| 86 | + let array = ByteArray::from(""); |
| 87 | + assert_eq!(array.to_string(), ""); |
| 88 | + } |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn test_fmt_single_word() { |
| 92 | + let array = ByteArray::from("Hello"); |
| 93 | + assert_eq!(array.to_string(), "Hello"); |
| 94 | + } |
| 95 | + |
| 96 | + #[test] |
| 97 | + fn test_fmt_multiple_words() { |
| 98 | + let array = ByteArray::from("Hello World! This is a test."); |
| 99 | + assert_eq!(array.to_string(), "Hello World! This is a test."); |
| 100 | + } |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn test_fmt_with_pending_word() { |
| 104 | + let array = ByteArray::from("abc"); |
| 105 | + assert_eq!(array.to_string(), "abc"); |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn test_fmt_special_chars() { |
| 110 | + let special_chars = "!@#$%^&*()_+-=[]{}|;:,.<>?"; |
| 111 | + let array = ByteArray::from(special_chars); |
| 112 | + assert_eq!(array.to_string(), special_chars); |
| 113 | + } |
| 114 | + |
| 115 | + #[test] |
| 116 | + #[should_panic(expected = "a Display implementation returned an error unexpectedly: Error")] |
| 117 | + fn test_fmt_with_null_bytes() { |
| 118 | + let with_nulls = "Hello\0World\0Test"; |
| 119 | + let array = ByteArray::from(with_nulls); |
| 120 | + assert_eq!(array.to_string(), with_nulls); |
| 121 | + } |
| 122 | + |
| 123 | + #[test] |
| 124 | + fn test_fmt_mixed_ascii() { |
| 125 | + let mixed = "Hello\tWorld\n123 !@#"; |
| 126 | + let array = ByteArray::from(mixed); |
| 127 | + assert_eq!(array.to_string(), mixed); |
| 128 | + } |
| 129 | + |
| 130 | + #[test] |
| 131 | + fn test_fmt_with_newlines() { |
| 132 | + let with_newlines = "First line\nSecond line\r\nThird line"; |
| 133 | + let array = ByteArray::from(with_newlines); |
| 134 | + assert_eq!(array.to_string(), with_newlines); |
| 135 | + } |
67 | 136 |
|
68 |
| - write!(f, "{full_words_string}{pending_word_string}") |
| 137 | + #[test] |
| 138 | + fn test_fmt_multiple_newlines() { |
| 139 | + let multiple_newlines = "Line1\n\n\nLine2\n\nLine3"; |
| 140 | + let array = ByteArray::from(multiple_newlines); |
| 141 | + assert_eq!(array.to_string(), multiple_newlines); |
69 | 142 | }
|
70 | 143 | }
|
0 commit comments