Thread overview
[Issue 3230] New: std.conv should provide facilities for converting from Roman numerals.
Aug 05, 2009
dsimcha@yahoo.com
Sep 08, 2009
Don
August 05, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3230

           Summary: std.conv should provide facilities for converting from
                    Roman numerals.
           Product: D
           Version: 2.031
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: patch
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: dsimcha@yahoo.com


Pretty simple, without further ado here's the patch.  Just to be absolutely clear, I wrote this code snippet entirely by myself and hereby release it into the public domain.

// Converts a single digit from Roman to arabic.
private uint convertDigit(char digit) pure {
    switch(digit) {
        case 'I' :
            return 1;
        case 'V' :
            return 5;
        case 'X' :
            return 10;
        case 'L' :
            return 50;
        case 'C' :
            return 100;
        case 'D' :
            return 500;
        case 'M' :
            return 1000;
        default :
            throw new ConvError(
                "Could not convert Roman digit " ~ digit ~ " to a number.");
    }
    assert(0);
}

/**Converts a string containing a Roman numeral to an unsigned integer
 * representation.
 *
 * Throws:  ConvError on invalid Roman digit.
 */
uint romanToInt(const char[] roman) pure {
    uint result = 0;
    uint maxDigit = 0;

    for(size_t i = roman.length - 1; i != size_t.max; i--) {
        auto romanDigit = roman[i];
        uint arabic = convertDigit(romanDigit);

        if(arabic > maxDigit) {
            maxDigit = arabic;
        }

        // Should we add or subtract?
        if(arabic >= maxDigit) {
            result += arabic;
        } else {
            result -= arabic;
        }
    }
    return result;
}

unittest {
    assert(romanToInt("XIX") == 19);
    assert(romanToInt("DCLXVI") == 666);
    assert(romanToInt("XXX") == 30);
    assert(romanToInt("MDCCCLXXXVIII") == 1888);
    assert(romanToInt("XCIX") == 99);
    assert(romanToInt("XLIX") == 49);
    assert(romanToInt("IV") == 4);
    assert(romanToInt("XLV") == 45);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 08, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3230


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug@yahoo.com.au


--- Comment #1 from Don <clugdbug@yahoo.com.au> 2009-09-08 09:46:17 PDT ---
Is this sufficiently commonly used that it belongs in the standard library? My feeling is that the standard library would need to get very large before this sort of stuff is in there. Do other languages include it?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 11, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3230


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |andrei@metalanguage.com
         AssignedTo|nobody@puremagic.com        |andrei@metalanguage.com


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 03, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3230


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |WONTFIX


--- Comment #2 from Andrei Alexandrescu <andrei@metalanguage.com> 2010-10-02 19:05:30 PDT ---
This is a bit too exotic for inclusion in the standard library. If people feel differently, please reopen and discuss.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------