Core: CParser::ReadDoubleNoE (to ignore E part of double)

This commit is contained in:
Mirek Fidler 2023-06-14 16:49:08 +02:00
parent 09aff092f3
commit 243af92ed7
3 changed files with 22 additions and 2 deletions

View file

@ -510,7 +510,7 @@ String FormatF(double x, int precision, dword flags)
}
template <typename CHAR, typename BYTE>
const CHAR *ScanDbl(double& result, const CHAR *s, int alt_dp)
const CHAR *ScanDbl(double& result, const CHAR *s, int alt_dp, bool E = true)
{
SkipSpaces__<CHAR, BYTE>(s);
@ -571,7 +571,7 @@ const CHAR *ScanDbl(double& result, const CHAR *s, int alt_dp)
ReadNumber();
exp += int(s0 - s) + ignored;
}
if(*s == 'e' || *s == 'E') {
if(E && (*s == 'e' || *s == 'E')) {
dword e = 0;
bool overflow = false;
s++;
@ -676,4 +676,17 @@ double CParser::ReadDouble()
return n;
}
double CParser::ReadDoubleNoE()
{
LTIMING("ReadDouble No E");
double n;
const char *t = ScanDbl<char, byte>(n, term, '.', false);
if(!t) ThrowError("missing number");
if(!IsFin(n))
ThrowError("invalid number");
term = t;
DoSpaces();
return n;
}
};

View file

@ -64,6 +64,7 @@ public:
bool IsDouble() const { return IsInt(); }
bool IsDouble2() const;
double ReadDouble();
double ReadDoubleNoE();
bool IsString() const { return IsChar('\"'); };
String ReadOneString(bool chkend = true);
String ReadString(bool chkend = true);

View file

@ -312,6 +312,12 @@ As an exception to C lexical rules, ReadDouble also recognizes
form starting with decimal point, like `".21`".&]
[s3; &]
[s4; &]
[s5;:Upp`:`:CParser`:`:ReadDoubleNoE`(`): [@(0.0.255) double] [* ReadDoubleNoE]()&]
[s2;%% Speacial variant of ReadDouble that ignores exponential part
of number. E.g. CParser(`"1.2em`").ReadDoubleNoE() returns 1.2
(and does not throw error for invalid double).&]
[s3; &]
[s4; &]
[s5;:CParser`:`:IsString`(`)const: [@(0.0.255) bool]_[* IsString]()_[@(0.0.255) const]&]
[s2;%% Tests for C`-like string literal at the current position.
Same as [* IsChar](`'`\`"`');&]