mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #12352 from ethereum/lineColumnTranslateForCharStream
LineColumn handling for CharStream.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user