[fpc-pascal]ExtractFileDir ?
Anton Tichawa
anton.tichawa at chello.at
Thu Feb 13 03:32:46 CET 2003
Hi!
Let's have a look for filename "C:A":
> function ExtractFileDir(const FileName: string): string;
> var i,j: longint;
> begin
> if FileName[2]=':' then j:=3
> else j:=1;
Now j = 3, pointing to the letter "A". OK.
> I := Length(FileName);
> while (I > 0) and not (FileName[I] in ['/', '\', ':']) do Dec(I);
Error: I is supposed to go from the string end back to the string start, but
never below j. But exactly this happens here. I is decremented below j, from
3 to 2, so shit might happen ..
> if (I > j) and (FileName[I] in ['\', '/']) and
> not (FileName[I - 1] in ['/', '\', ':']) then Dec(I);
skipped
> Result := Copy(FileName, j, I-j+1);
Result = Copy(Filename, 3, 0).
Is this an error or wrong documentation? The line result should be '', but it
actually is 'a:c'.
So there are two errors:
1. I decremented below j, but the programmer had in mind that i was above j.
the while-statement should read 'while (I > J) ..'
2. The Copy returns the full string for length 0 -
Note: I don't like the while structure for combined conditions. I prefer a
repeat-until with several breaks. That usually makes things more clear. I
know that this is not considered 'well-structured' programming, but the world
we live in is not 'well-structured'. Should I post a new, tested "repeat -
break - break - until" version of this function?
Anton.
> _______________________________________________
> fpc-pascal maillist - fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
--
----------
"Adas Methode war, wie sich zeigen wird, Tagträume in offenbar korrekte
Berechnungen einzuweben."
Doris Langley Moore: Ada, Countess of Lovelace (London 1977).
----------
Anton Tichawa
Volkertstrasse 19 / 20
A-1020 Wien
mobil: +43 664 52 07 907
email: anton.tichawa at chello.at
----------
More information about the fpc-pascal
mailing list