From 94c1bdfc898e4cad2577949345adb3e2373c692b Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 11 Aug 2020 13:17:48 +0200 Subject: Review find_option*() iterator version --- libbuild2/utility.hxx | 10 ++++++---- libbuild2/utility.ixx | 40 ++++++++++++++++++++++++++++++++-------- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/libbuild2/utility.hxx b/libbuild2/utility.hxx index 67c46d9..b747667 100644 --- a/libbuild2/utility.hxx +++ b/libbuild2/utility.hxx @@ -633,7 +633,8 @@ namespace build2 F&& get = [] (const string& s) {return s;}); // Check if a specified option is present in the variable or value. T is - // either target or scope. + // either target or scope. For the interator version use rbegin()/rend() to + // search backwards. // template bool @@ -695,8 +696,9 @@ namespace build2 // 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). + // Search backward (which is normally consistent with how options override + // each other). For the interator version use rbegin()/rend() to do the + // same. // template const string* @@ -708,7 +710,7 @@ namespace build2 template I - find_option_prefix (const char* prefix, I rbegin, I rend, bool ignore_case = false); + find_option_prefix (const char* prefix, I begin, I end, bool = false); LIBBUILD2_SYMEXPORT const string* find_option_prefix (const char* prefix, const lookup&, bool = false); diff --git a/libbuild2/utility.ixx b/libbuild2/utility.ixx index dc8b42a..9a77fae 100644 --- a/libbuild2/utility.ixx +++ b/libbuild2/utility.ixx @@ -139,12 +139,24 @@ namespace build2 return find_option (o, s[var], ic); } + inline bool + compare_option (const char* o, const char* s, bool ic) + { + return s != nullptr && (ic ? icasecmp (s, o) : strcmp (s, o)) == 0; + } + + inline bool + compare_option (const char* o, const string& s, bool ic) + { + return ic ? icasecmp (s, o) == 0 : s == o; + } + template - I + inline I find_option (const char* o, I b, I e, bool ic) { for (; b != e; ++b) - if ((ic ? icasecmp (*b, o) : strcmp (*b, o)) == 0) + if (compare_option (o, *b, ic)) return b; return e; @@ -184,17 +196,29 @@ namespace build2 return find_option_prefix (p, s[var], ic); } + inline bool + compare_option_prefix (const char* p, size_t n, const char* s, bool ic) + { + return s != nullptr && (ic ? icasecmp (s, p, n) : strncmp (s, p, n)) == 0; + } + + inline bool + compare_option_prefix (const char* p, size_t n, const string& s, bool ic) + { + return (ic ? icasecmp (s, p, n) : s.compare (0, n, p)) == 0; + } + template - I - find_option_prefix (const char* p, I rb, I re, bool ic) + inline I + find_option_prefix (const char* p, I b, I e, bool ic) { size_t n (strlen (p)); - for (; rb != re; ++rb) - if ((ic ? icasecmp (*rb, p, n) : strncmp (*rb, p, n)) == 0) - return rb; + for (; b != e; ++b) + if (compare_option_prefix (p, n, *b, ic)) + return b; - return re; + return e; } template -- cgit v1.1