diff options
author | Oleg Oshmyan <chortos@inbox.lv> | 2016-10-30 03:26:00 +0300 |
---|---|---|
committer | Grigori Goronzy <greg@chown.ath.cx> | 2016-11-21 11:05:34 +0100 |
commit | 858196e931e0076c61bc9c816b032a2f4991661c (patch) | |
tree | 0cfef0a3a9064391a6a2b572c15f2cf7aabccc64 | |
parent | 580ae2197df3559e25f0ea3cc98ef989d9cd139e (diff) | |
download | libass-858196e931e0076c61bc9c816b032a2f4991661c.tar.bz2 libass-858196e931e0076c61bc9c816b032a2f4991661c.tar.xz |
ass_strtod: skip leading zeros in mantissa
ass_strtod reads at most 18 leading digits of the mantissa.
This previously included zeros, even though they are not significant
digits, e. g. 0.000000000000000001e18 was converted to 0.0.
After this commit, leading zeros before and after the decimal point
will be skipped, so the above number will be correctly converted to 1.0.
-rw-r--r-- | libass/ass_strtod.c | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/libass/ass_strtod.c b/libass/ass_strtod.c index 9625491..1b1a5c2 100644 --- a/libass/ass_strtod.c +++ b/libass/ass_strtod.c @@ -95,6 +95,7 @@ ass_strtod( size_t mantSize; /* Number of digits in mantissa. */ size_t decPt; /* Number of mantissa digits BEFORE decimal * point. */ + size_t leadZeros; /* Number of leading zeros in mantissa. */ const char *pExp; /* Temporarily holds location of exponent * in string. */ @@ -122,6 +123,7 @@ ass_strtod( */ decPt = -1; + leadZeros = -1; for (mantSize = 0; ; mantSize += 1) { c = *p; @@ -130,6 +132,8 @@ ass_strtod( break; } decPt = mantSize; + } else if ((c != '0') && (leadZeros == (size_t) -1)) { + leadZeros = mantSize; } p += 1; } @@ -141,15 +145,21 @@ ass_strtod( * they can't affect the value anyway. */ + if (leadZeros == (size_t) -1) { + leadZeros = mantSize; + } pExp = p; - p -= mantSize; + p -= mantSize - leadZeros; if (decPt == (size_t) -1) { decPt = mantSize; } else { mantSize -= 1; /* One of the digits was the point. */ + if (decPt < leadZeros) { + leadZeros -= 1; + } } - if (mantSize > 18) { - mantSize = 18; + if (mantSize - leadZeros > 18) { + mantSize = leadZeros + 18; } if (decPt < mantSize) { fracExpSign = 1; @@ -164,6 +174,7 @@ ass_strtod( goto done; } else { int frac1, frac2; + mantSize -= leadZeros; frac1 = 0; for ( ; mantSize > 9; mantSize -= 1) { |