Files
externals/linux/include/ActivMiddleware/Misc/SimplePattern.h
T
2023-12-15 13:15:08 -05:00

473 lines
12 KiB
C++

/**
* @file include/ActivMiddleware/Misc/SimplePattern.h
*
* @brief Simple pattern class.
*/
#if (!defined ACTIV_MIDDLEWARE_MISC_SIMPLE_PATTERN_H)
#define ACTIV_MIDDLEWARE_MISC_SIMPLE_PATTERN_H
#include "ActivMiddleware/ActivMiddleware.h"
#include "ActivMiddleware/ActivBase/StatusCodeException.h"
#include "ActivMiddleware/CxxYZ/string_view.h"
#include "ActivMiddleware/Misc/StringView.h"
//! Activ namespace.
namespace Activ
{
/**
* @brief Simple pattern class.
*
* Parses patterns and matches strings against the pattern.
*/
class SimplePattern
{
public:
static const char ESCAPE_CHAR = '\\';
static const char NOT_CHAR = '~';
static const char NOT_START_CHAR = '(';
static const char NOT_END_CHAR = ')';
static const char WILDCARD_CHAR = '*';
/**
* @brief Part of a simple pattern.
*/
class Part
{
public:
//! Get the literal string to match against.
const String &GetString() const;
//! Is this part a wildcard?
bool IsWildcardMatch() const;
//! Is this part a not match?
bool IsNotMatch() const;
//! Reset to initial state.
void Reset();
private:
//! Default constructor.
Part();
String m_string; ///< String to match on.
bool m_isWildcardMatch; ///< Wildcard match.
bool m_isNotMatch; ///< NOT match on the string.
friend class SimplePattern;
};
/**
* @brief Default constructor.
*/
SimplePattern();
/**
* @brief Constructor.
*
* @param pattern
*
* @throw StatusCodeException if the pattern format is invalid.
*/
SimplePattern(const String &pattern);
/**
* @brief Parse a pattern string and initialize.
*
* @param pattern the string to parse. Format is A*B* Escape * with \*
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_INVALID_FORMAT
*/
StatusCode Initialize(const String &pattern);
//! Any pattern assigned?
bool IsEmpty() const;
/**
* @brief Match a string against the pattern.
*
* @param str the string to match.
* @param fromPart which part of the pattern to match from.
*
* @return true if matches.
*/
bool IsMatch(const CxxYZ::string_view str, const size_t fromPart) const;
/**
* @brief Get the original pattern string.
*
* @return Ref to the string.
*/
const String &GetPatternString() const;
/**
* @brief Get a part of the pattern.
*
* @param index index of the part of the pattern to return.
*
* @return Pattern section.
*/
const Part &GetPart(const size_t index) const;
/**
* @brief Check if the pattern has a wildcard in it.
*
* @return true if pattern has wildcard.
*/
bool IsPattern() const;
/**
* @brief Does the pattern have a literal start?
*
* The following do:
* MSFT*
* MSFT~(.Q)
*
* These don't:
* *.Q
* ~(MSFT)
*
* @return Boolean.
*/
bool HasLiteralStart() const;
/**
* @brief operator<.
*
* Performs less-than comparison on the pattern string.
*
* @param rhs to compare with.
*
* @return true if this < rhs.
*/
bool operator<(const SimplePattern &rhs) const;
/**
* @brief operator==.
*
* @param rhs to compare with.
*
* @return true if this == rhs.
*/
bool operator==(const SimplePattern &rhs) const;
/**
* @brief operator!=.
*
* @param rhs to compare with.
*
* @return true if this != rhs.
*/
bool operator!=(const SimplePattern &rhs) const;
private:
static const size_t NUMBER_OF_PARTS = 2; ///< Max number of wildcards.
String m_patternString; ///< Original string.
Part m_parts[NUMBER_OF_PARTS];
bool m_hasTrailingWildcard;
bool m_isPattern; ///< true if m_patternString is a pattern (i.e. has wildcard).
};
// ---------------------------------------------------------------------------------------------------------------------------------
inline void SimplePattern::Part::Reset()
{
m_string.clear();
m_isWildcardMatch = false;
m_isNotMatch = false;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline const String &SimplePattern::Part::GetString() const
{
return m_string;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool SimplePattern::Part::IsNotMatch() const
{
return m_isNotMatch;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool SimplePattern::Part::IsWildcardMatch() const
{
return m_isWildcardMatch;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline SimplePattern::Part::Part() :
m_isWildcardMatch(false),
m_isNotMatch(false)
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
inline SimplePattern::SimplePattern()
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline SimplePattern::SimplePattern(const String &pattern)
{
ACTIV_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, Initialize(pattern));
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline StatusCode SimplePattern::Initialize(const String &pattern)
{
m_patternString.clear();
const size_t length = pattern.length();
size_t lastPartOffset = 0, partIndex = 0;
m_isPattern = true;
if (0 == length)
return STATUS_CODE_INVALID_FORMAT;
for (size_t i = 0; i < NUMBER_OF_PARTS; ++i)
{
m_parts[i].Reset();
}
for (size_t i = 0; i < length; ++i)
{
if (partIndex >= NUMBER_OF_PARTS)
return STATUS_CODE_INVALID_FORMAT;
if (pattern[i] == WILDCARD_CHAR)
{
if ((i > 0) && (pattern[i - 1] == ESCAPE_CHAR))
{
// replace the escape char with the escaped char
*(m_parts[partIndex].m_string.end() - 1) = pattern[i];
}
else
{
// if (m_parts[partIndex].m_string.empty())
m_parts[partIndex].m_isWildcardMatch = true;
++partIndex;
lastPartOffset = i + 1;
}
}
else if (pattern[i] == NOT_CHAR)
{
if ((i > 0) && (pattern[i - 1] == ESCAPE_CHAR))
{
// replace the escape char with the escaped char
*(m_parts[partIndex].m_string.end() - 1) = pattern[i];
}
else
{
if (((i + 1) < length) && (pattern[i + 1] == NOT_START_CHAR))
{
if (!m_parts[partIndex].m_string.empty())
++partIndex;
if (partIndex >= NUMBER_OF_PARTS)
return STATUS_CODE_INVALID_FORMAT;
i += 2; // Skip not char and not start string
for ( ; ((i < length) && (pattern[i] != NOT_END_CHAR)); ++i)
{
m_parts[partIndex].m_string += pattern[i];
}
if (pattern[i] == NOT_END_CHAR)
{
m_parts[partIndex].m_isNotMatch = true;
++partIndex;
}
else
{
return STATUS_CODE_INVALID_FORMAT;
}
}
else
{
return STATUS_CODE_INVALID_FORMAT;
}
}
}
else
{
m_parts[partIndex].m_string += pattern[i];
}
}
if (0 == partIndex)
m_isPattern = false;
m_hasTrailingWildcard = (lastPartOffset == length);
m_patternString = pattern;
return STATUS_CODE_SUCCESS;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool SimplePattern::IsEmpty() const
{
return m_patternString.empty();
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool SimplePattern::IsMatch(const CxxYZ::string_view str, const size_t fromPart) const
{
if (!m_isPattern)
return (str == m_parts[0].m_string);
const size_t length = str.length();
size_t startOffset = 0, newOffset;
for (size_t i = 0; i < NUMBER_OF_PARTS; ++i)
{
const Part &currentPart = m_parts[i];
const size_t partLength = currentPart.m_string.length();
if (i < fromPart)
{
startOffset += partLength;
continue;
}
// empty match part always matches
if (!currentPart.m_isNotMatch && (0 == partLength))
continue;
// special case for last part if there wasn't a trailing * in the pattern
if ((i == NUMBER_OF_PARTS - 1) && !m_hasTrailingWildcard)
{
if (!currentPart.m_isNotMatch)
{
// if there's not enough string to match against, can't match
if ((length - startOffset) < partLength)
return false;
// match the end of the string against this part
return (0 == str.compare(length - partLength, partLength, ToStringType<CxxYZ::string_view>(currentPart.m_string)));
}
else
{
// if there's not enough string to match against, it must be a match
if ((length - startOffset) < partLength)
return true;
// if there was a * or not before this not, then compare against the last characters of the string
// otherwise compare against where we are up to. e.g.
// MSF*~(.Q): MSFT.Q(n) MSF.Q(n) MSFT.QD(y) (MSF followed by anything followed by anything other than .Q)
// MSF~(.Q) : MSFT.Q(y) MSF.Q(n) MSFT.QD(y) (MSF followed by anything other than .Q)
if ((m_parts[i - 1].m_isWildcardMatch) || (m_parts[i - 1].m_isNotMatch))
return (0 != str.compare(length - partLength, partLength, ToStringType<CxxYZ::string_view>(currentPart.m_string)));
else
return (0 != str.compare(startOffset, length - startOffset, ToStringType<CxxYZ::string_view>(currentPart.m_string)));
}
}
if (0 == i)
{
// case for matching the first part of the pattern at the start of the string
if (0 == str.compare(0, partLength, ToStringType<CxxYZ::string_view>(currentPart.m_string)))
{
if (currentPart.m_isNotMatch)
return false;
startOffset += partLength;
}
else
{
if (!currentPart.m_isNotMatch)
return false;
}
}
else
{
newOffset = str.find(ToStringType<CxxYZ::string_view>(currentPart.m_string), startOffset);
if (String::npos != newOffset)
{
if (currentPart.m_isNotMatch)
return false;
startOffset = newOffset + partLength;
}
else
{
if (!currentPart.m_isNotMatch)
return false;
}
}
}
return true;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline const String &SimplePattern::GetPatternString() const
{
return m_patternString;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline const SimplePattern::Part &SimplePattern::GetPart(const size_t index) const
{
return m_parts[index];
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool SimplePattern::IsPattern() const
{
return m_isPattern;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool SimplePattern::HasLiteralStart() const
{
return !m_parts[0].m_string.empty() && !m_parts[0].m_isNotMatch;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool SimplePattern::operator<(const SimplePattern &rhs) const
{
return (m_patternString < rhs.m_patternString);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool SimplePattern::operator==(const SimplePattern &rhs) const
{
return (m_patternString == rhs.m_patternString);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool SimplePattern::operator!=(const SimplePattern &rhs) const
{
return (m_patternString != rhs.m_patternString);
}
} // namespace Activ
#endif // (!defined ACTIV_MIDDLEWARE_MISC_SIMPLE_PATTERN_H)