Donner
C++20 SVG rendering library
Loading...
Searching...
No Matches
donner::svg::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/svg/xml/details/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.
 

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::svg::LineOffsets::LineOffsets ( std::string_view input)
inline

Construct a LineOffsets object for the given input string.

Parameters
inputInput string.

Member Function Documentation

◆ lineOffset()

size_t donner::svg::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::svg::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: