diff options
Diffstat (limited to 'third_party/libphonenumber/cpp/src/regexp_adapter.h')
-rw-r--r-- | third_party/libphonenumber/cpp/src/regexp_adapter.h | 176 |
1 files changed, 121 insertions, 55 deletions
diff --git a/third_party/libphonenumber/cpp/src/regexp_adapter.h b/third_party/libphonenumber/cpp/src/regexp_adapter.h index 233adbe..3568152 100644 --- a/third_party/libphonenumber/cpp/src/regexp_adapter.h +++ b/third_party/libphonenumber/cpp/src/regexp_adapter.h @@ -13,84 +13,150 @@ // limitations under the License. // Author: George Yakovlev +// Philippe Liard +// +// Regexp adapter to allow a pluggable regexp engine. It has been introduced +// during the integration of the open-source version of this library into +// Chromium to be able to use the ICU Regex engine instead of RE2, which is not +// officially supported on Windows. +// Since RE2 was initially used in this library, the interface of this adapter +// is very close to the subset of the RE2 API used in phonenumberutil.cc. #ifndef I18N_PHONENUMBERS_REGEXP_ADAPTER_H_ #define I18N_PHONENUMBERS_REGEXP_ADAPTER_H_ +#include <cstddef> #include <string> -// Regexp adapter to allow pluggable regexp engine, as it is external to -// libphonenumber. +namespace i18n { +namespace phonenumbers { -namespace reg_exp { +using std::string; -// The reg exp input class. -// It supports only functions used in phonelibrary. -class RegularExpressionInput { +// RegExpInput is the interface that abstracts the input that feeds the +// Consume() method of RegExp which may differ depending on its various +// implementations (StringPiece for RE2, UnicodeString for ICU Regex). +class RegExpInput { public: - virtual ~RegularExpressionInput() {}; - - // Matches string to regular expression, returns true if expression was - // matched, false otherwise, advances position in the match. - // |reg_exp| - expression to be matched. - // |beginning_only| - if true match would be successfull only if appears at - // the beginning of the tested region of the string. - // |matched_string1| - successfully matched first string. Can be NULL. - // |matched_string2| - successfully matched second string. Can be NULL. - virtual bool ConsumeRegExp(std::string const& reg_exp, - bool beginning_only, - std::string* matched_string1, - std::string* matched_string2) = 0; - // Convert unmatched input to a string. - virtual std::string ToString() const = 0; + virtual ~RegExpInput() {} + + // Creates a new instance of the default RegExpInput implementation. The + // deletion of the returned instance is under the responsibility of the + // caller. + static RegExpInput* Create(const string& utf8_input); + + // Converts to a C++ string. + virtual string ToString() const = 0; }; -// The regular expression class. -// It supports only functions used in phonelibrary. -class RegularExpression { +// The regular expression abstract class. It supports only functions used in +// phonenumberutil.cc. Consume(), Match() and Replace() methods must be +// implemented. +class RegExp { public: - RegularExpression() {} - virtual ~RegularExpression() {} + virtual ~RegExp() {} + + // Creates a new instance of the default RegExp implementation. The deletion + // of the returned instance is under the responsibility of the caller. + static RegExp* Create(const string& utf8_regexp); // Matches string to regular expression, returns true if expression was // matched, false otherwise, advances position in the match. - // |input_string| - string to be searched. - // |beginning_only| - if true match would be successfull only if appears at + // input_string - string to be searched. + // anchor_at_start - if true, match would be successful only if it appears at // the beginning of the tested region of the string. - // |matched_string1| - successfully matched first string. Can be NULL. - // |matched_string2| - successfully matched second string. Can be NULL. - // |matched_string3| - successfully matched third string. Can be NULL. - virtual bool Consume(RegularExpressionInput* input_string, - bool beginning_only, - std::string* matched_string1 = NULL, - std::string* matched_string2 = NULL, - std::string* matched_string3 = NULL) const = 0; + // matched_string1 - the first string extracted from the match. Can be NULL. + // matched_string2 - the second string extracted from the match. Can be NULL. + // matched_string3 - the third string extracted from the match. Can be NULL. + virtual bool Consume(RegExpInput* input_string, + bool anchor_at_start, + string* matched_string1, + string* matched_string2, + string* matched_string3) const = 0; + // Helper methods calling the Consume method that assume the match must start + // at the beginning. + inline bool Consume(RegExpInput* input_string, + string* matched_string1, + string* matched_string2, + string* matched_string3) const { + return Consume(input_string, true, matched_string1, matched_string2, + matched_string3); + } - // Matches string to regular expression, returns true if expression was + inline bool Consume(RegExpInput* input_string, + string* matched_string1, + string* matched_string2) const { + return Consume(input_string, true, matched_string1, matched_string2, NULL); + } + + inline bool Consume(RegExpInput* input_string, string* matched_string) const { + return Consume(input_string, true, matched_string, NULL, NULL); + } + + inline bool Consume(RegExpInput* input_string) const { + return Consume(input_string, true, NULL, NULL, NULL); + } + + // Helper method calling the Consume method that assumes the match can start + // at any place in the string. + inline bool FindAndConsume(RegExpInput* input_string, + string* matched_string) const { + return Consume(input_string, false, matched_string, NULL, NULL); + } + + // Matches string to regular expression, returns true if the expression was // matched, false otherwise. - // |input_string| - string to be searched. - // |full_match| - if true match would be successfull only if it matches the + // input_string - string to be searched. + // full_match - if true, match would be successful only if it matches the // complete string. - // |matched_string| - successfully matched string. Can be NULL. - virtual bool Match(const char* input_string, + // matched_string - the string extracted from the match. Can be NULL. + virtual bool Match(const string& input_string, bool full_match, - std::string* matched_string) const = 0; - - // Replaces match(es) in the |string_to_process|. if |global| is true, - // replaces all the matches, only the first match otherwise. - // |replacement_string| - text the matches are replaced with. - // Returns true if expression successfully processed through the string, - // even if no actual replacements were made. Returns false in case of an - // error. - virtual bool Replace(std::string* string_to_process, + string* matched_string) const = 0; + + // Helper methods calling the Match method with the right arguments. + inline bool PartialMatch(const string& input_string, + string* matched_string) const { + return Match(input_string, false, matched_string); + } + + inline bool PartialMatch(const string& input_string) const { + return Match(input_string, false, NULL); + } + + inline bool FullMatch(const string& input_string, + string* matched_string) const { + return Match(input_string, true, matched_string); + } + + inline bool FullMatch(const string& input_string) const { + return Match(input_string, true, NULL); + } + + // Replaces match(es) in 'string_to_process'. If 'global' is true, + // replaces all the matches, otherwise only the first match. + // replacement_string - text the matches are replaced with. The groups in the + // replacement string are referenced with the $[0-9] notation. + // Returns true if the pattern matches and a replacement occurs, false + // otherwise. + virtual bool Replace(string* string_to_process, bool global, - const char* replacement_string) const = 0; -}; + const string& replacement_string) const = 0; -RegularExpressionInput* CreateRegularExpressionInput(const char* utf8_input); -RegularExpression* CreateRegularExpression(const char* utf8_regexp); + // Helper methods calling the Replace method with the right arguments. + inline bool Replace(string* string_to_process, + const string& replacement_string) const { + return Replace(string_to_process, false, replacement_string); + } + + inline bool GlobalReplace(string* string_to_process, + const string& replacement_string) const { + return Replace(string_to_process, true, replacement_string); + } +}; -} // namespace reg_exp +} // namespace phonenumbers +} // namespace i18n #endif // I18N_PHONENUMBERS_REGEXP_ADAPTER_H_ |