diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2019-10-19 17:11:24 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2019-10-19 17:11:24 +0200 |
commit | 5f27918e56f7cb89226556836e35a1a64ba5cd99 (patch) | |
tree | 5a6fd47065c1bf9c418fc7cea72da67a565e50f7 | |
parent | 9d90382e1282045638ede144b89c7e94f4739466 (diff) |
Add find_stem() utility function
-rw-r--r-- | libbuild2/cc/guess.cxx | 27 | ||||
-rw-r--r-- | libbuild2/utility.hxx | 14 | ||||
-rw-r--r-- | libbuild2/utility.ixx | 21 |
3 files changed, 39 insertions, 23 deletions
diff --git a/libbuild2/cc/guess.cxx b/libbuild2/cc/guess.cxx index f40bb88..b18c516 100644 --- a/libbuild2/cc/guess.cxx +++ b/libbuild2/cc/guess.cxx @@ -306,38 +306,19 @@ namespace build2 size_t s_p (path::traits_type::find_leaf (s)); size_t s_n (s.size ()); - // Name separator characters (e.g., '-' in 'g++-4.8'). - // - auto sep = [] (char c) -> bool - { - return c == '-' || c == '_' || c == '.'; - }; - - auto stem = [&sep, &s, s_p, s_n] (const char* x) -> size_t - { - size_t m (strlen (x)); - size_t p (s.find (x, s_p, m)); - - return (p != string::npos && - ( p == s_p || sep (s[p - 1])) && // Separated beginning. - ((p + m) == s_n || sep (s[p + m]))) // Separated end. - ? p - : string::npos; - }; - using type = compiler_type; // If the user specified the compiler id, then only check the stem for // that compiler. // - auto check = [&xi, &stem] (type t, - const char* s, - const char* v = nullptr) + auto check = [&xi, &s, s_p, s_n] (type t, + const char* stem, + const char* v = nullptr) -> optional<pre_guess_result> { if (!xi || (xi->type == t && (v == nullptr || xi->variant == v))) { - size_t p (stem (s)); + size_t p (find_stem (s, s_p, s_n, stem)); if (p != string::npos) { diff --git a/libbuild2/utility.hxx b/libbuild2/utility.hxx index 0422786..536898e 100644 --- a/libbuild2/utility.hxx +++ b/libbuild2/utility.hxx @@ -686,6 +686,20 @@ namespace build2 const cstrings&, bool = false); + // Find in the string the stem separated from other characters with the + // specified separators or begin/end of the string. Return the stem's + // position or npos if not found. + // + size_t + find_stem (const string&, size_t pos, size_t n, + const char* stem, const char* seps = "-_."); + + inline size_t + find_stem (const string& s, const char* stem, const char* seps = "-_.") + { + return find_stem (s, 0, s.size (), stem, seps); + } + // Apply the specified substitution (stem) to a '*'-pattern. If pattern is // NULL or empty, then return the stem itself. Assume the pattern is valid, // i.e., contains a single '*' character. diff --git a/libbuild2/utility.ixx b/libbuild2/utility.ixx index 2846756..514d4ee 100644 --- a/libbuild2/utility.ixx +++ b/libbuild2/utility.ixx @@ -2,6 +2,8 @@ // copyright : Copyright (c) 2014-2019 Code Synthesis Ltd // license : MIT; see accompanying LICENSE file +#include <cstring> // strlen() strchr() + namespace build2 { inline void @@ -152,4 +154,23 @@ namespace build2 { return find_option_prefixes (ps, s[var], ic); } + + inline size_t + find_stem (const string& s, size_t s_p, size_t s_n, + const char* stem, const char* seps) + { + auto sep = [seps] (char c) -> bool + { + return strchr (seps, c) != nullptr; + }; + + size_t m (strlen (stem)); + size_t p (s.find (stem, s_p, m)); + + return (p != string::npos && + ( p == s_p || sep (s[p - 1])) && // Separated beginning. + ((p + m) == s_n || sep (s[p + m]))) // Separated end. + ? p + : string::npos; + } } |