Template Class string

Class Documentation

template<uint64_t Capacity>
class string

string implementation with some adjustments in the API, because we are not allowed to throw exceptions or use heap.

Public Functions

constexpr string() noexcept = default

creates an empty string with size 0

string(const string &other) noexcept

copy constructor

Parameters:

other[in] is the copy origin

string(string &&other) noexcept

move constructor

Parameters:

other[in] is the move origin

string &operator=(const string &rhs) noexcept

copy assignment

Parameters:

rhs[in] is the copy origin

Returns:

reference to self

string &operator=(string &&rhs) noexcept

move assignment

Parameters:

rhs[in] is the move origin

Returns:

reference to self

template<uint64_t N>
string(const string<N> &other) noexcept

creates a new string of given capacity as a copy of other with compile time check whether the capacity of other is less than or equal to this’ capacity

Parameters:

other[in] is the copy origin

template<uint64_t N>
string(string<N> &&other) noexcept

moves other to this with compile time check whether the capacity of other is less than or equal to this’ capacity

Parameters:

other[in] is the move origin

template<uint64_t N>
string &operator=(const string<N> &rhs) noexcept

assigns rhs fixed string to this with compile time check whether the capacity of rhs is less than or equal to this’ capacity

Parameters:

rhs[in] is the copy origin

Returns:

reference to self

template<uint64_t N>
string &operator=(string<N> &&rhs) noexcept

moves rhs fixed string to this with compile time check whether the capacity of rhs is less than or equal to this’ capacity

Parameters:

rhs[in] is the move origin

Returns:

reference to self

template<uint64_t N>
string(const char (&other)[N]) noexcept

conversion constructor for char array with compile time check if the array size is less than or equal to the string capacity

#include "iceoryx_hoofs/cxx/string.hpp"
using namespace iox::cxx;

int main()
{
    string<4> fuu("abcd");
}

Note

if the array is not zero-terminated, the last value will be overwritten with 0

Template Parameters:

N – is the implicit template parameter for the char array size

Parameters:

other[in] is the char array

string(TruncateToCapacity_t, const char *const other) noexcept

conversion constructor for cstring to string which truncates characters if the size is greater than the string capacity

#include "iceoryx_hoofs/cxx/string.hpp"
using namespace iox::cxx;

int main()
{
    string<4> fuu(TruncateToCapacity, "abcd");
}
Attention

truncates characters if the size is greater than the string capacity

Parameters:
  • TruncateToCapacity_t – is a compile time variable which is used to distinguish between constructors with certain behavior

  • other[in] is the cstring to convert

string(TruncateToCapacity_t, const std::string &other) noexcept

conversion constructor for std::string to string which truncates characters if the std::string size is greater than the string capacity

#include "iceoryx_hoofs/cxx/string.hpp"
using namespace iox::cxx;

int main()
{
    std::string bar = "bar";
    string<4> fuu(TruncateToCapacity, bar);
}
Attention

truncates characters if the std::string size is greater than the string capacity

Parameters:
  • TruncateToCapacity_t – is a compile time variable which is used to distinguish between constructors with certain behavior

  • other[in] is the std::string to convert

string(TruncateToCapacity_t, const char *const other, const uint64_t count) noexcept

constructor from cstring to string. Constructs the string with the first count characters of the cstring including null characters. If count is greater than the string capacity the remainder of the characters are truncated.

#include "iceoryx_hoofs/cxx/string.hpp"
using namespace iox::cxx;

int main()
{
    string<4> fuu(TruncateToCapacity, "abcd", 2);
}

Parameters:
  • TruncateToCapacity_t – is a compile time variable which is used to distinguish between constructors with certain behavior

  • other[in] is the cstring to convert

  • count[in] is the number of characters for constructing the string

template<uint64_t N>
string &operator=(const char (&rhs)[N]) noexcept

assigns a char array to string with compile time check if the array size is less than or equal to the string capacity

#include "iceoryx_hoofs/cxx/string.hpp"
using namespace iox::cxx;

int main()
{
    string<4> fuu = "abcd";
}

Note

if the array is not zero-terminated, the last value will be overwritten with 0

Template Parameters:

[in] – N is the implicit template parameter for the char array size

Parameters:

rhs[in] is the char array

Returns:

reference to self

template<uint64_t N>
string &assign(const string<N> &str) noexcept

fixed string assignment with compile time check if capacity of str is less than or equal to this’ capacity

Parameters:

str[in] is the fixed string object to assign

Returns:

reference to self

template<uint64_t N>
string &assign(const char (&str)[N]) noexcept

assigns a char array to string with compile time check if the array size is less than or equal to the string capacity

#include "iceoryx_hoofs/cxx/string.hpp"
using namespace iox::cxx;

int main()
{
    string<4> fuu;
    char bar[] = "abcd";
    fuu.assign(bar);
}

Note

if the array is not zero-terminated, the last value will be overwritten with 0

Template Parameters:

[in] – N is the implicit template parameter for the char array size

Parameters:

str[in] is the char array

Returns:

reference to self

bool unsafe_assign(const char *const str) noexcept

assigns a cstring to string. The assignment fails if the cstring size is greater than the string capacity.

Parameters:

str[in] is the cstring to assign

Returns:

true if the assignment succeeds, otherwise false

bool unsafe_assign(const std::string &str) noexcept

assigns a std::string to string. The assignment fails if the std::string size is greater than the string capacity.

Parameters:

str[in] is the std::string to assign

Returns:

true if the assignment succeeds, otherwise false

template<uint64_t N>
int64_t compare(const string<N> &other) const noexcept

compares two strings

Parameters:

other[in] is the string to compare with self

Returns:

an integer < 0 if the first character that does not match has a lower value in self than in other, 0 if the contents of both strings are equal, an integer > 0 if the first character that does not match has a greater value in self than in other

template<uint64_t N>
bool operator==(const string<N> &rhs) const noexcept

checks if self is equal to rhs

Parameters:

rhs[in] is the string to compare with self

Returns:

true if both strings are equal, otherwise false

template<uint64_t N>
bool operator!=(const string<N> &rhs) const noexcept

checks if self is not equal to rhs

Parameters:

rhs[in] is the string to compare with self

Returns:

true if both strings are not equal, otherwise false

template<uint64_t N>
bool operator<(const string<N> &rhs) const noexcept

checks if self is less than rhs, in lexicographical order

Parameters:

rhs[in] is the string to compare with self

Returns:

true if self is less than rhs, otherwise false

template<uint64_t N>
bool operator<=(const string<N> &rhs) const noexcept

checks if self is less than or equal to rhs, in lexicographical order

Parameters:

rhs[in] is the string to compare with self

Returns:

true if self is less than or equal to rhs, otherwise false

template<uint64_t N>
bool operator>(const string<N> &rhs) const noexcept

checks if self is greater than rhs, in lexicographical order

Parameters:

rhs[in] is the string to compare with self

Returns:

true if self is greater than rhs, otherwise false

template<uint64_t N>
bool operator>=(const string<N> &rhs) const noexcept

checks if self is greater than or equal to rhs, in lexicographical order

Parameters:

rhs[in] is the string to compare with self

Returns:

true if self is greater than or equal to rhs, otherwise false

bool operator==(const char *const rhs) const noexcept

The equality operator for fixed string and char pointer is disabled via a static_assert, because it may lead to undefined behavior if the char array is not null-terminated. Please convert the char array to a fixed string with string(TruncateToCapacity_t, const char* const other, const uint64_t count) before compare it to a fixed string.

Todo:

consider implementing the equality operator for a char array for which the size is known at compile time; it could have the following signature template <int N> bool operator==(const char (&rhs)[N]) const noexcept

Parameters:

rhs[in] is the char pointer to the array to compare

Returns:

false

bool operator!=(const char *const rhs) const noexcept

The inequality operator for fixed string and char pointer is disabled via a static_assert, because it may lead to undefined behavior if the char array is not null-terminated. Please convert the char array to a fixed string with string(TruncateToCapacity_t, const char* const other, const uint64_t count) before compare it to a fixed string.

Todo:

consider implementing the inequality operator for a char array for which the size is known at compile time; it could have the following signature template <int N> bool operator!=(const char (&rhs)[N]) const noexcept

Parameters:

rhs[in] is the char pointer to the array to compare

Returns:

false

const char *c_str() const noexcept

returns a pointer to the char array of self

Returns:

a pointer to the char array of self

constexpr uint64_t size() const noexcept

returns the number of characters stored in the string

Returns:

the number of characters stored in the string

constexpr bool empty() const noexcept

returns if the string is empty or not

Returns:

true if size() == 0 otherwise false

operator std::string() const noexcept

converts the string to a std::string

Returns:

a std::string with data equivalent to those stored in the string

template<typename T>
string &operator+=(const T&) noexcept

since there are two valid options for what should happen when appending a string larger than this’ capacity (failing or truncating), the fixed string does not support operator+=; use append for truncating or unsafe_append for failing in that case

template<typename T>
std::enable_if<internal::IsCharArray<T>::value || internal::IsCxxString<T>::value, string&>::type append(TruncateToCapacity_t, const T &t) noexcept

appends a fixed string or string literal to the end of this. If this’ capacity is too small for appending the whole string (literal) the remainder of the characters are truncated.

string<5> fuu("cde");
fuu.append(TruncateToCapacity, "fgahc");

Parameters:
  • TruncateToCapacity_t – is a compile time variable which is used to make the user aware of the possible truncation

  • t[in] is the fixed string/string literal to append

Returns:

reference to self

template<typename T>
std::enable_if<internal::IsCharArray<T>::value || internal::IsCxxString<T>::value, bool>::type unsafe_append(const T &t) noexcept

appends a fixed string or string literal to the end of this. The appending fails if the sum of both sizes is greater than this’ capacity.

Parameters:

fixed[in] string/string literal to append

Returns:

true if the appending succeeds, otherwise false

iox::cxx::optional<string<Capacity>> substr(const uint64_t pos, const uint64_t count) const noexcept

creates a substring containing the characters from pos until count; if pos+count is greater than the size of the original string the returned substring only contains the characters from pos until size(); iox::cxx::nullopt is returned if pos is greater than the size of the original string;

Parameters:
  • pos[in] is the position of the first character used for the substring

  • count[in] is the requested length of the substring

Returns:

an optional containing the substring, iox::cxx::nullopt if pos is greater than the size of the original string

iox::cxx::optional<string<Capacity>> substr(const uint64_t pos = 0U) const noexcept

creates a substring containing the characters from pos until size(); iox::cxx::nullopt is returned if pos is greater than the size of the original string

Parameters:

pos[in] is the position of the first character used for the substring

Returns:

an optional containing the substring, iox::cxx::nullopt if pos is greater than the size of the original string

template<typename T>
std::enable_if<std::is_same<T, std::string>::value || internal::IsCharArray<T>::value || internal::IsCxxString<T>::value, iox::cxx::optional<uint64_t>>::type find(const T &t, const uint64_t pos = 0U) const noexcept

finds the first occurence of the given character sequence; returns the position of the first character of the found substring, returns iox::cxx::nullopt if no substring is found or if pos is greater than this’ size

Parameters:
  • t[in] is the character sequence to search for; must be a cxx::string, string literal or std::string

  • pos[in] is the position at which to start the search

Returns:

an optional containing the position of the first character of the found substring, iox::cxx::nullopt if no substring is found

template<typename T>
std::enable_if<std::is_same<T, std::string>::value || internal::IsCharArray<T>::value || internal::IsCxxString<T>::value, iox::cxx::optional<uint64_t>>::type find_first_of(const T &t, const uint64_t pos = 0U) const noexcept

finds the first occurence of a character equal to one of the characters of the given character sequence and returns its position; returns iox::cxx::nullopt if no character is found or if pos is greater than this’ size

Parameters:
  • t[in] is the character sequence to search for; must be a cxx::string, string literal or std::string

  • pos[in] is the position at which to start the search

Returns:

an optional containing the position of the first character equal to one of the characters of the given character sequence, iox::cxx::nullopt if no character is found

template<typename T>
std::enable_if<std::is_same<T, std::string>::value || internal::IsCharArray<T>::value || internal::IsCxxString<T>::value, iox::cxx::optional<uint64_t>>::type find_last_of(const T &t, const uint64_t pos = Capacity) const noexcept

finds the last occurence of a character equal to one of the characters of the given character sequence and returns its position; returns iox::cxx::nullopt if no character is found

Parameters:
  • t[in] is the character sequence to search for; must be a cxx::string, string literal or std::string

  • pos[in] is the position at which to finish the search

Returns:

an optional containing the position of the last character equal to one of the characters of the given character sequence, iox::cxx::nullopt if no character is found

Public Static Functions

static constexpr uint64_t capacity() noexcept

returns the maximum number of characters that can be stored in the string

Returns:

the maximum number of characters that can be stored in the string

Friends

friend class string
template<typename T1, typename T2>
friend std::enable_if<(internal::IsCharArray<T1>::value || internal::IsCxxString<T1>::value) && (internal::IsCharArray<T2>::value || internal::IsCxxString<T2>::value), string<internal::GetCapa<T1>::capa + internal::GetCapa<T2>::capa>>::type concatenate(const T1 &t1, const T2 &t2) noexcept

concatenates two fixed strings/string literals

string<5> fuu("cdefg");
auto bar = iox::cxx::concatenate(fuu, "ahc");

Parameters:

fixed[in] strings/string literals to concatenate

Returns:

a new fixed string with capacity equal to the sum of the capacities of the concatenated strings