Unicode vs. Windows Console

If I run this, what the output would be?

#include <string>
#include <iostream>

#include <winrt\base.h>
#include <winrt\Windows.Foundation.h>
#include <winrt\Windows.Globalization.DateTimeFormatting.h>

#pragma comment(lib, "windowsapp.lib")

int main()
{
	auto const Now = winrt::clock::now();
	winrt::Windows::Globalization::DateTimeFormatting::DateTimeFormatter DateTimeFormatter { L"shortdate longtime" };
	std::wcout << "Now is " << static_cast<std::wstring>(DateTimeFormatter.Format(Now)) << std::endl;
	return 0;
}

Here we go:

What appears to be wrong is Unicode Left-to-Right mark character, which is killing the console and it stops accepting any further text!

Now if you’re going to do this:

#include <string>
#include <iostream>

#include <winrt\base.h>
#include <winrt\Windows.Foundation.h>
#include <winrt\Windows.Globalization.DateTimeFormatting.h>

#pragma comment(lib, "windowsapp.lib")

std::wstring Replace(std::wstring const& Input, std::wstring const& A, std::wstring const& B)
{
	std::wstring Output;
	for(size_t C = 0; ; )
	{
		auto const D = Input.find(A, C);
		if(D == Input.npos)
		{
			Output.append(Input.substr(C));
			break;
		}
		Output.append(Input.substr(C, D - C));
		Output.append(B);
		C = D + A.length();
	}
	return Output;
}

int main()
{
	auto const Now = winrt::clock::now();
	winrt::Windows::Globalization::DateTimeFormatting::DateTimeFormatter DateTimeFormatter { L"shortdate longtime" };
	std::wcout << "Now is " << Replace(static_cast<std::wstring>(DateTimeFormatter.Format(Now)), L"\u200E", L"") << std::endl;
	return 0;
}

Then you get what you want, and not the trailing EOL is in its place (and it’s not in the first run):

Leave a Reply