Flutter Engine
The Flutter Engine
SkSLPosition.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 Google LLC.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SKSL_POSITION
9#define SKSL_POSITION
10
12
13#include <cstdint>
14#include <string_view>
15
16namespace SkSL {
17
18class Position {
19public:
21 : fStartOffset(-1)
22 , fLength(0) {}
23
29 result.fStartOffset = startOffset;
30 result.fLength = length <= 0xFF ? length : 0xFF;
31 return result;
32 }
33
34 bool valid() const {
35 return fStartOffset != -1;
36 }
37
38 int line(std::string_view source) const;
39
40 int startOffset() const {
41 SkASSERT(this->valid());
42 return fStartOffset;
43 }
44
45 int endOffset() const {
46 SkASSERT(this->valid());
47 return fStartOffset + fLength;
48 }
49
50 // Returns the position from this through, and including the entirety of, end.
52 if (fStartOffset == -1 || end.fStartOffset == -1) {
53 return *this;
54 }
55 SkASSERTF(this->startOffset() <= end.startOffset() && this->endOffset() <= end.endOffset(),
56 "Invalid range: (%d-%d) - (%d-%d)\n", this->startOffset(), this->endOffset(),
57 end.startOffset(), end.endOffset());
58 return Range(this->startOffset(), end.endOffset());
59 }
60
61 // Returns a position representing the character immediately after this position
62 Position after() const {
63 int endOffset = this->endOffset();
64 return Range(endOffset, endOffset + 1);
65 }
66
67 bool operator==(const Position& other) const {
68 return fStartOffset == other.fStartOffset && fLength == other.fLength;
69 }
70
71 bool operator!=(const Position& other) const {
72 return !(*this == other);
73 }
74
75 bool operator>(const Position& other) const {
76 return fStartOffset > other.fStartOffset;
77 }
78
79 bool operator>=(const Position& other) const {
80 return fStartOffset >= other.fStartOffset;
81 }
82
83 bool operator<(const Position& other) const {
84 return fStartOffset < other.fStartOffset;
85 }
86
87 bool operator<=(const Position& other) const {
88 return fStartOffset <= other.fStartOffset;
89 }
90
91 static constexpr int kMaxOffset = 0x7FFFFF;
92
93private:
94 int32_t fStartOffset : 24;
95 uint32_t fLength : 8;
96};
97
102};
103
104} // namespace SkSL
105
106#endif
#define SkASSERT(cond)
Definition: SkAssert.h:116
#define SkASSERTF(cond, fmt,...)
Definition: SkAssert.h:117
bool operator<(const Position &other) const
Definition: SkSLPosition.h:83
int line(std::string_view source) const
bool operator==(const Position &other) const
Definition: SkSLPosition.h:67
bool operator!=(const Position &other) const
Definition: SkSLPosition.h:71
bool operator<=(const Position &other) const
Definition: SkSLPosition.h:87
int startOffset() const
Definition: SkSLPosition.h:40
bool operator>=(const Position &other) const
Definition: SkSLPosition.h:79
bool valid() const
Definition: SkSLPosition.h:34
bool operator>(const Position &other) const
Definition: SkSLPosition.h:75
static constexpr int kMaxOffset
Definition: SkSLPosition.h:91
Position after() const
Definition: SkSLPosition.h:62
static Position Range(int startOffset, int endOffset)
Definition: SkSLPosition.h:24
Position rangeThrough(Position end) const
Definition: SkSLPosition.h:51
int endOffset() const
Definition: SkSLPosition.h:45
SkBitmap source
Definition: examples.cpp:28
glong glong end
GAsyncResult * result
size_t length