Spicy
Public Types | Public Member Functions | Friends | List of all members
hilti::rt::Bytes Class Reference

#include <bytes.h>

Inheritance diagram for hilti::rt::Bytes:

Public Types

using Base = std::string
 
using const_iterator = bytes::Iterator
 
using Offset = uint64_t
 
using size_type = integer::safe< uint64_t >
 

Public Member Functions

 Bytes (std::string s, bytes::Charset cs)
 
 Bytes (Base &&str)
 
 Bytes (const Bytes &xs)
 
 Bytes (Bytes &&xs) noexcept
 
Bytesoperator= (const Bytes &b)
 
Bytesoperator= (Bytes &&b) noexcept
 
void append (const Bytes &d)
 
void append (const stream::View &view)
 
void append (const uint8_t x)
 
const std::string & str () const &
 
const_iterator begin () const
 
const_iterator end () const
 
const_iterator at (Offset o) const
 
bool isEmpty () const
 
size_type size () const
 
const_iterator find (value_type b, const const_iterator &n=const_iterator()) const
 
std::tuple< bool, const_iteratorfind (const Bytes &v, const const_iterator &n=const_iterator()) const
 
Bytes sub (const const_iterator &from, const const_iterator &to) const
 
Bytes sub (const const_iterator &to) const
 
Bytes sub (Offset from, Offset to) const
 
Bytes sub (Offset to) const
 
Bytes extract (unsigned char *dst, uint64_t n) const
 
std::string decode (bytes::Charset cs) const
 
bool startsWith (const Bytes &b) const
 
Bytes upper (bytes::Charset cs) const
 
Bytes lower (bytes::Charset cs) const
 
Bytes strip (const Bytes &set, bytes::Side side=bytes::Side::Both) const
 
Bytes strip (bytes::Side side=bytes::Side::Both) const
 
Vector< Bytessplit () const
 
std::tuple< Bytes, Bytessplit1 () const
 
Vector< Bytessplit (const Bytes &sep) const
 
std::tuple< Bytes, Bytessplit1 (const Bytes &sep) const
 
template<typename T >
Bytes join (const Vector< T > &parts) const
 
integer::safe< int64_t > toInt (uint64_t base=10) const
 
integer::safe< uint64_t > toUInt (uint64_t base=10) const
 
int64_t toInt (hilti::rt::ByteOrder byte_order) const
 
uint64_t toUInt (hilti::rt::ByteOrder byte_order) const
 
Time toTime (uint64_t base=10) const
 
Time toTime (hilti::rt::ByteOrder byte_order) const
 
Result< Bytesmatch (const RegExp &re, unsigned int group=0) const
 

Friends

bool operator== (const Bytes &a, const Bytes &b)
 
bool operator!= (const Bytes &a, const Bytes &b)
 
bool operator< (const Bytes &a, const Bytes &b)
 
bool operator<= (const Bytes &a, const Bytes &b)
 
bool operator> (const Bytes &a, const Bytes &b)
 
bool operator>= (const Bytes &a, const Bytes &b)
 
Bytes operator+ (const Bytes &a, const Bytes &b)
 

Detailed Description

HILTI's Bytes is a std::string-like type for wrapping raw bytes with additional safety guarantees.

If not otherwise specified, member functions have the semantics of std::string member functions.

Constructor & Destructor Documentation

◆ Bytes()

Bytes::Bytes ( std::string  s,
bytes::Charset  cs 
)

Creates a bytes instance from a UTF8 string, transforming the contents into a binary representation defined by a specified character set.

Parameters
sstring assumed to be in UTF8 (as all runtime strings)
cscharacter set to use for the binary encoding
Returns
bytes instances encoding s in character set cs

Member Function Documentation

◆ append() [1/3]

void hilti::rt::Bytes::append ( const Bytes d)
inline

Appends the contents of a stream view to the data.

◆ append() [2/3]

void Bytes::append ( const stream::View view)

Appends the contents of a stream view to the data.

◆ append() [3/3]

void hilti::rt::Bytes::append ( const uint8_t  x)
inline

Appends a single byte the data.

◆ at()

const_iterator hilti::rt::Bytes::at ( Offset  o) const
inline

Returns an iterator referring to the given offset.

◆ begin()

const_iterator hilti::rt::Bytes::begin ( ) const
inline

Returns an iterator representing the first byte of the instance.

◆ decode()

std::string Bytes::decode ( bytes::Charset  cs) const

Decodes the binary data into a string assuming its encoded in a specified character set.

Parameters
cscharacter set to assume the binary data to be encoded in
Returns
UTF8 string

◆ end()

const_iterator hilti::rt::Bytes::end ( ) const
inline

Returns an iterator representing the end of the instance.

◆ extract()

Bytes hilti::rt::Bytes::extract ( unsigned char *  dst,
uint64_t  n 
) const
inline

Extracts a fixed number of bytes from the data

Parameters
dstarray to writes bytes into
nnumber of bytes to extract
Returns
new bytes instance that has the first N bytes removed.

◆ find() [1/2]

const_iterator hilti::rt::Bytes::find ( value_type  b,
const const_iterator n = const_iterator() 
) const
inline

Returns the position of the first occurrence of a byte.

Parameters
bbyte to search
noptional starting point, which must be inside the same instance

◆ find() [2/2]

std::tuple< bool, Bytes::const_iterator > Bytes::find ( const Bytes v,
const const_iterator n = const_iterator() 
) const

Returns the position of the first occurrence of a range of bytes

Parameters
vbytes to search
noptional starting point, which must be inside the same instance
Returns
tuple where the 1st element is a boolean indicating whether v has been found; if yes, the 2nd element points to the 1st bytes; if no, the 2nd element points to the first byte so that no earlier position has even a partial match of v.

◆ isEmpty()

bool hilti::rt::Bytes::isEmpty ( ) const
inline

Returns true if the data's size is zero.

◆ join()

template<typename T >
Bytes hilti::rt::Bytes::join ( const Vector< T > &  parts) const
inline

Returns the concatenation of all elements in the parts list rendered as printable strings and separated by the bytes value providing this method.

◆ lower()

Bytes hilti::rt::Bytes::lower ( bytes::Charset  cs) const
inline

Returns an upper-case version of the instance.

Parameters
cscharacter set for decoding/encoding
Returns
a lower case version of the instance

◆ match()

Result< Bytes > Bytes::match ( const RegExp re,
unsigned int  group = 0 
) const

Matches the data against a regular expression.

Parameters
recompiled regular expression
groupcapture group to return
Returns
the matching group, or unset if no match

◆ operator=() [1/2]

Bytes& hilti::rt::Bytes::operator= ( const Bytes b)
inline

Replaces the contents of this Bytes with another Bytes.

This function invalidates all iterators.

Parameters
bthe Bytes to assign
Returns
a reference to the changed Bytes

◆ operator=() [2/2]

Bytes& hilti::rt::Bytes::operator= ( Bytes &&  b)
inlinenoexcept

Replaces the contents of this Bytes with another Bytes.

This function invalidates all iterators.

Parameters
bthe Bytes to assign
Returns
a reference to the changed Bytes

◆ size()

size_type hilti::rt::Bytes::size ( ) const
inline

Returns the size of instance in bytes.

◆ split() [1/2]

Vector<Bytes> hilti::rt::Bytes::split ( ) const
inline

Splits the data at sequences of whitespace, returning the parts.

◆ split() [2/2]

Vector<Bytes> hilti::rt::Bytes::split ( const Bytes sep) const
inline

Splits the data at occurrences of a separator, returning the parts.

◆ split1() [1/2]

std::tuple<Bytes, Bytes> hilti::rt::Bytes::split1 ( ) const
inline

Splits the data (only) at the first sequence of whitespace, returning the two parts.

◆ split1() [2/2]

std::tuple<Bytes, Bytes> hilti::rt::Bytes::split1 ( const Bytes sep) const
inline

Splits the data (only) at the first occurrence of a separator, returning the two parts.

Parameters
sepBytes sequence to split at
Returns
a tuple of head and tail of the split instance

◆ startsWith()

bool hilti::rt::Bytes::startsWith ( const Bytes b) const
inline

Returns true if the data begins with a given, other bytes instance.

◆ str()

const std::string& hilti::rt::Bytes::str ( ) const &
inline

Returns the bytes' data as a string instance.

◆ strip() [1/2]

Bytes Bytes::strip ( const Bytes set,
bytes::Side  side = bytes::Side::Both 
) const

Removes leading and/or trailing sequences of all characters of a set from the bytes instance.

Parameters
sideside of bytes instance to be stripped.
setcharacters to remove; removes all whitespace if empty
Returns
a stripped version of the instance

◆ strip() [2/2]

Bytes Bytes::strip ( bytes::Side  side = bytes::Side::Both) const

Removes leading and/or trailing sequences of white space from the bytes instance.

Parameters
sideside of bytes instance to be stripped.
Returns
a stripped version of the instance

◆ sub() [1/4]

Bytes hilti::rt::Bytes::sub ( const const_iterator from,
const const_iterator to 
) const
inline

Extracts a subrange of bytes.

Parameters
fromiterator pointing to start of subrange
toiterator pointing to just beyond subrange
Returns
a Bytes instance for the subrange

◆ sub() [2/4]

Bytes hilti::rt::Bytes::sub ( const const_iterator to) const
inline

Extracts a subrange of bytes from the beginning.

Parameters
toiterator pointing to just beyond subrange
Returns
a Bytes instance for the subrange

◆ sub() [3/4]

Bytes hilti::rt::Bytes::sub ( Offset  from,
Offset  to 
) const
inline

Extracts a subrange of bytes.

Parameters
offsetof start of subrage
offsetof one byeond end of subrage
Returns
a Bytes instance for the subrange

◆ sub() [4/4]

Bytes hilti::rt::Bytes::sub ( Offset  to) const
inline

Extracts a subrange of bytes from the beginning.

Parameters
tooffset of one beyond end of subrange
Returns
a Bytes instance for the subrange

◆ toInt() [1/2]

integer::safe< int64_t > Bytes::toInt ( uint64_t  base = 10) const

Interprets the data as an ASCII representation of a signed integer and extracts that.

Parameters
basebase to use for conversion
Returns
converted integer value

◆ toInt() [2/2]

int64_t Bytes::toInt ( hilti::rt::ByteOrder  byte_order) const

Interprets the data as an binary representation of a signed integer and extracts that.

Parameters
byte_orderbyte order that the integer is encoded in
Returns
converted integer value

◆ toTime() [1/2]

Time hilti::rt::Bytes::toTime ( uint64_t  base = 10) const
inline

Interprets the data as an ASCII representation of a integer value representing seconds since the UNIX epoch, and extracts that.

Parameters
basebase to use for conversion
Returns
converted time value

◆ toTime() [2/2]

Time hilti::rt::Bytes::toTime ( hilti::rt::ByteOrder  byte_order) const
inline

Interprets the data as an binary representation of a integer value representing seconds since the UNIX epoch, and extracts that.

Parameters
basebase to use for conversion
Returns
converted time value

◆ toUInt() [1/2]

integer::safe< uint64_t > Bytes::toUInt ( uint64_t  base = 10) const

Interprets the data as an ASCII representation of an unsigned integer and extracts that.

Parameters
basebase to use for conversion
Returns
converted integer value

◆ toUInt() [2/2]

uint64_t Bytes::toUInt ( hilti::rt::ByteOrder  byte_order) const

Interprets the data as an binary representation of an unsigned integer and extracts that.

Parameters
byte_orderbyte order that the integer is encoded in
Returns
converted integer value

◆ upper()

Bytes hilti::rt::Bytes::upper ( bytes::Charset  cs) const
inline

Returns an upper-case version of the instance. This internally first decodes the data assuming a specified character set, then encodes it back afterwards.

Parameters
cscharacter set for decoding/encoding
Returns
an upper case version of the instance

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