:Roland Mainz wrote: :> [The following falls under the category "micro-optimisation" but may IMO :> still be worth an investigation] :> After working on various parts of OpenSolaris I found that is common to :> use the following sequence to concatenate strings: :> -- snip -- :> ... :> char *s; :> ... :> strcat(s, "foo"); :> strcat(s, "/"); :> strcat(s, "bar"); :> -- snip -- :> (note: The example is simplified, normally { "foo", ",", "bar" } are :> normal strings and no static string literals) :> while this code is simple and easy to understand it is quite inefficient :> - |strcat()| will always walk |s| each time. If |s| already contains a :> large path this will be horrible time-consuming. :> :> Back in the 1990 timeframe there was the "DICE C" compiler for AmigaOS :> (AFAIK SAS C/C++ had something similar - but I am not sure) which solved :> this issue quite cleanly via having a special version of |strcat()| :> which returned the end of the string instead of the beginning (like :> ANSI-C |strcat()| does) ... I don't remember the functions's name in :> "DICE C" anymore - lets call it |strFOOcat()| for now... : :[Seems I finally found Matthew Dillons email address...] :Matthew: Do you remeber how your Amiga DICE-C compiler called the :function described above ? : :---- : :Bye, :Roland
I don't recall writing a function to do that, but I wrote DICE a long long time ago. The DICE code is open source now, you can get it from my web site: http://apollo.backplane.com/ and try to find it :-) It is fairly simple to use something like snprintf() to keep track of your target buffer's string length, then do further 'appends' using buf + n in successive calls. From the manual page for snprintf(): ... Upon success, these functions return the number of characters printed (not including the trailing `\0' used to end output to strings), or, in the case of snprintf() and vsnprintf(), the number of characters that would have been printed if the size were unlimited (again, not including the final `\0'). All of these function return a negative value if an output error occurs. Generally speaking, we rip out all uses of strcpy() or strcat() or even strncat() (because strncat was badly designed and does not guarentee that the target buffer will be terminated) that we find these days because those functions, with no bounded copying size or that have string termination edge cases, are simply too dangerous to use under any circumstances. snprintf() or asprintf() is the prefered method. I use snprintf() or asprintf() whenever I don't think the string operations in question are performance critical (which is most of the time), and I manually keep track of the string length and target buffer (string) length and just use bcopy (or memcpy) if performance is an issue. My recommendation is to get used to using snprintf(). -Matt Matthew Dillon <[EMAIL PROTECTED]> _______________________________________________ opensolaris-discuss mailing list opensolaris-discuss@opensolaris.org