News:

The new Release 25.03 is out! You can download binaries for Windows and many major Linux distros here .

Main Menu

wxString::Printf()

Started by sethjackson, June 09, 2006, 02:37:06 AM

Previous topic - Next topic

sethjackson

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 <<?

takeshimiya

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

mandrav

Quote from: sethjackson on June 09, 2006, 02:37:06 AM
So are there any major disadvantages to using <<?

Quote from: Takeshi Miya on June 09, 2006, 04:48:39 AM
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.:

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? ;)
Be patient!
This bug will be fixed soon...

sethjackson

#3
Quote from: mandrav on June 09, 2006, 08:42:50 AM
Quote from: sethjackson on June 09, 2006, 02:37:06 AM
So are there any major disadvantages to using <<?

Quote from: Takeshi Miya on June 09, 2006, 04:48:39 AM
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.:

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).

sethjackson

I just tested << with integers, and it works just fine with wx 2.6.3. :D

Balazs

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.

Game_Ender

Quote from: Balazs on June 10, 2006, 02:58:07 PMBesides, 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;".

Balazs

#7
QuoteThe 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?

sethjackson

#8
Quote from: Balazs on June 10, 2006, 07:11:59 PM
QuoteThe 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?

Ceniza

Code (cpp) Select
#include <stdio.h>

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


Quote from: gcc -Wall test.c -o test.exetest.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

sethjackson

Quote from: Ceniza on June 11, 2006, 12:52:51 AM
Code (cpp) Select
#include <stdio.h>

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


Quote from: gcc -Wall test.c -o test.exetest.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. :)

Balazs


Game_Ender

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 (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.

thomas

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.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

sethjackson

Quote from: 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.

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