I'm writing a program which involves comparing some strings which will be encoded in Shift-JIS. I only need to compare against a few possible values, and I'd like to encode them as string constants/literals.
I know how to do this with a user-defined literal using iconv or the MultiByteToWideChar/WideCharToMultiByte functions, but I'm wondering if there's some other way to do this. Or maybe a simpler way to define my own literal.
Some context if it's useful:
- The program will only be compiled for Windows.
- I'm using the Mingw64 cross compiler to build from Linux.
- The strings I'm checking against are all exactly 16 bytes long.
- I can use c++17 features, but probably not anything from c++20.
The user-defined literal I'm using now:
auto operator "" _sjs(const char* in, std::size_t len)
{
#ifdef HAS_ICONV
auto t = iconv_open("SHIFT-JIS", "UTF-8");
// max length of BS header name + 1 for terminator
char output[17] = "";
memset(output, ' ', 16);
size_t outLeft = 16;
char* tmpOut = output;
iconv(t, &in, &len, &tmpOut, &outLeft);
iconv_close(t);
return std::string{output};
#else
wchar_t output[34];
memset(output, 0, 34);
MultiByteToWideChar(CP_UTF8, 0, in, -1, output, len);
char output2[17] = "";
memset(output2, ' ', 16);
WideCharToMultiByte(932, 0, output, -1, output2, len*2, NULL, NULL);
return std::string{output2};
#endif
}