374 this->vsprintf (formatString, args); |
374 this->vsprintf (formatString, args); |
375 va_end (args); |
375 va_end (args); |
376 } |
376 } |
377 |
377 |
378 /*! |
378 /*! |
379 * \brief Formats this string using \c vsnprintf, using the provided arguments. The buffer string will be repeatedly |
379 * \brief Formats this string using \c vsnprintf, using the provided arguments. |
380 * allocated larger and larger until \c vsnprintf succeeds. |
|
381 * \param formatString Template string to use with formatting. |
380 * \param formatString Template string to use with formatting. |
382 * \param args Variadic arguments to use with formatting. |
381 * \param args Variadic arguments to use with formatting. |
383 */ |
382 */ |
384 void String::vsprintf (const char* formatString, va_list args) |
383 void String::vsprintf (const char* formatString, va_list args) |
385 { |
384 { |
386 char* buf = nullptr; |
385 // Copy the argument list so that we have something to provide to vsnprintf in case we have to call it again. |
387 int bufsize = 256; |
386 va_list argsCopy; |
388 |
387 va_copy(argsCopy, args); |
389 do |
388 |
390 { |
389 // First, attempt to format using a fixed-size buffer. |
391 bufsize *= 2; |
390 static char buffer[64]; |
392 delete[] buf; |
391 size_t length = vsnprintf(buffer, sizeof buffer, formatString, args); |
393 buf = new char[bufsize]; |
392 |
394 } |
393 if (length < sizeof buffer) |
395 while (vsnprintf (buf, bufsize, formatString, args) >= bufsize); |
394 { |
396 |
395 // vsnprintf succeeded in fitting the formatted string into the buffer, so we're done. |
397 m_string = buf; |
396 m_string = buffer; |
398 delete[] buf; |
397 } |
|
398 else |
|
399 { |
|
400 // vsnprintf needs more space, so we have to allocate a new buffer and try again. |
|
401 Vector<char> newBuffer(length + 1); |
|
402 vsnprintf(newBuffer.data(), length + 1, formatString, argsCopy); |
|
403 m_string = newBuffer; |
|
404 } |
399 } |
405 } |
400 |
406 |
401 /*! |
407 /*! |
402 * \brief Joins the elements of this string list into one longer string. |
408 * \brief Joins the elements of this string list into one longer string. |
403 * \param delimeter The delimeter to place between the element strings. |
409 * \param delimeter The delimeter to place between the element strings. |