28 static constexpr bool qSizeTypeCanHoldQuint32()
30 return sizeof(qsizetype) >
sizeof(quint32);
33 explicit SourceLocation(quint32 offset = 0, quint32 length = 0, quint32 line = 0, quint32 column = 0)
37 if constexpr (!qSizeTypeCanHoldQuint32()) {
38 constexpr quint32 maxLength = quint32(std::numeric_limits<qsizetype>::max());
42 Q_ASSERT_X(length < maxLength && offset < maxLength && offset + length < maxLength,
43 "QQmlJS::SourceLocation",
"File size is limited to 2GB!");
44 Q_ASSERT_X(line < maxLength,
"QQmlJS::SourceLocation",
45 "Line exceeded maxLength of 2^31 - 1");
46 Q_ASSERT_X(column < maxLength,
"QQmlJS::SourceLocation",
47 "Column exceeded maxLength of 2^31 - 1");
54 if constexpr (qSizeTypeCanHoldQuint32()) {
55 constexpr qsizetype maxLength = qsizetype(std::numeric_limits<quint32>::max());
57 Q_ASSERT_X(offset + length < maxLength,
"QQmlJS::SourceLocation",
58 "File size is limited to 4GB!");
59 Q_ASSERT_X(line < maxLength,
"QQmlJS::SourceLocation",
60 "Line exceeded maxLength of 2^32 - 1");
61 Q_ASSERT_X(column < maxLength,
"QQmlJS::SourceLocation",
62 "Column exceeded maxLength of 2^32 - 1");
64 return SourceLocation(quint32(offset), quint32(length), quint32(line), quint32(column));
75 template <
typename Predicate>
76 static LocationInfo findLocationIf(QStringView text, Predicate &&predicate,
79 quint32 i = startHint
.isValid() ? startHint.offset : 0;
80 quint32 endLine = startHint
.isValid() ? startHint.startLine : 1;
81 quint32 endColumn = startHint
.isValid() ? startHint.startColumn : 1;
82 const quint32 end = quint32(text.size());
84 for (; i < end; ++i) {
85 if (predicate(i, endLine, endColumn))
86 return LocationInfo{ i, endLine, endColumn };
88 const QChar currentChar = text.at(i);
89 const bool isLineFeed = currentChar == u'\n';
90 const bool isCarriageReturn = currentChar == u'\r';
93 const bool isHalfANewline =
94 isCarriageReturn && (i + 1 < end && text.at(i + 1) == u'\n');
96 if (isHalfANewline || (!isCarriageReturn && !isLineFeed)) {
102 if (predicate(i, endLine, std::numeric_limits<quint32>::max()))
103 return LocationInfo{ i, endLine, endColumn };
108 if (predicate(i, std::numeric_limits<quint32>::max(),
109 std::numeric_limits<quint32>::max())) {
110 return LocationInfo{ i, endLine, endColumn };
119 return LocationInfo{ i, endLine, endColumn };
128 (startHint.startLine < line
129 || (startHint.startLine == line && startHint.startColumn <= column))
133 const auto result = findLocationIf(
135 [line, column](quint32, quint32 currentLine, quint32 currentColumn) {
136 return line <= currentLine && column <= currentColumn;
139 return result.offset;
146 const SourceLocation hint = startHint.offset <= offset ? startHint : SourceLocation{};
148 const auto result = findLocationIf(
150 [offset](quint32 currentOffset, quint32, quint32) {
151 return offset == currentOffset;
154 return std::make_pair(result.startLine, result.startColumn);
165 return SourceLocation(offset, 0, startLine, startColumn);
170 auto [row, column] = rowAndColumnFrom(text, offset + length, *
this);
171 return SourceLocation{ offset + length, 0, row, column };
186 return qHashMulti(seed, location.offset, location.length,
187 location.startLine, location.startColumn);
192 return a.offset == b.offset && a.length == b.length
193 && a.startLine == b.startLine && a.startColumn == b.startColumn;
201 quint32 e = qMax(l1.end(), l2.end());
203 if (l1.offset <= l2.offset)
207 res.length = e - res.offset;