Delphi XE - 我应该使用String还是AnsiString? [英] Delphi XE - should I use String or AnsiString?
问题描述
我想将所有其他库转换为Unicode,但对于这个特定的库,我认为更好地遵守ANSI。优点是内存需求,因为在某些情况下,我加载非常大的TXT文件(仅包含Ansi字符)。缺点可能是当我使这些库与正常(unicode)库进行交互时,我必须做很多很多的类型转换。
有一些一般的指导来显示什么时候转换成Unicode,何时坚持使用Ansi?
一般准则的问题是这样的事情对一个人的情况非常具体。您的例子就是其中之一。
然而,对于Googling的人来到这里,一些一般的指导方针是:
-
是的,转换为Unicode。不要尝试使用
AnsiString
来完全保留旧的应用程序。原因是整个VCL是Unicode,你不应该尝试混合这两个,因为你将转换每次你分配一个Unicode字符串到一个ANSI字符串,这是一个有损的转换。试图保持旧的方式,因为它的工作较少(或类似的原因)会导致你的痛苦;只需拥抱新的字符串
类型,转换和使用它。 -
而不是随机混合两个,明确地执行你需要的任何转换,一次 - 例如,如果你从你的程序的旧版本加载数据,你知道它将是ANSI,所以读它到一个Unicode字符串那里,就是这样。以后,它将是Unicode。
-
您不需要更改
字符串的类型
变量 -string
pre-D2009是ANSI,D2009和alter是Unicode。而应遵循编译器警告,并观察您使用的字符串方法 - 有些仍然使用AnsiString
参数,我发现这一切都令人困惑。编译器会告诉你。 -
如果你使用字符串来保存字节(换句话说,使用它们作为字节数组,因为字符是一个字节)切换到
TBytes
。 -
您可能会遇到特定问题,如加密(字符串不再是字节/字符,所以'字符'为'字符',你可能会得到不同的输出);阅读文本文件(使用流类和 TEncoding );坦率地说,杂项的东西。
评论者,请添加更多建议...我大部分使用C ++ Builder,而不是Delphi,而且Delphi中可能有不少具体的东西我不知道。
现在就您的具体问题你应该转换这个图书馆吗?
如果:
- 这些值代表字符(A真的是A,不是字节值65 - 如果是,使用TBytes)和
- 加载大文本文件,内存是一个问题
< A和U之间的值真的只在这个范围内,
然后不转换为Unicode,相反,将字符串
s切换到 AnsiString
,这是有道理的。
请注意:
- 每次从ANSI转换为Unicode时都有一个开销
- 你可以使用
UTF8String
,这是一种特定类型的AnsiString
,不会有损转换,并且仍然将大多数文本(罗马字符)存储在单个字节中。 - 将
string
的所有实例更改为AnsiString
可能是一些工作,您将需要检查与它们调用的所有方法,以查看是否执行了太多的隐式转换(用于性能)等。 - 您可能需要更改库的外层以使用Unicode,以便转换代码或ANSI / Unicode编译器警告对您的库的用户不可见
- 如果您转换为Unicode,则不能记住这些语法,如果MySet中有S,则可能
?将不起作用。从你的描述字符A到U,我可以猜到你想使用这种语法。
我的建议? / strong>个人而言,我从您提供的信息中做到这一点的唯一原因是内存使用,可能的性能取决于您正在做的这个大量的 A..U
s。 如果真的很重要,那就是驱动程序和约束,你应该转换为ANSI。
I finally upgraded to Delphi XE. I have a library of units where I use strings to store plain ANSI characters (chars between A and U). I am 101% sure that I will never ever use UNICODE characters in those places.
I want to convert all other libraries to Unicode, but for this specific library I think it will be better to stick with ANSI. The advantage is the memory requirement as in some cases I load very large TXT files (containing ONLY Ansi characters). The disadvantage might be that I have to do lots and lots of typecasts when I make those libraries to interact with normal (unicode) libraries.
There are some general guidelines to show when is good to convert to Unicode and when to stick with Ansi?
The problem with general guidelines is that something like this can be very specific to a person's situation. Your example here is one of those.
However, for people Googling and arriving here, some general guidelines are:
Yes, convert to Unicode. Don't try to keep an old app fully using
AnsiString
s. The reason is that the whole VCL is Unicode, and you shouldn't try to mix the two, because you will convert every time you assign a Unicode string to an ANSI string, and that is a lossy conversion. Trying to keep the old way because it's less work (or some similar reason) will cause you pain; just embrace the newstring
type, convert, and go with it.Instead of randomly mixing the two, explicitly perform any conversions you need to, once - for example, if you're loading data from an old version of your program you know it will be ANSI, so read it into a Unicode string there, and that's it. Ever after, it will be Unicode.
You should not need to change the type of your
string
variables -string
pre-D2009 is ANSI, and in D2009 and alter is Unicode. Instead, follow compiler warnings and watch which string methods you use - some still take anAnsiString
parameter and I find it all confusing. The compiler will tell you.If you use strings to hold bytes (in other words, using them as an array of bytes because a character was a byte) switch to
TBytes
.You may encounter specific problems for things like encryption (strings are no longer byte/characters, so 'character' for 'character' you may get different output); reading text files (use the stream classes and TEncoding); and, frankly, miscellaneous stuff. Search here on SO, most things have been asked before.
Commenters, please add more suggestions... I mostly use C++Builder, not Delphi, and there are probably quite a few specific things for Delphi I don't know about.
Now for your specific question: should you convert this library?
If:
- The values between A and U are truly only ever in this range, and
- These values represent characters (A really is A, not byte value 65 - if so, use TBytes), and
- You load large text files and memory is a problem
then not converting to Unicode, and instead switching your string
s to AnsiString
s, makes sense.
Be aware that:
- There is an overhead every time you convert from ANSI to Unicode
- You could use
UTF8String
, which is a specific type ofAnsiString
that will not be lossy when converted, and will still store most text (Roman characters) in a single byte - Changing all the instances of
string
toAnsiString
could be a bit of work, and you will need to check all the methods called with them to see if too many implicit conversions are being performed (for performance), etc - You may need to change the outer layer of your library to use Unicode so that conversion code or ANSI/Unicode compiler warnings are not visible to users of your library
- If you convert to Unicode, sets of characters (can't remember the syntax, maybe
if 'S' in MySet
?) won't work. From your description of characters A to U, I could guess you would like to use this syntax.
My recommendation? Personally, the only reason I would do this from the information you've given is the memory use, and possibly performance depending on what you're doing with this huge amount of A..U
s. If that truly is significant, it's both the driver and the constraint, and you should convert to ANSI.
这篇关于Delphi XE - 我应该使用String还是AnsiString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!