tags:

views:

693

answers:

6

Hi,

my problem is as follows:

WideCompareStr(FName,'')<>0

returns false even with FName set to ''.

WideCompareStr(trim(FName),'')<>0

returns the desired result. Why do I have to trim an empty string ('') for a comparison with another empty sting to get the correct result?


EDIT:

to clear things up: I had the following Code to test wether a widestring-variable is the empty string or not.

function TSybVerwandlung.isEmpty: Boolean;
var
  I : Integer;
begin
  Result:=true;
  if WideCompareStr(FName,'')<>0 then Result:=false
  else if WideCompareStr(FInfo,'')<>0 then Result:=false
  else
  begin
    //additional tests
  end;
end;

This function returned true even with FName set to '' (I checked it in the debugger). After inserting trim(FName) and trim(FInfo) instead of the variables, it returned the desired result.

Did I miss something essential? The compiler I use is Borland Delphi 2006

A: 

I hate to state the obvious, but it must be because you have spaces in your string, perhaps you can build a function that does that for you and you can use for comparision across your application.

My two cents

Miau
A: 

Why don't you simply use Length? Whenever you will have an empty string it will return you zero.

Artem Barger
Because that makes the compiler assume you actually care for the string's length. This might result in a costly StrLen call, depending on how intelligent the compiler actually is.
dummzeuch
So, as for you compare function call is less expensive than length? O_o
Artem Barger
+5  A: 

WideCompareStr returns 0 if both strings are equal. So code:

WideCompareStr(FName,'')<>0

Returns false because both strings ARE equal which is what you are expecting (I guess!).

EDIT:

I am confused now. I just checked and in following code:

procedure TForm1.Button1Click(Sender: TObject);
var
  s1: WideString;
  r1, r2: Integer;
begin
  s1 := '';

  r1 := WideCompareStr (s1, '');
  MessageDlg (IntToStr (r1), mtWarning, [mbOK], 0);

  r2 := WideCompareStr (Trim (s1), '');
  MessageDlg (IntToStr (r2), mtWarning, [mbOK], 0);
end;

Both r1 and r2 are zero which is as expected. And your second line is actually a syntax error (Trim can receive only one parameter).

Hemant
A: 

Maybe this test will shed some light on the matter:

procedure TForm1.Button1Click(Sender: TObject);
var
  s1: string;
  s2: string;
begin
  s1 := ' ';
  s2 := '';
  if WideCompareStr(s1, '') <> 0 then showmessage('s1 test');
  if WideCompareStr(s2, '') <> 0 then showmessage('s2 test');
end;
Mattl
A: 

In C++ Builder, there is an IsEmpty() function. However, I cannot see how to get that working from Delphi. I've just asked a question on that topic.

Roddy
+1  A: 

I'm not that deep into widestrings, and if you compare purely on widestring level (not using pwidechar typecasts) it shouldn't be the problem, BUT

I know that with ansistrings that there are two states of empty. one is that the pointer value of the string is NIL, the other one is that it is assigned to some "XXXX_EMPTYCHAR" #0#0 constant. (the exact name of the symbol might vary between Delphi and FPC). If some of the routines compare using pointers, weird results may happen. Strangely enough above seems to be all RTL, which usually carefully checks this.

Marco van de Voort