1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef ANDROID_VINTF_REGEX_H_ 18 #define ANDROID_VINTF_REGEX_H_ 19 20 #include <regex.h> 21 #include <memory> 22 #include <string> 23 24 namespace android { 25 namespace vintf { 26 namespace details { 27 28 // A wrapper class around regex.h. This is used instead of C++ <regex> library because 29 // C++ regex library throws exceptions when an invalid regular expression is compiled. 30 // Use Extended Regular Expression (ERE) syntax. 31 class Regex { 32 public: 33 Regex() = default; 34 ~Regex(); 35 36 Regex& operator=(const Regex&) = delete; 37 Regex(const Regex&) = delete; 38 39 __attribute__((warn_unused_result)) bool compile(const std::string& pattern); 40 41 bool matches(const std::string& s) const; 42 43 /** 44 * Return nullptr if not a valid regex pattern, else the Regex object. 45 */ 46 static const Regex* Get(const std::string& pattern); 47 48 private: 49 std::unique_ptr<regex_t> mImpl; 50 51 void clear(); 52 }; 53 54 } // namespace details 55 } // namespace vintf 56 } // namespace android 57 58 #endif // ANDROID_VINTF_REGEX_H_ 59