pastojs: fixed removing leading zeroes from hex numbers

git-svn-id: trunk@39010 -
This commit is contained in:
Mattias Gaertner 2018-05-17 08:35:27 +00:00
parent 77dc0597e9
commit 0fcec04382
2 changed files with 29 additions and 10 deletions

View File

@ -6277,19 +6277,35 @@ function TPasToJSConverter.ConvertPrimitiveExpression(El: TPrimitiveExpr;
function DeleteLeadingZeroes(const s: string): string;
// Note: 01 is in JS octal, and in strict mode forbidden
// $00ff00 -> $ff00
// 00E001 -> 0E1
// 0.001 -> 0.001
// 0.00E1 -> 0.00E1
var
i: Integer;
begin
Result:=s;
i:=1;
while i<length(Result) do
begin
if (Result[i]='0') and (Result[i+1] in ['0'..'9'])
and ((i=1) or not (Result[i-1] in ['.','0'..'9'])) then
Delete(Result,i,1)
else
inc(i);
end;
if Result[1]='$' then
// hexadecimal -> can not be a float, 'E' is a hexdigit
while i<length(Result) do
begin
if (Result[i]='0') and (Result[i+1] in ['0'..'9','A'..'F','a'..'f'])
and ((i=1) or not (Result[i-1] in ['0'..'9','A'..'F','a'..'f'])) then
Delete(Result,i,1)
else
inc(i);
end
else
// decimal, can be a float, 'E' is a start of a new number
while i<length(Result) do
begin
if (Result[i]='0') and (Result[i+1] in ['0'..'9'])
and ((i=1) or not (Result[i-1] in ['.','0'..'9'])) then
Delete(Result,i,1)
else
inc(i);
end;
end;
Var
@ -6338,7 +6354,8 @@ begin
// number was rounded -> we lost precision
DoError(20161024230812,nInvalidNumber,sInvalidNumber,[El.Value],El);
L:=CreateLiteralNumber(El,Number);
S:=DeleteLeadingZeroes(copy(El.Value,2,length(El.Value)));
S:=DeleteLeadingZeroes(El.Value);
S:=copy(S,2,length(S));
case El.Value[1] of
'$': S:='0x'+S;
'&': if TargetProcessor=ProcessorECMAScript5 then

View File

@ -2154,6 +2154,7 @@ begin
Add(' i8: byte = 00;');
Add(' u8: nativeuint = $fffffffffffff;');
Add(' u9: nativeuint = $0000000000000;');
Add(' u10: nativeuint = $00ff00;');
Add('begin');
ConvertProgram;
CheckSource('TestVarBaseTypes',
@ -2175,7 +2176,8 @@ begin
'this.i7 =-0x10000000000000;',
'this.i8 = 0;',
'this.u8 = 0xfffffffffffff;',
'this.u9 = 0x0;'
'this.u9 = 0x0;',
'this.u10 = 0xff00;'
]),
'');
end;