File: MIUtilParse.cpp

package info (click to toggle)
llvm-toolchain-3.8 1%3A3.8.1-24
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 379,280 kB
  • ctags: 388,501
  • sloc: cpp: 2,309,705; ansic: 477,070; objc: 100,918; asm: 97,974; python: 95,911; sh: 18,634; makefile: 7,294; perl: 5,584; ml: 5,460; pascal: 4,661; lisp: 2,548; xml: 686; cs: 350; php: 212; csh: 117
file content (75 lines) | stat: -rw-r--r-- 2,600 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//===-- MIUtilParse.cpp ----------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
 
// Third party headers:
#include <memory>

// In-house headers:
#include "MIUtilParse.h"
 
//++ ------------------------------------------------------------------------------------
// Details: CRegexParser constructor.
// Type:    Method.
// Args:    regexStr - Pointer to the regular expression to compile.
// Return:  None.
// Throws:  None.
//--
MIUtilParse::CRegexParser::CRegexParser(const char *regexStr)
    : m_isValid(llvm_regcomp(&m_emma, regexStr, REG_EXTENDED) == 0)
{
}
 
//++ ------------------------------------------------------------------------------------
// Details: CRegexParser destructor.
// Type:    Method.
// Args:    None.
// Return:  None.
// Throws:  None.
//--
MIUtilParse::CRegexParser::~CRegexParser()
{
    // Free up memory held within regex.
    if (m_isValid)
        llvm_regfree(&m_emma);
}
 
//++ ------------------------------------------------------------------------------------
// Details: CRegexParser regex executer.
//          Match the input against the regular expression.  Return an error
//          if the number of matches is less than minMatches.  If the default
//          minMatches value of 0 is passed, an error will be returned if
//          the number of matches is less than the maxMatches value used to
//          initialize Match.
// Type:    Method.
// Args:    input (R) - Pointer to UTF8 text data to be parsed.
//          match (RW) - Reference to Match class.
//          minMatches (R) - Minimum number of regex matches expected.
// Return:  bool - True = minimum matches were met,
//                 false = minimum matches were not met or regex failed.
// Throws:  None.
//--
bool
MIUtilParse::CRegexParser::Execute(const char *input, Match& match, size_t minMatches)
{
    if (!m_isValid)
        return false;
 
    std::unique_ptr<llvm_regmatch_t[]> matches(new llvm_regmatch_t[match.m_maxMatches]); // Array of matches
   
    if (llvm_regexec(&m_emma, input, match.m_maxMatches, matches.get(), 0) != 0)
        return false;
 
    size_t i;
    for (i = 0; i < match.m_maxMatches && matches[i].rm_so >= 0; i++)
    {
        const int n = matches[i].rm_eo - matches[i].rm_so;
        match.m_matchStrs[i].assign(input + matches[i].rm_so, n);
    }
    return i >= minMatches;
}