Sign in Join

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

Mailing lists / fpc-pascal / 2003-02

CompareStr

14 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

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

Iván Montes

Hi,

Maybe it's how it's supposed to be but if use :

compareStr( 'hello', 'hello-bye' )

I get 0, so they are equal.

If one string is bigger than the other they should be different, isn't it?

Otherways what can I use to check if two ansistrings are different?

TIA, Ivan

Sebastian Günther
to Iván Montes
Show quoted text 9 lines
Hi, Maybe it's how it's supposed to be but if use : compareStr( 'hello', 'hello-bye' ) I get 0, so they are equal. If one string is bigger than the other they should be different, isn't it?

I think so, too... but I don't have time to check this now, sorry, hopefully somebody else has.

Otherways what can I use to check if two ansistrings are different?

You can just use "=" for ShortString, AnsiString and WideString comparisons. "=" just doesn't work for PChar or PWideChar strings.

- Sebastian

James Mills
to Iván Montes: Maybe it's how it's supposed to be but if use : compareStr( 'hello', 'hello-bye' ) I get…
Iv?n Montes wrote 13 lines
Hi, Maybe it's how it's supposed to be but if use : compareStr( 'hello', 'hello-bye' ) I get 0, so they are equal. If one string is bigger than the other they should be different, isn't it? Otherways what can I use to check if two ansistrings are different? TIA, Ivan

I think this may be a bug in the compareStr function of sysUtils. I have tested the compareStr of sysUtils, and the compStr of the Strings unit. The Strings unit seems to do the test ok, and the result: 'hello' < 'hello-byte'.

cheers
James

program compare;
(*
uses
   sysUtils;
*)
uses
   Strings;
var
   (*
   str1: String;
   str2: String;
   *)
   str1: pChar;
   str2: pChar;
   r:    Integer;
begin
   str1 := 'hello';
   str2 := 'hello-bye';
   writeLn('str1 = ', str1);
   writeLn('str2 = ', str2);
   writeLn;
   r := strComp(str1, str2);
   if r < 0 then
      begin
         writeLn('str1 < str2');
      end
   else if r = 0 then
      begin
         writeLn('str1 = str2');
      end
   else
      begin
         writeLn('str1 > str2');
      end;
end.
_______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Michael Van Canneyt
to Iván Montes: Maybe it's how it's supposed to be but if use : compareStr( 'hello', 'hello-bye' ) I get…
Show quoted text 7 lines
Hi, Maybe it's how it's supposed to be but if use : compareStr( 'hello', 'hello-bye' ) I get 0, so they are equal.

In the fixbranch version I get -45 ?

(
version:
Free Pascal Compiler version 1.0.7 [2003/02/03] for i386 )

What version are you using ? I have fixed a bug report about this some time ago:

Revision 1.1.2.14 2002/11/28 20:13:10 michael
+ Fixed comparestr

Michael.

Iván Montes
to Michael Van Canneyt

----- Original Message -----

Earlier message 21 lines
From: "Michael Van Canneyt" To: Sent: Thursday, February 13, 2003 1:59 PM Subject: Re: [fpc-pascal]CompareStr What version are you using ? I have fixed a bug report about this some time ago: Revision 1.1.2.14 2002/11/28 20:13:10 michael + Fixed comparestr Michael. ----------- I'm using the lastest snapshot of the 1.0 branch, I've check the CVS for the 1.1 branch and it's fixed, don't know about 1.0 one
Michael Van Canneyt
to Iván Montes
Show quoted text 20 lines
----- Original Message ----- From: "Michael Van Canneyt" To: Sent: Thursday, February 13, 2003 1:59 PM Subject: Re: [fpc-pascal]CompareStr What version are you using ? I have fixed a bug report about this some time ago: Revision 1.1.2.14 2002/11/28 20:13:10 michael + Fixed comparestr Michael. ----------- I'm using the lastest snapshot of the 1.0 branch, I've check the CVS for the 1.1 branch and it's fixed, don't know about 1.0 one

That is strange, because I explicitly fixed it in the fixbranch, and the compiler I used has version 1.0.7.. I'll have to do some more checking, it seems.

Michael.

Michał Kamburelis

Hello !

You found a bug : CompareStr( 'hello', 'hello-bye' ) = 0. It's already fixed in FPC 1.1 and 1.0.7, so I guess you're using FPC 1.0.6 or older. You can switch to FPC 1.0.7 or you can fix this by editing RTL sources :

At least for FPC 1.0.6, it's in file rtl/objpas/sysstr.inc, lines 109 - 110 :
You should replace these two lines

   if Count1 > Count2 then result := ord(s1[Count1 + 1])
   else result := -ord(s2[Count2 + 1]);

with

   if Count1 > Count2 then result := ord(s1[Count + 1])
   else result := -ord(s2[Count + 1]);

(that is, the "Count1" and "Count2" in array indexes should be replaced with "Count").

That's all for THAT bug.
But while reading the code of CompareStr function we can see there's another bug and it still exists in FPC 1.1 : CompareStr('hello', 'hello'#0'bye') will return 0 ! We can change the code in many ways to remove this disadvantage or we can state somewhere clearly that CompareStr doesn't work for strings that include #0 character inside (I myself would prefer the first option). This problem goes to the developers of FPC RTL.

Sorry for my poor english grammar.
Regards, Michalis Kamburelis

--------------r-e-k-l-a-m-a-----------------

Tanie bilety lotnicze!
http://samoloty.onet.pl

Jilani Khaldi
to Michał Kamburelis
Show quoted text 12 lines
At least for FPC 1.0.6, it's in file rtl/objpas/sysstr.inc, lines 109 - 110 : You should replace these two lines if Count1 > Count2 then result := ord(s1[Count1 + 1]) else result := -ord(s2[Count2 + 1]); with if Count1 > Count2 then result := ord(s1[Count + 1]) else result := -ord(s2[Count + 1]); (that is, the "Count1" and "Count2" in array indexes should be replaced with "Count"). That's all for THAT bug.

I have done this and run make (/usr/local/..rtl/ under Linux) but:

CompareStr('hello', 'hello wrld') still gives 0.

What am I missing here?

Thanks.

Jilani Khaldi http://www.jilani.net
Unknown author
to Jilani Khaldi
Jilani Khaldi wrote 17 lines
>At least for FPC 1.0.6, it's in file rtl/objpas/sysstr.inc, lines 109 - 110 >: >You should replace these two lines > if Count1 > Count2 then result := ord(s1[Count1 + 1]) > else result := -ord(s2[Count2 + 1]); >with > if Count1 > Count2 then result := ord(s1[Count + 1]) > else result := -ord(s2[Count + 1]); >(that is, the "Count1" and "Count2" in array indexes should be replaced >with "Count"). > >That's all for THAT bug. I have done this and run make (/usr/local/..rtl/ under Linux) but: CompareStr('hello', 'hello wrld') still gives 0. What am I missing here?

The official 1.0.6 had this bug. It's fixed now in the latest snapshots, as confirmed by the original poster.

Michael.

Jilani Khaldi
to Unknown author
Show quoted text 20 lines
> >At least for FPC 1.0.6, it's in file rtl/objpas/sysstr.inc, lines 109 - 110 > >: > >You should replace these two lines > > if Count1 > Count2 then result := ord(s1[Count1 + 1]) > > else result := -ord(s2[Count2 + 1]); > >with > > if Count1 > Count2 then result := ord(s1[Count + 1]) > > else result := -ord(s2[Count + 1]); > >(that is, the "Count1" and "Count2" in array indexes should be replaced > >with "Count"). > > > >That's all for THAT bug. > I have done this and run make (/usr/local/..rtl/ under Linux) but: > > CompareStr('hello', 'hello wrld') still gives 0. > > What am I missing here? The official 1.0.6 had this bug. It's fixed now in the latest snapshots, as confirmed by the original poster.

Well. But, what I want to do is just to correct this bug and stick with 1.0.6. Why do I have to update the hole compiler for just one bug when I could fix it with little editing?

Thank you.

ps.
If sombody has fixed this bug in fpc-1.0.6, please tells me how he/she did it.

Jilani Khaldi http://www.jilani.net
Unknown author
to Jilani Khaldi
Jilani Khaldi wrote 22 lines
>> >At least for FPC 1.0.6, it's in file rtl/objpas/sysstr.inc, lines 109 - 110 >> >: >> >You should replace these two lines >> > if Count1 > Count2 then result := ord(s1[Count1 + 1]) >> > else result := -ord(s2[Count2 + 1]); >> >with >> > if Count1 > Count2 then result := ord(s1[Count + 1]) >> > else result := -ord(s2[Count + 1]); >> >(that is, the "Count1" and "Count2" in array indexes should be replaced >> >with "Count"). >> > >> >That's all for THAT bug. >> I have done this and run make (/usr/local/..rtl/ under Linux) but: >> >> CompareStr('hello', 'hello wrld') still gives 0. >> >> What am I missing here? > >The official 1.0.6 had this bug. It's fixed now in the latest snapshots, >as confirmed by the original poster. Well. But, what I want to do is just to correct this bug and stick with 1.0.6. Why do I have to update the hole compiler for just one bug when I could fix it with little editing?

You can simply download the latest RTL sources. from ftp://ftp.freepascal.org/pub/fpc/source
and compile it. You don't need the whole compiler for it.

Michael.

Jilani Khaldi
to Unknown author
You can simply download the latest RTL sources. from ftp://ftp.freepascal.org/pub/fpc/source and compile it. You don't need the whole compiler for it.

Done, but:
CompareStr('hello', 'hello world') still gives 0.

Strange!!!

Jilani Khaldi http://www.jilani.net
Jilani Khaldi
to Jilani Khaldi
Show quoted text 8 lines
>You can simply download the latest RTL sources. from >ftp://ftp.freepascal.org/pub/fpc/source >and compile it. You don't need the whole compiler for it. Done, but: CompareStr('hello', 'hello world') still gives 0. Strange!!!

Sorry. That's Ok now.

Jilani Khaldi http://www.jilani.net