From d6427addaf7de41d401dd2a871b4789022e5f7cf Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 5 Jan 2018 15:06:24 +0200 Subject: Extend find_option_prefix() to return option --- build2/utility.cxx | 40 ++++++++++++++++++++-------------------- build2/utility.hxx | 25 ++++++++++++++----------- build2/utility.ixx | 8 ++++---- 3 files changed, 38 insertions(+), 35 deletions(-) diff --git a/build2/utility.cxx b/build2/utility.cxx index 8d63059..280edd3 100644 --- a/build2/utility.cxx +++ b/build2/utility.cxx @@ -397,73 +397,73 @@ namespace build2 return false; } - bool + const string* find_option_prefix (const char* p, const lookup& l, bool ic) { - return l && find_option_prefix (p, cast (l), ic); + return l ? find_option_prefix (p, cast (l), ic) : nullptr; } - bool + const string* find_option_prefix (const char* p, const strings& strs, bool ic) { size_t n (strlen (p)); - for (const string& s: strs) + for (const string& s: reverse_iterate (strs)) if ((ic ? casecmp (s, p, n) : s.compare (0, n, p)) == 0) - return true; + return &s; - return false; + return nullptr; } - bool + const char* find_option_prefix (const char* p, const cstrings& cstrs, bool ic) { size_t n (strlen (p)); - for (const char* s: cstrs) + for (const char* s: reverse_iterate (cstrs)) if (s != nullptr && (ic ? casecmp (s, p, n) : strncmp (s, p, n)) == 0) - return true; + return s; - return false; + return nullptr; } - bool + const string* find_option_prefixes (initializer_list ps, const lookup& l, bool ic) { - return l && find_option_prefixes (ps, cast (l), ic); + return l ? find_option_prefixes (ps, cast (l), ic) : nullptr; } - bool + const string* find_option_prefixes (initializer_list ps, const strings& strs, bool ic) { - for (const string& s: strs) + for (const string& s: reverse_iterate (strs)) for (const char* p: ps) if ((ic ? casecmp (s, p, strlen (p)) : s.compare (0, strlen (p), p)) == 0) - return true; + return &s; - return false; + return nullptr; } - bool + const char* find_option_prefixes (initializer_list ps, const cstrings& cstrs, bool ic) { - for (const char* s: cstrs) + for (const char* s: reverse_iterate (cstrs)) if (s != nullptr) for (const char* p: ps) if ((ic ? casecmp (s, p, strlen (p)) : strncmp (s, p, strlen (p))) == 0) - return true; + return s; - return false; + return nullptr; } string diff --git a/build2/utility.hxx b/build2/utility.hxx index db6460c..cfdaee0 100644 --- a/build2/utility.hxx +++ b/build2/utility.hxx @@ -511,51 +511,54 @@ namespace build2 bool find_options (initializer_list, const cstrings&, bool = false); - // As above but look for an option that has the specified prefix. + // As above but look for an option that has the specified prefix. Return the + // pointer to option or NULL if not found (thus can be used as bool). + // Search backward (which is normall consistent with how options override + // each other). // template - bool + const string* find_option_prefix (const char* prefix, T&, const variable&, bool = false); template - bool + const string* find_option_prefix (const char* prefix, T&, const char*, bool = false); - bool + const string* find_option_prefix (const char* prefix, const lookup&, bool = false); - bool + const string* find_option_prefix (const char* prefix, const strings&, bool = false); - bool + const char* find_option_prefix (const char* prefix, const cstrings&, bool = false); // As above but look for several option prefixes. // template - bool + const string* find_option_prefixes (initializer_list, T&, const variable&, bool = false); template - bool + const string* find_option_prefixes (initializer_list, T&, const char*, bool = false); - bool + const string* find_option_prefixes (initializer_list, const lookup&, bool = false); - bool + const string* find_option_prefixes (initializer_list, const strings&, bool = false); - bool + const char* find_option_prefixes (initializer_list, const cstrings&, bool = false); diff --git a/build2/utility.ixx b/build2/utility.ixx index 0622e99..a726b0c 100644 --- a/build2/utility.ixx +++ b/build2/utility.ixx @@ -131,21 +131,21 @@ namespace build2 } template - inline bool + inline const string* find_option_prefix (const char* p, T& s, const variable& var, bool ic) { return find_option_prefix (p, s[var], ic); } template - inline bool + inline const string* find_option_prefix (const char* p, T& s, const char* var, bool ic) { return find_option_prefix (p, s[var], ic); } template - inline bool + inline const string* find_option_prefixes (initializer_list ps, T& s, const variable& var, @@ -155,7 +155,7 @@ namespace build2 } template - inline bool + inline const string* find_option_prefixes (initializer_list ps, T& s, const char* var, -- cgit v1.1