如何判断Delphi变体是否为空字符串? [英] How to tell if a Delphi variant is an empty string?

查看:871
本文介绍了如何判断Delphi变体是否为空字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在处理D2007的最后一个遗留应用程序,将其迁移到Delphi XE。



在此期间,变体已经发生了很大变化。



这一行代码:


$ b $如果(VarType(Value)= varString)和(Value ='')然后
退出,那么b

  

返回True并在D2007中退出,但不在Delphi XE中。



我已将其更改为:

 如果VarIsStr(Value)和(VarToStr )='')然后
退出;

我不相信这是最好的方式。变式单位没有具体的要求来做这个,我当然记得这是过去人们的一个问题。但是,搜索显示没有图书馆功能或任何其他接受的方式。



有没有正确或更好的方式?

解决方案

更新:字符串特定以避免例外:

 如果VarIsStr值)和(长度(VarToStr(v))= 0)然后... 

Update3:If你想要更好的性能和更少的字符串堆内存浪费尝试这个。
想象一下,这个字符串的长度是64K。上面的代码是一个VarToStr,并且可以分配64K的UnicodeString堆空间来保存数据,所以我们可以在BSTR的字符串末尾寻找nul终结符,而对于其他类型的Nil指针。



下面的代码有点奇怪,因为它们通常不能达到变体的内部表示,但是David指出了错误,我重新测试了虽然没有任何明示或暗示的保证,但它似乎正常工作。这只小狗的单位测试会很好。在未来的日子里,如果Delphi RTL的上帝决定重命名Variant结构域的内部表示,下面的代码将需要更改。

  function VarStrEmpty(v:Variant):Boolean; 
var
data:PVarData;
begin
data:= FindVarData(V);
案例数据^ .VType
varOleStr:
result:=(data ^ .VOleStr ^ =#0);
varString:
result:=(data ^ .VString = nil);
varUString:
result:=(data ^ .VUString = nil);
else
result:= false;
结束
结束


Variants are always fun, eh?

I am working on a legacy application that was last in D2007 to migrate it to Delphi XE.

Variants have changed quite a bit in the interim.

This line of code:

if (VarType(Value) = varString) and (Value = '') then 
  Exit;

returned True and exited in D2007, but doesn't in Delphi XE.

I have changed it to this:

if VarIsStr(Value) and (VarToStr(Value) = '') then
    Exit;

I'm not convinced this is the "best" way to go. The Variants unit doesn't have a specific call to do this, and I certainly recall this being an issue for folks in the past. However, a search revealed no library function or any other accepted way.

Is there a "correct" or better way?

解决方案

Updated: String-specific to avoid exceptions:

    if VarIsStr(Value) and (Length(VarToStr(v))=0) then ...

Update3: If you want better performance and less string heap memory waste try this. Imagine that the strings are 64K in length. The code above does a VarToStr and allocates perhaps 64K of UnicodeString heap space to hold the data, just so we can just look for the nul terminator at the end of the string for BSTR, and for nil-pointers for other types.

The code below is a slightly odd in that one does not commonly reach into the internal representation of variants, but David pointed out the bugs and I re-re-tested it and it seems to work, although no warranty is expressed or implied. A unit test for this puppy would be good. At some future date if Delphi RTL gods decided to rename the internal representation of the Variant structure fields, the code below would need to be changed.

function VarStrEmpty(v:Variant):Boolean;
var
  data:PVarData;
begin
    data := FindVarData(V);
  case data^.VType of
     varOleStr:
            result := (data^.VOleStr^=#0);
     varString:
            result := (data^.VString=nil);
     varUString:
            result := (data^.VUString=nil);
     else
      result := false;
  end;
end;

这篇关于如何判断Delphi变体是否为空字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆