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 /libbuild2/utility.ixx | |
parent | 9d90382e1282045638ede144b89c7e94f4739466 (diff) |
Add find_stem() utility function
Diffstat (limited to 'libbuild2/utility.ixx')
-rw-r--r-- | libbuild2/utility.ixx | 21 |
1 files changed, 21 insertions, 0 deletions
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; + } } |