This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]