From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <33QuwXggKCrAWfheSYTQWeeWbU.SecbYRQRYWQYbiekhSUmQhU.ehW@flex--gprocida.bounces.google.com> Received: from mail-qv1-xf49.google.com (mail-qv1-xf49.google.com [IPv6:2607:f8b0:4864:20::f49]) by sourceware.org (Postfix) with ESMTPS id 5AE983885C03 for ; Mon, 4 May 2020 12:34:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 5AE983885C03 Received: by mail-qv1-xf49.google.com with SMTP id ce16so16968899qvb.15 for ; Mon, 04 May 2020 05:34:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=IQZrkZgO4+SFyaQSy5VQ4GTHvwXA/JGFR1iUBrwst7Q=; b=D1GTBgtXXlMg9OIHVr3OYnsrEcBPddWSIbBBGqta03xVL8cidJumuFVDTbt1+DvDa/ xbVHvX9WoYyojbtWDw6n7AhyTVL12v5QI7uBx+sblXo6XBZf1ce6tkytkAUg2mAQbxCU 5oVhHuQBBqa6uv8YpFo5at9cwfCCi8EooYYkauueGY0MpgY4k4qiPub5w5j2ciWwMg7j KsWV+gbvpL+Ng+PQnL4+EbTDmwdnRDVOXX5wywTZP9KVwSxaugjjPZmAt5jt/TvVxvB2 BJV9BOCs+C5oT5Gr6zhd8XLZm9HP70flettNHcgzfH+ptTWEsNakP5C6Bh3JMoIwij7n wNug== X-Gm-Message-State: AGi0PuYr/3sJtRyBtLcAcfDjyi+gvW5ToATaeMO8rj5g/+q0VF+/gzkO /ZE/n1AlbXJ5hED1UIXVS8niCN0uyaf+7yFGQgUaHor3b6BiB8dbyOmo1CFsYd7xfMNhc+Sxsfr CpWoUZX7ezs3WvR/FmOgim5Pm9DnzLdzVuhmmA2a6KboLEayE1+Y9h7r1pGDd1LhgKm8SsDc= X-Google-Smtp-Source: APiQypKgfDNMNQuCNu1017n8XbjzMpdgBM62dVnyi0jq/LswqcAjETen8QNLymmhOc8yq4UNPS15NMYJVWaq8Q== X-Received: by 2002:a0c:a986:: with SMTP id a6mr15416362qvb.79.1588595677787; Mon, 04 May 2020 05:34:37 -0700 (PDT) Date: Mon, 4 May 2020 13:34:07 +0100 In-Reply-To: <20200504123416.243214-1-gprocida@google.com> Message-Id: <20200504123416.243214-7-gprocida@google.com> Mime-Version: 1.0 References: <20200424092132.150547-1-gprocida@google.com> <20200504123416.243214-1-gprocida@google.com> X-Mailer: git-send-email 2.26.2.526.g744177e7f7-goog Subject: [PATCH v4 06/15] Add POSIX regex wrapper functions. From: Giuliano Procida To: libabigail@sourceware.org Cc: dodji@seketeli.org, kernel-team@android.com, gprocida@google.com, maennich@google.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-33.4 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libabigail@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libabigail mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 04 May 2020 12:34:40 -0000 libabigail code uses the POSIX regex library consistently: - compile std::string to regex, with the flag REG_EXTENDED - store regex using a shared pointer wrapper - check match of regex against std::string All the C string / std::string logic and so on is repeated at every call site. This patch introduces wrapper functions to take care of this logic. There are no behavioural changes. * include/abg-regex.h (compile): Declare new function. (match): Declare new function. * src/abg-regex.cc (compile): Add new function wrapping regcomp. (match): Add new function wrapping regexec. Reviewed-by: Matthias Maennich Signed-off-by: Giuliano Procida --- include/abg-regex.h | 6 ++++++ src/abg-regex.cc | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/include/abg-regex.h b/include/abg-regex.h index 164083cc..7ab9fc08 100644 --- a/include/abg-regex.h +++ b/include/abg-regex.h @@ -71,6 +71,12 @@ operator<<(std::ostream& os, const escape& esc); std::string generate_from_strings(const std::vector& strs); +regex_t_sptr +compile(const std::string& str); + +bool +match(const regex_t_sptr& r, const std::string& str); + }// end namespace regex namespace sptr_utils diff --git a/src/abg-regex.cc b/src/abg-regex.cc index a12ecc88..cd7c41ce 100644 --- a/src/abg-regex.cc +++ b/src/abg-regex.cc @@ -23,12 +23,22 @@ /// Some specialization for shared pointer utility templates. /// +#include "config.h" + #include #include +#include "abg-internal.h" + +// +ABG_BEGIN_EXPORT_DECLARATIONS + #include "abg-regex.h" #include "abg-sptr-utils.h" +ABG_END_EXPORT_DECLARATIONS +// + namespace abigail { @@ -104,6 +114,36 @@ generate_from_strings(const std::vector& strs) return os.str(); } +/// Compile a regex from a string. +/// +/// The result is held in a shared pointer. This will be null if regex +/// compilation fails. +/// +/// @param str the string representation of the regex. +/// +/// @return shared pointer holder of a compiled regex object. +regex_t_sptr +compile(const std::string& str) +{ + regex_t_sptr r = sptr_utils::build_sptr(new regex_t); + if (regcomp(r.get(), str.c_str(), REG_EXTENDED)) + r.reset(); + return r; +} + +/// See if a string matches a regex. +/// +/// @param r a shared pointer holder of a compiled regex object. +/// +/// @param str a string. +/// +/// @return whether there was a match. +bool +match(const regex_t_sptr& r, const std::string& str) +{ + return !regexec(r.get(), str.c_str(), 0, NULL, 0); +} + }//end namespace regex }//end namespace abigail -- 2.26.2.526.g744177e7f7-goog