]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/lldb-mi/MIUtilParse.cpp
Vendor import of lldb release_39 branch r287912:
[FreeBSD/FreeBSD.git] / tools / lldb-mi / MIUtilParse.cpp
1 //===-- MIUtilParse.cpp ----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9  
10 // Third party headers:
11 #include <memory>
12
13 // In-house headers:
14 #include "MIUtilParse.h"
15  
16 //++ ------------------------------------------------------------------------------------
17 // Details: CRegexParser constructor.
18 // Type:    Method.
19 // Args:    regexStr - Pointer to the regular expression to compile.
20 // Return:  None.
21 // Throws:  None.
22 //--
23 MIUtilParse::CRegexParser::CRegexParser(const char *regexStr)
24     : m_isValid(llvm_regcomp(&m_emma, regexStr, REG_EXTENDED) == 0)
25 {
26 }
27  
28 //++ ------------------------------------------------------------------------------------
29 // Details: CRegexParser destructor.
30 // Type:    Method.
31 // Args:    None.
32 // Return:  None.
33 // Throws:  None.
34 //--
35 MIUtilParse::CRegexParser::~CRegexParser()
36 {
37     // Free up memory held within regex.
38     if (m_isValid)
39         llvm_regfree(&m_emma);
40 }
41  
42 //++ ------------------------------------------------------------------------------------
43 // Details: CRegexParser regex executer.
44 //          Match the input against the regular expression.  Return an error
45 //          if the number of matches is less than minMatches.  If the default
46 //          minMatches value of 0 is passed, an error will be returned if
47 //          the number of matches is less than the maxMatches value used to
48 //          initialize Match.
49 // Type:    Method.
50 // Args:    input (R) - Pointer to UTF8 text data to be parsed.
51 //          match (RW) - Reference to Match class.
52 //          minMatches (R) - Minimum number of regex matches expected.
53 // Return:  bool - True = minimum matches were met,
54 //                 false = minimum matches were not met or regex failed.
55 // Throws:  None.
56 //--
57 bool
58 MIUtilParse::CRegexParser::Execute(const char *input, Match& match, size_t minMatches)
59 {
60     if (!m_isValid)
61         return false;
62  
63     std::unique_ptr<llvm_regmatch_t[]> matches(new llvm_regmatch_t[match.m_maxMatches]); // Array of matches
64    
65     if (llvm_regexec(&m_emma, input, match.m_maxMatches, matches.get(), 0) != 0)
66         return false;
67  
68     size_t i;
69     for (i = 0; i < match.m_maxMatches && matches[i].rm_so >= 0; i++)
70     {
71         const int n = matches[i].rm_eo - matches[i].rm_so;
72         match.m_matchStrs[i].assign(input + matches[i].rm_so, n);
73     }
74     return i >= minMatches;
75 }