xref: /aosp_15_r20/system/extras/simpleperf/RegEx.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker  * Copyright (C) 2022 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker  *
4*288bf522SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker  *
8*288bf522SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker  *
10*288bf522SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker  * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker  */
16*288bf522SAndroid Build Coastguard Worker 
17*288bf522SAndroid Build Coastguard Worker #include "RegEx.h"
18*288bf522SAndroid Build Coastguard Worker 
19*288bf522SAndroid Build Coastguard Worker #include <regex>
20*288bf522SAndroid Build Coastguard Worker 
21*288bf522SAndroid Build Coastguard Worker #include <android-base/logging.h>
22*288bf522SAndroid Build Coastguard Worker 
23*288bf522SAndroid Build Coastguard Worker namespace simpleperf {
24*288bf522SAndroid Build Coastguard Worker 
~RegExMatch()25*288bf522SAndroid Build Coastguard Worker RegExMatch::~RegExMatch() {}
26*288bf522SAndroid Build Coastguard Worker 
27*288bf522SAndroid Build Coastguard Worker class RegExMatchImpl : public RegExMatch {
28*288bf522SAndroid Build Coastguard Worker  public:
RegExMatchImpl(std::string_view s,const std::regex & re)29*288bf522SAndroid Build Coastguard Worker   RegExMatchImpl(std::string_view s, const std::regex& re)
30*288bf522SAndroid Build Coastguard Worker       : match_it_(s.data(), s.data() + s.size(), re) {}
31*288bf522SAndroid Build Coastguard Worker 
IsValid() const32*288bf522SAndroid Build Coastguard Worker   bool IsValid() const override { return match_it_ != std::cregex_iterator(); }
33*288bf522SAndroid Build Coastguard Worker 
GetField(size_t index) const34*288bf522SAndroid Build Coastguard Worker   std::string GetField(size_t index) const override { return match_it_->str(index); }
35*288bf522SAndroid Build Coastguard Worker 
MoveToNextMatch()36*288bf522SAndroid Build Coastguard Worker   void MoveToNextMatch() override { ++match_it_; }
37*288bf522SAndroid Build Coastguard Worker 
38*288bf522SAndroid Build Coastguard Worker  private:
39*288bf522SAndroid Build Coastguard Worker   std::cregex_iterator match_it_;
40*288bf522SAndroid Build Coastguard Worker };
41*288bf522SAndroid Build Coastguard Worker 
42*288bf522SAndroid Build Coastguard Worker class RegExImpl : public RegEx {
43*288bf522SAndroid Build Coastguard Worker  public:
RegExImpl(std::string_view pattern)44*288bf522SAndroid Build Coastguard Worker   RegExImpl(std::string_view pattern)
45*288bf522SAndroid Build Coastguard Worker       : RegEx(pattern), re_(pattern_, std::regex::ECMAScript | std::regex::optimize) {}
46*288bf522SAndroid Build Coastguard Worker 
Match(std::string_view s) const47*288bf522SAndroid Build Coastguard Worker   bool Match(std::string_view s) const override {
48*288bf522SAndroid Build Coastguard Worker     return std::regex_match(s.begin(), s.end(), re_);
49*288bf522SAndroid Build Coastguard Worker   }
Search(std::string_view s) const50*288bf522SAndroid Build Coastguard Worker   bool Search(std::string_view s) const override {
51*288bf522SAndroid Build Coastguard Worker     return std::regex_search(s.begin(), s.end(), re_);
52*288bf522SAndroid Build Coastguard Worker   }
SearchAll(std::string_view s) const53*288bf522SAndroid Build Coastguard Worker   std::unique_ptr<RegExMatch> SearchAll(std::string_view s) const override {
54*288bf522SAndroid Build Coastguard Worker     return std::unique_ptr<RegExMatch>(new RegExMatchImpl(s, re_));
55*288bf522SAndroid Build Coastguard Worker   }
Replace(const std::string & s,const std::string & format) const56*288bf522SAndroid Build Coastguard Worker   std::optional<std::string> Replace(const std::string& s,
57*288bf522SAndroid Build Coastguard Worker                                      const std::string& format) const override {
58*288bf522SAndroid Build Coastguard Worker     try {
59*288bf522SAndroid Build Coastguard Worker       return {std::regex_replace(s, re_, format)};
60*288bf522SAndroid Build Coastguard Worker     } catch (std::regex_error& e) {
61*288bf522SAndroid Build Coastguard Worker       LOG(ERROR) << "regex_error: " << e.what() << ", pattern " << pattern_ << ", format "
62*288bf522SAndroid Build Coastguard Worker                  << format;
63*288bf522SAndroid Build Coastguard Worker       return std::nullopt;
64*288bf522SAndroid Build Coastguard Worker     }
65*288bf522SAndroid Build Coastguard Worker   }
66*288bf522SAndroid Build Coastguard Worker 
67*288bf522SAndroid Build Coastguard Worker  private:
68*288bf522SAndroid Build Coastguard Worker   std::regex re_;
69*288bf522SAndroid Build Coastguard Worker };
70*288bf522SAndroid Build Coastguard Worker 
Create(std::string_view pattern)71*288bf522SAndroid Build Coastguard Worker std::unique_ptr<RegEx> RegEx::Create(std::string_view pattern) {
72*288bf522SAndroid Build Coastguard Worker   try {
73*288bf522SAndroid Build Coastguard Worker     return std::make_unique<RegExImpl>(pattern);
74*288bf522SAndroid Build Coastguard Worker   } catch (std::regex_error& e) {
75*288bf522SAndroid Build Coastguard Worker     LOG(ERROR) << "regex_error: " << e.what() << ", pattern " << pattern;
76*288bf522SAndroid Build Coastguard Worker     return nullptr;
77*288bf522SAndroid Build Coastguard Worker   }
78*288bf522SAndroid Build Coastguard Worker }
79*288bf522SAndroid Build Coastguard Worker 
SearchInRegs(std::string_view s,const std::vector<std::unique_ptr<RegEx>> & regs)80*288bf522SAndroid Build Coastguard Worker bool SearchInRegs(std::string_view s, const std::vector<std::unique_ptr<RegEx>>& regs) {
81*288bf522SAndroid Build Coastguard Worker   for (auto& reg : regs) {
82*288bf522SAndroid Build Coastguard Worker     if (reg->Search(s)) {
83*288bf522SAndroid Build Coastguard Worker       return true;
84*288bf522SAndroid Build Coastguard Worker     }
85*288bf522SAndroid Build Coastguard Worker   }
86*288bf522SAndroid Build Coastguard Worker   return false;
87*288bf522SAndroid Build Coastguard Worker }
88*288bf522SAndroid Build Coastguard Worker 
89*288bf522SAndroid Build Coastguard Worker }  // namespace simpleperf
90