1 /* 2 * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef REG_EXP_H 6 #define REG_EXP_H 7 8 9 #include <stddef.h> 10 11 12 class RegExp { 13 public: 14 enum PatternType { 15 PATTERN_TYPE_REGULAR_EXPRESSION, 16 PATTERN_TYPE_WILDCARD 17 }; 18 19 class MatchResult; 20 21 public: 22 RegExp(); 23 RegExp(const char* pattern, 24 PatternType patternType 25 = PATTERN_TYPE_REGULAR_EXPRESSION, 26 bool caseSensitive = true); 27 RegExp(const RegExp& other); 28 ~RegExp(); 29 30 bool IsValid() const 31 { return fData != NULL; } 32 33 bool SetPattern(const char* pattern, 34 PatternType patternType 35 = PATTERN_TYPE_REGULAR_EXPRESSION, 36 bool caseSensitive = true); 37 38 MatchResult Match(const char* string) const; 39 40 RegExp& operator=(const RegExp& other); 41 42 private: 43 struct Data; 44 struct MatchResultData; 45 46 private: 47 Data* fData; 48 }; 49 50 51 class RegExp::MatchResult { 52 public: 53 MatchResult(); 54 MatchResult(const MatchResult& other); 55 ~MatchResult(); 56 57 bool HasMatched() const; 58 59 size_t StartOffset() const; 60 size_t EndOffset() const; 61 62 size_t GroupCount() const; 63 size_t GroupStartOffsetAt(size_t index) const; 64 size_t GroupEndOffsetAt(size_t index) const; 65 66 MatchResult& operator=(const MatchResult& other); 67 68 private: 69 friend class RegExp; 70 71 private: 72 MatchResult(MatchResultData* data); 73 // takes over the data reference 74 75 private: 76 MatchResultData* fData; 77 }; 78 79 80 #endif // REG_EXP_H 81