hexathon

Common and uncommon hex string operations for python3
git clone git://git.defalsify.org/python-hexathon.git
Log | Files | Refs | LICENSE

commit 2f6a1bd8560387b9839aad7c66de1fea2c4ce933
parent 231319cafc193a0af2ab4bf6503f0d63220bb779
Author: lash <dev@holbrook.no>
Date:   Mon,  9 May 2022 08:01:13 +0000

Add 'same' comparison method

Diffstat:
MCHANGELOG | 4++++
Mhexathon/parse.py | 18++++++++++++++++--
Msetup.cfg | 2+-
Mtests/test_basic.py | 19+++++++++++++++++++
4 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG @@ -1,3 +1,7 @@ +- 0.1.7 + * Add "same" comparison method +- 0.1.6 + * Avoid unintended padding of uneven hex for strip and add 0x - 0.1.5 * Avoid reciprocal dependency between 0x and compact/even - 0.1.4 diff --git a/hexathon/parse.py b/hexathon/parse.py @@ -21,8 +21,10 @@ def even(hx, allow_empty=False, allow_compact=False): return valid(hx, allow_compact=allow_compact) -def uniform(hx): - return even(hx).lower() +def uniform(hx, allow_empty=False, compact_value=False): + if compact_value: + return hx.lower() + return even(hx, allow_empty=allow_empty).lower() def strip_0x(hx, allow_empty=False, compact_value=False, pad=True): @@ -109,3 +111,15 @@ def to_int(v, need_prefix=False): v = strip_0x(v) return int(v, 16) + + +def same(x, y, allow_empty=False, compact_value=False, pad=True): + no_uniform_compact = compact_value or not pad + + vl = strip_0x(x, allow_empty=allow_empty, compact_value=compact_value, pad=pad) + vl = uniform(vl, allow_empty=allow_empty, compact_value=no_uniform_compact) + + vr = strip_0x(y, allow_empty=allow_empty, compact_value=compact_value, pad=pad) + vr = uniform(vr, allow_empty=allow_empty, compact_value=no_uniform_compact) + + return vl == vr diff --git a/setup.cfg b/setup.cfg @@ -5,6 +5,6 @@ url=https://gitlab.com/nolash/python-hexathon author_email=dev@holbrook.no name=hexathon -version=0.1.6 +version=0.1.7 description=Common and uncommon hex string operations author=Louis Holbrook diff --git a/tests/test_basic.py b/tests/test_basic.py @@ -54,6 +54,25 @@ class HexTest(unittest.TestCase): self.assertEqual(hexathon.compact('000abc'), 'abc') self.assertEqual(hexathon.compact('abc'), 'abc') + + def test_same(self): + x = 'deadbeef' + y = '0xdeadbeef' + self.assertTrue(hexathon.same(x, y)) + + + x = '0deadbeef' + y = '00deadbeef' + self.assertTrue(hexathon.same(x, y)) + self.assertFalse(hexathon.same(x, y, pad=False)) + self.assertTrue(hexathon.same(x, y, compact_value=True)) + + x = '0x' + y = '' + with self.assertRaises(ValueError): + hexathon.same(x, y) + self.assertTrue(hexathon.same(x, y, allow_empty=True)) + if __name__ == '__main__': unittest.main()