--- Comment #2 from Daniel Fruzynski <> ---
You probably could optimize snprintf(buf, size, "%s", str) into this:

if (size > 0)
  buf[0] = 0;
  strncat(buf, str, size - 1);

Other optimizations would require to generate more code aa you wrote. Some of
them probably could be applied, e.g. transforming first line to second:

sprintf(buf, "%s %d", "123", arg);
memcpy(buf, "123 ", 4); sprintf(buf+4, "%d", arg);

Dedicated string concatenation function with multiple args also would be
helpful to perform following transformation:

sprintf(buf, "Content-Type: %s/%s\n", str1, str2);
multistrcat(buf, "Content-Type: ", str1, "/", str2, "\n");

It is also possible to parse format string and create sprintf with series of
commands which will create the same result. Of course this will increase code
size like you wrote, so user would have to explicitly enable this optimization.
There are many factors there which must be taken into account, e.g. number of
format arguments, use of advanced formatting (e.g. setting field width or
precision), etc.

Reply via email to