-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_range.h
More file actions
42 lines (36 loc) · 1.23 KB
/
file_range.h
File metadata and controls
42 lines (36 loc) · 1.23 KB
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
#pragma once
#include <algorithm>
#include <utility>
struct FilePos {
unsigned int index;
unsigned int line;
unsigned int col;
};
struct FileRange {
FilePos start;
unsigned int length;
constexpr FileRange(FilePos _start, unsigned int _length): start(_start), length(_length) { }
constexpr static FileRange firstLast(FilePos a, FilePos b) noexcept {
if (a.index > b.index) std::swap(a, b);
return FileRange { a, b.index - a.index + 1 };
}
constexpr static FileRange startEnd(FilePos a, FilePos b) {
if (a.index > b.index) throw 1932112;
return FileRange { a, b.index - a.index };
}
static constexpr FileRange none() noexcept {
return FileRange(FilePos{0, 0, 0}, 0);
}
constexpr unsigned int startIndex() const noexcept { return start.index; }
constexpr unsigned int lastIndex() const noexcept { return start.index + length - 1; }
static constexpr FileRange span(const FileRange& a, const FileRange& b) noexcept {
if (a.length == 0) return b;
if (b.length == 0) return a;
const FilePos& first = a.startIndex() < b.startIndex() ? a.start : b.start;
const unsigned int lastIndex = std::max(a.startIndex() + a.length, b.startIndex() + b.length) - 1;
return FileRange(
first,
lastIndex - first.index
);
}
};