233 lines
5.2 KiB
C++
233 lines
5.2 KiB
C++
/**
|
|
* @file include/ActivMiddleware/CxxYZ/regex.h
|
|
*
|
|
* @brief Header file for easing C++11 (or later) <regex> features across compilers.
|
|
*/
|
|
|
|
#if (!defined ACTIV_CXXYX_REGEX_H)
|
|
#define ACTIV_CXXYX_REGEX_H
|
|
|
|
#include "ActivMiddleware/ActivMiddleware.h"
|
|
|
|
#if (defined ACTIV_BOOST_NO_CXX11_HDR_REGEX) || \
|
|
(!defined ACTIV_CXXYZ_REGEX_USE_STD)
|
|
|
|
// For now, just use Boost everywhere. std doesn't seem to provide a way to have "." match anything and
|
|
// "[\x00-\xff]" is considered unpleasant at this time.
|
|
#include ACTIV_BOOST_INCLUDE(regex.hpp)
|
|
|
|
#define ACTIV_CXXYZ_REGEX_NAMESPACE Boost
|
|
|
|
#else
|
|
|
|
#include <regex>
|
|
|
|
#define ACTIV_CXXYZ_REGEX_NAMESPACE std
|
|
#define ACTIV_CXXYZ_REGEX_PROVIDE_BOOST_LIKE_API
|
|
|
|
#endif
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
//! CxxYZ namespace.
|
|
namespace CxxYZ
|
|
{
|
|
|
|
using ACTIV_CXXYZ_REGEX_NAMESPACE::regex;
|
|
using ACTIV_CXXYZ_REGEX_NAMESPACE::regex_match;
|
|
using ACTIV_CXXYZ_REGEX_NAMESPACE::regex_search;
|
|
using ACTIV_CXXYZ_REGEX_NAMESPACE::regex_replace;
|
|
using ACTIV_CXXYZ_REGEX_NAMESPACE::sub_match;
|
|
using ACTIV_CXXYZ_REGEX_NAMESPACE::match_results;
|
|
using ACTIV_CXXYZ_REGEX_NAMESPACE::cmatch;
|
|
using ACTIV_CXXYZ_REGEX_NAMESPACE::regex_error;
|
|
|
|
//! regex_constants namespace.
|
|
namespace regex_constants
|
|
{
|
|
|
|
using namespace ACTIV_CXXYZ_REGEX_NAMESPACE::regex_constants;
|
|
|
|
} // namespace regex_constants
|
|
|
|
} // namespace CxxYZ
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
#if (defined ACTIV_CXXYZ_REGEX_PROVIDE_BOOST_LIKE_API)
|
|
|
|
/**
|
|
* @brief Extended std::regex with some Boost::regex feautres (str(), empty(), operator<).
|
|
*
|
|
* Use CxxYZ::regex unless absolutely necessary!
|
|
*/
|
|
class Regex : public std::regex
|
|
{
|
|
public:
|
|
Regex()
|
|
{
|
|
}
|
|
|
|
explicit Regex(const char * const pStr, const std::regex::flag_type f = std::regex_constants::ECMAScript) :
|
|
std::regex(pStr, f),
|
|
m_str(pStr)
|
|
{
|
|
}
|
|
|
|
Regex(const char * const pStr, size_t strLength, const std::regex::flag_type f = std::regex_constants::ECMAScript) :
|
|
std::regex(pStr, strLength, f),
|
|
m_str(pStr)
|
|
{
|
|
}
|
|
|
|
Regex(const Regex &rhs) :
|
|
std::regex(rhs),
|
|
m_str(rhs.m_str)
|
|
{
|
|
}
|
|
|
|
Regex(Regex &&rhs) :
|
|
std::regex(static_cast<std::regex &&>(rhs)),
|
|
m_str(std::move(rhs.m_str))
|
|
{
|
|
}
|
|
|
|
explicit Regex(const String &str, const std::regex::flag_type f = std::regex_constants::ECMAScript) :
|
|
std::regex(str.begin(), str.end(), f),
|
|
m_str(str)
|
|
{
|
|
}
|
|
|
|
template<typename T>
|
|
Regex(const T first, const T last, const std::regex::flag_type f = std::regex_constants::ECMAScript) :
|
|
std::regex(first, last, f),
|
|
m_str(first, last)
|
|
{
|
|
}
|
|
|
|
Regex(std::initializer_list<char> initializerList, const std::regex::flag_type f = std::regex_constants::ECMAScript) :
|
|
std::regex(initializerList, f),
|
|
m_str(initializerList)
|
|
{
|
|
}
|
|
|
|
Regex &assign(const Regex &rhs)
|
|
{
|
|
std::regex::assign(rhs);
|
|
m_str = rhs.m_str;
|
|
|
|
return *this;
|
|
}
|
|
|
|
Regex &assign(Regex &&rhs)
|
|
{
|
|
std::regex::assign(static_cast<std::regex &&>(rhs));
|
|
m_str = std::move(rhs.m_str);
|
|
|
|
return *this;
|
|
}
|
|
|
|
Regex &assign(const char * const pStr, const std::regex::flag_type f = std::regex_constants::ECMAScript)
|
|
{
|
|
std::regex::assign(pStr, f);
|
|
m_str.assign(pStr);
|
|
|
|
return *this;
|
|
}
|
|
|
|
Regex &assign(const char * const pStr, const size_t strLength, const std::regex::flag_type f = std::regex_constants::ECMAScript)
|
|
{
|
|
std::regex::assign(pStr, strLength, f);
|
|
m_str.assign(pStr, strLength);
|
|
|
|
return *this;
|
|
}
|
|
|
|
Regex &assign(const String &str, const std::regex::flag_type f = std::regex_constants::ECMAScript)
|
|
{
|
|
std::regex::assign(str.begin(), str.end(), f);
|
|
m_str.assign(str);
|
|
|
|
return *this;
|
|
}
|
|
|
|
template<typename T>
|
|
Regex &assign(const T first, const T last, const std::regex::flag_type f = std::regex_constants::ECMAScript)
|
|
{
|
|
std::regex::assign(first, last, f);
|
|
m_str.assign(first, last);
|
|
|
|
return *this;
|
|
}
|
|
|
|
Regex &assign(std::initializer_list<char> initializerList, const std::regex::flag_type f = std::regex_constants::ECMAScript)
|
|
{
|
|
std::regex::assign(initializerList, f);
|
|
m_str.assign(initializerList);
|
|
|
|
return *this;
|
|
}
|
|
|
|
Regex &operator=(const Regex &rhs)
|
|
{
|
|
return assign(rhs);
|
|
}
|
|
|
|
Regex &operator=(Regex &&rhs)
|
|
{
|
|
return assign(std::move(rhs));
|
|
}
|
|
|
|
Regex &operator=(const char * const pStr)
|
|
{
|
|
return assign(pStr);
|
|
}
|
|
|
|
Regex &operator=(const String &str)
|
|
{
|
|
return assign(str);
|
|
}
|
|
|
|
Regex &operator=(std::initializer_list<char> initializerList)
|
|
{
|
|
return assign(initializerList);
|
|
}
|
|
|
|
//! Returns a copy of the character sequence passed to the object's constructor, or the last call to assign.
|
|
String str() const
|
|
{
|
|
return m_str;
|
|
}
|
|
|
|
//! Is the character sequence provided to the constructor or last assign empty?
|
|
bool empty() const
|
|
{
|
|
return m_str.empty();
|
|
}
|
|
|
|
//! Less than operator. Applies to the character sequence passed to the constructor or last assignment.
|
|
bool operator<(const Regex &rhs) const
|
|
{
|
|
return m_str < rhs.m_str;
|
|
}
|
|
|
|
private:
|
|
String m_str;
|
|
};
|
|
|
|
#else
|
|
|
|
typedef Boost::regex Regex;
|
|
|
|
#endif
|
|
|
|
} // namespace Activ
|
|
|
|
#undef ACTIV_CXXYZ_REGEX_NAMESPACE
|
|
#undef ACTIV_CXXYZ_REGEX_PROVIDE_BOOST_LIKE_API
|
|
|
|
#endif // (!defined ACTIV_CXXYX_REGEX_H)
|