I'm trying to build a Python extension with C++ 14. When the module is initialized in Python, I ask caller giving me a callback function which take a string as input.
When a function in the module is called, I want to do is reading a text file from disk, which may contain non-English characters. So I read it into memory as wstring
in C++ code.
The next step is to pass this wstring
to Python code. Should I define the user callback function like below:
typedef std::string (*UserCallbackFunc)(std::string);
and handle wstring
to utf-8 string
conversion like below:
string wstring2utf8string(wstring input)
{
wstring_convert<codecvt_utf8<wchar_t>> converter;
return converter.to_bytes(input);
}
or I could simply define the callback like
typedef std::wstring (*UserCallbackFunc)(std::wstring);
and pass in what I read from disk directly?
Let me answer the question by myself.
My goal is to pass non-English characters from C++ made Python 3 extension to Python callback function. Apparently UTF-8 is good enough for me to achieve this goal other than using UTF-16. In C++, string
is good enough for UTF-8 and wstring
is for UTF-16 and UTF-32. So after I change all wstring
to string
, the non-English character in UTF-8 working fine from both sides.