Author Topic: wxString::Printf()  (Read 13767 times)

sethjackson

  • Guest
wxString::Printf()
« 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 <<?

takeshimiya

  • Guest
Re: wxString::Printf()
« Reply #1 on: June 09, 2006, 04:48:39 am »
Yes, somehow the operator<< didn't worked ok with integers, eventrough the documentation says otherwise.

Offline mandrav

  • Project Leader
  • Administrator
  • Lives here!
  • *****
  • Posts: 4315
    • Code::Blocks IDE
Re: wxString::Printf()
« Reply #2 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? ;)
Be patient!
This bug will be fixed soon...

sethjackson

  • Guest
Re: wxString::Printf()
« Reply #3 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).
« Last Edit: June 09, 2006, 07:54:50 pm by sethjackson »

sethjackson

  • Guest
Re: wxString::Printf()
« Reply #4 on: June 09, 2006, 07:55:20 pm »
I just tested << with integers, and it works just fine with wx 2.6.3. :D

Balazs

  • Guest
Re: wxString::Printf()
« Reply #5 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.

Offline Game_Ender

  • Lives here!
  • ****
  • Posts: 551
Re: wxString::Printf()
« Reply #6 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;".

Balazs

  • Guest
Re: wxString::Printf()
« Reply #7 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?
« Last Edit: June 10, 2006, 07:16:18 pm by Balazs »

sethjackson

  • Guest
Re: wxString::Printf()
« Reply #8 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?
« Last Edit: June 11, 2006, 03:40:55 am by sethjackson »

Offline Ceniza

  • Developer
  • Lives here!
  • *****
  • Posts: 1441
    • CenizaSOFT
Re: wxString::Printf()
« Reply #9 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

sethjackson

  • Guest
Re: wxString::Printf()
« Reply #10 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. :)

Balazs

  • Guest
Re: wxString::Printf()
« Reply #11 on: June 11, 2006, 01:44:19 pm »
Thanks Ceniza! :)

Offline Game_Ender

  • Lives here!
  • ****
  • Posts: 551
Re: wxString::Printf()
« Reply #12 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 (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.

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: wxString::Printf()
« Reply #13 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.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

sethjackson

  • Guest
Re: wxString::Printf()
« Reply #14 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