Boost logo

Boost :

From: Blake Madden (madden_blake_at_[hidden])
Date: 2004-01-16 16:37:58

I am working on a string class that can represent a string constant, but can
be used with both char and wchar_t values. For example:

//assigned with unicode "A HREF"
const narrow_wide_string<> A_HREF = L"A HREF";

//ahref will be ANSI "A HREF"
std::string ahref = A_HREF;
//wahref will be unicode "A HREF"
std::wstring wahref = A_HREF;
//returns 6
size_t size = strlen(A_HREF);
//returns 6
size_t wsize = wcslen(A_HREF);

Essentially, this solves the problem of needing to embed string literals
inside of templatized string functions that are meant to work with both char
and wchar_t types. Currently, if you have to use string literals, then you
to overload your string functions: one version that takes char, the other
wchar_t. With this class, you can just make one templatized function and
use this class to string your constant.

It also can be initialized with individual unicode values and will convert
the internal char string using a customizable codecvt paramter. This means
that you could do this:

//"unicode_to_shiftjis" is a user-defined codecvt class that converts
Japanese unicode
//text to the corresonding Shift JIS values.
narrow_wide_string<unicode_to_shiftjis<wchar_t, char, mbstate_t> >
   japanese_text(3, 0x30A0, 0x30B4, 0);
//similar to saying
wchar_t japanese_text[3] = { 0x30A0, 0x30B4, 0; }

The benefit is that if you have ANSI (char) and unicode strings in your
code, you do not have to maintain both a unicode string literal and an ANSI
literal for the same string. Also, the class handles the conversion to char
format for you, so you never have to hard code encoded char literals (e.g.,
Shift JIS). Simply provide the (immutable) unicode value, and if you need
to use a different ANSI encoding then simply plug in a differetn codecvt

Would there be any interest in a class such as this for possible boost
consideration? I shall add this to Boost Yahoo files if anyone is

Blake Madden

Learn how to choose, serve, and enjoy wine at Wine @ MSN.

Boost list run by bdawes at, gregod at, cpdaniel at, john at