From 8d1e74202626993f8966ec09731f521f24024a7c Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sun, 10 Nov 2013 10:05:24 +0000 Subject: - custom bkstring class removed - code cleaning git-svn-id: http://svn.miranda-ng.org/main/trunk@6852 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/SmileyAdd/src/regexp/WCPattern.h | 1196 +++++++++++++++--------------- 1 file changed, 598 insertions(+), 598 deletions(-) (limited to 'plugins/SmileyAdd/src/regexp/WCPattern.h') diff --git a/plugins/SmileyAdd/src/regexp/WCPattern.h b/plugins/SmileyAdd/src/regexp/WCPattern.h index 143eff7eb7..e3455e8b31 100644 --- a/plugins/SmileyAdd/src/regexp/WCPattern.h +++ b/plugins/SmileyAdd/src/regexp/WCPattern.h @@ -771,7 +771,7 @@ class NFAQuantifierUNode;

Backslashes, escapes, and quoting

-

The backslash character ((wchar_t)'\') serves to introduce escaped +

The backslash character ('\') serves to introduce escaped constructs, as defined in the table above, as well as to quote characters that otherwise would be interpreted as unescaped constructs. Thus the expression \\ matches a single backslash and \{ matches a @@ -963,691 +963,691 @@ class NFAQuantifierUNode; */ class WCPattern { - friend class WCMatcher; - friend class NFAUNode; - friend class NFAQuantifierUNode; - private: - /** - This constructor should not be called directly. Those wishing to use the - WCPattern class should instead use the {@link compile compile} method. - - @param rhs The pattern to compile - @memo Creates a new pattern from the regular expression in rhs. - */ - WCPattern(const bkstring & rhs); - protected: - /** - This currently is not used, so don't try to do anything with it. - @memo Holds all the compiled patterns for quick access. - */ - static std::map compiledWCPatterns; - /** - Holds all of the registered patterns as strings. Due to certain problems - with compilation of patterns, especially with capturing groups, this seemed - to be the best way to do it. - */ - static std::map > registeredWCPatterns; - protected: - /** - Holds all the NFA nodes used. This makes deletion of a pattern, as well as - clean-up from an unsuccessful compile much easier and faster. - */ - std::map nodes; - /** - Used when methods like split are called. The matcher class uses a lot of - dynamic memeory, so having an instance increases speedup of certain - operations. - */ - WCMatcher * matcher; - /** - The front node of the NFA. - */ - NFAUNode * head; - /** - The actual regular expression we rerpesent - */ - bkstring pattern; - /** - Flag used during compilation. Once the pattern is successfully compiled, - error is no longer used. - */ - bool error; - /** - Used during compilation to keep track of the current index into - {@link pattern pattern}. Once the pattern is successfully - compiled, error is no longer used. - */ - int curInd; - /** - The number of capture groups this contains. - */ - int groupCount; - /** - The number of non-capture groups this contains. - */ - int nonCapGroupCount; - /** - The flags specified when this was compiled. - */ - unsigned long flags; - protected: - /** - Raises an error during compilation. Compilation will cease at that point - and compile will return NULL. - */ - void raiseError(); - /** - Convenience function for registering a node in nodes. - @param node The node to register - @return The registered node - */ - NFAUNode * registerNode(NFAUNode * node); - - /** - Calculates the union of two strings. This function will first sort the - strings and then use a simple selection algorithm to find the union. - @param s1 The first "class" to union - @param s2 The second "class" to union - @return A new string containing all unique characters. Each character - must have appeared in one or both of s1 and - s2. - */ - bkstring classUnion (bkstring s1, bkstring s2) const; - /** - Calculates the intersection of two strings. This function will first sort - the strings and then use a simple selection algorithm to find the - intersection. - @param s1 The first "class" to intersect - @param s2 The second "class" to intersect - @return A new string containing all unique characters. Each character - must have appeared both s1 and s2. - */ - bkstring classIntersect (bkstring s1, bkstring s2) const; - /** - Calculates the negation of a string. The negation is the set of all - characters between \x00 and \xFF not - contained in s1. - @param s1 The "class" to be negated. - @param s2 The second "class" to intersect - @return A new string containing all unique characters. Each character - must have appeared both s1 and s2. - */ - bkstring classNegate (bkstring s1) const; - /** - Creates a new "class" representing the range from low thru - hi. This function will wrap if low > - hi. This is a feature, not a buf. Sometimes it is useful - to be able to say [\x70-\x10] instead of [\x70-\x7F\x00-\x10]. - @param low The beginning character - @param hi The ending character - @return A new string containing all the characters from low thru hi. - */ - bkstring classCreateRange(wchar_t low, wchar_t hi) const; - - /** - Extracts a decimal number from the substring of member-variable - {@link pattern pattern} starting at start and - ending at end. - @param start The starting index in {@link pattern pattern} - @param end The last index in {@link pattern pattern} - @return The decimal number in {@link pattern pattern} - */ - int getInt(int start, int end); - /** - Parses a {n,m} string out of the member-variable - {@link pattern pattern} stores the result in sNum - and eNum. - @param sNum Output parameter. The minimum number of matches required - by the curly quantifier are stored here. - @param eNum Output parameter. The maximum number of matches allowed - by the curly quantifier are stored here. - @return Success/Failure. Fails when the curly does not have the proper - syntax - */ - bool quantifyCurly(int & sNum, int & eNum); - /** - Tries to quantify the currently parsed group. If the group being parsed - is indeed quantified in the member-variable - {@link pattern pattern}, then the NFA is modified accordingly. - @param start The starting node of the current group being parsed - @param stop The ending node of the current group being parsed - @param gn The group number of the current group being parsed - @return The node representing the starting node of the group. If the - group becomes quantified, then this node is not necessarily - a GroupHead node. - */ - NFAUNode * quantifyGroup(NFAUNode * start, NFAUNode * stop, const int gn); - - /** - Tries to quantify the last parsed expression. If the character was indeed - quantified, then the NFA is modified accordingly. - @param newNode The recently created expression node - @return The node representing the last parsed expression. If the - expression was quantified, return value != newNode - */ - NFAUNode * quantify(NFAUNode * newNode); - /** - Parses the current class being examined in - {@link pattern pattern}. - @return A string of unique characters contained in the current class being - parsed - */ - bkstring parseClass(); - /** - Parses the current POSIX class being examined in - {@link pattern pattern}. - @return A string of unique characters representing the POSIX class being - parsed - */ - bkstring parsePosix(); - /** - Returns a string containing the octal character being parsed - @return The string contained the octal value being parsed - */ - bkstring parseOctal(); - /** - Returns a string containing the hex character being parsed - @return The string contained the hex value being parsed - */ - bkstring parseHex(); - /** - Returns a new node representing the back reference being parsed - @return The new node representing the back reference being parsed - */ - NFAUNode * parseBackref(); - /** - Parses the escape sequence currently being examined. Determines if the - escape sequence is a class, a single character, or the beginning of a - quotation sequence. - @param inv Output parameter. Whether or not to invert the returned class - @param quo Output parameter. Whether or not this sequence starts a - quotation. - @return The characters represented by the class - */ - bkstring parseEscape(bool & inv, bool & quo); - /** - Parses a supposed registered pattern currently under compilation. If the - sequence of characters does point to a registered pattern, then the - registered pattern is appended to *end. The registered pattern - is parsed with the current compilation flags. - @param end The ending node of the thus-far compiled pattern - @return The new end node of the current pattern - */ - NFAUNode * parseRegisteredWCPattern(NFAUNode ** end); - /** - Parses a lookbehind expression. Appends the necessary nodes - *end. - @param pos Positive or negative look behind - @param end The ending node of the current pattern - @return The new end node of the current pattern - */ - NFAUNode * parseBehind(const bool pos, NFAUNode ** end); - /** - Parses the current expression and tacks on nodes until a \E is found. - @return The end of the current pattern - */ - NFAUNode * parseQuote(); - /** - Parses {@link pattern pattern}. This function is called - recursively when an or (|) or a group is encountered. - @param inParen Are we currently parsing inside a group - @param inOr Are we currently parsing one side of an or (|) - @param end The end of the current expression - @return The starting node of the NFA constructed from this parse - */ - NFAUNode * parse(const bool inParen = 0, const bool inOr = 0, NFAUNode ** end = NULL); - public: - /// We should match regardless of case - const static unsigned long CASE_INSENSITIVE; - /// We are implicitly quoted - const static unsigned long LITERAL; - /// @memo We should treat a . as [\x00-\x7F] - const static unsigned long DOT_MATCHES_ALL; - /** ^ and $ should anchor to the beginning and - ending of lines, not all input - */ - const static unsigned long MULTILINE_MATCHING; - /** When enabled, only instances of \n are recognized as - line terminators - */ - const static unsigned long UNIX_LINE_MODE; - /// The absolute minimum number of matches a quantifier can match (0) - const static int MIN_QMATCH; - /// The absolute maximum number of matches a quantifier can match (0x7FFFFFFF) - const static int MAX_QMATCH; - public: - /** - Call this function to compile a regular expression into a - WCPattern object. Special values can be assigned to - mode when certain non-standard behaviors are expected from - the WCPattern object. - @param pattern The regular expression to compile - @param mode A bitwise or of flags signalling what special behaviors are - wanted from this WCPattern object - @return If successful, compile returns a WCPattern - pointer. Upon failure, compile returns - NULL - */ - static WCPattern * compile (const bkstring & pattern, - const unsigned long mode = 0); - /** - Dont use this function. This function will compile a pattern, and cache - the result. This will eventually be used as an optimization when people - just want to call static methods using the same pattern over and over - instead of first compiling the pattern and then using the compiled - instance for matching. - @param pattern The regular expression to compile - @param mode A bitwise or of flags signalling what special behaviors are - wanted from this WCPattern object - @return If successful, compileAndKeep returns a - WCPattern pointer. Upon failure, compile - returns NULL. - */ - static WCPattern * compileAndKeep (const bkstring & pattern, - const unsigned long mode = 0); - - /** - Searches through replace and replaces all substrings matched - by pattern with str. str may - contain backreferences (e.g. \1) to capture groups. A typical - invocation looks like: -

- - WCPattern::replace(L"(a+)b(c+)", L"abcccbbabcbabc", L"\\2b\\1"); - -

- which would replace abcccbbabcbabc with - cccbabbcbabcba. - @param pattern The regular expression - @param str The replacement text - @param replacementText The string in which to perform replacements - @param mode The special mode requested of the WCPattern - during the replacement process - @return The text with the replacement string substituted where necessary - */ - static bkstring replace (const bkstring & pattern, - const bkstring & str, - const bkstring & replacementText, - const unsigned long mode = 0); - - /** - Splits the specified string over occurrences of the specified pattern. - Empty strings can be optionally ignored. The number of strings returned is - configurable. A typical invocation looks like: -

- - bkstring str(strSize, 0);
- FILE * fp = fopen(fileName, "r");
- fread((char*)str.data(), strSize * 2, 1, fp);
- fclose(fp);
-
- std::vector<bkstring> lines = WCPattern::split(L"[\r\n]+", str, true);
-
-
- - @param pattern The regular expression - @param replace The string to split - @param keepEmptys Whether or not to keep empty strings - @param limit The maximum number of splits to make - @param mode The special mode requested of the WCPattern - during the split process - @return All substrings of str split across pattern. - */ - static std::vector split (const bkstring & pattern, - const bkstring & str, - const bool keepEmptys = 0, - const unsigned long limit = 0, - const unsigned long mode = 0); - - /** - Finds all the instances of the specified pattern within the string. You - should be careful to only pass patterns with a minimum length of one. For - example, the pattern a* can be matched by an empty string, so - instead you should pass a+ since at least one character must - be matched. A typical invocation of findAll looks like: -

- - std::vector<td::string> numbers = WCPattern::findAll(L"\\d+", string); - -

- - @param pattern The pattern for which to search - @param str The string to search - @param mode The special mode requested of the WCPattern - during the find process - @return All instances of pattern in str - */ - static std::vector findAll (const bkstring & pattern, - const bkstring & str, - const unsigned long mode = 0); - - /** - Determines if an entire string matches the specified pattern - - @param pattern The pattern for to match - @param str The string to match - @param mode The special mode requested of the WCPattern - during the replacement process - @return True if str is recognized by pattern - */ - static bool matches (const bkstring & pattern, - const bkstring & str, - const unsigned long mode = 0); - - /** - Registers a pattern under a specific name for use in later compilations. - A typical invocation and later use looks like: -

- - WCPattern::registerWCPattern(L"ip", L"(?:\\d{1,3}\\.){3}\\d{1,3}");
- WCPattern * p1 = WCPattern::compile(L"{ip}:\\d+");
- WCPattern * p2 = WCPattern::compile(L"Connection from ({ip}) on port \\d+");
-
-

- Multiple calls to registerWCPattern with the same - name will result in the pattern getting overwritten. - - @param name The name to give to the pattern - @param pattern The pattern to register - @param mode Any special flags to use when compiling pattern - @return Success/Failure. Fails only if pattern has invalid - syntax - */ - static bool registerWCPattern(const bkstring & name, - const bkstring & pattern, - const unsigned long mode = 0); - - /** - Clears the pattern registry - */ - static void unregisterWCPatterns(); - /** - Don't use - */ - static void clearWCPatternCache(); - - /** - Searches through a string for the nth match of the - given pattern in the string. Match indeces start at zero, not one. - A typical invocation looks like this: -

- - std::pair<bkstring, int> match = WCPattern::findNthMatch(L"\\d{1,3}", L"192.168.1.101:22", 1);
- wprintf(L"%s %i\n", match.first.c_str(), match.second);
-
- Output: 168 4
-
- - @param pattern The pattern for which to search - @param str The string to search - @param matchNum Which match to find - @param mode Any special flags to use during the matching process - @return A string and an integer. The string is the string matched. The - integer is the starting location of the matched string in - str. You can check for success/failure by making sure - that the integer returned is greater than or equal to zero. - */ - static std::pair findNthMatch (const bkstring & pattern, - const bkstring & str, - const int matchNum, - const unsigned long mode = 0); - public: - /** - Deletes all NFA nodes allocated during compilation - */ - ~WCPattern(); - - bkstring replace (const bkstring & str, - const bkstring & replacementText); - std::vector split (const bkstring & str, const bool keepEmptys = 0, - const unsigned long limit = 0); - std::vector findAll (const bkstring & str); - bool matches (const bkstring & str); - /** - Returns the flags used during compilation of this pattern - @return The flags used during compilation of this pattern - */ - unsigned long getFlags () const; - /** - Returns the regular expression this pattern represents - @return The regular expression this pattern represents - */ - bkstring getWCPattern () const; - /** - Creates a matcher object using the specified string and this pattern. - @param str The string to match against - @return A new matcher using object using this pattern and the specified - string - */ - WCMatcher * createWCMatcher (const bkstring & str); + friend class WCMatcher; + friend class NFAUNode; + friend class NFAQuantifierUNode; +private: + /** + This constructor should not be called directly. Those wishing to use the + WCPattern class should instead use the {@link compile compile} method. + + @param rhs The pattern to compile + @memo Creates a new pattern from the regular expression in rhs. + */ + WCPattern(const CMString & rhs); +protected: + /** + This currently is not used, so don't try to do anything with it. + @memo Holds all the compiled patterns for quick access. + */ + static std::map compiledWCPatterns; + /** + Holds all of the registered patterns as strings. Due to certain problems + with compilation of patterns, especially with capturing groups, this seemed + to be the best way to do it. + */ + static std::map > registeredWCPatterns; +protected: + /** + Holds all the NFA nodes used. This makes deletion of a pattern, as well as + clean-up from an unsuccessful compile much easier and faster. + */ + std::map nodes; + /** + Used when methods like split are called. The matcher class uses a lot of + dynamic memeory, so having an instance increases speedup of certain + operations. + */ + WCMatcher * matcher; + /** + The front node of the NFA. + */ + NFAUNode * head; + /** + The actual regular expression we rerpesent + */ + CMString pattern; + /** + Flag used during compilation. Once the pattern is successfully compiled, + error is no longer used. + */ + bool error; + /** + Used during compilation to keep track of the current index into + {@link pattern pattern}. Once the pattern is successfully + compiled, error is no longer used. + */ + int curInd; + /** + The number of capture groups this contains. + */ + int groupCount; + /** + The number of non-capture groups this contains. + */ + int nonCapGroupCount; + /** + The flags specified when this was compiled. + */ + unsigned long flags; +protected: + /** + Raises an error during compilation. Compilation will cease at that point + and compile will return NULL. + */ + void raiseError(); + /** + Convenience function for registering a node in nodes. + @param node The node to register + @return The registered node + */ + NFAUNode * registerNode(NFAUNode * node); + + /** + Calculates the union of two strings. This function will first sort the + strings and then use a simple selection algorithm to find the union. + @param s1 The first "class" to union + @param s2 The second "class" to union + @return A new string containing all unique characters. Each character + must have appeared in one or both of s1 and + s2. + */ + CMString classUnion(CMString s1, CMString s2) const; + /** + Calculates the intersection of two strings. This function will first sort + the strings and then use a simple selection algorithm to find the + intersection. + @param s1 The first "class" to intersect + @param s2 The second "class" to intersect + @return A new string containing all unique characters. Each character + must have appeared both s1 and s2. + */ + CMString classIntersect(CMString s1, CMString s2) const; + /** + Calculates the negation of a string. The negation is the set of all + characters between \x00 and \xFF not + contained in s1. + @param s1 The "class" to be negated. + @param s2 The second "class" to intersect + @return A new string containing all unique characters. Each character + must have appeared both s1 and s2. + */ + CMString classNegate(CMString s1) const; + /** + Creates a new "class" representing the range from low thru + hi. This function will wrap if low > + hi. This is a feature, not a buf. Sometimes it is useful + to be able to say [\x70-\x10] instead of [\x70-\x7F\x00-\x10]. + @param low The beginning character + @param hi The ending character + @return A new string containing all the characters from low thru hi. + */ + CMString classCreateRange(wchar_t low, wchar_t hi) const; + + /** + Extracts a decimal number from the substring of member-variable + {@link pattern pattern} starting at start and + ending at end. + @param start The starting index in {@link pattern pattern} + @param end The last index in {@link pattern pattern} + @return The decimal number in {@link pattern pattern} + */ + int getInt(int start, int end); + /** + Parses a {n,m} string out of the member-variable + {@link pattern pattern} stores the result in sNum + and eNum. + @param sNum Output parameter. The minimum number of matches required + by the curly quantifier are stored here. + @param eNum Output parameter. The maximum number of matches allowed + by the curly quantifier are stored here. + @return Success/Failure. Fails when the curly does not have the proper + syntax + */ + bool quantifyCurly(int & sNum, int & eNum); + /** + Tries to quantify the currently parsed group. If the group being parsed + is indeed quantified in the member-variable + {@link pattern pattern}, then the NFA is modified accordingly. + @param start The starting node of the current group being parsed + @param stop The ending node of the current group being parsed + @param gn The group number of the current group being parsed + @return The node representing the starting node of the group. If the + group becomes quantified, then this node is not necessarily + a GroupHead node. + */ + NFAUNode * quantifyGroup(NFAUNode * start, NFAUNode * stop, const int gn); + + /** + Tries to quantify the last parsed expression. If the character was indeed + quantified, then the NFA is modified accordingly. + @param newNode The recently created expression node + @return The node representing the last parsed expression. If the + expression was quantified, return value != newNode + */ + NFAUNode * quantify(NFAUNode * newNode); + /** + Parses the current class being examined in + {@link pattern pattern}. + @return A string of unique characters contained in the current class being + parsed + */ + CMString parseClass(); + /** + Parses the current POSIX class being examined in + {@link pattern pattern}. + @return A string of unique characters representing the POSIX class being + parsed + */ + CMString parsePosix(); + /** + Returns a string containing the octal character being parsed + @return The string contained the octal value being parsed + */ + CMString parseOctal(); + /** + Returns a string containing the hex character being parsed + @return The string contained the hex value being parsed + */ + CMString parseHex(); + /** + Returns a new node representing the back reference being parsed + @return The new node representing the back reference being parsed + */ + NFAUNode * parseBackref(); + /** + Parses the escape sequence currently being examined. Determines if the + escape sequence is a class, a single character, or the beginning of a + quotation sequence. + @param inv Output parameter. Whether or not to invert the returned class + @param quo Output parameter. Whether or not this sequence starts a + quotation. + @return The characters represented by the class + */ + CMString parseEscape(bool & inv, bool & quo); + /** + Parses a supposed registered pattern currently under compilation. If the + sequence of characters does point to a registered pattern, then the + registered pattern is appended to *end. The registered pattern + is parsed with the current compilation flags. + @param end The ending node of the thus-far compiled pattern + @return The new end node of the current pattern + */ + NFAUNode * parseRegisteredWCPattern(NFAUNode ** end); + /** + Parses a lookbehind expression. Appends the necessary nodes + *end. + @param pos Positive or negative look behind + @param end The ending node of the current pattern + @return The new end node of the current pattern + */ + NFAUNode * parseBehind(const bool pos, NFAUNode ** end); + /** + Parses the current expression and tacks on nodes until a \E is found. + @return The end of the current pattern + */ + NFAUNode * parseQuote(); + /** + Parses {@link pattern pattern}. This function is called + recursively when an or (|) or a group is encountered. + @param inParen Are we currently parsing inside a group + @param inOr Are we currently parsing one side of an or (|) + @param end The end of the current expression + @return The starting node of the NFA constructed from this parse + */ + NFAUNode * parse(const bool inParen = 0, const bool inOr = 0, NFAUNode ** end = NULL); +public: + /// We should match regardless of case + const static unsigned long CASE_INSENSITIVE; + /// We are implicitly quoted + const static unsigned long LITERAL; + /// @memo We should treat a . as [\x00-\x7F] + const static unsigned long DOT_MATCHES_ALL; + /** ^ and $ should anchor to the beginning and + ending of lines, not all input + */ + const static unsigned long MULTILINE_MATCHING; + /** When enabled, only instances of \n are recognized as + line terminators + */ + const static unsigned long UNIX_LINE_MODE; + /// The absolute minimum number of matches a quantifier can match (0) + const static int MIN_QMATCH; + /// The absolute maximum number of matches a quantifier can match (0x7FFFFFFF) + const static int MAX_QMATCH; +public: + /** + Call this function to compile a regular expression into a + WCPattern object. Special values can be assigned to + mode when certain non-standard behaviors are expected from + the WCPattern object. + @param pattern The regular expression to compile + @param mode A bitwise or of flags signalling what special behaviors are + wanted from this WCPattern object + @return If successful, compile returns a WCPattern + pointer. Upon failure, compile returns + NULL + */ + static WCPattern * compile(const CMString & pattern, + const unsigned long mode = 0); + /** + Dont use this function. This function will compile a pattern, and cache + the result. This will eventually be used as an optimization when people + just want to call static methods using the same pattern over and over + instead of first compiling the pattern and then using the compiled + instance for matching. + @param pattern The regular expression to compile + @param mode A bitwise or of flags signalling what special behaviors are + wanted from this WCPattern object + @return If successful, compileAndKeep returns a + WCPattern pointer. Upon failure, compile + returns NULL. + */ + static WCPattern * compileAndKeep(const CMString & pattern, + const unsigned long mode = 0); + + /** + Searches through replace and replaces all substrings matched + by pattern with str. str may + contain backreferences (e.g. \1) to capture groups. A typical + invocation looks like: +

+ + WCPattern::replace(L"(a+)b(c+)", L"abcccbbabcbabc", L"\\2b\\1"); + +

+ which would replace abcccbbabcbabc with + cccbabbcbabcba. + @param pattern The regular expression + @param str The replacement text + @param replacementText The string in which to perform replacements + @param mode The special mode requested of the WCPattern + during the replacement process + @return The text with the replacement string substituted where necessary + */ + static CMString replace(const CMString & pattern, + const CMString & str, + const CMString & replacementText, + const unsigned long mode = 0); + + /** + Splits the specified string over occurrences of the specified pattern. + Empty strings can be optionally ignored. The number of strings returned is + configurable. A typical invocation looks like: +

+ + CMString str(strSize, 0);
+ FILE * fp = fopen(fileName, "r");
+ fread((char*)str.data(), strSize * 2, 1, fp);
+ fclose(fp);
+
+ std::vector<CMString> lines = WCPattern::split(L"[\r\n]+", str, true);
+
+
+ + @param pattern The regular expression + @param replace The string to split + @param keepEmptys Whether or not to keep empty strings + @param limit The maximum number of splits to make + @param mode The special mode requested of the WCPattern + during the split process + @return All substrings of str split across pattern. + */ + static std::vector split(const CMString & pattern, + const CMString & str, + const bool keepEmptys = 0, + const unsigned long limit = 0, + const unsigned long mode = 0); + + /** + Finds all the instances of the specified pattern within the string. You + should be careful to only pass patterns with a minimum length of one. For + example, the pattern a* can be matched by an empty string, so + instead you should pass a+ since at least one character must + be matched. A typical invocation of findAll looks like: +

+ + std::vector<td::string> numbers = WCPattern::findAll(L"\\d+", string); + +

+ + @param pattern The pattern for which to search + @param str The string to search + @param mode The special mode requested of the WCPattern + during the find process + @return All instances of pattern in str + */ + static std::vector findAll(const CMString & pattern, + const CMString & str, + const unsigned long mode = 0); + + /** + Determines if an entire string matches the specified pattern + + @param pattern The pattern for to match + @param str The string to match + @param mode The special mode requested of the WCPattern + during the replacement process + @return True if str is recognized by pattern + */ + static bool matches(const CMString & pattern, + const CMString & str, + const unsigned long mode = 0); + + /** + Registers a pattern under a specific name for use in later compilations. + A typical invocation and later use looks like: +

+ + WCPattern::registerWCPattern(L"ip", L"(?:\\d{1,3}\\.){3}\\d{1,3}");
+ WCPattern * p1 = WCPattern::compile(L"{ip}:\\d+");
+ WCPattern * p2 = WCPattern::compile(L"Connection from ({ip}) on port \\d+");
+
+

+ Multiple calls to registerWCPattern with the same + name will result in the pattern getting overwritten. + + @param name The name to give to the pattern + @param pattern The pattern to register + @param mode Any special flags to use when compiling pattern + @return Success/Failure. Fails only if pattern has invalid + syntax + */ + static bool registerWCPattern(const CMString & name, + const CMString & pattern, + const unsigned long mode = 0); + + /** + Clears the pattern registry + */ + static void unregisterWCPatterns(); + /** + Don't use + */ + static void clearWCPatternCache(); + + /** + Searches through a string for the nth match of the + given pattern in the string. Match indeces start at zero, not one. + A typical invocation looks like this: +

+ + std::pair<CMString, int> match = WCPattern::findNthMatch(L"\\d{1,3}", L"192.168.1.101:22", 1);
+ wprintf(L"%s %i\n", match.first.c_str(), match.second);
+
+ Output: 168 4
+
+ + @param pattern The pattern for which to search + @param str The string to search + @param matchNum Which match to find + @param mode Any special flags to use during the matching process + @return A string and an integer. The string is the string matched. The + integer is the starting location of the matched string in + str. You can check for success/failure by making sure + that the integer returned is greater than or equal to zero. + */ + static std::pair findNthMatch(const CMString & pattern, + const CMString & str, + const int matchNum, + const unsigned long mode = 0); +public: + /** + Deletes all NFA nodes allocated during compilation + */ + ~WCPattern(); + + CMString replace(const CMString & str, + const CMString & replacementText); + std::vector split(const CMString & str, const bool keepEmptys = 0, + const unsigned long limit = 0); + std::vector findAll(const CMString & str); + bool matches(const CMString & str); + /** + Returns the flags used during compilation of this pattern + @return The flags used during compilation of this pattern + */ + unsigned long getFlags() const; + /** + Returns the regular expression this pattern represents + @return The regular expression this pattern represents + */ + CMString getWCPattern() const; + /** + Creates a matcher object using the specified string and this pattern. + @param str The string to match against + @return A new matcher using object using this pattern and the specified + string + */ + WCMatcher * createWCMatcher(const CMString & str); }; class NFAUNode { - friend class WCMatcher; - public: - NFAUNode * next; - NFAUNode(); - virtual ~NFAUNode(); - virtual void findAllNodes(std::map & soFar); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const = 0; - inline virtual bool isGroupHeadNode() const { return false; } - inline virtual bool isStartOfInputNode() const { return false; } + friend class WCMatcher; +public: + NFAUNode * next; + NFAUNode(); + virtual ~NFAUNode(); + virtual void findAllNodes(std::map & soFar); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const = 0; + inline virtual bool isGroupHeadNode() const { return false; } + inline virtual bool isStartOfInputNode() const { return false; } }; class NFACharUNode : public NFAUNode { - protected: - wchar_t ch; - public: - NFACharUNode(const wchar_t c); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; +protected: + wchar_t ch; +public: + NFACharUNode(const wchar_t c); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; }; class NFACICharUNode : public NFAUNode { - protected: - wchar_t ch; - public: - NFACICharUNode(const wchar_t c); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; +protected: + wchar_t ch; +public: + NFACICharUNode(const wchar_t c); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; }; class NFAStartUNode : public NFAUNode { - public: - NFAStartUNode(); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; +public: + NFAStartUNode(); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; }; class NFAEndUNode : public NFAUNode { - public: - NFAEndUNode(); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; +public: + NFAEndUNode(); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; }; class NFAQuantifierUNode : public NFAUNode { - public: - int min, max; - NFAUNode * inner; - virtual void findAllNodes(std::map & soFar); - NFAQuantifierUNode(WCPattern * pat, NFAUNode * internal, - const int minMatch = WCPattern::MIN_QMATCH, - const int maxMatch = WCPattern::MAX_QMATCH); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; +public: + int min, max; + NFAUNode * inner; + virtual void findAllNodes(std::map & soFar); + NFAQuantifierUNode(WCPattern * pat, NFAUNode * internal, + const int minMatch = WCPattern::MIN_QMATCH, + const int maxMatch = WCPattern::MAX_QMATCH); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; }; class NFAGreedyQuantifierUNode : public NFAQuantifierUNode { - public: - NFAGreedyQuantifierUNode(WCPattern * pat, NFAUNode * internal, - const int minMatch = WCPattern::MIN_QMATCH, - const int maxMatch = WCPattern::MAX_QMATCH); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; - virtual int matchInternal(const bkstring & str, WCMatcher * matcher, const int curInd, const int soFar) const; +public: + NFAGreedyQuantifierUNode(WCPattern * pat, NFAUNode * internal, + const int minMatch = WCPattern::MIN_QMATCH, + const int maxMatch = WCPattern::MAX_QMATCH); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; + virtual int matchInternal(const CMString & str, WCMatcher * matcher, const int curInd, const int soFar) const; }; class NFALazyQuantifierUNode : public NFAQuantifierUNode { - public: - NFALazyQuantifierUNode(WCPattern * pat, NFAUNode * internal, - const int minMatch = WCPattern::MIN_QMATCH, - const int maxMatch = WCPattern::MAX_QMATCH); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; +public: + NFALazyQuantifierUNode(WCPattern * pat, NFAUNode * internal, + const int minMatch = WCPattern::MIN_QMATCH, + const int maxMatch = WCPattern::MAX_QMATCH); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; }; class NFAPossessiveQuantifierUNode : public NFAQuantifierUNode { - public: - NFAPossessiveQuantifierUNode(WCPattern * pat, NFAUNode * internal, - const int minMatch = WCPattern::MIN_QMATCH, - const int maxMatch = WCPattern::MAX_QMATCH); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; +public: + NFAPossessiveQuantifierUNode(WCPattern * pat, NFAUNode * internal, + const int minMatch = WCPattern::MIN_QMATCH, + const int maxMatch = WCPattern::MAX_QMATCH); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; }; class NFAAcceptUNode : public NFAUNode { - public: - NFAAcceptUNode(); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; +public: + NFAAcceptUNode(); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; }; class NFAClassUNode : public NFAUNode { - public: - bool inv; - std::map vals; - NFAClassUNode(const bool invert = 0); - NFAClassUNode(const bkstring & clazz, const bool invert); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; +public: + bool inv; + std::map vals; + NFAClassUNode(const bool invert = 0); + NFAClassUNode(const CMString & clazz, const bool invert); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; }; class NFACIClassUNode : public NFAUNode { - public: - bool inv; - std::map vals; - NFACIClassUNode(const bool invert = 0); - NFACIClassUNode(const bkstring & clazz, const bool invert); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; +public: + bool inv; + std::map vals; + NFACIClassUNode(const bool invert = 0); + NFACIClassUNode(const CMString & clazz, const bool invert); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; }; class NFASubStartUNode : public NFAUNode { - public: - NFASubStartUNode(); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; +public: + NFASubStartUNode(); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; }; class NFAOrUNode : public NFAUNode { - public: - NFAUNode * one; - NFAUNode * two; - NFAOrUNode(NFAUNode * first, NFAUNode * second); - virtual void findAllNodes(std::map & soFar); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; +public: + NFAUNode * one; + NFAUNode * two; + NFAOrUNode(NFAUNode * first, NFAUNode * second); + virtual void findAllNodes(std::map & soFar); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; }; class NFAQuoteUNode : public NFAUNode { - public: - bkstring qStr; - NFAQuoteUNode(const bkstring & quoted); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; +public: + CMString qStr; + NFAQuoteUNode(const CMString & quoted); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; }; class NFACIQuoteUNode : public NFAUNode { - public: - bkstring qStr; - NFACIQuoteUNode(const bkstring & quoted); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; +public: + CMString qStr; + NFACIQuoteUNode(const CMString & quoted); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; }; class NFALookAheadUNode : public NFAUNode { - public: - bool pos; - NFAUNode * inner; - NFALookAheadUNode(NFAUNode * internal, const bool positive); - virtual void findAllNodes(std::map & soFar); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; +public: + bool pos; + NFAUNode * inner; + NFALookAheadUNode(NFAUNode * internal, const bool positive); + virtual void findAllNodes(std::map & soFar); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; }; class NFALookBehindUNode : public NFAUNode { - public: - bool pos; - bkstring mStr; - NFALookBehindUNode(const bkstring & str, const bool positive); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; +public: + bool pos; + CMString mStr; + NFALookBehindUNode(const CMString & str, const bool positive); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; }; class NFAStartOfLineUNode : public NFAUNode { - public: - NFAStartOfLineUNode(); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; +public: + NFAStartOfLineUNode(); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; }; class NFAEndOfLineUNode : public NFAUNode { - public: - NFAEndOfLineUNode(); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; +public: + NFAEndOfLineUNode(); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; }; class NFAReferenceUNode : public NFAUNode { - public: - int gi; - NFAReferenceUNode(const int groupIndex); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; +public: + int gi; + NFAReferenceUNode(const int groupIndex); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; }; class NFAStartOfInputUNode : public NFAUNode { - public: - NFAStartOfInputUNode(); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; - inline virtual bool isStartOfInputNode() const { return false; } +public: + NFAStartOfInputUNode(); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; + inline virtual bool isStartOfInputNode() const { return false; } }; class NFAEndOfInputUNode : public NFAUNode { - public: - bool term; - NFAEndOfInputUNode(const bool lookForTerm); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; +public: + bool term; + NFAEndOfInputUNode(const bool lookForTerm); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; }; class NFAWordBoundaryUNode : public NFAUNode { - public: - bool pos; - NFAWordBoundaryUNode(const bool positive); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; +public: + bool pos; + NFAWordBoundaryUNode(const bool positive); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; }; class NFAEndOfMatchUNode : public NFAUNode { - public: - NFAEndOfMatchUNode(); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; +public: + NFAEndOfMatchUNode(); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; }; class NFAGroupHeadUNode : public NFAUNode { - public: - int gi; - NFAGroupHeadUNode(const int groupIndex); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; - inline virtual bool isGroupHeadNode() const { return false; } +public: + int gi; + NFAGroupHeadUNode(const int groupIndex); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; + inline virtual bool isGroupHeadNode() const { return false; } }; class NFAGroupTailUNode : public NFAUNode { - public: - int gi; - NFAGroupTailUNode(const int groupIndex); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; +public: + int gi; + NFAGroupTailUNode(const int groupIndex); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; }; class NFAGroupLoopPrologueUNode : public NFAUNode { - public: - int gi; - NFAGroupLoopPrologueUNode(const int groupIndex); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; +public: + int gi; + NFAGroupLoopPrologueUNode(const int groupIndex); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; }; class NFAGroupLoopUNode : public NFAUNode { - public: - int gi, min, max, type; - NFAUNode * inner; - NFAGroupLoopUNode(NFAUNode * internal, const int minMatch, - const int maxMatch, const int groupIndex, const int matchType); - virtual void findAllNodes(std::map & soFar); - virtual int match(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; - int matchGreedy(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; - int matchLazy(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; - int matchPossessive(const bkstring & str, WCMatcher * matcher, const int curInd = 0) const; +public: + int gi, min, max, type; + NFAUNode * inner; + NFAGroupLoopUNode(NFAUNode * internal, const int minMatch, + const int maxMatch, const int groupIndex, const int matchType); + virtual void findAllNodes(std::map & soFar); + virtual int match(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; + int matchGreedy(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; + int matchLazy(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; + int matchPossessive(const CMString & str, WCMatcher * matcher, const int curInd = 0) const; }; #endif -- cgit v1.2.3