Regex.h (1895B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #pragma once 4 #include <functional> 5 #include <memory> 6 #include <string_view> 7 8 9 namespace AppInstaller::Regex 10 { 11 // Options for regular expression use. 12 enum class Options 13 { 14 None = 0, 15 CaseInsensitive, 16 }; 17 18 // Stores the compiled regular expression. 19 // All pattern strings are considered UTF-8. 20 // All input strings are considered UTF-16, as this is what ICU operates on internally. 21 struct Expression 22 { 23 Expression(); 24 Expression(std::string_view pattern, Options options = Options::None); 25 26 Expression(const Expression&); 27 Expression& operator=(const Expression&); 28 29 Expression(Expression&&) noexcept; 30 Expression& operator=(Expression&&) noexcept; 31 32 ~Expression(); 33 34 // Determines if the expression contains a value. 35 operator bool() const; 36 37 // Returns a value indicating whether the *entire* input matches the expression. 38 bool IsMatch(std::wstring_view input) const; 39 40 // Replaces all matches in the input with the replacement. 41 std::wstring Replace(std::wstring_view input, std::wstring_view replacement) const; 42 43 // For each section of the input, invoke the given functor. This allows the caller 44 // to iterate over the entire string, taking action as appropriate for each part. 45 // The parameters are: 46 // bool :: indicates whether this section was a match 47 // string_view :: the text for the section 48 // The functor should return true to continue the loop, or false to break it. 49 void ForEach(std::wstring_view input, const std::function<bool(bool,std::wstring_view)>& f) const; 50 51 private: 52 struct impl; 53 std::unique_ptr<impl> pImpl; 54 }; 55 }