This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
RE: [RFC] libstdc++/9817 or "passing C++ strings to C functions"
- From: Pétur Runólfsson <peturr02 at ru dot is>
- To: "Paolo Carlini" <pcarlini at unitus dot it>,<libstdc++ at gcc dot gnu dot org>
- Cc: "Nathan Myers" <ncm at cantrip dot org>
- Date: Mon, 24 Feb 2003 09:42:15 -0000
- Subject: RE: [RFC] libstdc++/9817 or "passing C++ strings to C functions"
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