2017-04-05 08:06:18 +00:00
|
|
|
#include "position.h"
|
|
|
|
|
2017-04-07 06:10:17 +00:00
|
|
|
namespace {
|
|
|
|
// Skips until the character immediately following |skip_after|.
|
|
|
|
const char* SkipAfter(const char* input, char skip_after) {
|
|
|
|
while (*input && *input != skip_after)
|
|
|
|
++input;
|
|
|
|
++input;
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2017-04-05 08:06:18 +00:00
|
|
|
Position::Position() {}
|
|
|
|
|
2017-04-07 06:10:17 +00:00
|
|
|
Position::Position(int32_t line, int32_t column)
|
|
|
|
: line(line), column(column) {}
|
2017-04-05 08:06:18 +00:00
|
|
|
|
|
|
|
Position::Position(const char* encoded) {
|
|
|
|
assert(encoded);
|
|
|
|
line = atoi(encoded);
|
|
|
|
|
2017-04-07 06:10:17 +00:00
|
|
|
encoded = SkipAfter(encoded, ':');
|
2017-04-05 08:06:18 +00:00
|
|
|
assert(encoded);
|
|
|
|
column = atoi(encoded);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Position::ToString() {
|
|
|
|
// Output looks like this:
|
|
|
|
//
|
2017-04-07 06:10:17 +00:00
|
|
|
// 1:2
|
2017-04-05 08:06:18 +00:00
|
|
|
//
|
|
|
|
// 1 => line
|
|
|
|
// 2 => column
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
result += std::to_string(line);
|
|
|
|
result += ':';
|
|
|
|
result += std::to_string(column);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Position::ToPrettyString(const std::string& filename) {
|
|
|
|
// Output looks like this:
|
|
|
|
//
|
2017-04-07 06:10:17 +00:00
|
|
|
// 1:2:3
|
2017-04-05 08:06:18 +00:00
|
|
|
//
|
2017-04-07 06:10:17 +00:00
|
|
|
// 1 => filename
|
|
|
|
// 2 => line
|
|
|
|
// 3 => column
|
2017-04-05 08:06:18 +00:00
|
|
|
|
|
|
|
std::string result;
|
|
|
|
result += filename;
|
|
|
|
result += ':';
|
|
|
|
result += std::to_string(line);
|
|
|
|
result += ':';
|
|
|
|
result += std::to_string(column);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Position::operator==(const Position& that) const {
|
|
|
|
return line == that.line && column == that.column;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Position::operator!=(const Position& that) const { return !(*this == that); }
|
|
|
|
|
|
|
|
bool Position::operator<(const Position& that) const {
|
2017-05-11 07:20:00 +00:00
|
|
|
if (line < that.line)
|
|
|
|
return true;
|
|
|
|
return line == that.line && column < that.column;
|
2017-04-05 08:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Range::Range() {}
|
|
|
|
|
2017-04-19 05:28:33 +00:00
|
|
|
Range::Range(Position start, Position end) : start(start), end(end) {}
|
2017-04-05 08:06:18 +00:00
|
|
|
|
2017-04-07 06:10:17 +00:00
|
|
|
Range::Range(const char* encoded) {
|
2017-04-05 08:06:18 +00:00
|
|
|
end = start;
|
2017-04-05 08:29:15 +00:00
|
|
|
|
2017-04-07 06:10:17 +00:00
|
|
|
start.line = atoi(encoded);
|
|
|
|
|
|
|
|
encoded = SkipAfter(encoded, ':');
|
|
|
|
assert(encoded);
|
|
|
|
start.column = atoi(encoded);
|
|
|
|
|
|
|
|
encoded = SkipAfter(encoded, '-');
|
|
|
|
assert(encoded);
|
2017-04-05 08:06:18 +00:00
|
|
|
end.line = atoi(encoded);
|
|
|
|
|
2017-04-07 06:10:17 +00:00
|
|
|
encoded = SkipAfter(encoded, ':');
|
2017-04-05 08:06:18 +00:00
|
|
|
assert(encoded);
|
|
|
|
end.column = atoi(encoded);
|
|
|
|
}
|
|
|
|
|
2017-04-11 05:43:01 +00:00
|
|
|
bool Range::Contains(int line, int column) const {
|
|
|
|
if (line == start.line && line == end.line)
|
|
|
|
return column >= start.column && column <= end.column;
|
|
|
|
if (line == start.line)
|
|
|
|
return column >= start.column;
|
|
|
|
if (line == end.line)
|
|
|
|
return column <= end.column;
|
|
|
|
if (line > start.line && line < end.line)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-05 08:06:18 +00:00
|
|
|
std::string Range::ToString() {
|
2017-04-07 06:10:17 +00:00
|
|
|
// Output looks like this:
|
|
|
|
//
|
|
|
|
// *1:2-3:4
|
|
|
|
//
|
|
|
|
// * => if present, range is interesting
|
|
|
|
// 1 => start line
|
|
|
|
// 2 => start column
|
|
|
|
// 3 => end line
|
|
|
|
// 4 => end column
|
|
|
|
|
2017-04-05 08:06:18 +00:00
|
|
|
std::string output;
|
2017-04-05 08:29:15 +00:00
|
|
|
|
2017-04-07 06:10:17 +00:00
|
|
|
output += std::to_string(start.line);
|
|
|
|
output += ':';
|
|
|
|
output += std::to_string(start.column);
|
|
|
|
output += '-';
|
2017-04-05 08:06:18 +00:00
|
|
|
output += std::to_string(end.line);
|
2017-04-07 06:10:17 +00:00
|
|
|
output += ':';
|
2017-04-05 08:06:18 +00:00
|
|
|
output += std::to_string(end.column);
|
2017-04-05 08:29:15 +00:00
|
|
|
|
2017-04-05 08:06:18 +00:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Range::operator==(const Range& that) const {
|
|
|
|
return start == that.start && end == that.end;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Range::operator!=(const Range& that) const { return !(*this == that); }
|
|
|
|
|
|
|
|
bool Range::operator<(const Range& that) const {
|
2017-05-11 07:38:57 +00:00
|
|
|
if (start < that.start)
|
|
|
|
return true;
|
|
|
|
return start == that.start && end < that.end;
|
2017-04-05 08:06:18 +00:00
|
|
|
}
|