Stream

Stream buffers

class file_content

Represents the content of a file.

The file content is memory-mapped initially, but may later become in-memory if the non-utf-8 content gets converted to utf-8.

Public Functions

file_content(const file_content&) = delete
file_content &operator=(const file_content&) = delete
file_content()
file_content(file_content &&other)
file_content(std::string_view filepath)
~file_content()
const char *data() const

Obtain the memory address to the first character in the content buffer.

Returns:

pointer to the first character in the buffer.

size_t size() const

Return the size of the content i.e. the number of characters in the content buffer.

Returns:

size of the content.

bool empty() const

Query whether or not the content is empty.

Returns:

true if the content is empty, otherwise false.

void swap(file_content &other)

Swap content with another instance.

Parameters:

other – another instance to swap content with.

void load(std::string_view filepath)

Load from a new file. This will invalidate the pointer returned from the data() method prior to the call.

Parameters:

filepath – path of the file to load from.

void convert_to_utf8()

Convert a non-utf-8 stream to a utf-8 one if the source stream contains a byte order mark. If not, it does nothing. When the conversion happens, the converted content will be stored in-memory.

std::string_view str() const
class memory_content

Represents the content of an in-memory buffer. Note that this class will NOT own the content of the source buffer but simply will reference it, except when the original buffer is a non-utf-8 stream and the caller chooses to convert it to utf-8 by calling its convert_to_utf8() method.

Public Functions

memory_content(const file_content&) = delete
memory_content &operator=(const file_content&) = delete
memory_content()
memory_content(std::string_view s)
memory_content(memory_content &&other)
~memory_content()
const char *data() const
size_t size() const
bool empty() const
void swap(memory_content &other)
void convert_to_utf8()

Convert a non-utf-8 stream to a utf-8 one if the source stream contains a byte order mark. If not, it does nothing. When the conversion happens, the converted content will be owned by the object.

std::string_view str() const

Utility functions

struct line_with_offset

Public Functions

line_with_offset(std::string _line, std::size_t _line_number, std::size_t _offset_on_line)
line_with_offset(const line_with_offset &other)
line_with_offset(line_with_offset &&other)
~line_with_offset()
bool operator==(const line_with_offset &other) const
bool operator!=(const line_with_offset &other) const

Public Members

std::string line

content of the entire line.

std::size_t line_number

0-based line number.

std::size_t offset_on_line

0-based offset within the line.

std::string orcus::create_parse_error_output(std::string_view strm, std::ptrdiff_t offset)

Generate a sensible error output for parse error including the line where the error occurred and the offset of the error position on that line.

Parameters:
  • strm – entire character stream where the error occurred.

  • offset – offset of the error position within the stream.

Returns:

string formatted to be usable as an error message for stdout.

line_with_offset orcus::locate_line_with_offset(std::string_view strm, std::ptrdiff_t offset)

Given a string consisting of multiple lines i.e. multiple line breaks, find the line that contains the specified offset position.

Parameters:
  • strm – string stream containing multiple lines to search.

  • offset – offset position.

Throws:

std::invalid_argument – if the offset value equals or exceeds the length of the string stream being searched.

Returns:

structure containing information about the line containing the offset position.

size_t orcus::locate_first_different_char(std::string_view left, std::string_view right)

Given two strings, locate the position of the first character that is different between the two strings. Note that if one of the strings is empty (or both of them are empty), it returns 0.

Parameters:
  • left – one of the strings to compare.

  • right – one of the strings to compare.

Returns:

position of the first character that is different between the two compared strings.

std::size_t orcus::calc_logical_string_length(std::string_view s)

Calculate the logical length of a UTF-8 encoded string.

Parameters:

s – string to calculate the logical length of.

Returns:

logical length of the UTF-8 encoded string.