A C++ template library for embedded applications
MIT licensed
Designed and
maintained by
John Wellbelove

string / wstring / u16string / u32string

Fixed capacity string classes.
STL equivalent: std::string, std::wstring, std::u16string, std::u32string

etl::string<size_t Size>
etl::wstring<size_t Size>
etl::u8string<size_t Size>
etl::u16string<size_t Size>
etl::u32string<size_t Size>

etl::string_ext
etl::wstring_ext
etl::u16string_ext
etl::u32string_ext

Inherits from etl::istring, etl::iwstring, etl::iu16string or etl::iu32string
The base classes may be used as a size independent pointer or reference type for any string instance.

Has the ability to be copied by low level functions such as memcpy by use of a repair() function.
See the function reference for an example of use.

Note: cstring.h is deprecated
____________________________________________________________________________________________________
Macros

If ETL_DISABLE_STRING_TRUNCATION_CHECKS is defined then the string will not be checked for truncation and
the truncation functions will not be available.

If ETL_DISABLE_STRING_CLEAR_AFTER_USE is defined then the string will not be cleared after use and the
'secure' functions will not be available.

Defining both of these macros can reduce the overhead of an etl::string by up to 4 bytes.

If ETL_STRING_TRUNCATION_IS_ERROR is defined then an error will be asserted if the string would truncate.
____________________________________________________________________________________________________
External buffers

etl::string_ext
etl::wstring_ext
etl::u16string_ext
etl::u32string_ext
With these templates, a pointer to an external buffer, plus the size of the buffer must be supplied at construction.
Note: The buffer size must  include room for a terminating null.

Example
constexpr size_t Size = 10;

A string of a maximum of 10 characters, using an internal buffer.
etl::string<Size > text;

A string of a maximum of 10 characters, using an external buffer.
char buffer[Size + 1];
etl::string_ext text(buffer, etl::size(buffer));
____________________________________________________________________________________________________
If the external buffer contains a C string and you wish this to be the initial value, then use the following constructor with
the buffer as the initial text parameter.

etl::string_ext text(buffer, buffer, etl::size(buffer));
This constructs a string of capacity etl::size(buffer) containing the null terminated string in buffer.
The constructor will detect that the initial text and the buffer are at the same address.
____________________________________________________________________________________________________
Note:
When using external buffered strings, a memcpy + repair will NOT produce a copy of the string.
Both the original and copy will point to the same external buffer.
____________________________________________________________________________________________________
Truncation

Strings contain an internal flag that records the truncation state.
If a string Is constructed, assigned or modified in any way such that the text contained would have exceeded the
capacity then the truncate flag is set. The string will be truncated at the maximum capacity.
A string may become marked as truncated if it is assigned characters or constructed from another string marked as
truncated. This will occur even if the resulting string is not truncated itself.

The truncation flag is cleared if the string is assigned from a string that does not exceed the capacity or a non-
truncated string. It can also be forcibly cleared by calling clear_truncated().

If  it is required that truncation be an error then define ETL_STRING_TRUNCATION_IS_ERROR.
____________________________________________________________________________________________________
bool truncated() const
Returns true if the string was truncated (the assigned or inserted string was greater than capacity or marked as
truncated), otherwise false.
____________________________________________________________________________________________________
void clear_truncated()
Clears the internal truncated flag.
____________________________________________________________________________________________________
Example
Assuming ETL_STRING_TRUNCATION_IS_ERROR is not defined.

etl::string<11> text1 = "Hello";     // Not truncated.
etl::string<6>  text2 = " World!!!"; // Truncated to " World".
text1 += text2; // Truncated. text1 inherits the truncation state from text2.
text1.assign("Hello World"); // Not truncated.
____________________________________________________________________________________________________
Interfacing with C APIs

The ETL strings implement three functions to ease interfacing with C APIs.
initialize_free_space()
trim_to_terminator()
data_end()

20.3.0
initialize_free_space()
Initializes the free space in the string to null terminators.

20.3.0
trim_to_terminator()
Sets the string length to the first terminating null or the max size if the string.

20.3.0
data_end()
Returns a pointer to the current terminating null.

Example
// Send the ETL string data to a C function that will append characters.
etl::string<20> text("Hello ");
text.initialize_free_space(); // If the C function does not add its own null terminators.
CFunction(text.data_end(), text.available());
text.trim_to_terminator();
____________________________________________________________________________________________________
Member types

value_type              T
size_type               size_t
difference_type         ptrdiff_t
reference               value_type&
const_reference         const value_type&
pointer                 value_type*
const_pointer           const value_type*
iterator                Random access iterator
const_iterator          Constant random access iterator
reverse_iterator        ETL_OR_STD::reverse_iterator<iterator>
const_reverse_iterator  ETL_OR_STD::reverse_iterator<const_iterator>
____________________________________________________________________________________________________
Constructor

Internal buffer
etl::string<const size_t Size>();
etl::string<const size_t Size>(const istring& str);
etl::string<const size_t Size>(const istring& str, size_t pos, size_t len = npos);
etl::string<const size_t Size>(const char* s);
etl::string<const size_t Size>(const char* s, size_t n);
etl::string<const size_t Size>(size_t n, char c);
etl::string<const size_t Size>(const etl::string_view& view);

template <typename TIterator>
etl::string<const size_t Size>(TIterator begin, TIterator end);
Truncates the string if too long.

External buffer
etl::string<const size_t Size>(char* buffer, size_t buffer_size);
etl::string<const size_t Size>(const istring& str, char* buffer, size_t buffer_size);
etl::string<const size_t Size>(const istring& str, char* buffer, size_t buffer_size, size_t pos, size_t
len = npos);
etl::string<const size_t Size>(const char* s, char* buffer, size_t buffer_size);
etl::string<const size_t Size>(const char* s, size_t n, char* buffer, size_t buffer_size);
etl::string<const size_t Size>(size_t n, char c, char* buffer, size_t buffer_size);
etl::string<const size_t Size>(const etl::string_view& view, char* buffer, size_t buffer_size);

template <typename TIterator>
etl::string<const size_t Size>(TIterator begin, TIterator end, char* buffer, size_t buffer_size);
Truncates the string if too long.
____________________________________________________________________________________________________
Element access

T& at(size_t i)
const T& at(size_t i) const
Returns a reference or const reference to the indexed element. Emits an etl::string_out_of_bounds if the index is
out of range of the array.
____________________________________________________________________________________________________
T& operator[](size_t i)
const T& operator[](size_t i) const
Returns a reference or const reference to the indexed element.
____________________________________________________________________________________________________
T& front()
const T& front() const
Returns a reference or const reference to the first element.
Undefined behaviour if the string is empty.
____________________________________________________________________________________________________
T& back()
const T& back() const
Returns a reference or const reference to the last element.
Undefined behaviour if the string is empty.
____________________________________________________________________________________________________
Iterators

iterator begin()
const_iterator begin() const
const_iterator cbegin() const
Returns an iterator to the beginning of the string.
____________________________________________________________________________________________________
iterator end()
const_iterator end() const
const_iterator cend() const
Returns an iterator to the end of the string.
____________________________________________________________________________________________________
iterator rbegin()
const_reverse_iterator rbegin() const
const_reverse_iterator crbegin() const
Returns a reverse iterator to the beginning of the string.
____________________________________________________________________________________________________
iterator rend()
const_reverse_iterator rend() const
const_reverse_iterator crend() const
Returns a reverse iterator to the end of the string.
____________________________________________________________________________________________________
value_type* data()
const value_type* data() const
Returns a pointer to the start of the string.
____________________________________________________________________________________________________
value_type* data_end()
const value_type* data_end() const
Returns a pointer to the terminator of the string.
____________________________________________________________________________________________________
Capacity

bool empty() const
Returns true if the size of the string is zero, otherwise false.
____________________________________________________________________________________________________
bool full() const
Returns true if the size of the string is SIZE, otherwise false.
____________________________________________________________________________________________________
size_t size() const
Returns the size of the vector.
____________________________________________________________________________________________________
void resize(size_t new_size, T value = T())
Resizes the string, up to the maximum capacity. Emits an etl::string_full if the string does not have the capacity.
____________________________________________________________________________________________________
void uninitialized_resize(size_t new_size)
20.4.0
Resizes the string, up to the maximum capacity, without initialising the new elements.
____________________________________________________________________________________________________
size_t max_size() const
Returns the maximum possible size of the string.
____________________________________________________________________________________________________
size_t capacity() const
Returns the maximum possible size of the string.
____________________________________________________________________________________________________
size_t available() const
Returns the remaining available capacity in the string.
____________________________________________________________________________________________________
Modifiers

template <typename TIterator>
void assign(TIterator begin, TIterator end);

void assign(size_t n, value_type value);
Fills the vector with the values. Emits etl::string_iterator if the distance between begin and end is illegal.
Truncates the string if too long.
____________________________________________________________________________________________________
void push_back(value_type value);
Pushes a value to the back of the string.
No action if the string is already full.
____________________________________________________________________________________________________
void pop_back();
Pop a value from the back of the string.
No action if the string is already empty.
____________________________________________________________________________________________________
<=20.19.0
iterator insert(iterator position, T value)

iterator insert(iterator position, size_type n, T value)

template <typename TIterator>
iterator insert(iterator position, TIterator first, TIterator last)

>=20.20.0
iterator insert(const_iterator position, T value)

iterator insert(const_iterator position, size_type n, T value)

template <typename TIterator>
iterator insert(const_iterator position, TIterator first, TIterator last)
_______________
etl::ibasic_string<T>& insert(size_type position, const etl::ibasic_string<T>& str)

etl::ibasic_string<T>& insert(size_type position,
                              const etl::ibasic_string<T>& str,
                              size_type subposition,
                              size_type sublength)

etl::ibasic_string<T>& insert(size_type position, const_pointer s)

etl::ibasic_string<T>& insert(size_type position, const_pointer s, size_type n)

etl::ibasic_string<T>& insert(size_type position, size_type n, value_type c)
Inserts values in to the string. Truncates the string if too long.
Note: Strings that insert from themselves are only supported if the insert position is after the input range.
____________________________________________________________________________________________________
etl::ibasic_string<T>& erase(size_type position, size_type length_ = npos)

<=20.19.0
iterator erase(iterator i_element)
iterator erase(iterator first, iterator last)

>=20.20.0
iterator erase(iterator i_element)
iterator erase(const_iterator i_element)
iterator erase(const_iterator first, const_iterator last)
Erases values in the string.
Iterators are not checked.
____________________________________________________________________________________________________
void clear();
Clears the string to a size of zero.
Resets the truncate flag
____________________________________________________________________________________________________
string& assign(const string& str);
string& assign(const string& str, size_t subpos, size_t sublen);
string& assign(const char* s);
string& assign(const char* s, size_t n);
string& assign(size_t n, char c);

template <typename TIterator>
string& assign(TIterator first, TIterator last);
Assigns to the string. Truncates the string if too long.
May inherit the truncation state if the parameter is an etl::string.
____________________________________________________________________________________________________
string& append(const string& str);
string& append(const string& str, size_t subpos, size_t sublen);
string& append(const char* s);
string& append(const char* s, size_t n);
string& append(size_t n, char c);

template <typename TIterator>
string& append(TIterator first, Titerator last);
Appends to the string. Truncates the string if too long.
May inherit the truncation state if the parameter is an etl::string.
____________________________________________________________________________________________________
string& insert(size_t pos, const string& str);
string& insert(size_t pos, const string& str, size_t subpos, size_t sublen);
string& insert(size_t pos, const char* s);
string& insert(size_t pos, const char* s, size_t n);
string& insert(size_t pos, size_t n, char c);
void insert (iterator p, size_t n, char c);
iterator insert(iterator p, char c);

template <typename TIterator>
void insert (iterator p, Titerator first, Titerator last);
Inserts into the string.
May inherit the truncation state if the parameter is an etl::string.

Note: Strings that insert from themselves are only supported if the insert position is after the input range.
____________________________________________________________________________________________________
string& replace(size_t pos,  size_t len,  const string& str);
string& replace(iterator i1, iterator i2, const string& str);
string& replace(size_t pos,  size_t len,  const string& str, size_t subpos, size_t sublen);
string& replace(size_t pos,  size_t len,  const char* s);
string& replace(iterator i1, iterator i2, const char* s);
string& replace(size_t pos,  size_t len,  const char* s, size_t n);
string& replace(iterator i1, iterator i2, const char* s, size_t n);
string& replace(size_t pos,  size_t len,  size_t n, char c);
string& replace(iterator i1, iterator i2, size_t n, char c);

template <typename TIterator>
string& replace(iterator i1, iterator i2, Titerator first, Titerator last);
Replaces parts of the string. Truncates the string if too long.
May inherit the truncation state if the parameter is an etl::string.
____________________________________________________________________________________________________
void repair()
This function must be called if the string has been copied via a low level method such as memcpy.
This can only be called from an etl::string, etl::wstring, etl::u16string or etl::u32string instance.

If ETL_ISTRING_REPAIR_ENABLE is defined then repair() may be called from the etl::istring base class.
Be aware that doing this introduces a virtual function to the class.

Has no effect if the object has not been copied in this way.

Example
typedef etl::string<10> String;
String text = "ABCDEF";
char buffer[sizeof(String)];
memcpy(&buffer, &text, sizeof(String));
String& rtext(*reinterpret_cast<String*>(buffer));
// Do not access the string in any way until you have called this.
rtext.repair();
____________________________________________________________________________________________________
void initialize_free_space()
This function will initialize the free space in a string to null terminators.
____________________________________________________________________________________________________
void trim_to_terminator()
The string length will be set to the position of the first null terminator.
If the last item in the buffer is not a null terminator then one will be inserted.
____________________________________________________________________________________________________
string& operator = (const string& rhs)
string& operator = (const value_type* text)

Assignment operator.
May inherit the truncation state is the parameter is an etl::string.
____________________________________________________________________________________________________
string& operator += (const string& rhs)
string& operator += (const value_type* text)

Addition operator.
May inherit the truncation state is the parameter is an etl::string.
____________________________________________________________________________________________________
Find

size_t find(const string& str, size_t pos = 0) const;
size_t find(const char* s, size_t pos = 0) const;
size_t find(const char* s, size_t pos, size_t n) const;
size_t find(char c, size_t pos = 0) const;
Find in a string
____________________________________________________________________________________________________
size_t rfind(const string& str, size_t pos = npos) const;
size_t rfind(const char* s, size_t pos = npos) const;
size_t rfind(const char* s, size_t pos, size_t n) const;
size_t rfind(char c, size_t pos = npos) const;
Reverse find in a string
____________________________________________________________________________________________________
size_t find_first_of(const string& str, size_t pos = 0) const;
size_t find_first_of(const char* s, size_t pos = 0) const;
size_t find_first_of(const char* s, size_t pos, size_t n) const;
size_t find_first_of(char c, size_t pos = 0) const;
Find first of in a string
____________________________________________________________________________________________________
size_t find_last_of(const string& str, size_t pos = npos) const;
size_t find_last_of(const char* s, size_t pos = npos) const;
size_t find_last_of(const char* s, size_t pos, size_t n) const;
size_t find_last_of(char c, size_t pos = npos) const;
Find last of in a string
____________________________________________________________________________________________________

size_t find_first_not_of(const string& str, size_t pos = 0) const;
size_t find_first_not_of(const char* s, size_t pos = 0) const;
size_t find_first_not_of(const char* s, size_t pos, size_t n) const;
size_t find_first_not_of(char c, size_t pos = 0) const;
Find first not of in a string
____________________________________________________________________________________________________
size_t find_last_not_of(const string& str, size_t pos = npos) const;
size_t find_last_not_of(const char* s, size_t pos = npos) const;
size_t find_last_not_of(const char* s, size_t pos, size_t n) const;
size_t find_last_not_of(char c, size_t pos = npos) const;
Find last not of in a string
____________________________________________________________________________________________________
string substr(size_t pos = 0, size_t len = npos) const;
Extract a substring
____________________________________________________________________________________________________
int compare(const string& str) const;
int compare(size_t pos, size_t len, const string& str) const;
int compare(size_t pos, size_t len, const string& str, size_t subpos, size_t sublen) const;
int compare(const char* s) const;
int compare(size_t pos, size_t len, const char* s) const;
int compare(size_t pos, size_t len, const char* s, size_t n) const;
Compare strings
____________________________________________________________________________________________________
Non-member functions

Make a string from a literal or array
char
template <const size_t Max_Size>
etl::string<Max_Size> make_string(const char (&text) [Max_Size])

template <const size_t Max_Size, size_t Size>
etl::string<Max_Size> make_string_with_capacity(const char (&text) [Size])

wchar_t
template <const size_t Max_Size>
etl::wstring<Max_Size> make_string(const wchar_t (&text) [Max_Size])

template <const size_t Max_Size, size_t Size>
etl::wstring<Max_Size> make_string_with_capacity(const wchar(&text) [Size])

char8_t
20.38.7
template <const size_t Max_Size>
etl::u16string<Max_Size> make_string(const char16_t (&text) [Max_Size])

template <const size_t Max_Size, size_t Size>
etl::u16string<Max_Size> make_string_with_capacity(const char16_t(&text) [Size])

char16_t
template <const size_t Max_Size>
etl::u16string<Max_Size> make_string(const char16_t (&text) [Max_Size])

template <const size_t Max_Size, size_t Size>
etl::u16string<Max_Size> make_string_with_capacity(const char16_t(&text) [Size])

char32_t
template <const size_t Max_Size>
etl::u32string<Max_Size> make_string(const char32_t (&text) [Max_Size])

template <const size_t Max_Size, size_t Size>
etl::u32string<Max_Size> make_string_with_capacity(const char32_t(&text) [Size])

Examples
// 'text' contains "Hello World" with a size and capacity of 11.
auto text = etl::make_string("Hello World");

// 'text' contains "Hello World" with a size of 11 and capacity of 20.
auto text = etl::make_string_with_capacity<20>("Hello World");

// 'text' contains "Hello Worl" with a size of 10 and capacity of 10. Marked as truncated.
auto text = etl::make_string_with_capacity<10>("Hello World");

Comparisons
==  true if the contents of the vectors are equal, otherwise false.
!=  true if the contents of the vectors are not equal, otherwise false.
<   true if the contents of the lhs are lexicographically less than the contents of the rhs,  otherwise false.
<=  true if the contents of the lhs are lexicographically less than or equal to the contents of the rhs, otherwise false.
>   true if the contents of the lhs are lexicographically greater than the contents of the rhs,  otherwise false.
>=  true if the contents of the lhs are lexicographically greater than or equal to the contents of the rhs, otherwise
false.
string.h / wstring.h / u16string.h / u32string.h