ConvertStringToDouble/FallbackStringToDouble thinks any string is OK #5

Open
opened 2021-08-11 12:32:13 +00:00 by benibela · 1 comment
benibela commented 2021-08-11 12:32:13 +00:00 (Migrated from github.com)

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);
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 commented 2021-08-12 12:21:06 +00:00 (Migrated from github.com)

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
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 in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
BeRo1985/pasdblstrutils#5
No description provided.