[Issue 9895] New: Add functional style regex pattern-matching

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Apr 7 04:25:12 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=9895

           Summary: Add functional style regex pattern-matching
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: GenericNPC at gmail.com


--- Comment #0 from IdanArye <GenericNPC at gmail.com> 2013-04-07 04:25:10 PDT ---
Based on Scala's pattern-matching with regular expressions syntax, I created
the function std.regex.regexSwitch(formerly switchRegex). It's main use-case is
for reading textual input fields that can be in multiple formats:

    enum WeekDay
    {
        Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
    }

    struct Schedule
    {
        WeekDay day;
        int hour;
    }

    assert(equal(
                [
                    Schedule(WeekDay.Sunday, 1),
                    Schedule(WeekDay.Monday, 14),
                    Schedule(WeekDay.Tuesday, 3),
                    Schedule(WeekDay.Wednesday, 4),
                    Schedule(WeekDay.Thursday, 17),
                    Schedule(WeekDay.Friday, 6),
                ],
                [
                    "Sunday 1AM",
                    "Monday 2PM",
                    "Tuesday 3",
                    "4AM Wednesday",
                    "5PM Thursday",
                    "6 Friday",
                ].map!(regexSwitch!(
                        `(\w+) (\d+)AM`, (WeekDay day, int hour) =>
Schedule(day, hour % 12),
                        `(\w+) (\d+)PM`, (WeekDay day, int hour) =>
Schedule(day, hour % 12 + 12),
                        `(\w+) (\d+)`, (WeekDay day, int hour) => Schedule(day,
hour),
                        `(\d+)AM (\w+)`, (int hour, WeekDay day) =>
Schedule(day, hour % 12),
                        `(\d+)PM (\w+)`, (int hour, WeekDay day) =>
Schedule(day, hour % 12 + 12),
                        `(\d+) (\w+)`, (int hour, WeekDay day) => Schedule(day,
hour),
                        ))()));

See https://github.com/D-Programming-Language/phobos/pull/1241

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


More information about the Digitalmars-d-bugs mailing list