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::SafeIterator
 
using unsafe_const_iterator = bytes::detail::UnsafeConstIterator
 
using Offset = uint64_t
 
using size_type = integer::safe< uint64_t >
 
using C = std::shared_ptr< const Base * >
 

Public Member Functions

 Bytes (Base s)
 
 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 &
 
std::string str () &&
 
const_iterator begin () const
 
const_iterator cbegin () const
 
auto unsafeBegin () const
 
const_iterator end () const
 
const_iterator cend () const
 
auto unsafeEnd () const
 
const_iterator at (Offset o) const
 
bool isEmpty () const
 
size_type size () const
 
const_iterator find (value_type needle, const const_iterator &start=const_iterator()) const
 
Tuple< bool, const_iteratorfind (const Bytes &needle, const const_iterator &start=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 (unicode::Charset cs, unicode::DecodeErrorStrategy errors=unicode::DecodeErrorStrategy::REPLACE) const
 
bool startsWith (const Bytes &prefix) const
 
bool endsWith (const Bytes &suffix) const
 
Bytes upper (unicode::Charset cs, unicode::DecodeErrorStrategy errors=unicode::DecodeErrorStrategy::REPLACE) const
 
Bytes lower (unicode::Charset cs, unicode::DecodeErrorStrategy errors=unicode::DecodeErrorStrategy::REPLACE) 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
 
Tuple< Bytes, Bytessplit1 () const
 
Vector< Bytessplit (const Bytes &sep) const
 
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
 
integer::safe< int64_t > toInt (hilti::rt::ByteOrder byte_order) const
 
integer::safe< uint64_t > toUInt (hilti::rt::ByteOrder byte_order) const
 
double toReal () 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()

hilti::rt::Bytes::Bytes ( Base  s)
inline

Creates a bytes instance from a raw string representation.

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 hilti::rt::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.

◆ cbegin()

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

Same as begin(), just for compatibility with std types.

◆ cend()

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

Same as end(), just for compatibility with std types.

◆ decode()

std::string hilti::rt::Bytes::decode ( unicode::Charset  cs,
unicode::DecodeErrorStrategy  errors = unicode::DecodeErrorStrategy::REPLACE 
) 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
errorshow to handle errors when decoding the data
Returns
UTF8 string

◆ end()

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

Returns an iterator representing the end of the instance.

◆ endsWith()

bool hilti::rt::Bytes::endsWith ( const Bytes suffix) const
inline

Returns true if the data begins with a given, other bytes 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]

Tuple<bool, const_iterator> hilti::rt::Bytes::find ( const Bytes needle,
const const_iterator start = const_iterator() 
) const

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

Parameters
needlebytes to search
startoptional 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.

◆ find() [2/2]

const_iterator hilti::rt::Bytes::find ( value_type  needle,
const const_iterator start = const_iterator() 
) const
inline

Returns the position of the first occurrence of a byte.

Parameters
needlebyte to search
startoptional starting point, which must be inside the same instance

◆ 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 ( unicode::Charset  cs,
unicode::DecodeErrorStrategy  errors = unicode::DecodeErrorStrategy::REPLACE 
) const

Returns an upper-case version of the instance.

Parameters
cscharacter set for decoding/encoding
errorshow to handle errors when decoding/encoding the data
Returns
a lower case version of the instance

◆ match()

Result<Bytes> hilti::rt::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= ( 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

◆ operator=() [2/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

◆ 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]

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]

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 prefix) const
inline

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

◆ str() [1/2]

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

Returns the bytes' data as a string instance.

◆ str() [2/2]

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

Returns the bytes' data as a string instance.

◆ strip() [1/2]

Bytes hilti::rt::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

◆ strip() [2/2]

Bytes hilti::rt::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

◆ 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> hilti::rt::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

◆ toInt() [2/2]

integer::safe<int64_t> hilti::rt::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

◆ toReal()

double hilti::rt::Bytes::toReal ( ) const

Interprets the data as an ASCII representation of a floating point value and extracts that. The data must be in a format that strtod can handle.

Returns
converted real value

◆ toTime() [1/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

◆ toTime() [2/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

◆ toUInt() [1/2]

integer::safe<uint64_t> hilti::rt::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

◆ toUInt() [2/2]

integer::safe<uint64_t> hilti::rt::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

◆ unsafeBegin()

auto hilti::rt::Bytes::unsafeBegin ( ) const
inline

Returns an unchecked (but fast) iterator representing the first byte of the instance.

◆ unsafeEnd()

auto hilti::rt::Bytes::unsafeEnd ( ) const
inline

Returns an unchecked (but fast) iterator representing the end of the instance.

◆ upper()

Bytes hilti::rt::Bytes::upper ( unicode::Charset  cs,
unicode::DecodeErrorStrategy  errors = unicode::DecodeErrorStrategy::REPLACE 
) const

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
errorshow to handle errors when decoding/encoding the data
Returns
an upper case version of the instance

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