Still improving! (Was: Re: [PATCH] Avoid strtok for thread safety)
Martin Sebor
sebor@roguewave.com
Wed Nov 27 08:23:00 GMT 2002
Paolo Carlini wrote:
...
> Nice, I didn't consider that... However, in the meanwhile, I tried to post
> (without success, due to a problem with my smtp server :( a different
> version
> (tested x86-linux), which basically avoid half the finds thanks to the a
> priori knowldege of the names of the various categories. Something like
> this:
>
> {
> char* __new;
> const char* __end;
> const char* __beg = __s + strlen(_S_categories[0]) + 1;
> for (size_t __i = 0;
> __i < _S_categories_size + _S_extra_categories_size - 1; ++__i)
> {
> __end = strpbrk(__beg, ";");
> __new = new char[__end - __beg + 1];
> memcpy(__new, __beg, __end - __beg);
> __new[__end - __beg] = '\0';
> _M_names[__i] = __new;
> __beg = __end + strlen(_S_categories[__i + 1]) + 2;
> }
> __new = new char[__s + __len - __beg];
> memcpy(__new, __beg, __s + __len - __beg);
> _M_names[_S_categories_size + _S_extra_categories_size - 1] = __new;
> }
Sorry for butting in, but the code above doesn't seem exception
safe :) (I.e., it will leak if any of the new expressions except
the first one throws).
Regards
Martin
More information about the Libstdc++
mailing list