Donner
C++20 SVG rendering library
Loading...
Searching...
No Matches
donner::base::parser::LineOffsets Class Reference

Helper class for finding newlines in a string, so that error messages can convert string-relative offsets into line numbers. More...

#include "donner/base/parser/LineOffsets.h"

Public Member Functions

 LineOffsets (std::string_view input)
 Construct a LineOffsets object for the given input string.
 
const std::vector< size_t > & offsets () const
 Return the offsets of the start of each line.
 
size_t offsetToLine (size_t offset) const
 Return line numbers for the given offset.
 
size_t lineOffset (size_t line) const
 Returns the offset of a given 1-indexed line number.
 
FileOffset fileOffset (size_t offset) const
 Returns the offset as a FileOffset with line information.
 

Detailed Description

Helper class for finding newlines in a string, so that error messages can convert string-relative offsets into line numbers.

This supports all newline styles, including \r\n and \r.

Constructor & Destructor Documentation

◆ LineOffsets()

donner::base::parser::LineOffsets::LineOffsets ( std::string_view input)
inlineexplicit

Construct a LineOffsets object for the given input string.

Parameters
inputInput string.

Member Function Documentation

◆ fileOffset()

FileOffset donner::base::parser::LineOffsets::fileOffset ( size_t offset) const
inline

Returns the offset as a FileOffset with line information.

Parameters
offsetCharacter index.

◆ lineOffset()

size_t donner::base::parser::LineOffsets::lineOffset ( size_t line) const
inline

Returns the offset of a given 1-indexed line number.

Parameters
lineLine number, starting from 1.

◆ offsetToLine()

size_t donner::base::parser::LineOffsets::offsetToLine ( size_t offset) const
inline

Return line numbers for the given offset.

For example, given a string: "abc\n123", offsets 0-3 would be considered line 1, and offsets after 4 (corresponding to the index of '1'), would be line 2. Values beyond the length of the string return the last line number.

Parameters
offsetCharacter index.
Returns
size_t Line number, 1-indexed.

The documentation for this class was generated from the following file: