1 //===-- MIUtilParse.cpp ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Third party headers:
14 #include "MIUtilParse.h"
16 //++ ------------------------------------------------------------------------------------
17 // Details: CRegexParser constructor.
19 // Args: regexStr - Pointer to the regular expression to compile.
23 MIUtilParse::CRegexParser::CRegexParser(const char *regexStr)
24 : m_isValid(llvm_regcomp(&m_emma, regexStr, REG_EXTENDED) == 0)
28 //++ ------------------------------------------------------------------------------------
29 // Details: CRegexParser destructor.
35 MIUtilParse::CRegexParser::~CRegexParser()
37 // Free up memory held within regex.
39 llvm_regfree(&m_emma);
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
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.
58 MIUtilParse::CRegexParser::Execute(const char *input, Match& match, size_t minMatches)
63 std::unique_ptr<llvm_regmatch_t[]> matches(new llvm_regmatch_t[match.m_maxMatches]); // Array of matches
65 if (llvm_regexec(&m_emma, input, match.m_maxMatches, matches.get(), 0) != 0)
69 for (i = 0; i < match.m_maxMatches && matches[i].rm_so >= 0; i++)
71 const int n = matches[i].rm_eo - matches[i].rm_so;
72 match.m_matchStrs[i].assign(input + matches[i].rm_so, n);
74 return i >= minMatches;