Skip to content

Instantly share code, notes, and snippets.

@Delta-in-hub
Created March 1, 2022 01:39
Show Gist options
  • Save Delta-in-hub/6f0b780f17d0cb7dfeb8c2be7ba9f1f8 to your computer and use it in GitHub Desktop.
Save Delta-in-hub/6f0b780f17d0cb7dfeb8c2be7ba9f1f8 to your computer and use it in GitHub Desktop.
utf8.cc
#include <ciso646>
#include <string>
#include <string_view>
#include <iostream>
constexpr auto MAX_CMD_LINE = 1024;
#ifdef _WIN32
#include <windows.h>
#include <fcntl.h>
#include <io.h>
#include <winbase.h>
#include <winnls.h>
#endif
#ifdef _WIN32
// utf16le(unicode of windows) to utf8
std::string utf16ToUtf8(std::wstring_view utf16Str)
{
int buffLen = WideCharToMultiByte(
CP_UTF8, 0, utf16Str.data(), utf16Str.size(), NULL, 0,
/*// For the CP_UTF7 and CP_UTF8 settings for CodePage, this parameter must be set to NULL.*/
NULL, NULL);
std::string utf8Str(buffLen, 0);
WideCharToMultiByte(CP_UTF8, 0, utf16Str.data(), utf16Str.size(), utf8Str.data(), buffLen, NULL, NULL);
return utf8Str;
}
#endif
// Get a line from stdin with utf8 encoding
std::string getLineUtf8()
{
#if defined _WIN32
wchar_t buffer[MAX_CMD_LINE] = {};
DWORD numRead = 0;
ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), buffer, MAX_CMD_LINE, &numRead, NULL);
if (numRead >= MAX_CMD_LINE)
{
//
}
std::string utf8str = utf16ToUtf8(buffer);
int len = utf8str.size();
if (len >= 2 and utf8str[len - 2] == 0x0D and utf8str[len - 1] == 0x0A) // remove cr lf
{
utf8str.pop_back();
utf8str.pop_back();
}
return utf8str;
#else
std::string s;
std::getline(std::cin, s);
return s;
#endif
}
int main(int argc, char **argv)
{
using namespace std;
#if defined _WIN32
SetConsoleOutputCP(CP_UTF8);
#endif
string u8 = u8"Ελληνικά -- Español -- 中国 -- ĐĄßĞĝ 🕹🕹🕹🕹♥️♥️♥️♥️♥️♥️🎹🎹🎹🎹🎹🎹🎹";
cout << u8 << endl;
cout << "Input anything here ,more than emoji:" << endl;
string input = getLineUtf8();
cout << input << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment