Printing UTF-8 Text to the Windows Console: Sample Code

In a previous blog post, you saw that to print some UTF-8-encoded text to the Windows console, you first have to convert it to UTF-16.

In fact, calling _setmode to change stdout to _O_U8TEXT, and then trying to print UTF-8-encoded text with cout, resulted in a debug assertion failure in the VC++ runtime library. (Please take a look at the aforementioned blog post for more details.)

That blog post lacked some compilable demo code showing the solution, though. So, here you are:

// Test printing UTF-8-encoded text to the Windows console

#include "UnicodeConv.hpp"  // My Unicode conversion helpers

#include <fcntl.h>
#include <io.h>
#include <stdint.h>
#include <iostream>
#include <string>

int main()
{
    // Change stdout to Unicode UTF-16.
    //
    // Note: _O_U8TEXT doesn't seem to work, e.g.:
    // https://blogs.msmvps.com/gdicanio/2017/08/22/printing-utf-8-text-to-the-windows-console/
    //
    _setmode(_fileno(stdout), _O_U16TEXT);


    // Japanese name for Japan, encoded in UTF-8
    uint8_t utf8[] = {
        0xE6, 0x97, 0xA5, // U+65E5
        0xE6, 0x9C, 0xAC, // U+672C
        0x00
    };
    std::string japan(reinterpret_cast<const char*>(utf8));

    // Print UTF-16-encoded text
    std::wcout << Utf16FromUtf8("Connie") << L"\n\n";
    std::wcout << Utf16FromUtf8(japan)    << L'\n';
}

Note that in the above code I test printing out both a pure ASCII string like “Connie” (which is automatically a valid UTF-8-encoded string), and a couple of Japanese kanjis, encoded in UTF-8.

This is the output:

Unicode UTF-8 text converted to UTF-16 and printed out to the Windows console

All right.

Note also that I set the Windows console font to MS Gothic to be able to correctly render the Japanese kanjis.

The compilable C++ source code, including the implementation of the Utf16FromUtf8 Unicode conversion helper function, can be found here on GitHub.

 

C++ WinReg Update

The Windows Registry native C API is very low-level and hard to use.

A while back I developed some convenient higher-level C++ wrappers, that should make Windows Registry programming easier and more fun. You can find them on GitHub.

Just to give you a taste of this C++ WinReg library, you can simply open a registry key with code like this:

RegKey key{ HKEY_CURRENT_USER, L"SOFTWARE\\Connie" };

And you can read registry values like that:

DWORD dw = key.GetDwordValue(L"MagicCode");
wstring s = key.GetStringValue(L"ReadMe");

Enumerating values under a given key is as easy as:

auto values = key.EnumValues();

Error management is performed using C++ exceptions (instead of “raw” error codes like in the native C API).

You can find more info in the README file.

I haven’t touched that project in a few months (being very busy), but yesterday I accepted an external contribution and merged a pull request adding a DeleteTree feature.

I take this occasion to thank everyone who showed interest and appreciation for this project.

Happy coding!

 

Is There “Connie” in the Map?

Today is Pi (π) Day, and I wanted to celebrate with this blog post (even if it’s C++-related and not math-related). Hope you’ll find it useful, and maybe even enjoy it.

 

std::map is one of those useful containers implemented in the C++ Standard Library. It basically stores key-value pairs, with unique keys. In other words, std::map maps unique keys to values.

As a concrete example, you can think of an address book: The “key” is the person’s name, and the “value” associated with it can be a custom data structure storing information like address, e-mail address, telephone number, and so on.

This can be translated in C++ code like that:

std::map<std::string, Person> addressBook;

Another interesting application could be counting words in a document. In this case, you still have a string key (i.e. the word you’re counting), and the associated value in the key-value pair would be an integer representing the occurrences of that word:

map<string, int> wordCount;

E.g.: If you have 500 “Connie”s in a document, “Connie” is mapped to 500.

You can use a map from string to int for word counting applications.

A common operation you can do on a map is checking if there is an element with a given key in the container (i.e. a given name in the previous address book example, or a given word in the word counting one).

So, assuming that you have some std::map object with std::string keys, how to check if “Connie” is a key contained in the map?

Method #1: Invoking map::find()

Well, std::map offers a find() method. You pass as input the key of the element to search for, and find() returns an iterator to the element with that given key. If such element doesn’t exist in the map container, then std::find() returns the map’s end iterator.

So, if you want to check if “Connie” is a key in the map, you can call std::map::find(), and compare the returned iterator to the map’s end iterator:

if (myMap.find("Connie") != myMap.end())
{
    // "Connie" is in the map
    cout << "Found! \n";
}

Method #2: Invoking map::count()

There’s also another option. std::map offers the count() method, which returns the number of elements with the given key, which is either 1 or 0 for std::map (as std::map contains unique keys).

So, translated in C++ you get something like that:

if (myMap.count("Connie") == 1)
{
    // "Connie" is in the map
    cout << "Found! \n";
}

 

Two options for checking if an element with a given key is contained in a std::map, invoking the find or count methods

Method #3: Back to the Future, and Thank You C++20

Honestly, I found the previous two methods kind of obscure and unclear (especially before someone has already introduced them to you). I mean: If I want to check if a std::map contains an element with a given key, why can’t I just ask the container in a simple straightforward way? Something like that:

if (myMap.contains("Connie"))
{
    // "Connie" is in the map
    cout << "Found! \n";
}

I even found a newsgroup post dating back to 2009, in which I expressed my wish for a simple clear map::contains() method:

Fortunately, it seems that C++20 will finally add it! 😊

As a general rule of class interface design, I believe it’s better to add a direct clear straightforward method to the public interface, than forcing your class users to “fight” the interface and circumnavigate it to get the result.

 

OK, since today is π Day, I think there’s room for some math 😊 So, all these methods (find, count and contains) have logarithmic asymptotic complexity (O(log(N)) in the size of the container. If you are curious about the Big-O notation and asymptotic complexity, you may want to check out my course on Introduction to Data Structures and Algorithms in C++.

Big-O doesn’t have to be boring!

 

New Pluralsight Course: Introduction to Data Structures and Algorithms in C++

A new course of mine was published in the Pluralsight library: Introduction to Data Structures and Algorithms in C++.

In this course, you’ll learn how to implement some fundamental data structures and algorithms in C++ from scratch, with a combination of theoretical introduction using slides, and practical C++ implementation code.

Introducing the stack with an interesting metaphor

No prior data structure or algorithm theory knowledge is required. You only need a basic knowledge of C++ language features (please watch the “Prerequisites” clip in the first module for more details about that).

Explaining linear search using slides

During this course journey, you’ll also learn some practical C++ coding techniques (ranging from move semantic optimization, to proper safe array copying techniques, insertion operator overloading, etc.) that you’ll be able to use in your own C++ projects, as well.

So, this course is both theory and practice!

Spotting a subtle bug

Here’s just a couple of feedback notes from my reviewers:

The callouts are helpful and keep the demo engaging as you explain the code. [Peer Review]

To say that this is an excellent explanation of Big-O notation would be an understatement. The way you illustrate and explain it is far better than the way it was taught to me in college! [Peer Review]

Big-O doesn’t have to be boring!

Starting from this course page, you can freely play the course overview, and read a more detailed course description and table of content.

I hope you’ll enjoy watching this course!

 

C++ String Guidance

Last time, I enumerated a few types of strings available in C++.

These days, I’d suggest as the default option for cross-platform standard C++ code to use std::string, storing UTF-8-encoded text inside it. Note that pure ASCII is a proper subset of UTF-8, so storing pure ASCII text in std::string objects is just fine.

In addition, for those platform-specific sections of C++ code, I’d suggest using whatever string class and encoding are typical and “natural” for that platform. For example, at the Windows API boundary, use the UTF-16 encoding, and the std::wstring class in C++ code that doesn’t use ATL or MFC.

In addition, in C++ Windows-specific code that already uses ATL or MFC, another option is to use CString (or the explicit CStringW) enabling Visual Studio Unicode builds (“Configuration Properties” | “General” | “Character Set”: “Use Unicode Character Set”, which has been the default since probably Visual Studio 2005).

On the other hand, Qt-based C++ code can use the QString class, and so on.

 

How Many Strings Does C++ Have?

(…OK, a language lawyer would nitpick suggesting “How many string types…”, but I wanted a catchier title.)

So, if you program in Python and you see something enclosed by either single quotes or double quotes, you have a string:

s = 'Connie'

Something similar happens in Java, with string literals like “Connie” implemented as instances of the java.lang.String class:

String s = "Connie";

All right.

Now, let’s enter – drumroll, please – The Realm of C++! And the fun begins 😊

So, let’s consider this simple line of C++ code:

auto s1 = "Connie";

What is the type of s1?

std::string? A char[7] array? (Hey, “Connie” is six characters, but don’t forget the terminating NUL!)

…Something else?

So, you can use your favorite IDE, and hover over the variable name, and get the deduced type. Visual Studio C++ IntelliSense suggests it’s “const char*”. Wow!

Visual Studio IntelliSense deduces const char pointer.

And what about “Connie”s?

auto s2 = "Connie"s;

No, it’s not the plural of “Connie”. And it’s not a malformed Saxon genitive either. This time s2 is of type std::string! Thank you operator””s introduced in C++14!

Visual Studio IntelliSense deduces std::string

But, are we done? Of course, not! Don’t forget: It’s C++! 😊

For example, you can have u8”Connie”, which represents a UTF-8 literal. And, of course, we need a thread on StackOverflow to figure out “How are u8-literals supposed to work?”

And don’t forget L”Connie”, u”Connie” and U”Connie”, which represent const wchar_t*, const char16_t* (UTF-16 encoded) and const char32_t* (UTF-32 encoded) respectively.

Now we are done, right? Not yet!

In fact, you can combine the previous prefixes with the standard s-suffix, for example: L”Connie”s is a std::wstring! U”Connie”s is a std::u32string. And so on.

Done, right? Not yet!! In fact, there are raw string literals to consider, too. For example: R”(C:\Path\To\Connie)”, which is a const char* to “C:\Path\To\Connie” (well, this saves you escaping \ with \\).

And don’t forget the combinations of raw string literals with the above prefixes and optionally the standard s-suffix, as well: LR”(C:\Path\To\Connie)”, UR”(C:\Path\To\Connie)”, LR”(C:\Path\To\Connie)”s, UR”(C:\Path\To\Connie)”s, and more!

Oh, and in addition to the standard std::string class, and other standard std::basic_string-based typedefs (e.g. std::wstring, std::u16string, std::u32string, etc.), there are platform/library specific string classes, like ATL/MFC’s CString, CStringA and CStringW. And Qt brings QString to the table. And wxWidgets does the same with its wxString.

Wow! And I would not be surprised if I missed some other string variation out 😊

P.S. With all this string variety (maybe too much…), what about adding to the C++ Standard Library some convenient functions for at least common string operations like trimming spaces and converting strings to upper case and lower case? All in all, C++ does already have rocket-science stuff like Bessel functions in its Standard Library. While, back in the old MFC days, CString already offered convenient methods like Trim, MakeLower and MakeUpper, just to name a few.


Sample slide: Introducing the std::string class
Sample slide: Introducing the std::string class

If you want to learn modern C++ from scratch, in a fun and interesting way, with engaging slides and demo code, please check out my course!

Is Your “C++11 from Scratch” Course Still Valid Today? Yes, Absolutely!

I’m very proud of my “C++11 from Scratch” course published by Pluralsight.

We are in 2018, and there have been C++14 and C++17 in the meantime. So, a legit question is: “Does it make sense for me to watch your C++11 course today for a beginner-oriented introduction to C++?” And the answer is a BIG STRONG YES! 😊

In fact, in that course you will learn modern C++ topics that are valid in both C++11, and also in next versions of the language. For example, what you will learn about the parameter passing rules, like passing by reference vs. passing by value, is perfectly valid in C++11, C++14, and C++17 as well.

Moreover, the practical introductions I gave to standard library’s classes like std::string, or std::vector, and to the std::sort algorithm, just to name a few, are totally valid also in C++14 and C++17.

Similarly, my discussions on defining custom types, constructors, destructor, the RAII pattern and the scope-based lifetime of objects are still valid in C++14 and C++17, as well.

Maybe a better title for that course would be “Modern C++ from Scratch”. Anyway, the content is already there, available for an enjoyable learning experience, with a mix of slides containing interesting visuals, and demo code.

Sample slide: Introducing the std::string class
Sample slide: Introducing the std::string class

And, if you are already familiar with C++11, you may enjoy my follow-up course on “Practical C++14 and C++17 Features”.

Happy learning!

 

ATL::CStringW vs. std::wstring Performance: String Sorting and Comparisons

According to previous measurements, std::wstring performs better than ATL::CStringW:

  1. in all string concatenation tests
  2. when sorting string vectors that are made by small strings, thanks to std::basic_string’s SSO

So, I focused my attention on the string vector sorting scenario, and it seems to me that (at least in the VS2015 implementation), the slowdown of (non-SSO) wstrings is caused by wmemcmp calls, that are used to compare wstrings when sorting the string vectors. On the other hand, CStringW invokes wcscmp, that seems to run faster.

In fact, invoking std::sort with a custom comparator function that calls wcscmp to compare the C-style pointers returned by wstring::c_str, results in faster vector<wstring> sorting times. So, in this case wstring sorting performs better than CStringW even for non-SSO strings.

However, as Stephan T. Lavavej pointed out, std::basic_string supports embedded nulls, so wstring cannot use wcscmp (that works only for C-style null-terminated strings, without embedded nulls).

 

String Performance Tests: ATL vs. STL

I wrote some C++ code to test the performance of the ATL CStringW class vs. the C++ Standard Library’s std::wstring.

There are several aspects that can be considered when comparing string class performance: In the aforementioned code, I tested string vector sorting and string concatenation.

The code is available in this repository on GitHub.

You can take a look at the README for further details (including my test results).