Skip to content

Instantly share code, notes, and snippets.

@hzeller
Created December 7, 2015 01:29
Show Gist options
  • Save hzeller/e377cc8be916b191d499 to your computer and use it in GitHub Desktop.
Save hzeller/e377cc8be916b191d499 to your computer and use it in GitHub Desktop.
Experiments with various wxString formatting options (background: investigating ThrowIOError formatting problems in pcbnew/*specctra* files)
/* simplification to narrow down the format problems */
/*
* Compiled and run (you might need to adapt include directories)
g++ -Wall -o format-test -isystem /usr/lib/x86_64-linux-gnu/wx/include/gtk2-unicode-3.0 -isystem /usr/include/wx-3.0 -D__WXGTK__ format-test.cpp -L/usr/lib/x86_64-linux-gnu/ -lwx_baseu-3.0 && ./format-test
*/
#include <stdio.h>
#include <wx/string.h>
// Macros from macros.h
static inline const wxChar* GetChars( const wxString& s ) {
return (const wxChar*) s.c_str();
}
#define TO_UTF8( wxstring ) ( (const char*) (wxstring).utf8_str() )
// Method to test:
void FormattedPrint(const char* what, const wxString& fmt, ... ) {
wxString result;
va_list args;
va_start( args, fmt );
result.PrintfV( fmt, args );
va_end( args );
printf("%s: '%s'\n", what, TO_UTF8(result));
}
int main() {
wxString fmt = wxString::FromUTF8("this is a test >%s<");
wxString value = wxString::FromUTF8("SomeTest value");
printf("Input: format:'%s' and value:'%s'\n", TO_UTF8(fmt), TO_UTF8(value));
printf("sizeof(wxChar) = %zd\n", sizeof(wxChar));
FormattedPrint("wxstring::PrintfV(fmt, GetChars()) ", fmt, GetChars(value));
FormattedPrint("wxstring::PrintfV(fmt, value.GetData()) ", fmt, value.GetData());
FormattedPrint("wxstring::PrintfV(fmt, TO_UTF8()) ", fmt, TO_UTF8(value));
// Same test with wxString::Printf
wxString out;
out.Printf(fmt, value);
printf("wxstring::Printf(fmt, wxstring) : '%s'\n", TO_UTF8(out));
out.Printf(fmt, GetChars(value));
printf("wxstring::Printf(fmt, GetChars(...)): '%s'\n", TO_UTF8(out));
out.Printf(fmt, TO_UTF8(value));
printf("wxstring::Printf(fmt, TO_UTF8(..)) : '%s'\n", TO_UTF8(out));
}
$ g++ -Wall -o format-test -isystem /usr/lib/x86_64-linux-gnu/wx/include/gtk2-unicode-3.0 -isystem /usr/include/wx-3.0 -D__WXGTK__ format-test.cpp -L/usr/lib/x86_64-linux-gnu/ -lwx_baseu-3.0 && ./format-test
Input: format:'this is a test >%s<' and value:'SomeTest value'
sizeof(wxChar) = 4
wxstring::PrintfV(fmt, GetChars()) : 'this is a test >S<'
wxstring::PrintfV(fmt, value.GetData()) : 'this is a test >'
wxstring::PrintfV(fmt, TO_UTF8()) : 'this is a test >SomeTest value<'
wxstring::Printf(fmt, wxstring) : 'this is a test >SomeTest value<'
wxstring::Printf(fmt, GetChars(...)): 'this is a test >SomeTest value<'
wxstring::Printf(fmt, TO_UTF8(..)) : 'this is a test >SomeTest value<'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment