Code::Blocks Forums

Developer forums (C::B DEVELOPMENT STRICTLY!) => Development => Topic started by: sethjackson on June 09, 2006, 02:37:06 am

Title: wxString::Printf()
Post by: sethjackson on June 09, 2006, 02:37:06 am
Hey I have a simple (read probably dumb) question.

Why does alot of the C::B code use wxString::Printf()?

I never use wxString::Printf() in my code. I always use <<. In my opnion it is easier, and better to use <<. So are there any major disadvantages to using <<?
Title: Re: wxString::Printf()
Post by: takeshimiya on June 09, 2006, 04:48:39 am
Yes, somehow the operator<< didn't worked ok with integers, eventrough the documentation says otherwise.
Title: Re: wxString::Printf()
Post by: mandrav on June 09, 2006, 08:42:50 am
So are there any major disadvantages to using <<?

Yes, somehow the operator<< didn't worked ok with integers, eventrough the documentation says otherwise.

Exactly. In unicode builds, streaming integers to wxString resulted to nothing  :shock: . I don't know if it has been fixed in the current wx version but we wouldn't go back and change everything now.
Another reason is translations. E.g.:
Code
1. _("Welcome %s to %s (build %d).");
2. << _("Welcome ") << s << _(" to ") << s2 << _(" (build ") << i << _(").");
In many languages a word's translation might differ based on context. In the second case, there is no context (each string would be translated separately).
Which is easier for the translators? ;)
Title: Re: wxString::Printf()
Post by: sethjackson on June 09, 2006, 01:26:41 pm
So are there any major disadvantages to using <<?

Yes, somehow the operator<< didn't worked ok with integers, eventrough the documentation says otherwise.

Exactly. In unicode builds, streaming integers to wxString resulted to nothing  :shock: . I don't know if it has been fixed in the current wx version but we wouldn't go back and change everything now.
Another reason is translations. E.g.:
Code
1. _("Welcome %s to %s (build %d).");
2. << _("Welcome ") << s << _(" to ") << s2 << _(" (build ") << i << _(").");
In many languages a word's translation might differ based on context. In the second case, there is no context (each string would be translated separately).
Which is easier for the translators? ;)

Actually << works with integers in wx 2.6.3 IIRC (don't take my word for it though I need to check it first).
Title: Re: wxString::Printf()
Post by: sethjackson on June 09, 2006, 07:55:20 pm
I just tested << with integers, and it works just fine with wx 2.6.3. :D
Title: Re: wxString::Printf()
Post by: Balazs on June 10, 2006, 02:58:07 pm
If printfs work, there is no reason to change.
Also, as a Hungarian, I can tell, that most often the whole sentence needs to be restructured often to get a meaningful translation, and with <<, this is impossible.

Besides, what would be the advantages of <<? Is it a 100-times faster or what? :roll:

--
Greets,
B.
Title: Re: wxString::Printf()
Post by: Game_Ender on June 10, 2006, 05:34:48 pm
Besides, what would be the advantages of <<? Is it a 100-times faster or what? :roll:

The standard C printf is not type safe and you can crash your program by telling printf you are giving it one kind of variable and passing it another.  So the << operator makes this a compile time check instead of a run time crash.  I am not sure if wxString::printf, does the same.  I do know that Boost format allows printf like syntax with the same type safety and still giving you the translation benifits.  You can also add the << operator to your classes so you just do "cout << MyClassInstance;".
Title: Re: wxString::Printf()
Post by: Balazs on June 10, 2006, 07:11:59 pm
Quote
The standard C printf is not type safe and you can crash your program by telling printf you are giving it one kind of variable and passing it another.
GCC checks for this at compile time.

And how do you solve translation?
Title: Re: wxString::Printf()
Post by: sethjackson on June 10, 2006, 11:24:24 pm
Quote
The standard C printf is not type safe and you can crash your program by telling printf you are giving it one kind of variable and passing it another.
GCC checks for this at compile time.

Can you show me please?
Title: Re: wxString::Printf()
Post by: Ceniza on June 11, 2006, 12:52:51 am
Code: cpp
#include <stdio.h>

int main()
{
  printf("%s %d %g", 1, 2);
  return 0;
}

Quote from: gcc -Wall test.c -o test.exe
test.c: In function `main':
test.c:5: warning: format argument is not a pointer (arg 2)
test.c:5: warning: too few arguments for format
Title: Re: wxString::Printf()
Post by: sethjackson on June 11, 2006, 03:42:27 am
Code: cpp
#include <stdio.h>

int main()
{
  printf("%s %d %g", 1, 2);
  return 0;
}

Quote from: gcc -Wall test.c -o test.exe
test.c: In function `main':
test.c:5: warning: format argument is not a pointer (arg 2)
test.c:5: warning: too few arguments for format

Cool  8) I didn't know GCC checked that. I always use -Wall, but I never use printf so that is probably why I missed it...... Anyways I stand corrected. :)
Title: Re: wxString::Printf()
Post by: Balazs on June 11, 2006, 01:44:19 pm
Thanks Ceniza! :)
Title: Re: wxString::Printf()
Post by: Game_Ender on June 12, 2006, 10:53:50 pm
Very cool, I didn't know GCC did that check with printf.  Is that a general variage argument function check or is it just something special for the printf function?  If it is a special printf check, then it probably doesn't work with wxString::Printf.  Don't get me wrong, I like the printf syntax I just don't like the unsafeness (Looks to be a little miss placed with GCC+printf) of variable argument functions.  Boost.format (http://www.boost.org/libs/format/doc/format.html) (supports stand printf syntax, and positional arguments) of course solves both the type safety and translation issues, but at a 50% to 80% drop in performance.
Title: Re: wxString::Printf()
Post by: thomas on June 12, 2006, 11:20:03 pm
It works for gcc builtin variadic functions, such as [fs]?(print|scan)f, and it does confirmedly not work with wxString::Printf - we have seen it crash and burn many times in the past.
Title: Re: wxString::Printf()
Post by: sethjackson on June 13, 2006, 02:49:34 am
It works for gcc builtin variadic functions, such as [fs]?(print|scan)f, and it does confirmedly not work with wxString::Printf - we have seen it crash and burn many times in the past.

Cool 8) Too bad it doesn't work for wxString::Printf()

Anyways I don't like printf(), or wxString::Printf() (I never use them). I love the << operator. :D