| OLD | NEW |
| (Empty) |
| 1 // Copyright 2005-2009 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 // | |
| 16 // ATL Regular expression class that implements the RE interface. | |
| 17 // See MSDN help for example patterns (lookup help on CAtlRegExp) | |
| 18 // NOTE: This class adds about 8k to release builds. | |
| 19 // TODO(omaha): add a unit test, showing examples, testing functionality | |
| 20 // and perf. | |
| 21 | |
| 22 #ifndef OMAHA_COMMON_ATL_REGEXP_H__ | |
| 23 #define OMAHA_COMMON_ATL_REGEXP_H__ | |
| 24 | |
| 25 #pragma warning(push) | |
| 26 // enumerator 'identifier' in switch of enum 'enumeration' is not explicitly | |
| 27 // handled by a case label | |
| 28 #pragma warning(disable:4061) | |
| 29 #include <atlrx.h> | |
| 30 #pragma warning(pop) | |
| 31 #include <atlstr.h> | |
| 32 #include "base/basictypes.h" | |
| 33 #include "omaha/base/regexp.h" | |
| 34 #include "omaha/base/string.h" | |
| 35 | |
| 36 namespace omaha { | |
| 37 | |
| 38 // This class is essentially a copy of CAtlRECharTraitsWide from <atlrx.h>, but | |
| 39 // I've replaced CRT functions that are not in our minicrt. | |
| 40 // | |
| 41 // TODO(omaha): do we need this? | |
| 42 class CAtlRECharTraitsWideNoCrt | |
| 43 { | |
| 44 public: | |
| 45 typedef WCHAR RECHARTYPE; | |
| 46 | |
| 47 // ATL80 addition. | |
| 48 static size_t GetBitFieldForRangeArrayIndex(const RECHARTYPE *sz) throw() | |
| 49 { | |
| 50 #ifndef ATL_NO_CHECK_BIT_FIELD | |
| 51 ATLASSERT(UseBitFieldForRange()); | |
| 52 #endif | |
| 53 return static_cast<size_t>(*sz); | |
| 54 } | |
| 55 | |
| 56 static RECHARTYPE *Next(const RECHARTYPE *sz) throw() | |
| 57 { | |
| 58 return (RECHARTYPE *) (sz+1); | |
| 59 } | |
| 60 | |
| 61 static int Strncmp(const RECHARTYPE *szLeft, | |
| 62 const RECHARTYPE *szRight, size_t nCount) throw() | |
| 63 { | |
| 64 return String_StrNCmp(szLeft, szRight, nCount,false); | |
| 65 } | |
| 66 | |
| 67 static int Strnicmp(const RECHARTYPE *szLeft, | |
| 68 const RECHARTYPE *szRight, size_t nCount) throw() | |
| 69 { | |
| 70 return String_StrNCmp(szLeft, szRight, nCount,true); | |
| 71 } | |
| 72 | |
| 73 static RECHARTYPE *Strlwr(RECHARTYPE *sz) throw() | |
| 74 { | |
| 75 return String_FastToLower(sz); | |
| 76 } | |
| 77 | |
| 78 // In ATL 80 Strlwr must be passed a buffer size for security reasons. | |
| 79 // TODO(omaha): Implement the function to consider the nSize param. | |
| 80 static RECHARTYPE *Strlwr(RECHARTYPE *sz, int) throw() | |
| 81 { | |
| 82 return Strlwr(sz); | |
| 83 } | |
| 84 | |
| 85 static long Strtol(const RECHARTYPE *sz, | |
| 86 RECHARTYPE **szEnd, int nBase) throw() | |
| 87 { | |
| 88 return Wcstol(sz, szEnd, nBase); | |
| 89 } | |
| 90 | |
| 91 static int Isdigit(RECHARTYPE ch) throw() | |
| 92 { | |
| 93 return String_IsDigit(ch) ? 1 : 0; | |
| 94 } | |
| 95 | |
| 96 static const RECHARTYPE** GetAbbrevs() | |
| 97 { | |
| 98 static const RECHARTYPE *s_szAbbrevs[] = | |
| 99 { | |
| 100 L"a([a-zA-Z0-9])", // alpha numeric | |
| 101 L"b([ \\t])", // white space (blank) | |
| 102 L"c([a-zA-Z])", // alpha | |
| 103 L"d([0-9])", // digit | |
| 104 L"h([0-9a-fA-F])", // hex digit | |
| 105 L"n(\r|(\r?\n))", // newline | |
| 106 L"q(\"[^\"]*\")|(\'[^\']*\')", // quoted string | |
| 107 L"w([a-zA-Z]+)", // simple word | |
| 108 L"z([0-9]+)", // integer | |
| 109 NULL | |
| 110 }; | |
| 111 | |
| 112 return s_szAbbrevs; | |
| 113 } | |
| 114 | |
| 115 static BOOL UseBitFieldForRange() throw() | |
| 116 { | |
| 117 return FALSE; | |
| 118 } | |
| 119 | |
| 120 static int ByteLen(const RECHARTYPE *sz) throw() | |
| 121 { | |
| 122 return int(lstrlen(sz)*sizeof(WCHAR)); | |
| 123 } | |
| 124 }; | |
| 125 | |
| 126 typedef CAtlRegExp<CAtlRECharTraitsWideNoCrt> AtlRegExp; | |
| 127 typedef CAtlREMatchContext<CAtlRECharTraitsWideNoCrt> AtlMatchContext; | |
| 128 | |
| 129 // implements the RE class using the ATL Regular Expressions class | |
| 130 class AtlRE : public RE { | |
| 131 public: | |
| 132 | |
| 133 AtlRE(const TCHAR* pattern, bool case_sensitive = true); | |
| 134 virtual ~AtlRE(); | |
| 135 | |
| 136 protected: | |
| 137 // See regexp.h for an explanation. | |
| 138 virtual bool DoMatchImpl(const TCHAR* text, | |
| 139 CString* args[], | |
| 140 int n, | |
| 141 const TCHAR** match_end) const; | |
| 142 | |
| 143 private: | |
| 144 mutable AtlRegExp re_; | |
| 145 DISALLOW_EVIL_CONSTRUCTORS(AtlRE); | |
| 146 }; | |
| 147 | |
| 148 } // namespace omaha | |
| 149 | |
| 150 #endif // OMAHA_COMMON_ATL_REGEXP_H__ | |
| OLD | NEW |