So this is what i've come up with now (i edited the answer bc it was kinda bad before):
unit BetterStrToInt;
interface
uses
System.SysUtils, System.SysConst;
type
EStrToIntOverflowTooBig = class(EConvertError);
EStrToIntOverflowTooSmall = class(EConvertError);
EStrToIntBadChar = class(EConvertError);
function BStrToInt(const s: string): integer;
implementation
function BStrToInt(const s: string): integer;
var
e: integer;
begin
Val(s, Result, e);
if e <> 0 then
if s = '-' then
raise EStrToIntBadChar.CreateFmt(SInvalidInteger, ['-'])
else
if CharInSet(s[e], ['0' .. '9']) then
if s.StartsWith('-') then
raise EStrToIntOverflowTooSmall.Create(SIntOverflow)
else
raise EStrToIntOverflowTooBig.Create(SIntOverflow)
else
raise EStrToIntBadChar.CreateFmt(SInvalidInteger, [s[e]]);
end;
end.
the if s = '-' then
is needed becaus when s = '-'
Val()
sets e
to 2
. Then s[e]
in if CharInSet(s[e], ['0' .. '9']) then
creates an Index Error. That behavior is unwanted (imo atleast).
maybe there is a better solution but i dont know any atm.