Skip to content

Commit

Permalink
Improved integer suffix
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Zoppi <[email protected]>
  • Loading branch information
TexZK committed Mar 2, 2024
1 parent 8baa708 commit d581eea
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 23 deletions.
6 changes: 6 additions & 0 deletions docs/_autosummary/hexrec.hexdump.CHAR_PRINTABLE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CHAR\_PRINTABLE
===============

.. currentmodule:: hexrec.hexdump

.. autodata:: CHAR_PRINTABLE
6 changes: 6 additions & 0 deletions docs/_autosummary/hexrec.hexdump.CHAR_TOKENS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CHAR\_TOKENS
============

.. currentmodule:: hexrec.hexdump

.. autodata:: CHAR_TOKENS
6 changes: 6 additions & 0 deletions docs/_autosummary/hexrec.hexdump.DEFAULT_FORMAT_ORDER.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DEFAULT\_FORMAT\_ORDER
======================

.. currentmodule:: hexrec.hexdump

.. autodata:: DEFAULT_FORMAT_ORDER
6 changes: 6 additions & 0 deletions docs/_autosummary/hexrec.hexdump.hexdump.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
hexdump
=======

.. currentmodule:: hexrec.hexdump

.. autofunction:: hexdump
49 changes: 49 additions & 0 deletions docs/_autosummary/hexrec.hexdump.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
hexdump
=======

.. automodule:: hexrec.hexdump




.. rubric:: Attributes

.. autosummary::
:toctree:
:template: custom-base-template.rst
:nosignatures:

~CHAR_PRINTABLE
~CHAR_TOKENS
~DEFAULT_FORMAT_ORDER






.. rubric:: Functions

.. autosummary::
:toctree:
:template: custom-base-template.rst
:nosignatures:

~hexdump

















6 changes: 6 additions & 0 deletions docs/_autosummary/hexrec.utils.SUFFIX_SCALE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SUFFIX\_SCALE
=============

.. currentmodule:: hexrec.utils

.. autodata:: SUFFIX_SCALE
1 change: 1 addition & 0 deletions docs/_autosummary/hexrec.utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
:template: custom-base-template.rst
:nosignatures:

~SUFFIX_SCALE
~DEFAULT_DELETE


Expand Down
1 change: 1 addition & 0 deletions docs/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ Reference
hexrec.formats.srec
hexrec.formats.titxt
hexrec.formats.xtek
hexrec.hexdump
hexrec.utils
hexrec.xxd
22 changes: 10 additions & 12 deletions src/hexrec/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,10 +660,10 @@ def flood(
""")
@click.option('-n', '--length', 'length', type=BASED_INT, help="""
Interpret only length bytes of input.
""") # FIXME: SUFFIXED_INT for hexdump compatible integers
""")
@click.option('-s', '--skip', 'skip', type=BASED_INT, help="""
Skip offset bytes from the beginning of the input.
""") # FIXME: SUFFIXED_INT for hexdump compatible integers
""")
@click.option('-v', '--no_squeezing', 'no_squeezing', is_flag=True, help="""
The -v option causes hexdump to display all input data.
Without the -v option, any number of groups of output lines
Expand All @@ -678,8 +678,8 @@ def flood(
Forces the input file format.
Required for the standard input.
""")
@click.option('-V', '--version', is_flag=True, is_eager=True, expose_value=False,
callback=print_hexdump_version, help="""
@click.option('-V', '--version', is_flag=True, is_eager=True,
expose_value=False, callback=print_hexdump_version, help="""
Print version and exit.
""")
@click.argument('infile', type=FILE_PATH_IN)
Expand All @@ -698,7 +698,6 @@ def hexdump(
upper: bool,
input_format: Optional[str], # TODO:
) -> None:
# TODO: __doc__
r"""Display file contents in hexadecimal, decimal, octal, or ascii.
The hexdump utility is a filter which displays the specified
Expand Down Expand Up @@ -788,10 +787,10 @@ def hexdump(
""")
@click.option('-n', '--length', 'length', type=BASED_INT, help="""
Interpret only length bytes of input.
""") # FIXME: SUFFIXED_INT for hexdump compatible integers
""")
@click.option('-s', '--skip', 'skip', type=BASED_INT, help="""
Skip offset bytes from the beginning of the input.
""") # FIXME: SUFFIXED_INT for hexdump compatible integers
""")
@click.option('-v', '--no_squeezing', 'no_squeezing', is_flag=True, help="""
The -v option causes hexdump to display all input data.
Without the -v option, any number of groups of output lines
Expand All @@ -806,8 +805,8 @@ def hexdump(
Forces the input file format.
Required for the standard input.
""")
@click.option('-V', '--version', is_flag=True, is_eager=True, expose_value=False,
callback=print_hexdump_version, help="""
@click.option('-V', '--version', is_flag=True, is_eager=True,
expose_value=False, callback=print_hexdump_version, help="""
Print version and exit.
""")
@click.argument('infile', type=FILE_PATH_IN)
Expand All @@ -825,7 +824,6 @@ def hd(
upper: bool,
input_format: Optional[str], # TODO:
) -> None:
# TODO: __doc__
r"""Display file contents in hexadecimal, decimal, octal, or ascii.
The hexdump utility is a filter which displays the specified
Expand Down Expand Up @@ -1199,8 +1197,8 @@ def del_header(
@click.option('-u', '--upper', 'upper', is_flag=True, help="""
Uses upper case hex letters on data only.
""")
@click.option('-v', '--version', is_flag=True, is_eager=True, expose_value=False,
callback=print_version, help="""
@click.option('-v', '--version', is_flag=True, is_eager=True,
expose_value=False, callback=print_version, help="""
Prints the package version number.
""")
@click.argument('infile', type=FILE_PATH_IN)
Expand Down
5 changes: 3 additions & 2 deletions src/hexrec/hexdump.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,13 +523,14 @@ def hexdump(
offset = 0
last_chunk = None
squeezing = False
read = instream.read
write = outstream.write

while True:
if length is None:
chunk = instream.read(width)
chunk = read(width)
else:
chunk = instream.read(min(width, length - offset))
chunk = read(min(width, length - offset))

if not chunk:
break
Expand Down
45 changes: 38 additions & 7 deletions src/hexrec/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,45 @@
BIN8_TO_BYTES: List[bytes] = [bin(i)[2:].zfill(8).encode() for i in range(256)]
BYTES_TO_BIN8: Mapping[bytes, int] = {s: i for i, s in enumerate(BIN8_TO_BYTES)}

SUFFIX_SCALE: Mapping[str, int] = {
'k': 2**10,
'm': 2**20,
'g': 2**30,
't': 2**40,
'p': 2**50,
'e': 2**60,
'z': 2**70,
'y': 2**80,

'kib': 2**10,
'mib': 2**20,
'gib': 2**30,
'tib': 2**40,
'pib': 2**50,
'eib': 2**60,
'zib': 2**70,
'yib': 2**80,

'kb': 10**3,
'mb': 10**6,
'gb': 10**9,
'tb': 10**12,
'pb': 10**15,
'eb': 10**18,
'zb': 10**21,
'yb': 10**24,
}
r"""Integer suffix to scale factor."""

INT_REGEX = re.compile(r'^\s*(?P<sign>[+-]?)\s*'
r'(?P<prefix>(0x|0b|0o|0)?)'
r'(?P<value>[a-f0-9]+)'
r'(?P<suffix>h?)'
r'(?P<scale>[km]?)\s*$')
r'\s*(?P<scale>('
r'k|m|g|t|p|e|z|y|'
r'kib|mib|gib|tib|pib|eib|zib|yib|'
r'kb|mb|gb|tb|pb|eb|zb|yb'
r')?)\s*$')

DEFAULT_DELETE: bytes = b' \t.-:\r\n'
r"""Delete from hex strings.
Expand Down Expand Up @@ -167,8 +201,8 @@ def parse_int(
either prefixed with ``0x`` or postfixed with ``h`` to convert
from a hexadecimal representation, or prefixed with ``0b`` from
binary; a prefix of only ``0`` converts from octal.
A further suffix of ``k`` or ``m`` scales as *kibibyte* or
*mebibyte*.
A further suffix applies a scale factor as per
:data:`SUFFIX_SCALE`.
A ``None`` value evaluates as ``None``.
Any other object class will call the standard :func:`int`.
Expand Down Expand Up @@ -214,10 +248,7 @@ def parse_int(
else:
i = int(value, 10)

if scale == 'k':
i <<= 10
elif scale == 'm':
i <<= 20
i *= SUFFIX_SCALE.get((scale or '').lower(), 1)

if sign == '-':
i = -i
Expand Down
13 changes: 11 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,17 @@
'0o1234567': 0o1234567,
'0O1234567': 0o1234567,

'1k': 1 << 10,
'1m': 1 << 20,
'1k': 2**10,
'1M': 2**20,
'1 G': 2**30,

'1KiB': 2**10,
'1 mib': 2**20,
'1GiB': 2**30,

'1 KB': 10**3,
'1MB': 10**6,
'1gb': 10**9,

b'456': 456,
123: 123,
Expand Down

0 comments on commit d581eea

Please sign in to comment.