diff --git a/pycgtool/util.py b/pycgtool/util.py index 6648692b9a4257bfbda67d79292e433e52717864..aa88d684460d3454f4b986f9acf416a1cc8d9b36 100644 --- a/pycgtool/util.py +++ b/pycgtool/util.py @@ -8,6 +8,7 @@ import random import math import filecmp import logging +import re from collections import namedtuple @@ -470,18 +471,9 @@ class FixedFormatUnpacker(object): :param format_string: C format string :return: List of FormatItems """ - items = [] types = {"d": int, "s": str, "f": float} - format_string = format_string.lower() - for item in format_string.split("%")[1:]: - try: - item_width = int(item[:-1]) - except ValueError: - # Probably a float format specifier, ignore precision, just use width - item_width = int(float(item[:-1])) - - item_type = types[item[-1]] - items.append(cls.FormatItem(item_type, item_width)) + p = re.compile(r'%-?(?P<width>[0-9]+)[.0-9]*(?P<type>[dsfDSF])') + items = [cls.FormatItem(types[match.group("type")], int(match.group("width"))) for match in p.finditer(format_string)] return items @classmethod @@ -494,15 +486,12 @@ class FixedFormatUnpacker(object): """ items = [] types = {"i": int, "a": str, "f": float, "x": None} - format_string = format_string.lower() - for item in format_string.split(","): - try: - item_width = int(item[1:]) - except ValueError: - # Probably a float format specifier, ignore precision, just use width - item_width = int(float(item[1:])) - item_type = types[item[0]] - items.append(cls.FormatItem(item_type, item_width)) + p = re.compile(r'\s*(?P<repeat>[0-9]*)(?P<type>[ixafIXAF])(?P<width>[0-9]*)\s*') + for match in p.finditer(format_string): + repeat = int(match.group("repeat")) if match.group("repeat") else 1 + width = int(match.group("width")) if match.group("width") else 1 + for _ in range(repeat): + items.append(cls.FormatItem(types[match.group("type")], width)) return items def unpack(self, string): diff --git a/test/test_util.py b/test/test_util.py index 8f85cdcd4e37142825110536f343aafcbd6f82b9..5d50a1efb137a41b2d9375fc674e8b913efa3b10 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -163,7 +163,7 @@ class UtilTest(unittest.TestCase): self.assertFalse("four" in enum) def test_fixed_format_unpacker_c(self): - unpacker = FixedFormatUnpacker("%4d%5s%4.1f") + unpacker = FixedFormatUnpacker("%-4d%5s%4.1f") toks = unpacker.unpack("1234hello12.3") self.assertEqual(3, len(toks)) self.assertEqual(1234, toks[0]) @@ -188,9 +188,8 @@ class UtilTest(unittest.TestCase): self.assertEqual("hello", toks[1]) self.assertAlmostEqual(12.3, toks[2]) - @unittest.expectedFailure def test_fixed_format_unpacker_fortran_repeat(self): - unpacker = FixedFormatUnpacker("2I2,X3,A5,X2,F4.1", + unpacker = FixedFormatUnpacker("2I2,X3,A5,2X,F4.1", FixedFormatUnpacker.FormatStyle.Fortran) toks = unpacker.unpack("1234 x hello x12.3") self.assertEqual(4, len(toks))