29 static constexpr bool qSizeTypeCanHoldQuint32()
31 return sizeof(qsizetype) >
sizeof(quint32);
34 explicit constexpr SourceLocation(quint32 offset = 0, quint32 length = 0, quint32 line = 0, quint32 column = 0)
38 if constexpr (!qSizeTypeCanHoldQuint32()) {
39 constexpr quint32 maxLength = quint32(std::numeric_limits<qsizetype>::max());
43 Q_ASSERT_X(length < maxLength && offset < maxLength && offset + length < maxLength,
44 "QQmlJS::SourceLocation",
"File size is limited to 2GB!");
45 Q_ASSERT_X(line < maxLength,
"QQmlJS::SourceLocation",
46 "Line exceeded maxLength of 2^31 - 1");
47 Q_ASSERT_X(column < maxLength,
"QQmlJS::SourceLocation",
48 "Column exceeded maxLength of 2^31 - 1");
55 if constexpr (qSizeTypeCanHoldQuint32()) {
56 constexpr qsizetype maxLength = qsizetype(std::numeric_limits<quint32>::max());
58 Q_ASSERT_X(offset + length < maxLength,
"QQmlJS::SourceLocation",
59 "File size is limited to 4GB!");
60 Q_ASSERT_X(line < maxLength,
"QQmlJS::SourceLocation",
61 "Line exceeded maxLength of 2^32 - 1");
62 Q_ASSERT_X(column < maxLength,
"QQmlJS::SourceLocation",
63 "Column exceeded maxLength of 2^32 - 1");
65 return SourceLocation(quint32(offset), quint32(length), quint32(line), quint32(column));
76 template <
typename Predicate>
77 static LocationInfo findLocationIf(QStringView text, Predicate &&predicate,
80 quint32 i = startHint
.isValid() ? startHint.offset : 0;
81 quint32 endLine = startHint
.isValid() ? startHint.startLine : 1;
82 quint32 endColumn = startHint
.isValid() ? startHint.startColumn : 1;
83 const quint32 end = quint32(text.size());
85 for (; i < end; ++i) {
86 if (predicate(i, endLine, endColumn))
87 return LocationInfo{ i, endLine, endColumn };
89 const QChar currentChar = text.at(i);
90 const bool isLineFeed = currentChar == u'\n';
91 const bool isCarriageReturn = currentChar == u'\r';
94 const bool isHalfANewline =
95 isCarriageReturn && (i + 1 < end && text.at(i + 1) == u'\n');
97 if (isHalfANewline || (!isCarriageReturn && !isLineFeed)) {
103 if (predicate(i, endLine, std::numeric_limits<quint32>::max()))
104 return LocationInfo{ i, endLine, endColumn };
109 if (predicate(i, std::numeric_limits<quint32>::max(),
110 std::numeric_limits<quint32>::max())) {
111 return LocationInfo{ i, endLine, endColumn };
120 return LocationInfo{ i, endLine, endColumn };
129 (startHint.startLine < line
130 || (startHint.startLine == line && startHint.startColumn <= column))
134 const auto result = findLocationIf(
136 [line, column](quint32, quint32 currentLine, quint32 currentColumn) {
137 return line <= currentLine && column <= currentColumn;
140 return result.offset;
147 const SourceLocation hint = startHint.offset <= offset ? startHint : SourceLocation{};
149 const auto result = findLocationIf(
151 [offset](quint32 currentOffset, quint32, quint32) {
152 return offset == currentOffset;
155 return std::make_pair(result.startLine, result.startColumn);
166 return SourceLocation(offset, 0, startLine, startColumn);
171 auto [row, column] = rowAndColumnFrom(text, offset + length, *
this);
172 return SourceLocation{ offset + length, 0, row, column };
187 return qHashMulti(seed, location.offset, location.length,
188 location.startLine, location.startColumn);
193 return a.offset == b.offset && a.length == b.length
194 && a.startLine == b.startLine && a.startColumn == b.startColumn;
202 quint32 e = qMax(l1.end(), l2.end());
204 if (l1.offset <= l2.offset)
208 res.length = e - res.offset;