Tools.h++ Manual
5-10 104011 Tandem Computers Incorporated
5
use null bytes. You should also keep in mind that while
RWCString::data()
always returns a null-terminated string, there may be earlier nulls in the string.
All of these effects can be summarized by the following program:
RWCString a(“abc”); // 1
RWCString b(“abc\0def”); // 2
RWCString c(“abc\0def”, 7); // 3
cout << a.length(); // Prints “3”
cout << strlen(a.data()); // Prints “3”
cout << b.length(); // Prints “3”
cout << strlen(b.data()); // Prints “3”
cout << c.length(); // Prints “7”
cout << strlen(c.data()); // Prints “3”
Note that two different constructors were used above. The constructor in lines
1 and 2 take a single argument of "
const char*
", a null-terminated string.
Because it takes a single argument, it may be used in type conversion (ARM
12.3.1). The length of the results is determined in the usual manner: the
number of bytes before the null. The constructor in line 3 takes a "
const
char
*" and a run length. The constructor will copy this many bytes, including
any embedded nulls.
The length of an
RWCString
(in bytes) is always given by
RWCString::length()
. Because the string may include embedded nulls,
this length may not match the results given by
strlen()
.
Note that indexing and other operators (basically, all functions using an
argument of type
size_t
) work in bytes. Hence, these operators will not
work for
RWCStrings
containing multibyte strings.
5.8 Wide character strings
Class
RWWString
is extremely similar to
RWCString
, except that it works with
wide characters. These are much easier to manipulate than multibyte
characters because they are all the same size: the size of a
wchar_t
.
Tools.h++ makes it easy to convert back and forth between multibyte and wide
character strings. Here’s an example that builds on the previous section: