Patch: RomanToInt conversion rewrite
Original Reporter info from Mantis: Bart @flyingsheep
-
Reporter name: Bart Broersma
Original Reporter info from Mantis: Bart @flyingsheep
- Reporter name: Bart Broersma
Description:
After disussing this on the mailinglist (http://lists.freepascal.org/lists/fpc-pascal/2013-September/039339.html) and private discussions with Michael I post her a patch that adresses the Roman to Integer conversion issue.
It fixes the problem that the current implementation allows for rather ludicrous input like 'IIIMMIIIXDCII' which is by all means not a valid Roman numeral.
For consistency with other RTL StringToSomeNumber conversion functions I have provided
- TryRomanToInt
- RomanToIntDef
- RomanToInt
Since the "proper way of parsing a Roman numeral" is up for debate, an optional parameter "Strictness" can be passed to each ot these functions.
The meaning of these parameter is explained in the comments above the implementation of the TryRomanToInt() function.
As discussed in the mailinglist this change will break existing programs (since current implementation returns 0 on faulty input, and do not raise an excetion). However the expected number of programs using this function is small, and the remedy is quit simple: change each RomanToInt() in RomantoIntDef(), and the function will still return 0 on faulty input.
Steps to reproduce:
Apply attached patch.
Additional information:
Instead of adding a const to objpas/rtlconst.pp, I have done:
resourcestring
SInvalidRomanNumeral = '%s is not a valid Roman numeral';
Reason:
- there already is a resourcestring in this unit.
Feel free to move it to rtlconst.pp (or ask me to make a new diff).
Mantis conversion info:
- Mantis ID: 25112
- OS: Window
- OS Build: Win7
- Platform: i386
- Version: 2.6.2
- Fixed in version: 2.6.4
- Fixed in revision: 25842 (#e0a78a21)
- Target version: 3.0.0