GitHub PullRequestReviewCommentEvent at 2014-02-15 01:43:04
Repository: phobos
Actor: someboddy
Payload: {"comment":{"url":"https://api.github.com/repos/D-Programming-Language/phobos/pulls/comments/9770403","id":9770403,"diff_hunk":"@@ -6597,6 +6597,265 @@ public class RegexException : Exception\n     }\n }\n \n+\n+@system:\n+\n+/+\n+    Runs a delegate with the Captures of a regex. Each capture group will be\n+    transferred as a parameter, after being converted to the type of the\n+    parameter.\n+\n+    Params:\n+        captures = The regex's captures.\n+        dlg      = The delegate to call.\n++/\n+private auto callWithCaptures(CapturesType, DelegateType)(CapturesType captures, DelegateType dlg)\n+{\n+    alias dlgPatameters = ParameterTypeTuple!dlg;\n+    immutable delegateInvocation = {\n+        auto argsBuilder = appender!(string[])();\n+        foreach(i; 0 .. arity!dlg)\n+        {\n+            argsBuilder.put(\"captures[%d].to!(dlgPatameters[%s])()\".format(i + 1, i));\n+        }\n+        return \"dlg(%s)\".format(argsBuilder.data().join(\", \"));\n+    }();\n+\n+    return mixin(delegateInvocation);\n+}\n+\n+/++\n+    Tries to match text with multiple regular expressions. On the first match,\n+    calls the delegate associated with the regular expression that matched.\n+\n+    A last delegate with no arguments and no associated regular expression will\n+    be called if no regular expression matches the string. If no such delegate\n+    is supplied and no regular expression matches the string, an error will be\n+    thrown.\n+\n+    If a delegate's return type is void, the delegate must throw an exception,\n+    unless all the delegates are void. In that case, regexSwitch itself will\n+    return void.\n+\n+    Params:\n+        PatternsAndHandlers = Pairs of regular expressions and delegates.\n+        input    = The text to match with.\n++/\n+public template regexSwitch(PatternsAndHandlers...)\n+{\n+    auto regexSwitch(T)(T input)\n+    {\n+        // Check which type of string we have\n+        static if(is(typeof(input[0]) : char))\n+        {\n+            alias StringType = string;\n+        }else static if(is(typeof(input[0]) : wchar))\n+        {\n+            alias StringType = wstring;\n+        }else static if(is(typeof(input[0]) : dchar))\n+        {\n+            alias StringType = dstring;\n+        }else\n+        {\n+            static assert(false, \"%s is not a string type\".format(T.stringof));\n+        }\n+\n+\n+        // Check to see if all handlers return void.\n+        enum areAllHandlersVoidResult={\n+            foreach(index, handler; PatternsAndHandlers)\n+            {\n+                static if(index % 2 == 1)\n+                {\n+                    if(!is(ReturnType!handler == void))\n+                    {\n+                        return false;\n+                    }\n+                }\n+            }\n+            // If the template arguments' length is odd, the last one is also an handler.\n+            static if(PatternsAndHandlers.length % 2 == 1)\n+            {\n+                return is(ReturnType!(PatternsAndHandlers[$ - 1]) == void);\n+            }\n+            else\n+            {\n+                return true;\n+            }\n+        }();\n+\n+        foreach(index, pattern;PatternsAndHandlers)\n+        {\n+            // The patterns are the template arguments at even positions, but\n+            // if the number or template arguments is odd, the last one is a\n+            // handler, not a pattern.\n+            static if(index % 2 == 0 && index < PatternsAndHandlers.length - 1)\n+            {\n+                auto matches = match(input, pattern.to!StringType());","path":"std/regex.d","position":110,"original_position":110,"commit_id":"0adee9d9b35f7859486bb3f9affa95dc4d96cf24","original_commit_id":"1a73aeefca576f480724c1bfa02c35607f705b1d","user":{"login":"someboddy","id":1149255,"avatar_url":"https://gravatar.com/avatar/4d396047126200a98ba0df407998dcdd?d=https%3A%2F%2Fidenticons.github.com%2F5c1fc831d1b02c360a76cc56aa5160f4.png&r=x","gravatar_id":"4d396047126200a98ba0df407998dcdd","url":"https://api.github.com/users/someboddy","html_url":"https://github.com/someboddy","followers_url":"https://api.github.com/users/someboddy/followers","following_url":"https://api.github.com/users/someboddy/following{/other_user}","gists_url":"https://api.github.com/users/someboddy/gists{/gist_id}","starred_url":"https://api.github.com/users/someboddy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/someboddy/subscriptions","organizations_url":"https://api.github.com/users/someboddy/orgs","repos_url":"https://api.github.com/users/someboddy/repos","events_url":"https://api.github.com/users/someboddy/events{/privacy}","received_events_url":"https://api.github.com/users/someboddy/received_events","type":"User","site_admin":false},"body":"This is for better `wstring`\\`dstring` support. `match` requires that both the pattern and the string-to-match will be of the same string type.","created_at":"2014-02-15T01:43:04Z","updated_at":"2014-02-15T01:43:04Z","html_url":"https://github.com/D-Programming-Language/phobos/pull/1392#discussion_r9770403","pull_request_url":"https://api.github.com/repos/D-Programming-Language/phobos/pulls/1392","_links":{"self":{"href":"https://api.github.com/repos/D-Programming-Language/phobos/pulls/comments/9770403"},"html":{"href":"https://github.com/D-Programming-Language/phobos/pull/1392#discussion_r9770403"},"pull_request":{"href":"https://api.github.com/repos/D-Programming-Language/phobos/pulls/1392"}}}}