/usr/include/leatherman/util/regex.hpp is in libleatherman-dev 1.4.0+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | /**
* @file
* Defines an abstraction for using regular expression calls.
*/
#pragma once
#include <boost/regex.hpp>
#include <boost/lexical_cast.hpp>
namespace leatherman { namespace util {
/**
* Helper function for resolving variadic arguments to re_search.
* @tparam Text The type of the text to search.
* @param txt The text to search.
* @param what The pattern to search the text with.
* @param depth The current argument depth.
* @return Returns true if the match group was found or false if it was not.
*/
template <typename Text>
inline bool re_search_helper(Text &txt, const boost::smatch &what, size_t depth)
{
return true;
}
/**
* Helper function for resolving variadic arguments to re_search.
* @tparam Text The type of the text to search.
* @tparam Arg The type of the current match group argument.
* @tparam Args The variadic types of the remaining match group arguments.
* @param txt The text to search.
* @param what The pattern to search the text with.
* @param depth The current argument depth.
* @param arg The current match group argument.
* @param args The remaining match group arguments.
* @return Returns true if the match group was found or false if it was not.
*/
template <typename Text, typename Arg, typename... Args>
inline bool re_search_helper(Text const& txt, const boost::smatch &what, size_t depth, Arg arg, Args&&... args)
{
if (depth >= what.size()) {
return false;
}
// If the match was optional and unmatched, skip it and leave the variable uninitialized.
if (what[depth].matched) {
try {
using ArgType = typename std::pointer_traits<Arg>::element_type;
auto val = boost::lexical_cast<ArgType>(what[depth]);
*arg = val;
} catch (const boost::bad_lexical_cast &e) {
return false;
}
}
return re_search_helper(txt, what, depth+1, std::forward<Args>(args)...);
}
/**
* Searches the given text for the given pattern. Optional variadic arguments return matched
* subgroups. If a subgroup is optional and unmatched, leaves the argument uninitialized.
* @tparam Text The type of the text.
* @tparam Args The variadic type of the match group arguments.
* @param txt The text to search.
* @param pattern The pattern to search the text with.
* @param args The returned match groups.
* @return Returns true if the text matches the given pattern or false if it does not.
*/
template <typename Text, typename... Args>
inline bool re_search(Text const& txt, boost::regex const& pattern, Args&&... args)
{
boost::smatch what;
if (!boost::regex_search(txt, what, pattern)) {
return false;
}
return re_search_helper(txt, what, 1, std::forward<Args>(args)...);
}
}} // namespace leatherman::util
|