forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline_column_map.h
119 lines (96 loc) · 3.84 KB
/
line_column_map.h
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2017-2020 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// LineColumnMap translates byte-offset into line-column.
//
// usage:
// absl::string_view text = ...;
// LineColumnMap lcmap(text);
// token_error_offset = ...; // some file diagnosis
// LineColumn error_location = lcmap(token_error_offset);
// std::cout << "Error at line " << error_location << std::endl;
#ifndef VERIBLE_COMMON_STRINGS_LINE_COLUMN_MAP_H_
#define VERIBLE_COMMON_STRINGS_LINE_COLUMN_MAP_H_
#include <algorithm>
#include <cstddef>
#include <iosfwd>
#include <vector>
#include "absl/strings/string_view.h"
namespace verible {
// Pair: line number and column number.
struct LineColumn {
int line; // 0-based index
int column; // 0-based index
constexpr bool operator==(const LineColumn& r) const {
return line == r.line && column == r.column;
}
constexpr bool operator<(const LineColumn& r) const {
if (line < r.line) return true;
if (line > r.line) return false;
return column < r.column;
}
constexpr bool operator>=(const LineColumn& r) const { return !(*this < r); }
};
std::ostream& operator<<(std::ostream&, const LineColumn&);
// A complete range.
struct LineColumnRange {
LineColumn start; // Inclusive
LineColumn end; // Exclusive
constexpr bool operator==(const LineColumnRange& r) const {
return start == r.start && end == r.end;
}
constexpr bool PositionInRange(const LineColumn& pos) const {
return pos >= start && pos < end;
}
};
std::ostream& operator<<(std::ostream&, const LineColumnRange&);
class LineColumnMap {
public:
explicit LineColumnMap(absl::string_view);
explicit LineColumnMap(const std::vector<absl::string_view>& lines);
void Clear() { beginning_of_line_offsets_.clear(); }
bool Empty() const { return beginning_of_line_offsets_.empty(); }
// Returns byte offset corresponding to the 0-based line number.
// If lineno exceeds number of lines, return the final byte offset.
int OffsetAtLine(size_t lineno) const {
const size_t index =
std::min(lineno, beginning_of_line_offsets_.size() - 1);
return beginning_of_line_offsets_[index];
}
// Get line number at the given byte offset.
int LineAtOffset(int bytes_offset) const;
// Get line and column at the given offset. The column takes multi-byte
// encodings into account, so the column represents the true character not
// simply the byte-offset within the line.
//
// TODO(hzeller): technically, we don't need the base as we already got it
// in the constructor, but change separately after lifetime questions have
// been considered.
LineColumn GetLineColAtOffset(absl::string_view base, int bytes_offset) const;
const std::vector<int>& GetBeginningOfLineOffsets() const {
return beginning_of_line_offsets_;
}
// Byte-offset of start of last line after last newline in file.
int LastLineOffset() const {
if (Empty()) return 0;
return beginning_of_line_offsets_.back();
}
private:
// Index: line number, Value: byte offset that starts the line.
// The first value will always be 0 because the beginning of the first line
// has offset 0. The last value will be the offset following the last
// newline, which is the length of the original text.
std::vector<int> beginning_of_line_offsets_;
};
} // namespace verible
#endif // VERIBLE_COMMON_STRINGS_LINE_COLUMN_MAP_H_