Retire StrPrintf entirely, {fmt} is better than c-isms.

This commit is contained in:
Mark Roszko
2025-12-04 23:23:38 -05:00
parent 5cbafd6903
commit 6da131bdb0
7 changed files with 2 additions and 119 deletions
-43
View File
@@ -50,49 +50,6 @@
#endif
static int vprint( std::string* result, const char* format, va_list ap )
{
va_list tmp;
va_copy( tmp, ap );
size_t len = vsnprintf( nullptr, 0, format, tmp );
va_end( tmp );
// Resize the output to hold the required data
size_t size = result->size();
result->resize( size + len );
// Now do the actual printing
len = vsnprintf( result->data() + size, len + 1, format, ap );
return len;
}
int StrPrintf( std::string* result, const char* format, ... )
{
va_list args;
va_start( args, format );
int ret = vprint( result, format, args );
va_end( args );
return ret;
}
std::string StrPrintf( const char* format, ... )
{
std::string ret;
va_list args;
va_start( args, format );
ignore_unused( vprint( &ret, format, args ) );
va_end( args );
return ret;
}
wxString SafeReadFile( const wxString& aFilePath, const wxString& aReadType )
{
// Check the path exists as a file first
-1
View File
@@ -32,7 +32,6 @@
#include <core/map_helpers.h>
#include <fmt/core.h>
#include <macros.h>
#include <richio.h> // StrPrintf
#include <string_utils.h>
#include <wx_filename.h>
#include <fmt/chrono.h>
-28
View File
@@ -42,34 +42,6 @@
#include <kicommon.h>
#include <io/kicad/kicad_io_utils.h>
/**
* This is like sprintf() but the output is appended to a std::string instead of to a
* character array.
*
* @param aResult is the string to append to, previous text is not clear()ed.
* @param aFormat is a printf() style format string.
* @return the count of bytes appended to the result string, no terminating nul is included.
*/
KICOMMON_API int
#if defined(__GNUG__)
__attribute__ ((format (printf, 2, 3)))
#endif
StrPrintf( std::string* aResult, const char* aFormat, ... );
/**
* This is like sprintf() but the output is returned in a std::string instead of to a
* character array.
*
* @param format is a printf() style format string.
* @return std::string - the result of the sprintf().
*/
KICOMMON_API std::string
#if defined(__GNUG__)
__attribute__ ((format (printf, 1, 2)))
#endif
StrPrintf( const char* format, ... );
/**
* Nominally opens a file and reads it into a string. But unlike other facilities, this handles
-1
View File
@@ -60,7 +60,6 @@ bool IsUTF8( const char* aString );
* wxString() with many member functions. There are multiple ways to create text into
* a std::string without the need of too many member functions:
*
* - richio.h's StrPrintf().
* - std::ostringstream.
*
* Because this class uses no virtuals, it should be possible to cast any std::string
+1 -2
View File
@@ -464,8 +464,7 @@ bool PCB_EDIT_FRAME::OpenProjectFiles( const std::vector<wxString>& aFileSet, in
// This is for python:
if( aFileSet.size() != 1 )
{
UTF8 msg = StrPrintf( "Pcbnew:%s() takes a single filename", __func__ );
DisplayError( this, msg );
DisplayError( this, wxString::Format( "Pcbnew:%s() takes a single filename", __func__ ) );
return false;
}
@@ -132,7 +132,7 @@ void PCB_IO_KICAD_SEXPR_PARSER::init()
for( int i=1; i<=14; ++i )
{
std::string key = StrPrintf( "Inner%d.Cu", i );
std::string key = fmt::format( "Inner{}.Cu", i );
m_layerMasks[key] = LSET( { PCB_LAYER_ID( In15_Cu - 2 * i ) } );
}
-43
View File
@@ -36,48 +36,5 @@
*/
BOOST_AUTO_TEST_SUITE( RichIO )
/**
* Test the #vprint method.
*/
BOOST_AUTO_TEST_CASE( VPrint )
{
std::string output;
// Test 1: Basic strings and numbers
StrPrintf( &output, "Hello %s! ", "World" );
StrPrintf( &output, "Number: %d, ", 42 );
StrPrintf( &output, "Float: %.2f, ", 3.14 );
StrPrintf( &output, "Char: %c. ", 'A' );
BOOST_CHECK_EQUAL( output, std::string( "Hello World! Number: 42, Float: 3.14, Char: A. " ) );
output.clear();
// Test 2: Large string
std::string longString( 500, 'A' );
StrPrintf( &output, "%s", longString.c_str() );
BOOST_CHECK_EQUAL( output, longString );
output.clear();
// Test 3: Edge case with zero characters
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wformat-zero-length"
#endif
StrPrintf( &output, "" );
#ifdef __GNUC__
#pragma GCC diagnostic warning "-Wformat-zero-length"
#endif
BOOST_ASSERT( output.empty() );
// Test 4: Mixing small and large strings
StrPrintf( &output, "Small, " );
StrPrintf( &output, "%s, ", longString.c_str() );
StrPrintf( &output, "End." );
BOOST_CHECK_EQUAL( output, std::string( "Small, " + longString + ", End." ) );
output.clear();
// Test 5: Formatting with various data types
StrPrintf( &output, "%d %s %c %.2f", 123, "Hello", 'X', 9.876 );
BOOST_CHECK_EQUAL( output, std::string( "123 Hello X 9.88" ) );
output.clear();
}
BOOST_AUTO_TEST_SUITE_END()