Repository: nion-software/nionutils

Issue:
Regex in IntegerToStringConverter not handling negative numbers
https://github.com/nion-software/nionutils/blob/c4b09457ab9433dde6f224279fe8f35265c6c041/nion/utils/Converter.py#L46

This regex is not sufficient to properly handle negative numbers. It will also fail to convert floating point numbers. See the following examples:

```
from nion.utils import Converter
conv = Converter.IntegerToStringConverter()
conv.convert_back("-1")
>>> 1
conv.convert_back("1.243")
>>> 1243


Discussion:

Patch:
diff --git a/nion/utils/Converter.py b/nion/utils/Converter.py
index 8bfacb2..178a127 100644
--- a/nion/utils/Converter.py
+++ b/nion/utils/Converter.py
@@ -43,7 +43,7 @@ class IntegerToStringConverter(ConverterLike[int, str]):
 
     def convert_back(self, formatted_value: typing.Optional[str]) -> typing.Optional[int]:
         """ Convert string to value using standard int conversion """
-        formatted_value = re.sub("[^0-9]", "", formatted_value) if self.__fuzzy and formatted_value else None
+        formatted_value = re.sub("[+-](?!\d)|(?<=\.)\w*|[^-+0-9]", "", formatted_value) if self.__fuzzy and formatted_value else None
         if formatted_value:
             return int(formatted_value)
         else:


Test patch:
diff --git a/nion/utils/test/Converter_test.py b/nion/utils/test/Converter_test.py
index 2fcbf1b..65b8717 100644
--- a/nion/utils/test/Converter_test.py
+++ b/nion/utils/test/Converter_test.py
@@ -23,6 +23,12 @@ class TestConverter(unittest.TestCase):
         self.assertAlmostEqual(converter.convert_back(converter.convert(-100)) or 0.0, -100)
         self.assertAlmostEqual(converter.convert_back(converter.convert(100)) or 0.0, 100)
 
+    def test_integer_to_string_converter(self) -> None:
+        converter = Converter.IntegerToStringConverter()
+        self.assertEqual(converter.convert_back("-1"), -1)
+        self.assertEqual(converter.convert_back("2.45653"), 2)
+        self.assertEqual(converter.convert_back("-adcv-2.15sa56aas"), -2)
+        self.assertEqual(converter.convert_back("xx4."), 4)
 
 
 if __name__ == '__main__':