Is there some advantage in use resourcestring instead of a const string?

社会主义新天地 提交于 2019-11-27 03:07:01

问题


Would you tell me if there is some advantage (less sotorage space, increase speed, etc) in using:

resourcestring
    MsgErrInvalidInputRange = 'Invalid Message Here!';

instead of

const
    MsgErrInvalidInputRange : String = 'Invalid Message Here!';

回答1:


As others have mentioned, resourcestring strings will be included in a separate resource within your exe, and as such have advantages when you need to cater for multiple languages in the UI of your app.

As some have mentioned as well, const strings are included in the data section of your app.

Up to D2007

In Delphi versions up to D2007, const strings were stored as Ansi strings, requiring a single byte per character, whereas resource strings would be stored in UTF-16: the windows default encoding (though perhaps not for Win9x). IIRC D2007 and prior versions didn't support UTF-8 encoded unit files. So any strings coded in your sources would have to be supported by the ANSI code pages, and as such probably didn't go beyond the Unicode Basic Multilingual Plane. Which means that only the UCS-2 part of UTF-16 would be used and all strings could be stored in two bytes per character.

In short: up to D2007 const strings take a single byte per character, resource strings take two bytes per character.

D2009 and up

Delphi was unicode enabled in version D2009. Since then things are a little different. Resourcestring strings are still stored as UTF-16. No other option here as they are "managed" by Windows.

Consts strings however are a completely different story. Since D2009 Delphi stores multiple versions of each const string in your exe. Each version in a different encoding. Const can be stored as Ansi strings, UTF-8 strings and UTF-16 strings.

Which of these encodings is stored depends on the use of the const. By default UTf-16 will be used, as that is the default internal Delphi encoding. Assign the same const to a "normal" (UTF-16) string, as well as to an AnsiString variable, and the const will be stored in the exe both UTF-16 and Ansi encoded...

De-duping

By the looks of it (experimenting with D5 and D2009), Delphi "de-dupes" const strings, whereas it doesn't do this for resourcestring strings.




回答2:


The const option will be faster than resourcestring, because the later will call the Windows API to get the resource text. You can make it faster by using some caching mechanism. This is what we do in our Enhanced Delphi RTL.

And it's a good idea to first load the resourcestring into a string, if you'll have to access many times to a resourcestring content.

The main point of resourcestring is to allow i18n (internationalization) of your program.

You've got the Translation Manager with some editions of the Delphi IDE. But it relies on external DLL.

You can use the gettext system, coming from the Linux world, from http://dxgettext.po.dk which relies on external .po files.

We included our own i18n mechanism in our framework, which translates and caches the resourcestring text, and relies on external .txt files (you can use UTF-8 or Unicode text files, from Delphi 6 up to XE). The caching make it quite as fast as the const usage. See http://synopse.info/fossil/finfo?name=SQLite3/SQLite3i18n.pas

There are other open source or commercial solutions around.

About size storage, resourcestring are stored as UC2 buffers. So resourcestring will use more memory than string up to Delphi 2009. Since Delphi 2009, all string are unicodestring i.e. UCS2, so you won't have much more storage size. In all cases, storage size of text is not the bigger size parameter for an application (bitmaps and code size have a much bigger effect to the final exe).




回答3:


Resource strings are stored as STRINGTABLE entries in your exe resource, consts are stored as part of the fixed data segment. Since they're part of the resource section you can extract them and the DFMs, translate them, and store them in a resource module (data-only DLL). When a Delphi app starts, it looks for that DLL and will use the strings from it instead of the ones included in your EXE to load translations.

The Embarcadero docwiki covers using the Translation Manager, but a lot of other Delphi translation tools use resource strings too.




回答4:


With resourcestring, the compiler places those strings as a stringtable resource in the executable, allowing anyone (say your translation team) to edit them with a resource editor without needing to recompile the application, or have access to the source code.




回答5:


There's also a third options that is:

const MsgErrInvalidInputRange = 'Invalid Message Here!';

The latter shoud be the more performant one because tell the compiler to not allocate space in the data segment, it could put the string in the code segment. Also remember that what coould be done with typed constants depends on the $WRITEABLECONST directive, although I do not know what the compiler exactly when it is on or off.



来源:https://stackoverflow.com/questions/4292611/is-there-some-advantage-in-use-resourcestring-instead-of-a-const-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!