|
Boost Users : |
From: John Maddock (john_at_[hidden])
Date: 2007-08-18 13:13:57
Venkat Rangan wrote:
> John,
>
> Thanks for following up. We know the size of the buffer we allocated.
> A workaround is to insert a NULL character at the end of the buffer,
> but given that these messages are created elsewhere in other
> components and are delivered to our search module, we do not have an
> opportunity to insert NULL character, forcing a copy to separate
> buffer under our control.
>
> The ideal interface would be:
> 1) terminate when NULL character is found
> 2) if NULL character not found, terminate when end marker is reached.
I'm still somewhat confused how can use the string at all (leaving aside
regex for now), if you don't know how long it is, or whether there is a
terminating NULL or not. But I assume that either the buffer is full (no
NULL) or not full (there is a NULL), if you know from an error code or
something whether the buffer is full, then the string length is either:
* The size of the buffer if the error was raised, or:
* strlen(buffer) if no error was raised.
Failing that you would need a "safe strlen" as you already mentioned:
int strlen_s(const char* p, int len)
{
for(int r = 0; r < len; ++r)
{
if(p[r] == 0) return r;
}
return len;
}
Or you could create your own iterator type that equals the end-of-sequence
iterator when either a NULL character or an end of buffer is found, but to
be honest it would probably be quicker to calculate the length first as
above.
HTH, John.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net