Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2003-01

ansistring question

6 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mark Emerson

Consider the equation in the if statement:

var
  a, b : ansistring;
begin
  a := 'hello world..and a lot of other stuff to make this string long';
  b := a;
  if a=b then write ('idential');
end.

How is a=b evaluated? I hope/presume the text in the string is not taken into consideration at all, and that the system can immediately see that they are identical. Am I right?

Mark

Jonas Maebe
to Mark Emerson
Mark Emerson wrote 16 lines
Consider the equation in the if statement: var a, b : ansistring; begin a := 'hello world..and a lot of other stuff to make this string long'; b := a; if a=b then write ('idential'); end. How is a=b evaluated? I hope/presume the text in the string is not taken into consideration at all, and that the system can immediately see that they are identical. Am I right?

No. How would the system know that? You can have two completely unrelated ansistrings with the same content (just do a readln() of two ansistrings and type in twice the same text), so a full string compare is necessary. It would be possible to make the comparison a two step process, in which we first check whether the ansistrings point to the same ansistring structure and only if that fails, start doing an actual string compare, but I doubt this will gain you much a most real world programs.

Jonas

Peter Vreman
to Mark Emerson: Consider the equation in the if statement: var a, b : ansistring; begin a := 'hello…
Show quoted text 14 lines
Consider the equation in the if statement: var a, b : ansistring; begin a := 'hello world..and a lot of other stuff to make this string long'; b := a; if a=b then write ('idential'); end. How is a=b evaluated? I hope/presume the text in the string is not taken into consideration at all, and that the system can immediately see that they are identical. Am I right?

Yes

Jonas Maebe
to Peter Vreman
Peter Vreman wrote 6 lines
> How is a=b evaluated? I hope/presume the text in the string is not > taken into consideration at all, and that the system can immediately > see > that they are identical. Am I right? Yes

Ok, I should check the source before replying :) Peter is right.

Jonas

Mark Emerson
to Jonas Maebe: No. How would the system know that? You can have two completely unrelated ansistrings…
Jonas Maebe wrote 20 lines
On vrijdag, jan 3, 2003, at 16:28 Europe/Brussels, Mark Emerson wrote: > Consider the equation in the if statement: > > var > a, b : ansistring; > > begin > a := 'hello world..and a lot of other stuff to make this string > long'; > b := a; > if a=b then write ('idential'); > end. > > How is a=b evaluated? I hope/presume the text in the string is not > taken into consideration at all, and that the system can immediately > see that they are identical. Am I right? No. How would the system know that?

Well...you answered your own question, Jonas, when you say "it would be possible"....

It would be possible to make the comparison a two step process, in which we first check whether the ansistrings point to the same ansistring structure and only if that fails, start doing an actual string compare,
MY question is...does FPC in fact do this?  In my example, a and b DO
point to the same ansistring structure, because b := a.

Peter Vremen has answered my question:

Yes.

And I am delighted with Peter's answer, but concerned that the two of you gave opposite answers.

Mark

Peter Vreman
to Mark Emerson
Show quoted text 9 lines
MY question is...does FPC in fact do this? In my example, a and b DO point to the same ansistring structure, because b := a. Peter Vremen has answered my question: > Yes. And I am delighted with Peter's answer, but concerned that the two of you gave opposite answers.

To take away you concern, here is the source of the compare routine: Function fpc_AnsiStr_Compare(const S1,S2 : AnsiString): Longint;[Public,Alias : 'FPC_ANSISTR_COMPARE']; {$ifdef hascompilerproc} c
{

  Compares 2 AnsiStrings;
  The result is
   <0 if S1<S2
   0 if S1=S2
0 if S1>S2
}
Var
  MaxI,Temp : Longint;
begin
  if pointer(S1)=pointer(S2) then
   begin
     fpc_AnsiStr_Compare:=0;
     exit;
   end;
  Maxi:=Length(S1);
  temp:=Length(S2);
  If MaxI>Temp then
   MaxI:=Temp;
  Temp:=CompareByte(S1[1],S2[1],MaxI);
  if temp=0 then
   temp:=Length(S1)-Length(S2);
  fpc_AnsiStr_Compare:=Temp;
end;