[HACKERS] Add Roman numeral conversion to to_number
От
Oliver Ford
Тема
[HACKERS] Add Roman numeral conversion to to_number
Дата
Msg-id
CAGMVOduAJ9wKqJXBYnmFmEetKxapJxrG3afUwpbOZ6n_dWaUnA@mail.gmail.com
Список
Дерево обсуждения
[HACKERS] Add Roman numeral conversion to to_number Oliver Ford <ojford@gmail.com>
Re: [HACKERS] Add Roman numeral conversion to to_number Robert Haas <robertmhaas@gmail.com>
Re: [HACKERS] Add Roman numeral conversion to to_number David Fetter <david@fetter.org>
Re: [HACKERS] Add Roman numeral conversion to to_number Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
Re: [HACKERS] Add Roman numeral conversion to to_number Christoph Berg <myon@debian.org>
Re: [HACKERS] Add Roman numeral conversion to to_number Merlin Moncure <mmoncure@gmail.com>
Re: [HACKERS] Add Roman numeral conversion to to_number Doug Doole <DougDoole@gmail.com>
Re: [HACKERS] Add Roman numeral conversion to to_number Oliver Ford <ojford@gmail.com>
Re: [HACKERS] Add Roman numeral conversion to to_number Douglas Doole <dougdoole@gmail.com>
Re: [HACKERS] Add Roman numeral conversion to to_number Douglas Doole <dougdoole@gmail.com>
Re: [HACKERS] Add Roman numeral conversion to to_number David Fetter <david@fetter.org>
Re: [HACKERS] Add Roman numeral conversion to to_number Chris Travers <chris.travers@adjust.com>
Re: [HACKERS] Add Roman numeral conversion to to_number Douglas Doole <dougdoole@gmail.com>
Re: [HACKERS] Add Roman numeral conversion to to_number Oliver Ford <ojford@gmail.com>
Re: [HACKERS] Add Roman numeral conversion to to_number Tom Lane <tgl@sss.pgh.pa.us>
Re: [HACKERS] Add Roman numeral conversion to to_number Michael Paquier <michael.paquier@gmail.com>
Re: [HACKERS] Add Roman numeral conversion to to_number Doug Doole <DougDoole@gmail.com>
Re: [HACKERS] Add Roman numeral conversion to to_number Thomas Munro <thomas.munro@enterprisedb.com>
Re: [HACKERS] Add Roman numeral conversion to to_number Oliver Ford <ojford@gmail.com>
Re: [HACKERS] Add Roman numeral conversion to to_number Robert Haas <robertmhaas@gmail.com>
Re: [HACKERS] Add Roman numeral conversion to to_number Oleg Bartunov <obartunov@gmail.com>
Adds to the to_number() function the ability to convert Roman numerals
to a number. This feature is on the formatting.c TODO list. It is not
currently implemented in either Oracle, MSSQL or MySQL so gives
PostgreSQL an edge :-)
==Usage==
Call: to_number(numerals, 'RN') or to_number(numerals, 'rn').
Example: to_number('MMMXIII', 'RN') returns 3013. to_number('xiv',
'rn') returns 14.
The function is case insensitive for the numerals. It accepts a mix of
cases and treats them the same. So to_number ('MCI, 'rn') and
to_number ('McI', 'RN') both return 1101. The format mask must however
be either 'RN' or 'rn'. If there are other elements in the mask, those
other elements will be ignored. So to_number('MMM', 'FMRN') returns
3000.
Whitespace before the numerals is ignored.
==Validation==
The new function roman_to_int() in formatting.c performs the
conversion. It strictly validates the numerals based on the following
Roman-to-Arabic conversion rules:
1. The power-of-ten numerals (I, X, C, M) can be repeated up to three
times in a row. The beginning-with-5 numerals (V, L, D) can each
appear only once.
2. Subtraction from a power-of-ten numeral cannot occur if a
beginning-with-5 numeral appears later.
3. Subtraction cannot occur if the smaller numeral is less than a
tenth of the greater numeral (so IX is valid, but IC is invalid).
4. There cannot be two subtractions in a row.
5. A beginning-with-5 numeral cannot subtract.
If any of these rules are violated, an error is raised.
==Testing==
This has been tested on a Windows build of the master branch with
MinGW. The included regression tests positively test every value from
1 to 3999 (the Roman numeral max value) by calling the existing
to_char() function to get the Roman value, then converting it back to
an Arabic value. There are also negative tests for each invalid code
path and some positive mixed-case tests.
Documentation is updated to include this new feature.
==References==
http://sierra.nmsu.edu/morandi/coursematerials/RomanNumerals.html
Documents the strict Roman numeral standard.
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
В списке pgsql-hackers по дате отправления
От: Rushabh Lathia
Дата: