From patchwork Fri Sep 12 22:09:50 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roland McGrath X-Patchwork-Id: 2825 Received: (qmail 21510 invoked by alias); 12 Sep 2014 22:09:55 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 21442 invoked by uid 89); 12 Sep 2014 22:09:54 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.2 required=5.0 tests=AWL, BAYES_00 autolearn=no version=3.3.2 X-HELO: topped-with-meat.com MIME-Version: 1.0 From: Roland McGrath To: "GNU C. Library" Subject: [COMMITTED PATCH] Don't use a nested function in rpmatch. Message-Id: <20140912220950.533C92C3977@topped-with-meat.com> Date: Fri, 12 Sep 2014 15:09:50 -0700 (PDT) X-CMAE-Score: 0 X-CMAE-Analysis: v=2.1 cv=SvUDtp+0 c=1 sm=1 tr=0 a=WkljmVdYkabdwxfqvArNOQ==:117 a=14OXPxybAAAA:8 a=LYT2gFN2lakA:10 a=HU8KVcL6yqEA:10 a=Z6MIti7PxpgA:10 a=kj9zAlcOel0A:10 a=hOe2yjtxAAAA:8 a=iIDszPzm8RQ8EEgWWjAA:9 a=CjuIK1q_8ugA:10 In this case, the generated code is actually a bit better, and the local state carried into the helper function is so trivial that it makes no difference one way or the other to the maintainability of the source. Thanks, Roland 2014-09-12 Roland McGrath * stdlib/rpmatch.c (try): New function, broken out of ... (rpmatch): ... local function here. Also, prototypify definition. --- a/stdlib/rpmatch.c +++ b/stdlib/rpmatch.c @@ -22,42 +22,40 @@ #include -int -rpmatch (response) - const char *response; +/* Match against one of the response patterns, compiling the pattern + first if necessary. */ +static int +try (const char *response, + const int tag, const int match, const int nomatch, + const char **lastp, regex_t *re) { - /* Match against one of the response patterns, compiling the pattern - first if necessary. */ - auto int try (const int tag, const int match, const int nomatch, - const char **lastp, regex_t *re); - - int try (const int tag, const int match, const int nomatch, - const char **lastp, regex_t *re) + const char *pattern = nl_langinfo (tag); + if (pattern != *lastp) { - const char *pattern = nl_langinfo (tag); - if (pattern != *lastp) - { - /* The pattern has changed. */ - if (*lastp) - { - /* Free the old compiled pattern. */ - __regfree (re); - *lastp = NULL; - } - /* Compile the pattern and cache it for future runs. */ - if (__regcomp (re, pattern, REG_EXTENDED) != 0) - return -1; - *lastp = pattern; - } - - /* Try the pattern. */ - return __regexec (re, response, 0, NULL, 0) == 0 ? match : nomatch; + /* The pattern has changed. */ + if (*lastp != NULL) + { + /* Free the old compiled pattern. */ + __regfree (re); + *lastp = NULL; + } + /* Compile the pattern and cache it for future runs. */ + if (__regcomp (re, pattern, REG_EXTENDED) != 0) + return -1; + *lastp = pattern; } + /* Try the pattern. */ + return __regexec (re, response, 0, NULL, 0) == 0 ? match : nomatch; +} + +int +rpmatch (const char *response) +{ /* We cache the response patterns and compiled regexps here. */ static const char *yesexpr, *noexpr; static regex_t yesre, nore; - return (try (YESEXPR, 1, 0, &yesexpr, &yesre) ?: - try (NOEXPR, 0, -1, &noexpr, &nore)); + return (try (response, YESEXPR, 1, 0, &yesexpr, &yesre) ?: + try (response, NOEXPR, 0, -1, &noexpr, &nore)); }