GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/parser/line_buffer.cpp Lines: 1 39 2.6 %
Date: 2021-03-22 Branches: 2 70 2.9 %

Line Exec Source
1
/*********************                                                        */
2
/*! \file line_buffer.cpp
3
 ** \verbatim
4
 ** Top contributors (to current version):
5
 **   Andres Noetzli, Mathias Preiner, Aina Niemetz
6
 ** This file is part of the CVC4 project.
7
 ** Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
8
 ** in the top-level source directory and their institutional affiliations.
9
 ** All rights reserved.  See the file COPYING in the top-level source
10
 ** directory for licensing information.\endverbatim
11
 **
12
 ** \brief The LineBuffer class stores lines from an input stream
13
 **
14
 ** For each line, the class allocates a separate buffer.
15
 **/
16
17
#include "parser/line_buffer.h"
18
19
#include <cstring>
20
#include <iostream>
21
#include <string>
22
23
#include "base/check.h"
24
25
namespace CVC4 {
26
namespace parser {
27
28
LineBuffer::LineBuffer(std::istream* stream) : d_stream(stream) {}
29
30
LineBuffer::~LineBuffer() {
31
  for (size_t i = 0; i < d_lines.size(); i++) {
32
    delete[] d_lines[i];
33
  }
34
}
35
36
uint8_t* LineBuffer::getPtr(size_t line, size_t pos_in_line) {
37
  if (!readToLine(line)) {
38
    return NULL;
39
  }
40
  Assert(pos_in_line < d_sizes[line]);
41
  return d_lines[line] + pos_in_line;
42
}
43
44
uint8_t* LineBuffer::getPtrWithOffset(size_t line, size_t pos_in_line,
45
                                   size_t offset) {
46
  if (!readToLine(line)) {
47
    return NULL;
48
  }
49
  if (pos_in_line + offset >= d_sizes[line]) {
50
    return getPtrWithOffset(line + 1, 0,
51
                            offset - (d_sizes[line] - pos_in_line - 1));
52
  }
53
  Assert(pos_in_line + offset < d_sizes[line]);
54
  return d_lines[line] + pos_in_line + offset;
55
}
56
57
bool LineBuffer::isPtrBefore(uint8_t* ptr, size_t line, size_t pos_in_line) {
58
  for (size_t j = 0; j < line; j++)
59
  {
60
    // NOTE: std::less is guaranteed to give consistent results when comparing
61
    // pointers of different arrays (in contrast to built-in comparison
62
    // operators).
63
    size_t i = line - j;
64
    uint8_t* end = d_lines[i] + ((i == line) ? pos_in_line : d_sizes[i]);
65
    if (std::less<uint8_t*>()(d_lines[i] - 1, ptr) &&
66
        std::less<uint8_t*>()(ptr, end)) {
67
      return true;
68
    }
69
  }
70
  return false;
71
}
72
73
bool LineBuffer::readToLine(size_t line_size)
74
{
75
  while (line_size >= d_lines.size())
76
  {
77
    if (!(*d_stream)) {
78
      return false;
79
    }
80
81
    std::string line;
82
    std::getline(*d_stream, line);
83
    uint8_t* segment = new uint8_t[line.size() + 1];
84
    std::memcpy(segment, line.c_str(), line.size());
85
    segment[line.size()] = LineBuffer::NewLineChar;
86
    d_lines.push_back(segment);
87
    d_sizes.push_back(line.size() + 1);
88
  }
89
  return true;
90
}
91
92
}  // namespace parser
93
26664
}  // namespace CVC4