Handling of const strings/char arrays, e.g. with KActionCollection

Friedrich W. H. Kossebau kossebau at kde.org
Tue Apr 22 17:58:35 BST 2008


Hi,

I just saw a commit from Dirk with the comment
	"
	some QLatin1* ification. quickly gave up as it is
	too much work for all of kdelibs, but wanted to commit this anyway
	"
which reminded me of some longtime questions I am unsure with the answers. 
Could you help me?

Let's do this by the example KActionCollection: 
http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKActionCollection.html

All actions are identified by a name, which is a QString in the API.
If you grep through the KDE sources you see that almost everywhere instead 
plain C char arrays are used, e.g.
	actionCollection->addAction( "identifier" );
Which will result in a deep copy due to the implicit QString(const char*), 
even if this is a static string and never to be changed.
Payed with both runtime speed (on program start even) and heap memory (a 
KActionCollection stays e.g. for the complete life of a KMainWindow). No 
numbers/measurement if this is of practical relevance, but I am interested 
even theorethically.

Question 1:
Now I could go and wrap all strings with QLatin1String on the caller side.
Another, perhaps more simple fix would be to overload all functions with their 
const-char* equivalents as convenience functions, which internally call the 
old one using QLatin1String.
What is to be preferred? How costly are the additional symbols in the library 
against all the explicit QLatin1String calls in the binary code, which are 
also making the source code harder to read? Any idea?
Should I prepare such a patch for KActionCollection?

Question 2:
All KDE source files are in UTF-8 AFAIK. So if someone  puts non-latin1 chars 
in a string, e.g.
	const char identifier[] = "strânge ïdēntìfĩȩr <JAPANESE chars>";
the C++ compiler will create a char array which matches the UTF-8 
representation in bytes, so sizeof(identifier) > numbers of chars. Right?
And the content of QString( identifier ) or QLatinString( identifier ) will 
not be the original string as in the source file, but the bytes encoded in 
Latin1 (if no other code uses QTextCodec::setCodecForCStrings(), do we catch 
this?). Right?
Still the identifiers from the rc file are read as UTF-8 strings and contained 
as such in QString. 
So this restricts all action identifiers to be latin1 chars. Right?
Than this should be noted with the API Dox of KActionCollection. I would 
prepare a patch if the above is correct.

Thanks
Friedrich




More information about the kde-core-devel mailing list