2018-08-21 05:27:52 +00:00
|
|
|
// Copyright 2017-2018 ccls Authors
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-10-29 04:21:21 +00:00
|
|
|
#include "position.hh"
|
2017-04-05 08:06:18 +00:00
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
#include "serializer.hh"
|
2017-04-05 08:06:18 +00:00
|
|
|
|
2018-04-08 06:32:35 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2017-04-05 08:06:18 +00:00
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
namespace ccls {
|
2018-08-09 17:08:14 +00:00
|
|
|
Position Position::FromString(const std::string &encoded) {
|
|
|
|
char *p = const_cast<char *>(encoded.c_str());
|
2018-04-08 06:32:35 +00:00
|
|
|
int16_t line = int16_t(strtol(p, &p, 10)) - 1;
|
2018-03-15 04:12:55 +00:00
|
|
|
assert(*p == ':');
|
|
|
|
p++;
|
2018-04-08 06:32:35 +00:00
|
|
|
int16_t column = int16_t(strtol(p, &p, 10)) - 1;
|
|
|
|
return {line, column};
|
2017-04-05 08:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Position::ToString() {
|
2018-04-08 06:32:35 +00:00
|
|
|
char buf[99];
|
|
|
|
snprintf(buf, sizeof buf, "%d:%d", line + 1, column + 1);
|
|
|
|
return buf;
|
2017-04-05 08:06:18 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
Range Range::FromString(const std::string &encoded) {
|
2018-04-08 06:32:35 +00:00
|
|
|
Position start, end;
|
2018-08-09 17:08:14 +00:00
|
|
|
char *p = const_cast<char *>(encoded.c_str());
|
2018-01-14 19:39:29 +00:00
|
|
|
start.line = int16_t(strtol(p, &p, 10)) - 1;
|
2018-01-07 23:52:46 +00:00
|
|
|
assert(*p == ':');
|
|
|
|
p++;
|
2018-01-14 19:39:29 +00:00
|
|
|
start.column = int16_t(strtol(p, &p, 10)) - 1;
|
2018-01-07 23:52:46 +00:00
|
|
|
assert(*p == '-');
|
|
|
|
p++;
|
|
|
|
|
2018-01-14 19:39:29 +00:00
|
|
|
end.line = int16_t(strtol(p, &p, 10)) - 1;
|
2018-01-07 23:52:46 +00:00
|
|
|
assert(*p == ':');
|
|
|
|
p++;
|
2018-01-14 19:39:29 +00:00
|
|
|
end.column = int16_t(strtol(p, nullptr, 10)) - 1;
|
2018-04-08 06:32:35 +00:00
|
|
|
return {start, end};
|
2017-04-05 08:06:18 +00:00
|
|
|
}
|
|
|
|
|
2017-04-11 05:43:01 +00:00
|
|
|
bool Range::Contains(int line, int column) const {
|
2018-04-08 06:32:35 +00:00
|
|
|
if (line > INT16_MAX)
|
|
|
|
return false;
|
|
|
|
Position p{int16_t(line), int16_t(std::min(column, INT16_MAX))};
|
|
|
|
return !(p < start) && p < end;
|
2017-04-11 05:43:01 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 05:01:31 +00:00
|
|
|
Range Range::RemovePrefix(Position position) const {
|
|
|
|
return {std::min(std::max(position, start), end), end};
|
|
|
|
}
|
|
|
|
|
2017-04-05 08:06:18 +00:00
|
|
|
std::string Range::ToString() {
|
2018-04-08 06:32:35 +00:00
|
|
|
char buf[99];
|
|
|
|
snprintf(buf, sizeof buf, "%d:%d-%d:%d", start.line + 1, start.column + 1,
|
|
|
|
end.line + 1, end.column + 1);
|
|
|
|
return buf;
|
2017-05-19 07:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Position
|
2018-08-09 17:08:14 +00:00
|
|
|
void Reflect(Reader &visitor, Position &value) {
|
2018-01-08 01:41:24 +00:00
|
|
|
if (visitor.Format() == SerializeFormat::Json) {
|
2018-04-08 06:32:35 +00:00
|
|
|
value = Position::FromString(visitor.GetString());
|
2018-01-07 09:27:14 +00:00
|
|
|
} else {
|
2018-01-08 04:10:16 +00:00
|
|
|
Reflect(visitor, value.line);
|
|
|
|
Reflect(visitor, value.column);
|
2018-01-07 05:08:25 +00:00
|
|
|
}
|
2017-05-19 07:02:01 +00:00
|
|
|
}
|
2018-08-09 17:08:14 +00:00
|
|
|
void Reflect(Writer &visitor, Position &value) {
|
2018-01-07 09:27:14 +00:00
|
|
|
if (visitor.Format() == SerializeFormat::Json) {
|
|
|
|
std::string output = value.ToString();
|
|
|
|
visitor.String(output.c_str(), output.size());
|
|
|
|
} else {
|
|
|
|
Reflect(visitor, value.line);
|
|
|
|
Reflect(visitor, value.column);
|
|
|
|
}
|
2017-05-19 07:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Range
|
2018-08-09 17:08:14 +00:00
|
|
|
void Reflect(Reader &visitor, Range &value) {
|
2018-01-08 01:41:24 +00:00
|
|
|
if (visitor.Format() == SerializeFormat::Json) {
|
2018-04-08 06:32:35 +00:00
|
|
|
value = Range::FromString(visitor.GetString());
|
2018-01-07 09:27:14 +00:00
|
|
|
} else {
|
2018-01-08 04:10:16 +00:00
|
|
|
Reflect(visitor, value.start.line);
|
|
|
|
Reflect(visitor, value.start.column);
|
|
|
|
Reflect(visitor, value.end.line);
|
|
|
|
Reflect(visitor, value.end.column);
|
2018-01-07 05:08:25 +00:00
|
|
|
}
|
2017-05-19 07:02:01 +00:00
|
|
|
}
|
2018-08-09 17:08:14 +00:00
|
|
|
void Reflect(Writer &visitor, Range &value) {
|
2018-01-07 09:27:14 +00:00
|
|
|
if (visitor.Format() == SerializeFormat::Json) {
|
|
|
|
std::string output = value.ToString();
|
|
|
|
visitor.String(output.c_str(), output.size());
|
|
|
|
} else {
|
|
|
|
Reflect(visitor, value.start.line);
|
|
|
|
Reflect(visitor, value.start.column);
|
|
|
|
Reflect(visitor, value.end.line);
|
|
|
|
Reflect(visitor, value.end.column);
|
|
|
|
}
|
2018-01-06 21:46:41 +00:00
|
|
|
}
|
2018-10-28 17:49:31 +00:00
|
|
|
} // namespace ccls
|