This is the mail archive of the
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();
int ret = strcoll(p, q);
p += strlen(p);
q += strlen(q);
if (p == pend && q == qend)
else if (p == pend)
else if (q == qend)
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