ROSE 0.11.145.192
Public Member Functions | List of all members
Sawyer::Container::LineVector Class Reference

Description

A buffer of characters indexed by line number.

This is a character array indexed lines. The line indexes are computed only when necessary.

Definition at line 24 of file LineVector.h.

#include <Sawyer/LineVector.h>

Public Member Functions

 LineVector ()
 Constructor that creates an empty line vector.
 
 LineVector (const boost::filesystem::path &)
 Constructor that opens a file.
 
 LineVector (const Buffer< size_t, char >::Ptr &)
 Construct a line vector from a buffer.
 
 LineVector (size_t nBytes, const char *buf)
 Construct a line vector from a string.
 
void load (const boost::filesystem::path &)
 (Re)load a line vector from a file.
 
void load (const Buffer< size_t, char >::Ptr &)
 (Re)load a line vector from a buffer.)
 
void load (size_t nBytes, const char *buf)
 (Re)load a line vector from a string.)
 
void clear ()
 Clears data.
 
size_t nLines () const
 Number of lines.
 
size_t nCharacters () const
 Number of characters.
 
size_t nCharacters (size_t lineIdx) const
 Number of characters in a line.
 
int character (size_t charIdx) const
 Character at file offset.
 
const char * characters (size_t charIdx) const
 Characters at file offset.
 
int character (size_t lineIdx, size_t colIdx) const
 Character at line offset.
 
const char * lineChars (size_t lineIdx) const
 Characters for a line.
 
std::string lineString (size_t lineIdx) const
 Line as a string.
 
size_t characterIndex (size_t lineIdx) const
 Character index for start of line.
 
size_t lineIndex (size_t charIndex) const
 Convert a character index to a line index.
 
std::pair< size_t, size_t > location (size_t charIndex) const
 Convert a character index to a line and column index.
 
bool isLastLineTerminated () const
 Determines whether the file ends with line termination.
 
bool isEmpty () const
 Determines whether the file is empty.
 
std::string contentAsString (size_t begin, size_t end) const
 Returns part of the buffer as a string.
 

Constructor & Destructor Documentation

◆ LineVector() [1/2]

Sawyer::Container::LineVector::LineVector ( )
inline

Constructor that creates an empty line vector.

Definition at line 33 of file LineVector.h.

◆ LineVector() [2/2]

Sawyer::Container::LineVector::LineVector ( const boost::filesystem::path &  )
explicit

Constructor that opens a file.

This constructor doesn't actually read the entire file, it only maps it into memory.

Member Function Documentation

◆ clear()

void Sawyer::Container::LineVector::clear ( )
inline

Clears data.

Resets this object to the state created by the default constructor.

Definition at line 58 of file LineVector.h.

◆ nLines()

size_t Sawyer::Container::LineVector::nLines ( ) const

Number of lines.

Total number of lines including any final line that lacks line termination. Calling this function will cause the entire file to be read (if it hasn't been already) in order to find all line termination characters.

◆ nCharacters() [1/2]

size_t Sawyer::Container::LineVector::nCharacters ( ) const
inline

Number of characters.

Total number of characters including all line termination characters.

Definition at line 74 of file LineVector.h.

Referenced by character(), and Sawyer::Lexer::TokenStream< T >::locationEof().

◆ nCharacters() [2/2]

size_t Sawyer::Container::LineVector::nCharacters ( size_t  lineIdx) const

Number of characters in a line.

Returns the number of characters in the specified line including line termination characters, if any. If lineIdx refers to a line that doesn't exist then the return value is zero.

◆ character() [1/2]

int Sawyer::Container::LineVector::character ( size_t  charIdx) const
inline

Character at file offset.

Returns the character (as an int) at the specified file offset. If the charIdx is beyond the end of the file then EOF is returned.

Definition at line 88 of file LineVector.h.

References nCharacters().

◆ characters()

const char * Sawyer::Container::LineVector::characters ( size_t  charIdx) const

Characters at file offset.

Returns a pointer to the character at the specified file offset. The arrary of characters is valid through at least the following line terminator or the end of the file, whichever comes first. The end of the file is not necessarily NUL-terminated. Returns a null pointer if the character index is beyond the end of the file.

Referenced by Sawyer::Lexer::TokenStream< T >::lexeme(), and Sawyer::Lexer::TokenStream< T >::match().

◆ character() [2/2]

int Sawyer::Container::LineVector::character ( size_t  lineIdx,
size_t  colIdx 
) const

Character at line offset.

Returns the character (as an int) at the specified offset within a line. If lineIdx is beyond the end of the file then EOF is returned. Otherwise, if colIdx is beyond the end of a line then NUL characters are returned (which are also valid characters within a line).

◆ lineChars()

const char * Sawyer::Container::LineVector::lineChars ( size_t  lineIdx) const

Characters for a line.

Returns a pointer to a line's characters. The array is guaranteed to contain at least those characters that appear in the specified line. The array is not necessarily NUL-terminated, and if a line contains NUL characters then the array will have NUL characters. If lineIdx is beyond the end of the file then a null pointer is returned. See also, lineString.

◆ lineString()

std::string Sawyer::Container::LineVector::lineString ( size_t  lineIdx) const

Line as a string.

Returns a string containing the characters on the line. The line termination is included in the returned string. If the line index is out of range then an empty string is returned. This method is slower than using the direct character pointers because it requires that the characters be copied from the file buffer into the return value. See also, lineChars.

Referenced by Sawyer::Lexer::TokenStream< T >::lineString().

◆ characterIndex()

size_t Sawyer::Container::LineVector::characterIndex ( size_t  lineIdx) const

Character index for start of line.

Returns the character index for the first character in the specified line. If lineIdx is beyond the end of the file then the total number of characters is returned.

◆ lineIndex()

size_t Sawyer::Container::LineVector::lineIndex ( size_t  charIndex) const

Convert a character index to a line index.

Returns the zero-origin line index that contains the specified character. LF characters (line feeds) are considered to be at the end of a line. If charIndex is beyond the end of the file then the number of lines is returned.

◆ location()

std::pair< size_t, size_t > Sawyer::Container::LineVector::location ( size_t  charIndex) const

Convert a character index to a line and column index.

In error messages, line and column numbers are usually 1-origin, but this function returns zero-origin indexes.

Referenced by Sawyer::Lexer::TokenStream< T >::location(), and Sawyer::Lexer::TokenStream< T >::locationEof().

◆ isLastLineTerminated()

bool Sawyer::Container::LineVector::isLastLineTerminated ( ) const

Determines whether the file ends with line termination.

Determining whether the file ends with line termination can be done without reading the entire file. An empty file is considered to be not terminated.

◆ isEmpty()

bool Sawyer::Container::LineVector::isEmpty ( ) const

Determines whether the file is empty.

Returns true if the file is empty without trying to read any data.


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