Home | History | Annotate | Download | only in vintf
      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 <string>
     22 
     23 namespace android {
     24 namespace vintf {
     25 namespace details {
     26 
     27 // A wrapper class around regex.h. This is used instead of C++ <regex> library because
     28 // C++ regex library throws exceptions when an invalid regular expression is compiled.
     29 // Use Extended Regular Expression (ERE) syntax.
     30 class Regex {
     31    public:
     32     Regex() = default;
     33     ~Regex();
     34 
     35     Regex& operator=(const Regex&) = delete;
     36     Regex(const Regex&) = delete;
     37 
     38     __attribute__((warn_unused_result)) bool compile(const std::string& pattern);
     39 
     40     bool matches(const std::string& s) const;
     41 
     42     /**
     43      * Return nullptr if not a valid regex pattern, else the Regex object.
     44      */
     45     static const Regex* Get(const std::string& pattern);
     46 
     47    private:
     48     std::unique_ptr<regex_t> mImpl;
     49 
     50     void clear();
     51 };
     52 
     53 }  // namespace details
     54 }  // namespace vintf
     55 }  // namespace android
     56 
     57 #endif  // ANDROID_VINTF_REGEX_H_
     58