Merge pull request #12352 from ethereum/lineColumnTranslateForCharStream

LineColumn handling for CharStream.
This commit is contained in:
chriseth
2021-12-01 15:59:21 +01:00
committed by GitHub
3 changed files with 120 additions and 1 deletions
+29
View File
@@ -144,3 +144,32 @@ string CharStream::singleLineSnippet(string const& _sourceCode, SourceLocation c
return cut;
}
optional<int> CharStream::translateLineColumnToPosition(LineColumn const& _lineColumn) const
{
return translateLineColumnToPosition(m_source, _lineColumn);
}
optional<int> CharStream::translateLineColumnToPosition(std::string const& _text, LineColumn const& _input)
{
if (_input.line < 0)
return nullopt;
size_t offset = 0;
for (int i = 0; i < _input.line; i++)
{
offset = _text.find('\n', offset);
if (offset == _text.npos)
return nullopt;
offset++; // Skip linefeed.
}
size_t endOfLine = _text.find('\n', offset);
if (endOfLine == string::npos)
endOfLine = _text.size();
if (offset + static_cast<size_t>(_input.column) > endOfLine)
return nullopt;
return offset + static_cast<size_t>(_input.column);
}
+7
View File
@@ -51,6 +51,7 @@
#pragma once
#include <cstdint>
#include <optional>
#include <string>
#include <tuple>
#include <utility>
@@ -101,6 +102,12 @@ public:
LineColumn translatePositionToLineColumn(int _position) const;
///@}
/// Translates a line:column to the absolute position.
std::optional<int> translateLineColumnToPosition(LineColumn const& _lineColumn) const;
/// Translates a line:column to the absolute position for the given input text.
static std::optional<int> translateLineColumnToPosition(std::string const& _text, LineColumn const& _input);
/// Tests whether or not given octet sequence is present at the current position in stream.
/// @returns true if the sequence could be found, false otherwise.
bool prefixMatch(std::string_view _sequence)