Commit 777d33bc authored by Joseph Noir's avatar Joseph Noir

Add function to find option by long name

parent 53977143
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "caf/fwd.hpp" #include "caf/fwd.hpp"
#include "caf/string_view.hpp" #include "caf/string_view.hpp"
#include "caf/string_algorithms.hpp"
namespace caf { namespace caf {
...@@ -123,4 +124,35 @@ private: ...@@ -123,4 +124,35 @@ private:
mutable void* value_; mutable void* value_;
}; };
/// Finds `config_option` string with a matching long name in (`first`, `last`],
/// where each entry is a pointer to a string. Returns a `ForwardIterator` to
/// the match and a `caf::string_view` of the option value if the entry is found
/// and a `ForwardIterator` to `last` with an empty `string_view` otherwise.
template <class ForwardIterator, class Sentinel>
std::pair<ForwardIterator, string_view>
find_by_long_name(const config_option& x, ForwardIterator first, Sentinel last) {
auto long_name = x.long_name();
for (; first != last; ++first) {
string_view str{*first};
// Make sure this is a long option starting with "--".
if (!starts_with(str, "--"))
continue;
str.remove_prefix(2);
// Skip optional "caf#" prefix.
if (starts_with(str, "caf#"))
str.remove_prefix(4);
// Make sure we are dealing with the right key.
if (!starts_with(str, long_name))
continue;
// Make sure the key is followed by an assignment.
str.remove_prefix(long_name.size());
if (!starts_with(str, "="))
continue;
// Remove leading '=' and return the value.
str.remove_prefix(1);
return {first, str};
}
return {first, string_view{}};
}
} // namespace caf } // namespace caf
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment