X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fmoof%2Fstring.hh;fp=src%2Fmoof%2Fstring.hh;h=dd9d7918deb13ef9e27feab36423a66154db410d;hp=d666aa1eba748037ddb79100d384c7d375dfb877;hb=6f1b787a10d8ab1a3117a4b8c004dd2d90599608;hpb=c143f7e806766a73cd69dc6e084e977641019ce6 diff --git a/src/moof/string.hh b/src/moof/string.hh index d666aa1..dd9d791 100644 --- a/src/moof/string.hh +++ b/src/moof/string.hh @@ -14,11 +14,13 @@ /** * \file string.hh - * Functions related to string manipulations. + * Functions and classes related to string manipulation. */ #include +#include + namespace moof { @@ -42,8 +44,152 @@ wstring multi_to_wide(const string& multi); string wide_to_multi(const wstring& wide); -} // namespace moof +/** + * Class exposing the pattern-matching and substitution methods used in + * Lua. + */ +class regex : public boost::noncopyable +{ +public: + + /** + * Construct a regex object. + */ + regex() {} + + /** + * Construct a regex object with a pattern. + * \param pattern The pattern. + */ + regex(const string& pattern); + + /** + * Construct a regex object with a pattern and source to match. + * \param pattern The pattern. + * \param source The source string. + */ + regex(const string& pattern, const string& source); + + /** + * Deconstruct the regex. + */ + ~regex(); + + + /** + * Get the regex pattern. + */ + string pattern() const; + + /** + * Set the regex pattern. + */ + void pattern(const string& pattern); + + + /** + * Match a string against the pattern iteratively. + * \param source The source string. + */ + void match(const string& source); + + /** + * Get the next match. If the pattern contains captures, this version + * will only get the first capture. + * \param match Reference to a string to be assigned the match. + * \return True if there was a match to get, false otherwise. + */ + bool get(string& match); + + /** + * Get the next match. Use this version if the pattern contains more + * than one capture to get all of the captures. + * \param captures Reference to a vector of strings to hold the result. + * \return True if there was a match to get, false otherwise. + */ + bool get(std::vector& captures); + + + /** + * Match a string against a pattern all at one time. + * \param pattern The pattern. + * \param source The source string. + * \param position The index of the first character of source to match. + * \return The match. + */ + static string match(const string& pattern, + const string& source, + int position = 0) + { + string match; + regex::match(match, pattern, source, position); + return match; + } + + /** + * Match a string against a pattern all at one time. + * \param match A reference to a string to be assigned the match. + * \param pattern The pattern. + * \param source The source string. + * \param position The index of the first character of source to match. + * \return True if a match was made, false otherwise. + */ + static bool match(string& match, + const string& pattern, + const string& source, + int position = 0); + + /** + * Match a string against a pattern all at one time. If the pattern + * contains captures, the resulting vector will contain all of the + * captures. + * \param captures A reference to a vector of strings to contain the + * result. + * \param pattern The pattern. + * \param source The source string. + * \param position The index of the first character of source to match. + * \return True if a match was made, false otherwise. + */ + static bool match(std::vector& captures, + const string& pattern, + const string& source, + int position = 0); + + + /** + * Substitute a string using a pattern and replacement string. + * \param pattern The pattern. + * \param source The source string. + * \param replacement The replacement string; may also contain + * references to captures. + * \return The string with any substitutions made. + */ + static string sub(const string& pattern, + const string& source, + const string& replacement) + { + string substitution; + regex::sub(substitution, pattern, source, replacement); + return substitution; + } + + /** + * Substitute a string using a pattern and replacement string. + * \param substitution A reference to a string to contain the result. + * \param pattern The pattern. + * \param source The source string. + * \param replacement The replacement string; may also contain + * references to captures. + * \return The number of substitutions made. + */ + static int sub(string& substitution, + const string& pattern, + const string& source, + const string& replacement); +}; + +} // namespace moof #endif // _MOOF_STRINGTOOLS_HH_