summaryrefslogtreecommitdiffstats
path: root/src/web_buffer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/web_buffer.c')
-rw-r--r--src/web_buffer.c43
1 files changed, 26 insertions, 17 deletions
diff --git a/src/web_buffer.c b/src/web_buffer.c
index 9f9ceda63..f5452452f 100644
--- a/src/web_buffer.c
+++ b/src/web_buffer.c
@@ -84,6 +84,19 @@ inline char *print_number_llu_r(char *str, unsigned long long uvalue) {
return wstr;
}
+inline char *print_number_llu_r_smart(char *str, unsigned long long uvalue) {
+#ifdef ENVIRONMENT32
+ if(uvalue > (unsigned long long)0xffffffff)
+ str = print_number_llu_r(str, uvalue);
+ else
+ str = print_number_lu_r(str, uvalue);
+#else
+ do *str++ = (char)('0' + (uvalue % 10)); while(uvalue /= 10);
+#endif
+
+ return str;
+}
+
void buffer_print_llu(BUFFER *wb, unsigned long long uvalue)
{
buffer_need_bytes(wb, 50);
@@ -201,29 +214,25 @@ void buffer_sprintf(BUFFER *wb, const char *fmt, ...)
{
if(unlikely(!fmt || !*fmt)) return;
- buffer_need_bytes(wb, 2);
-
- size_t len = wb->size - wb->len - 1;
- size_t wrote;
-
va_list args;
- va_start(args, fmt);
- wrote = (size_t) vsnprintfz(&wb->buffer[wb->len], len, fmt, args);
- va_end(args);
+ size_t wrote = 0, need = 2, multiplier = 0, len;
+
+ do {
+ need += wrote + multiplier * WEB_DATA_LENGTH_INCREASE_STEP;
+ multiplier++;
- if(unlikely(wrote >= len)) {
- // truncated
- buffer_overflow_check(wb);
+ debug(D_WEB_BUFFER, "web_buffer_sprintf(): increasing web_buffer at position %zu, size = %zu, by %zu bytes (wrote = %zu)\n", wb->len, wb->size, need, wrote);
+ buffer_need_bytes(wb, need);
- debug(D_WEB_BUFFER, "web_buffer_sprintf(): increasing web_buffer at position %zu, size = %zu\n", wb->len, wb->size);
- buffer_need_bytes(wb, len + WEB_DATA_LENGTH_INCREASE_STEP);
+ len = wb->size - wb->len - 1;
va_start(args, fmt);
- buffer_vsprintf(wb, fmt, args);
+ wrote = (size_t) vsnprintfz(&wb->buffer[wb->len], len, fmt, args);
va_end(args);
- }
- else
- wb->len += wrote;
+
+ } while(wrote >= len);
+
+ wb->len += wrote;
// the buffer is \0 terminated by vsnprintf
}