0

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

1
  • 5
    The bracketing of the trim function seems wrong... Commented Jun 19, 2009 at 11:48

7 Answers 7

7

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).

Sign up to request clarification or add additional context in comments.

Comments

2

WideCompareStr NEVER returns FALSE! ( it does not return a boolean value )

instead, it returns 0, if S1=S2.

if S1 <> S2, then:

it either returns a negative value if S1 < S2

or

a positive value if S1 > S2.

Also: In Delphi, Length(S) never calls strlen(). All string types (Shortstring, AnsiString, WideString and UnicodeString * ) contain a length field, which is a 32-bit integer (except in the Shortstring type it is only 8 bits).

So, in All strings except Shortstring a length() call will first check if the string variable is NIL, if it is, a ZERO is immediately returned, otherwise the content of the length field is returned. Absolutely NO memory scanning for a terminating NULL character.

  • = the UnicodeString type is available in Delphi 2009 or newer. Delphi 2009 and newer also redefines:

type

String = UnicodeString;  // in older Delphis: String = AnsiString;

Char   = Widechar;       // in older Delphis: Char = AnsiChar;

Comments

1

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

2 Comments

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.
So, as for you compare function call is less expensive than length? O_o
1

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.

Comments

0

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

Comments

0

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;

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.