Table of Contents:
Boost.Nowide is a library implemented by Artyom Beilis that make cross platform Unicode aware programming easier.
The library provides an implementation of standard C and C++ library functions, such that their inputs are UTF-8 aware on Windows without requiring to use Wide API.
Consider a simple application that splits a big file into chunks, such that they can be sent by e-mail. It requires doing few very simple taks:
int main(int argc,char **argv)
std::fstream::open(char const *,std::ios::openmode m)
std::remove(char const *file)
std::cout << file_name
Unfortunately it is impossible to implement this simple task in a plain C++ if the file names contain non-ASCII characters
The simple program that uses the API would work on the systems that use UTF-8 internally – the vast majority of Unix-Line operating systems: Linux, Mac OS X, Solaris, BSD. But it would fail on files like
War and Peace - Война и мир - מלחמה ושלום.zip under Microsoft Windows because the native Windows Unicode aware API is Wide-API - UTF-16.
This, such a trivial task is very hard to implement in a cross platform manner.
Boost.Nowide provides a set of standard library functions that are UTF-8 aware and makes Unicode aware programming easier.
The library provides:
mainparameters to use UTF-8
Why not to provide both Wide and Narrow implementations so the developer can choose to use Wide characters on Unix-Like platforms
wchar_tis not really portable, it can be 2 bytes, 4 bytes or even 1 byte making Unicode aware programming harder
fopen(wchar_t const *,wchar_t const *)in the standard library, so it is better to stick to the standards rather than re-implement Wide API in "Microsoft Windows Style"
The library is mostly header only library, only console I/O requires separate compilation under Windows.
As a developer you are expected to to
boost::nowide functions instead of the function avalible in the
For example, Unicode unaware implementation of line counter:
To make this program handle Unicode properly we do the following changes:
This is very simple and straight forward approach helps writing Unicode aware programs.
Of course this simple set of functions does not cover all needs. However if you need to access Wide API from Windows application using UTF-8 encoding internally you can use functions like
So the conversion is done at the last stage and you continue using UTF-8 strings anywhere and only at glue points you switch to Wide API.
Such that the example above can be rewritten as:
wshort_stackstringthat use buffers of size 256 or 16 characters, and if the string is longer, they fall-back to memory allocation
The library does not include the
windows.h in order to prevent namespace pollution with numerous defines and types. The library rather defines the prototypes to the Win32 API functions.
However if you may request to use original
windows.h header by setting
BOOST_NOWIDE_USE_WINDOWS_H define before including any of the Boost.Nowide headers
Under POSIX platforms the boost::nowide::fopen and all other functions are aliases to standard library functions:
Console I/O implemented as wrapper over ReadConsoleW/WriteConsoleW unless the stream is not "atty" like a pipe than ReadFile/WriteFile is used.
This approach eliminates a need of manual code page handling. If TrueType fonts are used the Unicode aware input and output would work.
Q: Why the library does not convert the string from Locale's encoding not UTF-8 and wise versa on POSIX systems
A: It is inherently incorrect to convert strings to/from locale encodings on POSIX platforms.
You can create a file named "\xFF\xFF.txt" (invalid UTF-8), remove it, pass its name as a parameter to program and it would work whether the current locale is UTF-8 locale or not. Also changing the locale from let's say
en_US.ISO-8859-1 would not magically change all files in OS or the strings a user may pass to the program (which is different on Windows)
POSIX OSs treat strings as
NUL terminated cookies.
So altering their content according to the locale would actually lead to incorrect behavior.
For example, this is a naive implementation of a standard program "rm"
It would work with ANY locale and changing the strings would lead to incorrect behavior.
The meaning of a locale under POSIX and Windows paltforms is different and has very different effects.
It is possible to use Nowide library without dependency on the huge Boost project. There is a standalone version that all the functionality is placed in
nowide namespace instead of
boost::nowide. The example above would look like
The upstream sources can be found at GitHub: https://github.com/artyom-beilis/nowide
You can download the latest sources there: