xref: /freebsd/contrib/llvm-project/lldb/source/Utility/RegularExpression.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 //===-- RegularExpression.cpp ---------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Utility/RegularExpression.h"
10 
11 #include <string>
12 
13 using namespace lldb_private;
14 
RegularExpression(llvm::StringRef str,llvm::Regex::RegexFlags flags)15 RegularExpression::RegularExpression(llvm::StringRef str,
16                                      llvm::Regex::RegexFlags flags)
17     : m_regex_text(std::string(str)),
18       // m_regex does not reference str anymore after it is constructed.
19       m_regex(llvm::Regex(str, flags)) {}
20 
RegularExpression(const RegularExpression & rhs)21 RegularExpression::RegularExpression(const RegularExpression &rhs)
22     : RegularExpression(rhs.GetText()) {}
23 
Execute(llvm::StringRef str,llvm::SmallVectorImpl<llvm::StringRef> * matches) const24 bool RegularExpression::Execute(
25     llvm::StringRef str,
26     llvm::SmallVectorImpl<llvm::StringRef> *matches) const {
27   if (!IsValid())
28     return false;
29   return m_regex.match(str, matches);
30 }
31 
IsValid() const32 bool RegularExpression::IsValid() const { return m_regex.isValid(); }
33 
GetText() const34 llvm::StringRef RegularExpression::GetText() const { return m_regex_text; }
35 
GetError() const36 llvm::Error RegularExpression::GetError() const {
37   std::string error;
38   if (!m_regex.isValid(error))
39     return llvm::make_error<llvm::StringError>(error,
40                                                llvm::inconvertibleErrorCode());
41   return llvm::Error::success();
42 }
43