Skip to content

Commit

Permalink
Merge pull request #82 from Chia-Network/20240417-fix-zero-pad-repr
Browse files Browse the repository at this point in the history
20240417 fix zero pad repr
  • Loading branch information
prozacchiwawa authored Apr 18, 2024
2 parents a9deaba + 2ddc3bb commit a679745
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
5 changes: 4 additions & 1 deletion resources/tests/test_binutils_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
"((4 5 6) (7 8 9))",
"(i (q . 1) (q . 2) (q . 3))",
"((0x33 . i))",
"(g . (138935604050210 ()))"
"(g . (138935604050210 ()))",
"0x0001",
"0x00",
"0x5c4859"
]

def extend_atom():
Expand Down
10 changes: 5 additions & 5 deletions src/classic/clvm/__type_compatibility__.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn vec_to_string(r: &[u8]) -> String {
* @see https://github.com/python/cpython/blob/main/Objects/bytesobject.c#L1337
* @param {Uint8Array} r - byteArray to stringify
*/
pub fn pybytes_repr(r: &[u8], dquoted: bool) -> String {
pub fn pybytes_repr(r: &[u8], dquoted: bool, full_repr: bool) -> String {
let mut squotes = 0;
let mut dquotes = 0;
for b in r.iter() {
Expand All @@ -59,7 +59,7 @@ pub fn pybytes_repr(r: &[u8], dquoted: bool) -> String {

for b in r.iter() {
let c = *b as char;
if c == quote || c == '\\' {
if c == quote || (c == '\\' && full_repr) {
s = (s + "\\") + char_to_string(c).as_str();
} else if c == '\t' {
s += "\\t";
Expand Down Expand Up @@ -172,11 +172,11 @@ impl Bytes {
}

pub fn to_formal_string(&self) -> String {
pybytes_repr(&self._b, true)
pybytes_repr(&self._b, true, false)
}

pub fn pybytes(&self) -> String {
pybytes_repr(&self._b, false)
pybytes_repr(&self._b, false, true)
}

pub fn hex(&self) -> String {
Expand Down Expand Up @@ -209,7 +209,7 @@ impl Bytes {

impl Display for Bytes {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
fmt.write_str(&pybytes_repr(&self._b, false))?;
fmt.write_str(&pybytes_repr(&self._b, false, true))?;
Ok(())
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/classic/clvm_tools/binutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ pub fn assemble_from_ir(
}

fn has_oversized_sign_extension(atom: &Bytes) -> bool {
let data = atom.data();

// Can't have an extra sign extension if the number is too short.
// With the exception of 0.
if atom.length() < 2 {
return false;
return data.len() == 1 && data[0] == 0;
}

let data = atom.data();
if data[0] == 0 {
// This is a canonical value. The opposite is non-canonical.
// 0x0080 -> 128
Expand Down
2 changes: 1 addition & 1 deletion src/tests/classic/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ fn test_io_err_from_syntax_err() {
fn test_bytes_to_pybytes_repr_0() {
let b = b"\x11\x01abc\r\ntest\ttest\r\n";
assert_eq!(
pybytes_repr(b, false),
pybytes_repr(b, false, true),
"b'\\x11\\x01abc\\r\\ntest\\ttest\\r\\n'"
);
}
Expand Down

0 comments on commit a679745

Please sign in to comment.