From 6da131bdb0c6cbb68d8ba95b67d0dc42fb7da17b Mon Sep 17 00:00:00 2001 From: Mark Roszko Date: Thu, 4 Dec 2025 23:23:38 -0500 Subject: [PATCH] Retire StrPrintf entirely, {fmt} is better than c-isms. --- common/richio.cpp | 43 ------------------- common/string_utils.cpp | 1 - include/richio.h | 28 ------------ libs/core/include/core/utf8.h | 1 - pcbnew/files.cpp | 3 +- .../kicad_sexpr/pcb_io_kicad_sexpr_parser.cpp | 2 +- qa/tests/common/test_richio.cpp | 43 ------------------- 7 files changed, 2 insertions(+), 119 deletions(-) diff --git a/common/richio.cpp b/common/richio.cpp index ff740fa592..27732127e2 100644 --- a/common/richio.cpp +++ b/common/richio.cpp @@ -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 diff --git a/common/string_utils.cpp b/common/string_utils.cpp index b892cafb55..00e021cfe3 100644 --- a/common/string_utils.cpp +++ b/common/string_utils.cpp @@ -32,7 +32,6 @@ #include #include #include -#include // StrPrintf #include #include #include diff --git a/include/richio.h b/include/richio.h index e70a24d9cd..f6c1aa75c0 100644 --- a/include/richio.h +++ b/include/richio.h @@ -42,34 +42,6 @@ #include #include -/** - * 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 diff --git a/libs/core/include/core/utf8.h b/libs/core/include/core/utf8.h index 8ace934491..f63f0b7344 100644 --- a/libs/core/include/core/utf8.h +++ b/libs/core/include/core/utf8.h @@ -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 diff --git a/pcbnew/files.cpp b/pcbnew/files.cpp index f2b5df1ec7..753e64b373 100644 --- a/pcbnew/files.cpp +++ b/pcbnew/files.cpp @@ -464,8 +464,7 @@ bool PCB_EDIT_FRAME::OpenProjectFiles( const std::vector& 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; } diff --git a/pcbnew/pcb_io/kicad_sexpr/pcb_io_kicad_sexpr_parser.cpp b/pcbnew/pcb_io/kicad_sexpr/pcb_io_kicad_sexpr_parser.cpp index f85bfdc283..a4720c23c2 100644 --- a/pcbnew/pcb_io/kicad_sexpr/pcb_io_kicad_sexpr_parser.cpp +++ b/pcbnew/pcb_io/kicad_sexpr/pcb_io_kicad_sexpr_parser.cpp @@ -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 ) } ); } diff --git a/qa/tests/common/test_richio.cpp b/qa/tests/common/test_richio.cpp index ef5572641e..e9cac58bd7 100644 --- a/qa/tests/common/test_richio.cpp +++ b/qa/tests/common/test_richio.cpp @@ -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() \ No newline at end of file