I am writing a unit test where a string has to end in a binary zero.
Consider const char * data_c = "1234";
, here data_c
contains 5 characters including a zero. std::string removes that zero and tracks the size like a vector. std::string data_cpp = "1234";
The string I need to create has a binary zero at the end. Initialising std::string with simple means seems problematic. std::string data_cpp{"ABC\0"};
Gives back a string of size 3;
The following minimal example show passing and not passing examples to illustrate my problem further:
#include <iostream>
#include <string>
void testString(std::string name, std::string str)
{
int e = 0;
std::cout << name << "\n";
std::cout << "----------------------" << "\n";
if (4 != str.size())
{
std::cout << "Size was not 4" << "\n";
e += 1;
}
char testvals[] = {'A', 'B', 'C', '\0'};
for (size_t n = 0; n < 4 && n < str.size(); ++n)
{
if (str[n] != testvals[n])
{
std::cout << "Character " << std::to_string(n) << " '" << str[n] << "'" << " did not match" << "\n";
e += 1;
}
}
std::cout << "ERRORS: " << std::to_string(e) << "\n";
std::cout << "----------------------" << std::endl;
}
template<size_t N>
std::string CutomInitString(const char(&str)[N])
{
return std::string{str, str + N - 1};
}
int main()
{
std::string one{"ABC\0"};
testString("ONE", one); //FAILS
const char two_c[] = "ABC\0";
std::string two{two_c};
testString("TWO", two); //FAILS
const char three_c[] = "ABC\0";
std::string three{three_c, three_c + (sizeof(three_c) / sizeof(char)) - 1};
testString("THREE", three); //PASS, also ugly
const char four_c[] = "ABC\0";
std::string four{CutomInitString(four_c)};
testString("FOUR", four); //PASS, also ugly
}
An example for simple would be std::string one
.
Is there a simple form that I can use?