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: 388806 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 8FD3614010C for ; Sat, 13 Sep 2014 08:10:03 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:mime-version:content-type :content-transfer-encoding:from:to:subject:message-id:date; q= dns; s=default; b=YtwKSpU501FDTkoZdh9BYTSMZASyi5Fc7JfmJx9kl4ae8I SMRDMszBkrX3UVHA5a+NTWEqa53afyCjM4mo7sL2Pt8EphcH0RsbjhQoam2dm2Vz /p23dlqaipqyywiY3mnS61ipGHl+XIQn5FJgKUszM/EKfFZ8ja/reOJRLkZN0= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:mime-version:content-type :content-transfer-encoding:from:to:subject:message-id:date; s= default; bh=bRrcw4/ZQgcp7XgYWaYen6FR9V8=; b=wHQXrDUQbMRjlLk+kboD 6d+CMX15LrCnH4s018ph+EEulnE/i7ccjBjo2ZoH1Kdh8XtfIS/Kmfx4vaJvErm4 8liNDKItvcACNILrf5sotmapdviVGl4llsSPbA7EWy9w0DvSCLbUuX/AVYDH1JU6 1PdX6GIeM0r0lxAjnCa8CWc= 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)); }