3 ms·
For the first idea: Yes, but both of those would have to interpret the format string and determine the character lengths of the arguments which is duplicated wo
by IAmLiterallyAB 3y ago
For the first idea: Yes, but both of those would have to interpret the format string and determine the character lengths of the arguments which is duplicated work. I'd imagine its a large portion of the work. I'm not convinced its any more efficient than snprintf.
The second idea is neat though... it could return an extra argument which is a pointer to the first part of the format string that wasn't printed
- charcircuit 3y ago>which is duplicated work The caller can allocate memory to be shared between the 2 function calls to avoid needing duplicate work
- vore 3y agoHow do you know how much memory you need to allocate when you need to do the work in the first place to determine it? Maybe if snprintf was resumable by supplying a larger buffer while it was doing its work, but this seems like a lot of complexity.
- charcircuit 3y agoIn this case the state takes a constant amount of space so it is just a matter of allocating a single struct. With the approach of having the caller allocate you have carefully design the API to make it possible.
- lifthrasiir 3y agoA more realistic version of the former is that the call is opportunistically given a buffer and returns an error with the required buffer size if the buffer is too small. If the buffer is big enough you only need a single call, otherwise you need two calls with a redundant processing, which is better then the original version---this even works when the caller can't preallocate a buffer. This approach is pretty common in the Windows API. ---- ADDED: I thought charcircuit was talking about always calling two functions in a row, missing the original comment that charcircuit was replying to. My bad, so I'd like to update this comment as follows... I think the former approach would indeed work if the first call is always given a pointer to the space that can be used to record what has been done so far. Like, `snprintf(ptr, n, &recover, "format", ...)`, and `recover` can be relatively small, like 16 bytes. I would record original `ptr` and `format` arguments to be safe, and largest offsets to `ptr` and `format` that are known to be written and synchronized to each other. Of course this is just a workaround for C's inability to construct and return a sum type.
- IAmLiterallyAB 3y ago? This is exactly how snprintf works, which is what we're discussing.
- lifthrasiir 3y agoOh wait, I missed the original comment. I see what did you actually mean there.