1 /*
2  * Copyright (C) 2024 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 #pragma once
18 
19 #include <regex.h>
20 
21 #include <memory>
22 #include <string>
23 
24 namespace android {
25 namespace os {
26 namespace statsd {
27 
28 class Regex {
29 public:
30     Regex(regex_t impl);  // Do not use. It is public for std::make_unique. Use Regex::create.
31     ~Regex();
32     Regex& operator=(const Regex&) = delete;
33     Regex(const Regex&) = delete;
34 
35     // Returns nullptr if pattern is not valid POSIX regex.
36     static std::unique_ptr<Regex> create(const std::string& pattern);
37 
38     // Looks for a regex match in str and replaces the matched portion with replacement in-place.
39     // Returns true if there was a match, false otherwise.
40     bool replace(std::string& str, const std::string& replacement);
41 
42 private:
43     regex_t mImpl;
44 };
45 
46 }  // namespace statsd
47 }  // namespace os
48 }  // namespace android
49