Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Linux codecvt wide string conversion with multibyte chars and locale + concatenation
//Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64 #include <iostream> #include <string.h> #include <codecvt> // ===================================================================================================================== // Returns the length of a wchar_t based string. This function is necessary when specifying the -fshort-wchar option size_t PalWcslen( const wchar_t* pWideStr) ///< [in] wide string to query { size_t len = 0; if (pWideStr != nullptr) { const wchar_t* pWchar = pWideStr; while (*pWchar != 0) { ++pWchar; ++len; } } return len; } // ===================================================================================================================== // Convert UTf-16 string to UTF-8 bool ConvertUtf16StringToUtf8( char* pDst, ///< [out] dst string const wchar_t* pSrc, ///< [in] src string size_t dstSizeInBytes) ///< size of the destination buffer in bytes { std::codecvt_utf8_utf16<char16_t> converter; mbstate_t state = {0}; char* pCharNext = nullptr; const char16_t* pUtf16Next = nullptr; std::codecvt_base::result retCode = converter.out(state, reinterpret_cast<const char16_t*>(pSrc), reinterpret_cast<const char16_t*>(pSrc)+PalWcslen(pSrc)+1, pUtf16Next, pDst, pDst + dstSizeInBytes, pCharNext); return (retCode == std::codecvt_base::ok); } // ===================================================================================================================== // Convert UTF-16 or UTF-32 string (depending on wchar_t size) to UTF-8 bool ConvertWcharStringToUtf8( char* pDst, ///< [out] dst string const wchar_t* pSrc, ///< [in] src string size_t dstSizeInBytes) ///< size of the destination buffer in bytes { bool result = false; if (sizeof(wchar_t) == 2) { result = ConvertUtf16StringToUtf8(pDst, pSrc, dstSizeInBytes); } else { std::codecvt_utf8<char32_t> converter; mbstate_t state = {0}; char* pCharNext = nullptr; const char32_t* pUtf32Next = nullptr; std::codecvt_base::result retCode = converter.out(state, reinterpret_cast<const char32_t*>(pSrc), reinterpret_cast<const char32_t*>(pSrc) + PalWcslen(pSrc) + 1, pUtf32Next, pDst, pDst + dstSizeInBytes, pCharNext); result = (retCode == std::codecvt_base::ok); } return result; } int main() { const wchar_t* ppInput[] = { L"José", L"測試", L"αβ", L"Środa" }; for(size_t i = 0; i < (sizeof(ppInput)/sizeof(ppInput[0])); ++i) { std::wcout << L"Original string: " << ppInput[i] << std::endl; char pOutput[256] = { '\0' }; bool ret = ConvertWcharStringToUtf8(pOutput, ppInput[i], sizeof(pOutput) - 1); std::cout << "\tret = " << ret << ", output = " << pOutput << std::endl; strncat(pOutput, "-concatenated", sizeof(pOutput)); std::cout << "\tconcatenated = " << pOutput << std::endl; } }
run
|
edit
|
history
|
help
0
Расстановка восьми ферзей на шахматной доске так, чтобы ни один не угрожал другому - C++
HerbSutter-Virtuality
Fun with Pointers #1
Crow unordered_map Quiz
using directives: qualified lookup rules are different from unqualified lookup rules
Linear search
K combinator - Lazy evaluation
hello world 3
CS1428 SI Tuesday
Clang-IsBaseOf