[fpc-devel] Re: fpc-devel Digest, Vol 40, Issue 16
L
L at z505.com
Wed Oct 17 06:28:09 CEST 2007
I wrote:
> In KOL, Kladov also returns a zero with his Str2Int function if string is bad.
> You may be thinking.. but what if the string really is a zero and we want to
> know that it is?
> Well then we can do this:
> if (s = '0') or (StrToInt(s) <> 0) then writeln('S is an integer');
> if StrToInt('SomeString') = 0 then writeln('s is not an integer');
>
Rather..
var
i: integer;
begin
i:= StrToInt(s);
if (s = '0') or (i <> 0) then
writeln('S is an integer, and i is now: ', i)
else
writeln('S is not an integer: ', s);
end;
Alternatively:
function IsInteger(s: string; i: integer);
begin
result:= false;
if (s = '0') or (i <> 0) then result:= true;
end;
var
i: integer;
begin
i:= StrToInt(s);
if IsInteger(s, i) then
writeln('S was an integer')
else
writeln('S was not an integer: ', s);
end;
That's pseudo code.. I haven't of course checked it.
People can use Tinyutils.pp (IntUtils.pp) way of doing it as I show above.. or
they can use sysutils and use try/except style coding. They just choose which to
put in their uses clause.
Either tinyutils contains all the functions in one unit, such as
ExtractFilePath, StrToInt,WrapText, CompareText.. or we separate
it into several units such as:
IntUtils.pp (StrToInt, IntToStr, and others)
TextUtils/TxtUtils.pp (CompareText, WrapText, and others)
fsutils.pp (ExtractFilePath and friends)
Or.. someone else may have better ideas for naming.
More information about the fpc-devel
mailing list