850de30a19
This limits our error reporting to the first 16MB of SkSL code in a program, and error marks are limited to a run of 255 characters or less. In practice, these limits do not affect normal code in any way. This gives us the same tight memory footprint we originally had when positions were stored as `int32 fLine`. Change-Id: Idef04344324870a7b92aca154feb5e1a0121d284 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/533699 Reviewed-by: Ethan Nicholas <ethannicholas@google.com> Commit-Queue: John Stiles <johnstiles@google.com> Auto-Submit: John Stiles <johnstiles@google.com>
103 lines
2.6 KiB
C++
103 lines
2.6 KiB
C++
/*
|
|
* 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
|
|
|
|
#include "include/core/SkTypes.h"
|
|
#include <string_view>
|
|
|
|
namespace SkSL {
|
|
|
|
class Position {
|
|
public:
|
|
Position()
|
|
: fStartOffset(-1)
|
|
, fLength(0) {}
|
|
|
|
static Position Range(int startOffset, int endOffset) {
|
|
SkASSERT(startOffset <= endOffset);
|
|
SkASSERT(startOffset <= 0xFFFFFF);
|
|
int length = endOffset - startOffset;
|
|
Position result;
|
|
result.fStartOffset = startOffset;
|
|
result.fLength = length <= 0xFF ? length : 0xFF;
|
|
return result;
|
|
}
|
|
|
|
bool valid() const {
|
|
return fStartOffset != -1;
|
|
}
|
|
|
|
int line(std::string_view source) const;
|
|
|
|
int startOffset() const {
|
|
SkASSERT(this->valid());
|
|
return fStartOffset;
|
|
}
|
|
|
|
int endOffset() const {
|
|
SkASSERT(this->valid());
|
|
return fStartOffset + fLength;
|
|
}
|
|
|
|
// Returns the position from this through, and including the entirety of, end.
|
|
Position rangeThrough(Position end) const {
|
|
if (fStartOffset == -1 || end.fStartOffset == -1) {
|
|
return *this;
|
|
}
|
|
SkASSERTF(this->startOffset() <= end.startOffset() && this->endOffset() <= end.endOffset(),
|
|
"Invalid range: (%d-%d) - (%d-%d)\n", this->startOffset(), this->endOffset(),
|
|
end.startOffset(), end.endOffset());
|
|
return Range(this->startOffset(), end.endOffset());
|
|
}
|
|
|
|
// Returns a position representing the character immediately after this position
|
|
Position after() const {
|
|
int endOffset = this->endOffset();
|
|
return Range(endOffset, endOffset + 1);
|
|
}
|
|
|
|
bool operator==(const Position& other) const {
|
|
return fStartOffset == other.fStartOffset && fLength == other.fLength;
|
|
}
|
|
|
|
bool operator!=(const Position& other) const {
|
|
return !(*this == other);
|
|
}
|
|
|
|
bool operator>(const Position& other) const {
|
|
return fStartOffset > other.fStartOffset;
|
|
}
|
|
|
|
bool operator>=(const Position& other) const {
|
|
return fStartOffset >= other.fStartOffset;
|
|
}
|
|
|
|
bool operator<(const Position& other) const {
|
|
return fStartOffset < other.fStartOffset;
|
|
}
|
|
|
|
bool operator<=(const Position& other) const {
|
|
return fStartOffset <= other.fStartOffset;
|
|
}
|
|
|
|
private:
|
|
int32_t fStartOffset : 24;
|
|
uint32_t fLength : 8;
|
|
};
|
|
|
|
struct ForLoopPositions {
|
|
Position initPosition = Position();
|
|
Position conditionPosition = Position();
|
|
Position nextPosition = Position();
|
|
};
|
|
|
|
} // namespace SkSL
|
|
|
|
#endif
|