string

Overview

estd::string is similar to the standard C++ std::basic_string class which offers multiple functions like size(), length(), resize(), data() etc. It also contains a special template function named as strtonum<T>() which converts a string as a slice to an integer of type T, where T must be a primitive number type.

Usage

The usage constraints and guidelines apply for string.

Constraint

The string has a fixed max_size which is given at the time of declaration.

Usage guidance

The size of the string should have a positive integer value.

While accessing the elements using at() and operator[] functions, the index passed to the function should not be out of bounds, as this will result in a runtime error. The index ranges for estd::string starts from 0 to size() - 1.

Example

The below code shows how operator=() assigns values to the string.

estd::declare::string<10> s;
s = "Hello";
ASSERT_EQ('H', s[0]);
ASSERT_EQ('e', s[1]);
ASSERT_EQ('\0', s[5]);
ASSERT_EQ(5U, s.size());
ASSERT_EQ(10U, s.max_size());

The below code shows how append() appends characters to the string.

estd::declare::string<10> s("Hello");

s.append("1234", 4);
ASSERT_EQ("Hello1234", s);

The below code shows how operator+=() appends characters to the string.

estd::declare::string<10> s;
s += "Hello";
ASSERT_EQ('H', s[0]);
ASSERT_EQ('e', s[1]);
ASSERT_EQ('\0', s[5]);
ASSERT_EQ(5U, s.size());
ASSERT_EQ(10U, s.max_size());

The below code shows how resize() resize the string and add values to it.

estd::declare::string<10> s("Hello");
ASSERT_EQ(5U, s.size());

s.resize(7, 'c');
ASSERT_EQ(7U, s.size());

ASSERT_EQ('H', s[0]);
ASSERT_EQ('e', s[1]);
ASSERT_EQ('l', s[2]);
ASSERT_EQ('l', s[3]);
ASSERT_EQ('o', s[4]);
ASSERT_EQ('c', s[5]);
ASSERT_EQ('c', s[6]);
ASSERT_EQ('\0', s[7]);

The below code shows how operator==() compares two strings.

estd::declare::string<10> s("Hello");
ASSERT_FALSE(s == "ABC");
ASSERT_TRUE(s == "Hello");

The below code shows how to get the length, size and max_size of a string.

estd::declare::string<10> s("Hello");
ASSERT_EQ(5U, s.length());
ASSERT_EQ(5U, s.size());
ASSERT_EQ(10U, s.max_size());

The below code shows a conversion of an array of chars of a lower case hex number with prefix “0x” to a uint_8t with base 16.

uint8_t const bytes[] = {'0', 'x', 'F'};
auto const res        = ::estd::strtonum<uint8_t>(bytes, 16);
EXPECT_EQ(res.get(), 0xFU);