[RFC] libstdc++/9817 or "passing C++ strings to C functions"
Pétur Runólfsson
peturr02@ru.is
Mon Feb 24 09:43:00 GMT 2003
Paolo Carlini wrote:
> Other proposals?
I think something like this may work (untested code ahead):
const char* p = str1.c_str();
const char* pend = str1.c_str() + str1.length();
const char* q = str2.c_str();
const char* qend = str2.c_str() + str2.length();
for (;;)
{
int ret = strcoll(p, q);
if (ret)
return ret;
p += strlen(p);
q += strlen(q);
if (p == pend && q == qend)
return 0;
else if (p == pend)
return -1;
else if (q == qend)
return 1;
++p;
++q;
}
This breaks the strings into chunks separated by \0 and then
compares the chunks until it finds a pair that compares unequal,
or one or both strings are empty. Note that strlen has to be
called for both strings since strings of different length can
collate equal.
Petur
More information about the Libstdc++
mailing list