ROSE  0.11.145.0
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 <util/Sawyer/LineVector.h>

Public Member Functions

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

Constructor & Destructor Documentation

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

Constructor that creates an empty line vector.

Definition at line 33 of file LineVector.h.

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.

Sawyer::Container::LineVector::LineVector ( const Buffer< size_t, char >::Ptr &  )
explicit

Construct a line vector from a buffer.

Sawyer::Container::LineVector::LineVector ( size_t  nBytes,
const char *  buf 
)

Construct a line vector from a string.

Member Function Documentation

void Sawyer::Container::LineVector::load ( const boost::filesystem::path &  )

(Re)load a line vector from a file.

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.

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.

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< Token >::locationEof().

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.

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().

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< Token >::lexeme(), and Sawyer::Lexer::TokenStream< Token >::match().

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).

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.

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< Token >::lineString().

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.

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.

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< Token >::location(), and Sawyer::Lexer::TokenStream< Token >::locationEof().

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.

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.

std::string Sawyer::Container::LineVector::contentAsString ( size_t  begin,
size_t  end 
) const

Returns part of the buffer as a string.


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