incubator-stdcxx-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Martin Sebor <se...@roguewave.com>
Subject Re: svn commit: r387687 - in /incubator/stdcxx/trunk/tests: include/rw_char.h self/0.char.cpp src/char.cpp
Date Fri, 24 Mar 2006 17:55:19 GMT
Anton Pevtsov wrote:

> Martin, the problem may occurred when someone call rw_narrow without the
> string length specified:
[...]
> You can see the following output:
> 
> source string   : "ab\0cd"
> after rw_widen  : "ab\0cd"
> after rw_narrow : "ab"		<-- here we lost the rest symbols
> following '\0' in the middle of the string
> 
> Actually this is not important in tests because we always know the array
> length. But is this behaviour of rw_narrow expected?

Yes. When invoked with just two arguments (i.e., without the size
argument), both rw_narrow() and rw_wide() compute the length of
the source string as if by calling strlen() (or is equivalent for
wchar_t* and UserChar*). There's no other way for the function to
compute the length of the source string but to rely on the first
NUL character to determine where it ends.

Martin

#include <rw_printf.h>
#include <rw_char.h>

template <class charT>
void test ()
{
     const char str[] = "ab\0cd";
     rw_printf ("source string   : %{/*.*Gs}\n", 1, 5, str);

     charT wstr [10];
     rw_widen (wstr, str, 5);
     rw_printf ("after rw_widen  : %{/*.*Gs}\n",
                int (sizeof (charT)), 5, wstr);

     char res [10];
     rw_narrow (res, wstr);
     rw_printf ("after rw_narrow : %{s}\n", res);

}

int main ()
{
     test<char>();
     test<wchar_t>();
     test<UserChar>();
}

source string   : "ab\0cd"
after rw_widen  : "ab\0cd"
after rw_narrow : "ab"
source string   : "ab\0cd"
after rw_widen  : "ab\0cd"
after rw_narrow : "ab"
source string   : "ab\0cd"
after rw_widen  : "ab\0cd"
after rw_narrow : "ab"



Mime
View raw message