Text functions

Problems compiling? Don't understand the source code? Don't know how to code your feature? Post here.

Moderator: Moderators

Locked
stevensheehy
Posts: 4
Joined: 2006-03-22 21:34

Text functions

Post by stevensheehy » 2006-09-09 14:12

I understand the need for the duplicate functions in Text.h that have an extra temporary string (less string copying), but why are they sometimes used outside of Text.h? Shouldn't these functions be private so that only the single parameter functions can be called by other classes?

e.g. In Stringsearch.h, instead of:

Code: Select all

string lower;
Text::toLower(aText, lower);
use

Code: Select all

string lower = Text::toLower(aText);
This occurs in a number of places. For consistency, shouldn't only the non-temporary functions be used? Or am I missing some important point?

FarCry
Programmer
Posts: 34
Joined: 2003-05-01 10:49

Re: Text functions

Post by FarCry » 2006-09-13 05:30

stevensheehy wrote:This occurs in a number of places. For consistency, shouldn't only the non-temporary functions be used? Or am I missing some important point?
You brought the point up yourself: Operating on an object passed by the caller by reference involves less copying and (more importantly) fewer heap operations. This is equally true outside and inside of "Text.h". For the short example you gave, the compiler is likely to optimize the tempory object away on its own, of course.

Wether that performance advantage justifies a fatter interface (or at least worse readability if you remove the functions returning a temp object) is another matter. But since you called it a necessity...

arnetheduck
The Creator Himself
Posts: 296
Joined: 2003-01-02 17:15

Post by arnetheduck » 2006-09-17 03:36

Indeed, farcry is right...most of the doubled functions are used in some text intensive areas like queue saving, so it makes sense to avoid temporaries, even if the code is slightly uglier to read...
That said, stlport5 strings come with some optimisations I think regarding these temporaries, but I still haven't upgraded / investigated...

Locked