Re: [PATCH 3/3] introduce "format" date-mode
- From
Eric Sunshine <sunshine@sunshineco.com>
- Date
- Jun 30, 2015, 18:13 UTC
- Message-ID
- <CAPig+cSJXZjrZ=OcV+C6bkCEHQFrw35-cjQ3At_Qo5KrpYxaQA@mail.gmail.com>
- In-Reply-To
- <20150630175852.GB5349@peff.net>
On Tue, Jun 30, 2015 at 1:58 PM, Jeff King <peff@peff.net> wrote:
Show 7 quoted lines
> On Tue, Jun 30, 2015 at 12:58:33PM -0400, Eric Sunshine wrote: >> Beyond the extra allocation, I was also concerned about the >> sledgehammer approach of "%s " to append a single character when there >> are much less expensive ways to do so. > > I don't think there's any other way. We have to feed a contiguous buffer > to strftime, and we don't own the buffer, so we have to make a new copy.
Sorry, I meant that the interpolation expense of "%s ". A cheaper (but more verbose) alternative might be:
size_t n = strlen(fmt);
const char *f = xmalloc(n + 2);
strcpy(f, fmt);
f[n] = ' ';
f[n + 1] = '\0';
...
free(f);or something similar.