blob: c1e4f511defb41e6229827ad4f3a4b0a300c5d2d [file] [log] [blame]
Yabin Cuif00f4fc2022-11-23 15:15:30 -08001/*
2 * Copyright (C) 2022 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 <memory>
20#include <optional>
21#include <string>
22#include <string_view>
23
24namespace simpleperf {
25
26class RegExMatch {
27 public:
28 virtual ~RegExMatch();
29 virtual bool IsValid() const = 0;
30 virtual std::string GetField(size_t index) const = 0;
31 virtual void MoveToNextMatch() = 0;
32};
33
34// A wrapper of std::regex, converting std::regex_error exception into return value.
35class RegEx {
36 public:
37 static std::unique_ptr<RegEx> Create(std::string_view pattern);
38 virtual ~RegEx() {}
39 const std::string& GetPattern() const { return pattern_; }
40
41 virtual bool Match(std::string_view s) const = 0;
42 virtual bool Search(std::string_view s) const = 0;
43 // Always return a not-null RegExMatch. If no match, RegExMatch->IsValid() is false.
44 virtual std::unique_ptr<RegExMatch> SearchAll(std::string_view s) const = 0;
45 virtual std::optional<std::string> Replace(const std::string& s,
46 const std::string& format) const = 0;
47
48 protected:
49 RegEx(std::string_view pattern) : pattern_(pattern) {}
50
51 std::string pattern_;
52};
53
54} // namespace simpleperf