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
|
|
|
namespace SkSL {
|
|
|
|
|
|
|
|
class Position {
|
|
|
|
public:
|
|
|
|
Position()
|
2022-04-08 15:37:08 +00:00
|
|
|
: fStartOffset(-1)
|
2022-04-26 13:31:26 +00:00
|
|
|
, fLength(0) {}
|
2022-03-14 15:11:37 +00:00
|
|
|
|
|
|
|
static Position Range(int startOffset, int endOffset) {
|
2022-03-25 16:39:10 +00:00
|
|
|
SkASSERT(startOffset <= endOffset);
|
2022-04-26 14:12:38 +00:00
|
|
|
SkASSERT(startOffset <= 0xFFFFFF);
|
|
|
|
int length = endOffset - startOffset;
|
2022-03-14 15:11:37 +00:00
|
|
|
Position result;
|
2022-04-08 15:37:08 +00:00
|
|
|
result.fStartOffset = startOffset;
|
2022-04-26 14:12:38 +00:00
|
|
|
result.fLength = length <= 0xFF ? length : 0xFF;
|
2022-03-14 15:11:37 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool valid() const {
|
2022-04-08 15:37:08 +00:00
|
|
|
return fStartOffset != -1;
|
2022-03-14 15:11:37 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 15:37:08 +00:00
|
|
|
int line(std::string_view source) const;
|
2022-03-14 15:11:37 +00:00
|
|
|
|
|
|
|
int startOffset() const {
|
2022-04-01 19:05:18 +00:00
|
|
|
SkASSERT(this->valid());
|
2022-04-08 15:37:08 +00:00
|
|
|
return fStartOffset;
|
2022-03-14 15:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int endOffset() const {
|
2022-04-01 19:05:18 +00:00
|
|
|
SkASSERT(this->valid());
|
2022-04-26 13:31:26 +00:00
|
|
|
return fStartOffset + fLength;
|
2022-03-14 15:11:37 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2022-04-26 13:31:26 +00:00
|
|
|
if (fStartOffset == -1 || end.fStartOffset == -1) {
|
2022-03-25 16:39:10 +00:00
|
|
|
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-04-06 15:33:00 +00:00
|
|
|
// Returns a position representing the character immediately after this position
|
|
|
|
Position after() const {
|
2022-04-26 13:31:26 +00:00
|
|
|
int endOffset = this->endOffset();
|
|
|
|
return Range(endOffset, endOffset + 1);
|
2022-04-06 15:33:00 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 15:11:37 +00:00
|
|
|
bool operator==(const Position& other) const {
|
2022-04-26 13:31:26 +00:00
|
|
|
return fStartOffset == other.fStartOffset && fLength == other.fLength;
|
2022-03-14 15:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const Position& other) const {
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator>(const Position& other) const {
|
2022-04-08 15:37:08 +00:00
|
|
|
return fStartOffset > other.fStartOffset;
|
2022-03-14 15:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator>=(const Position& other) const {
|
2022-04-08 15:37:08 +00:00
|
|
|
return fStartOffset >= other.fStartOffset;
|
2022-03-14 15:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const Position& other) const {
|
2022-04-08 15:37:08 +00:00
|
|
|
return fStartOffset < other.fStartOffset;
|
2022-03-14 15:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<=(const Position& other) const {
|
2022-04-08 15:37:08 +00:00
|
|
|
return fStartOffset <= other.fStartOffset;
|
2022-03-14 15:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-04-26 14:12:38 +00:00
|
|
|
int32_t fStartOffset : 24;
|
|
|
|
uint32_t fLength : 8;
|
2022-03-14 15:11:37 +00:00
|
|
|
};
|
|
|
|
|
2022-04-20 20:51:26 +00:00
|
|
|
struct ForLoopPositions {
|
|
|
|
Position initPosition = Position();
|
|
|
|
Position conditionPosition = Position();
|
|
|
|
Position nextPosition = Position();
|
|
|
|
};
|
|
|
|
|
2022-03-14 15:11:37 +00:00
|
|
|
} // namespace SkSL
|
|
|
|
|
|
|
|
#endif
|