2022-03-14 15:11:37 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2021 Google LLC.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SKSL_POSITION
|
|
|
|
#define SKSL_POSITION
|
|
|
|
|
2022-03-17 14:01:59 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2022-03-18 20:40:58 +00:00
|
|
|
#include <string_view>
|
2022-03-17 14:01:59 +00:00
|
|
|
|
2022-03-14 15:11:37 +00:00
|
|
|
#ifndef __has_builtin
|
|
|
|
#define __has_builtin(x) 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace SkSL {
|
|
|
|
|
|
|
|
class Position {
|
|
|
|
public:
|
|
|
|
Position()
|
2022-03-17 15:08:34 +00:00
|
|
|
: fStartOffsetOrLine(-1)
|
2022-03-14 15:11:37 +00:00
|
|
|
, fEndOffset(-1) {}
|
|
|
|
|
2022-03-17 15:08:34 +00:00
|
|
|
static Position Line(int line) {
|
2022-03-14 15:11:37 +00:00
|
|
|
Position result;
|
|
|
|
result.fStartOffsetOrLine = line;
|
|
|
|
result.fEndOffset = -1;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Position Range(int startOffset, int endOffset) {
|
2022-03-25 16:39:10 +00:00
|
|
|
SkASSERT(startOffset <= endOffset);
|
2022-03-14 15:11:37 +00:00
|
|
|
Position result;
|
|
|
|
result.fStartOffsetOrLine = startOffset;
|
|
|
|
result.fEndOffset = endOffset;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if __has_builtin(__builtin_FILE) && __has_builtin(__builtin_LINE)
|
2022-03-17 15:08:34 +00:00
|
|
|
static Position Capture(const char* file = __builtin_FILE(), int line = __builtin_LINE());
|
2022-03-14 15:11:37 +00:00
|
|
|
#else
|
|
|
|
static Position Capture() { return Position(); }
|
|
|
|
#endif // __has_builtin(__builtin_FILE) && __has_builtin(__builtin_LINE)
|
|
|
|
|
|
|
|
bool valid() const {
|
|
|
|
return fStartOffsetOrLine != -1;
|
|
|
|
}
|
|
|
|
|
2022-03-17 14:01:59 +00:00
|
|
|
int line(std::string_view source = std::string_view()) const;
|
2022-03-14 15:11:37 +00:00
|
|
|
|
|
|
|
int startOffset() const {
|
2022-04-01 19:05:18 +00:00
|
|
|
SkASSERT(this->valid());
|
|
|
|
SkASSERTF(fEndOffset != -1, "checking offsets of line position (%d)", fStartOffsetOrLine);
|
2022-03-14 15:11:37 +00:00
|
|
|
return fStartOffsetOrLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
int endOffset() const {
|
2022-04-01 19:05:18 +00:00
|
|
|
SkASSERT(this->valid());
|
|
|
|
SkASSERTF(fEndOffset != -1, "checking offsets of line position (%d)", fStartOffsetOrLine);
|
2022-03-14 15:11:37 +00:00
|
|
|
return fEndOffset;
|
|
|
|
}
|
|
|
|
|
2022-03-25 16:39:10 +00:00
|
|
|
// Returns the position from this through, and including the entirety of, end.
|
|
|
|
Position rangeThrough(Position end) const {
|
|
|
|
if (fEndOffset == -1 || end.fEndOffset == -1) {
|
|
|
|
return *this;
|
|
|
|
}
|
2022-04-01 16:07:39 +00:00
|
|
|
SkASSERTF(this->startOffset() <= end.startOffset() && this->endOffset() <= end.endOffset(),
|
|
|
|
"Invalid range: (%d-%d) - (%d-%d)\n", this->startOffset(), this->endOffset(),
|
|
|
|
end.startOffset(), end.endOffset());
|
2022-03-25 16:39:10 +00:00
|
|
|
return Range(this->startOffset(), end.endOffset());
|
|
|
|
}
|
|
|
|
|
2022-03-14 15:11:37 +00:00
|
|
|
bool operator==(const Position& other) const {
|
2022-03-17 15:08:34 +00:00
|
|
|
return fStartOffsetOrLine == other.fStartOffsetOrLine &&
|
2022-03-14 15:11:37 +00:00
|
|
|
fEndOffset == other.fEndOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const Position& other) const {
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator>(const Position& other) const {
|
|
|
|
return fStartOffsetOrLine > other.fStartOffsetOrLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator>=(const Position& other) const {
|
|
|
|
return fStartOffsetOrLine >= other.fStartOffsetOrLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const Position& other) const {
|
|
|
|
return fStartOffsetOrLine < other.fStartOffsetOrLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<=(const Position& other) const {
|
|
|
|
return fStartOffsetOrLine <= other.fStartOffsetOrLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Contains either a start offset (if fEndOffset != -1) or a line number (if fEndOffset == -1)
|
|
|
|
int32_t fStartOffsetOrLine;
|
|
|
|
int32_t fEndOffset;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace SkSL
|
|
|
|
|
|
|
|
#endif
|