Skip to content

Commit

Permalink
Conversion: StrToDec, StrToDec64 - small optimizations, EConvertError…
Browse files Browse the repository at this point in the history
… is raised for negative binary values
  • Loading branch information
jackdp committed Aug 8, 2022
1 parent 9400c20 commit 21a6e77
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions Base/JPL.Conversion.pas
Original file line number Diff line number Diff line change
Expand Up @@ -953,23 +953,31 @@ function HexToUInt64(Hex: string): UInt64;

function StrToDec(const s: string): integer;
var
s2: string;
s2, sOne, sTwo: string;
begin
s2 := RemoveSpaces(s);
if Copy(s2, 1, 1) = '$' then Result := HexToInt(s2)
else if Copy(s2, 1, 1) = '%' then Result := BinToInt(s2)
else if UpperCase(Copy(s, 1, 2)) = '0X' then Result := HexToInt(Copy(s, 3, Length(s)))
s2 := UpperCase(RemoveSpaces(s));
sOne := Copy(s2, 1, 1);
sTwo := Copy(s2, 1, 2);

if sOne = '$' then Result := HexToInt(s2)
else if sTwo = '0X' then Result := HexToInt('$' + Copy(s2, 3, Length(s2)))
else if sTwo = '-%' then raise EConvertError.Create('StrToDec: Only positive binary numbers are supported!')
else if sOne = '%' then Result := BinToInt(s2)
else Result := StrToInt(s2);
end;

function StrToDec64(const s: string): Int64;
var
s2: string;
s2, sOne, sTwo: string;
begin
s2 := RemoveSpaces(s);
if Copy(s2, 1, 1) = '$' then Result := HexToInt(s2)
else if Copy(s2, 1, 1) = '%' then Result := BinToInt64(s2)
else if UpperCase(Copy(s, 1, 2)) = '0X' then Result := HexToInt64(Copy(s, 3, Length(s)))
s2 := UpperCase(RemoveSpaces(s));
sOne := Copy(s2, 1, 1);
sTwo := Copy(s2, 1, 2);

if sOne = '$' then Result := HexToInt64(s2)
else if sTwo = '0X' then Result := HexToInt64('$' + Copy(s2, 3, Length(s2)))
else if sTwo = '-%' then raise EConvertError.Create('StrToDec64: Only positive binary numbers are supported!')
else if sOne = '%' then Result := BinToInt64(s2)
else Result := StrToInt64(s2);
end;

Expand Down

0 comments on commit 21a6e77

Please sign in to comment.