From 9711a9eecbf960789bcc775928ab080ba55fe118 Mon Sep 17 00:00:00 2001 From: yangbeom Date: Wed, 22 Jun 2022 23:48:43 +0900 Subject: [PATCH 1/8] add binascii.rlecode_hqx, binascii.rledecode_hqx --- stdlib/src/binascii.rs | 71 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/stdlib/src/binascii.rs b/stdlib/src/binascii.rs index 21f3a80f8b..6a95230dd1 100644 --- a/stdlib/src/binascii.rs +++ b/stdlib/src/binascii.rs @@ -182,6 +182,77 @@ mod decl { Ok((*c - 0x20) & 0x3f) } + #[pyfunction] + fn rlecode_hqx(s: ArgAsciiBuffer) -> PyResult> { + let runchar = 0x90; //RUNCHAR = b"\x90" + s.with_ref(|buffer| { + let len = buffer.len(); + let mut out_data = Vec::::with_capacity((len * 2) + 2); + + let mut idx = 0; + while idx < len { + let ch = buffer[idx]; + + if ch == runchar { + out_data.push(runchar); + out_data.push(0); + return Ok(out_data); + } else { + let mut inend = idx + 1; + while inend < len && buffer[inend] == ch && inend < idx + 255 { + inend = inend + 1; + } + if inend - idx > 3 { + out_data.push(ch); + out_data.push(runchar); + out_data.push(((inend - idx) % 256) as u8); + idx = inend - 1; + } else { + out_data.push(ch); + } + } + idx = idx + 1; + } + Ok(out_data) + }) + } + + #[pyfunction] + fn rledecode_hqx(s: ArgAsciiBuffer) -> PyResult> { + let runchar = 0x90; //RUNCHAR = b"\x90" + s.with_ref(|buffer| { + let len = buffer.len(); + let mut out_data = Vec::::with_capacity(len); + let mut idx = 0; + + if buffer[idx] == runchar { + out_data.push(runchar); + } else { + out_data.push(buffer[idx]); + } + idx = idx + 1; + + while idx < len { + if buffer[idx] == runchar { + if buffer[idx + 1] == 0 { + out_data.push(runchar); + } else { + let ch = buffer[idx - 1]; + let range = buffer[idx + 1]; + idx = idx + 1; + for _ in 1..range { + out_data.push(ch); + } + } + } else { + out_data.push(buffer[idx]); + } + idx = idx + 1; + } + Ok(out_data) + }) + } + #[pyfunction] fn a2b_uu(s: ArgAsciiBuffer, vm: &VirtualMachine) -> PyResult> { s.with_ref(|b| { From ed69400e3099ef07bf23d7280f500d099a56693f Mon Sep 17 00:00:00 2001 From: yangbeom Date: Wed, 22 Jun 2022 23:49:38 +0900 Subject: [PATCH 2/8] enable test_rle in test_binascii.py --- Lib/test/test_binascii.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Lib/test/test_binascii.py b/Lib/test/test_binascii.py index f97df4cb2b..1e04905405 100644 --- a/Lib/test/test_binascii.py +++ b/Lib/test/test_binascii.py @@ -220,8 +220,6 @@ def test_hqx(self): res = binascii.rledecode_hqx(b) self.assertEqual(res, self.rawdata) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_rle(self): # test repetition with a repetition longer than the limit of 255 data = (b'a' * 100 + b'b' + b'c' * 300) From 673c123e2c9aef962c32628ec20827b5bc266049 Mon Sep 17 00:00:00 2001 From: yangbeom Date: Thu, 23 Jun 2022 00:20:50 +0900 Subject: [PATCH 3/8] fix clippy Errors --- stdlib/src/binascii.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/stdlib/src/binascii.rs b/stdlib/src/binascii.rs index 6a95230dd1..a4378a021b 100644 --- a/stdlib/src/binascii.rs +++ b/stdlib/src/binascii.rs @@ -200,7 +200,7 @@ mod decl { } else { let mut inend = idx + 1; while inend < len && buffer[inend] == ch && inend < idx + 255 { - inend = inend + 1; + inend += 1; } if inend - idx > 3 { out_data.push(ch); @@ -211,7 +211,7 @@ mod decl { out_data.push(ch); } } - idx = idx + 1; + idx += 1; } Ok(out_data) }) @@ -230,7 +230,7 @@ mod decl { } else { out_data.push(buffer[idx]); } - idx = idx + 1; + idx += 1; while idx < len { if buffer[idx] == runchar { @@ -239,7 +239,7 @@ mod decl { } else { let ch = buffer[idx - 1]; let range = buffer[idx + 1]; - idx = idx + 1; + idx += 1; for _ in 1..range { out_data.push(ch); } @@ -247,7 +247,7 @@ mod decl { } else { out_data.push(buffer[idx]); } - idx = idx + 1; + idx += 1; } Ok(out_data) }) From 11f5db9040d69d6f435b249150a09b9771edc88d Mon Sep 17 00:00:00 2001 From: yangbeom Date: Thu, 23 Jun 2022 21:33:11 +0900 Subject: [PATCH 4/8] Update stdlib/src/binascii.rs Co-authored-by: Jeong YunWon <69878+youknowone@users.noreply.github.com> --- stdlib/src/binascii.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/src/binascii.rs b/stdlib/src/binascii.rs index a4378a021b..df8eb44509 100644 --- a/stdlib/src/binascii.rs +++ b/stdlib/src/binascii.rs @@ -184,7 +184,7 @@ mod decl { #[pyfunction] fn rlecode_hqx(s: ArgAsciiBuffer) -> PyResult> { - let runchar = 0x90; //RUNCHAR = b"\x90" + const RUNCHAR: u8 = 0x90; // b'\x90' s.with_ref(|buffer| { let len = buffer.len(); let mut out_data = Vec::::with_capacity((len * 2) + 2); From 0084e084524f87d803e41f217901c0e288275f4b Mon Sep 17 00:00:00 2001 From: yangbeom Date: Thu, 23 Jun 2022 21:34:31 +0900 Subject: [PATCH 5/8] Update stdlib/src/binascii.rs Co-authored-by: Jeong YunWon <69878+youknowone@users.noreply.github.com> --- stdlib/src/binascii.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stdlib/src/binascii.rs b/stdlib/src/binascii.rs index df8eb44509..afe9005d05 100644 --- a/stdlib/src/binascii.rs +++ b/stdlib/src/binascii.rs @@ -225,11 +225,11 @@ mod decl { let mut out_data = Vec::::with_capacity(len); let mut idx = 0; - if buffer[idx] == runchar { - out_data.push(runchar); + out_data.push(if buffer[idx] == runchar { + runchar } else { - out_data.push(buffer[idx]); - } + buffer[idx] + }); idx += 1; while idx < len { From a526da1bf6eb3d0b487d6f5089e99cf04fc9c90d Mon Sep 17 00:00:00 2001 From: yangbeom Date: Thu, 23 Jun 2022 21:37:55 +0900 Subject: [PATCH 6/8] Apply PR comment --- stdlib/src/binascii.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/stdlib/src/binascii.rs b/stdlib/src/binascii.rs index afe9005d05..f1bd7332d1 100644 --- a/stdlib/src/binascii.rs +++ b/stdlib/src/binascii.rs @@ -225,11 +225,7 @@ mod decl { let mut out_data = Vec::::with_capacity(len); let mut idx = 0; - out_data.push(if buffer[idx] == runchar { - runchar - } else { - buffer[idx] - }); + out_data.push(buffer[idx]); idx += 1; while idx < len { From d1e3b29d83332ce1b40969a9c3001d5b6ff8bf48 Mon Sep 17 00:00:00 2001 From: yangbeom Date: Thu, 23 Jun 2022 21:51:11 +0900 Subject: [PATCH 7/8] Apply missed PR comment --- stdlib/src/binascii.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/stdlib/src/binascii.rs b/stdlib/src/binascii.rs index f1bd7332d1..be7ecc1a6d 100644 --- a/stdlib/src/binascii.rs +++ b/stdlib/src/binascii.rs @@ -193,8 +193,8 @@ mod decl { while idx < len { let ch = buffer[idx]; - if ch == runchar { - out_data.push(runchar); + if ch == RUNCHAR { + out_data.push(RUNCHAR); out_data.push(0); return Ok(out_data); } else { @@ -204,7 +204,7 @@ mod decl { } if inend - idx > 3 { out_data.push(ch); - out_data.push(runchar); + out_data.push(RUNCHAR); out_data.push(((inend - idx) % 256) as u8); idx = inend - 1; } else { @@ -219,7 +219,7 @@ mod decl { #[pyfunction] fn rledecode_hqx(s: ArgAsciiBuffer) -> PyResult> { - let runchar = 0x90; //RUNCHAR = b"\x90" + const RUNCHAR: u8 = 0x90; //b'\x90' s.with_ref(|buffer| { let len = buffer.len(); let mut out_data = Vec::::with_capacity(len); @@ -229,9 +229,9 @@ mod decl { idx += 1; while idx < len { - if buffer[idx] == runchar { + if buffer[idx] == RUNCHAR { if buffer[idx + 1] == 0 { - out_data.push(runchar); + out_data.push(RUNCHAR); } else { let ch = buffer[idx - 1]; let range = buffer[idx + 1]; From fbc1f0833bb117fc42f7f540c7461bcf9229ce2f Mon Sep 17 00:00:00 2001 From: yangbeom Date: Thu, 23 Jun 2022 21:56:59 +0900 Subject: [PATCH 8/8] Apply rustfmt --- stdlib/src/binascii.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/src/binascii.rs b/stdlib/src/binascii.rs index be7ecc1a6d..f38314b13d 100644 --- a/stdlib/src/binascii.rs +++ b/stdlib/src/binascii.rs @@ -219,7 +219,7 @@ mod decl { #[pyfunction] fn rledecode_hqx(s: ArgAsciiBuffer) -> PyResult> { - const RUNCHAR: u8 = 0x90; //b'\x90' + const RUNCHAR: u8 = 0x90; //b'\x90' s.with_ref(|buffer| { let len = buffer.len(); let mut out_data = Vec::::with_capacity(len);