Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConvertStringToDouble/FallbackStringToDouble thinks any string is OK #5

Open
benibela opened this issue Aug 11, 2021 · 1 comment
Open

Comments

@benibela
Copy link

benibela commented Aug 11, 2021

It always seems to set OK to true:

var
  ok: TPasDblStrUtilsBoolean;
WriteLn(ConvertStringToDouble('xyasssdfsdf', rmNearest, @ok));
writeln(ok);
WriteLn(FallbackStringToDouble('xyasssdfsdf', rmNearest, @ok));
writeln(ok);
@benibela
Copy link
Author

Or you could have the functions take an already parsed and syntactically validated number to convert:

record
  mantissaSigned: boolean;
  mantissaIntegerStart: pchar;     //assumed to match [0-9]* for base 10
  mantissaIntegerLength: SizeUInt; 
  mantissaFractionStart: pchar     //assumed to match [0-9]* for base 10
  mantissaFractionLength: SizeUInt;
  exponentSigned: boolean;
  exponentStart: pchar;            //assumed to match [0-9]* for base 10
  exponentLength: SizeUInt;
end;

Then the functions do not need to parse it again.

Also I just learned that there is a faster way to check if an 8-character string only consists of decimal digits. That might make it faster:

const
   selectFirstHalfByte8 = UInt64($F0F0F0F0F0F0F0F0);
   decimalZeros8        = UInt64($3030303030303030);
   overflowMaxDigit8    = UInt64($0606060606060606);
var temp8: UInt64;
begin
   temp8 := PUInt64(stringof8characters)^;
   if (temp8 and selectFirstHalfByte8) <> decimalZeros8 then exit(false);
   temp8 := temp8 - decimalZeros8;
   if ((temp8 + overflowMaxDigit8) and selectFirstHalfByte8) <> 0 then exit(false);
   //now it is known to have all decimals

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant