所有程序应该做的一个常见的条件是检查字符串是否为空。
采取以下陈述:
(1)
if Length(Str)=0 then // do something
(2)
if Str='' then // do something
解决方法
在XE2中,如果str =”编译为更快的Ansi和Unicode字符串代码。如果Length(str)= 0编译为更宽的字符串更快的代码。
测试程序:
{$A8,B-,C+,D+,E-,F-,G+,H+,I+,J-,K-,L+,M-,N-,O+,P+,Q-,R-,S-,T-,U-,V+,W-,X+,Y+,Z1} program Project172; {$APPTYPE CONSOLE} {$R *.res} var sa1,sa2: AnsiString; sw1,sw2: WideString; su1,su2: UnicodeString; begin if Length(sa1) = 0 then ; if sa2 = '' then ; if Length(sw1) = 0 then ; if sw2 = '' then ; if Length(su1) = 0 then ; if su2 = '' then ; end.
编译代码:
Project172.dpr.14: if Length(sa1) = 0 then 004050E2 A19C9B4000 mov eax,[$00409b9c] 004050E7 85C0 test eax,eax 004050E9 7405 jz $004050f0 004050EB 83E804 sub eax,$04 004050EE 8B00 mov eax,[eax] 004050F0 85C0 test eax,eax Project172.dpr.16: if sa2 = '' then 004050F2 833DA09B400000 cmp dword ptr [$00409ba0],$00 Project172.dpr.18: if Length(sw1) = 0 then 004050F9 A1A49B4000 mov eax,[$00409ba4] 004050FE 85C0 test eax,eax 00405100 7407 jz $00405109 00405102 83E804 sub eax,$04 00405105 8B00 mov eax,[eax] 00405107 D1E8 shr eax,1 00405109 85C0 test eax,eax Project172.dpr.20: if sw2 = '' then 0040510B A1A89B4000 mov eax,[$00409ba8] 00405110 33D2 xor edx,edx 00405112 E839E8FFFF call @WStrEqual Project172.dpr.22: if Length(su1) = 0 then 00405117 A1AC9B4000 mov eax,[$00409bac] 0040511C 85C0 test eax,eax 0040511E 7405 jz $00405125 00405120 83E804 sub eax,$04 00405123 8B00 mov eax,[eax] 00405125 85C0 test eax,eax Project172.dpr.24: if su2 = '' then 00405127 833DB09B400000 cmp dword ptr [$00409bb0],$00
如果优化被禁用,差异甚至更大。
Project172.dpr.14: if Length(sa1) = 0 then 004050E2 A19C9B4000 mov eax,[$00409b9c] 004050E7 8945EC mov [ebp-$14],eax 004050EA 837DEC00 cmp dword ptr [ebp-$14],$00 004050EE 740B jz $004050fb 004050F0 8B45EC mov eax,[ebp-$14] 004050F3 83E804 sub eax,$04 004050F6 8B00 mov eax,[eax] 004050F8 8945EC mov [ebp-$14],eax 004050FB 837DEC00 cmp dword ptr [ebp-$14],$00 Project172.dpr.16: if sa2 = '' then 004050FF 833DA09B400000 cmp dword ptr [$00409ba0],$00 Project172.dpr.18: if Length(sw1) = 0 then 00405106 A1A49B4000 mov eax,[$00409ba4] 0040510B 8945E8 mov [ebp-$18],eax 0040510E 837DE800 cmp dword ptr [ebp-$18],$00 00405112 740D jz $00405121 00405114 8B45E8 mov eax,[ebp-$18] 00405117 83E804 sub eax,$04 0040511A 8B00 mov eax,[eax] 0040511C D1E8 shr eax,1 0040511E 8945E8 mov [ebp-$18],eax 00405121 837DE800 cmp dword ptr [ebp-$18],$00 Project172.dpr.20: if sw2 = '' then 00405125 A1A89B4000 mov eax,[$00409ba8] 0040512A 33D2 xor edx,edx 0040512C E81FE8FFFF call @WStrEqual Project172.dpr.22: if Length(su1) = 0 then 00405131 A1AC9B4000 mov eax,[$00409bac] 00405136 8945E4 mov [ebp-$1c],eax 00405139 837DE400 cmp dword ptr [ebp-$1c],$00 0040513D 740B jz $0040514a 0040513F 8B45E4 mov eax,[ebp-$1c] 00405142 83E804 sub eax,$04 00405145 8B00 mov eax,[eax] 00405147 8945E4 mov [ebp-$1c],eax 0040514A 837DE400 cmp dword ptr [ebp-$1c],$00 Project172.dpr.24: if su2 = '' then 0040514E 833DB09B400000 cmp dword ptr [$00409bb0],$00