Skip to content

Commit

Permalink
[共通] fix #1173
Browse files Browse the repository at this point in the history
  • Loading branch information
Reputeless authored Dec 27, 2023
1 parent 2b594cd commit ee9371a
Show file tree
Hide file tree
Showing 24 changed files with 4,000 additions and 1,883 deletions.
2 changes: 0 additions & 2 deletions Linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -867,8 +867,6 @@ set(SIV3D_INTERNAL_SOURCES
../Siv3D/src/ThirdParty/double-conversion/double-to-string.cc
../Siv3D/src/ThirdParty/double-conversion/fast-dtoa.cc
../Siv3D/src/ThirdParty/double-conversion/fixed-dtoa.cc
../Siv3D/src/ThirdParty/double-conversion/string-to-double.cc
../Siv3D/src/ThirdParty/double-conversion/strtod.cc

../Siv3D/src/ThirdParty/easyexif/exif.cpp

Expand Down
2 changes: 1 addition & 1 deletion Siv3D/src/Siv3D/FormatFloat/SivFormatFloat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# include <Siv3D/String.hpp>
# include <Siv3D/FormatData.hpp>
# include <Siv3D/PredefinedYesNo.hpp>
# include <ThirdParty/double-conversion/double-conversion.h>
# include <ThirdParty/double-conversion/double-to-string.h>

namespace s3d
{
Expand Down
23 changes: 23 additions & 0 deletions Siv3D/src/Siv3D/LicenseManager/LicenseList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,29 @@ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.)-"
},

{
UR"-(fast_float)-",

UR"-(Copyright (c) 2021 The fast_float authors)-",

UR"-(Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.)-"
},

{
UR"-(fmt)-",

Expand Down
51 changes: 24 additions & 27 deletions Siv3D/src/Siv3D/ParseFloat/SivParseFloat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,52 @@
//
//-----------------------------------------------

# include <ThirdParty/fast_float/fast_float.h>
# include <Siv3D/ParseFloat.hpp>
# include <Siv3D/Error.hpp>
# include <Siv3D/Char.hpp>
# include <Siv3D/FormatLiteral.hpp>
# include <ThirdParty/double-conversion/double-conversion.h>

namespace s3d
{
namespace detail
{
inline static constexpr double sNaN = std::numeric_limits<double>::signaling_NaN();

static double ParseDouble(const StringView s)
[[noreturn]]
static void ThrowParseError(const StringView s)
{
using namespace double_conversion;
throw ParseError{ U"ParseFloat(\"{}\") failed"_fmt(s) };
}

const int flags =
StringToDoubleConverter::ALLOW_LEADING_SPACES
| StringToDoubleConverter::ALLOW_TRAILING_SPACES
| StringToDoubleConverter::ALLOW_SPACES_AFTER_SIGN
| StringToDoubleConverter::ALLOW_CASE_INSENSIBILITY;
StringToDoubleConverter conv(flags, 0.0, sNaN, "inf", "nan");
static double ParseDouble(StringView s)
{
while (IsSpace(s.front()))
{
s.remove_prefix(1);
}

int unused;
const double result = conv.Siv3D_StringToIeee(s.data(), static_cast<int>(s.length()), true, &unused);
double result;
auto [p, ec] = fast_float::from_chars(s.data(), (s.data() + s.size()), result);

if (std::memcmp(&result, &sNaN, sizeof(double)) == 0)
if (ec != std::errc{})
{
throw ParseError(U"ParseFloat(\"{}\") failed"_fmt(s));
ThrowParseError(s);
}

return result;
}

template <class FloatType>
static Optional<FloatType> ParseFloatingPointOpt(const StringView s) noexcept
static Optional<FloatType> ParseFloatingPointOpt(StringView s) noexcept
{
using namespace double_conversion;

const int flags =
StringToDoubleConverter::ALLOW_LEADING_SPACES
| StringToDoubleConverter::ALLOW_TRAILING_SPACES
| StringToDoubleConverter::ALLOW_SPACES_AFTER_SIGN
| StringToDoubleConverter::ALLOW_CASE_INSENSIBILITY;
StringToDoubleConverter conv(flags, 0.0, sNaN, "inf", "nan");
while (IsSpace(s.front()))
{
s.remove_prefix(1);
}

int unused;
const double result = conv.Siv3D_StringToIeee(s.data(), static_cast<int>(s.length()), true, &unused);
double result;
auto [p, ec] = fast_float::from_chars(s.data(), (s.data() + s.size()), result);

if (std::memcmp(&result, &sNaN, sizeof(double)) == 0)
if (ec != std::errc{})
{
return none;
}
Expand Down
6 changes: 3 additions & 3 deletions Siv3D/src/ThirdParty/double-conversion/bignum-dtoa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator,

// Let v = numerator / denominator < 10.
// Then we generate 'count' digits of d = x.xxxxx... (without the decimal point)
// from left to right. Once 'count' digits have been produced we decide wether
// from left to right. Once 'count' digits have been produced we decide whether
// to round up or down. Remainders of exactly .5 round upwards. Numbers such
// as 9.999999 propagate a carry all the way, and change the
// exponent (decimal_point), when rounding upwards.
Expand Down Expand Up @@ -370,7 +370,7 @@ static void BignumToFixed(int requested_digits, int* decimal_point,
// Returns an estimation of k such that 10^(k-1) <= v < 10^k where
// v = f * 2^exponent and 2^52 <= f < 2^53.
// v is hence a normalized double with the given exponent. The output is an
// approximation for the exponent of the decimal approimation .digits * 10^k.
// approximation for the exponent of the decimal approximation .digits * 10^k.
//
// The result might undershoot by 1 in which case 10^k <= v < 10^k+1.
// Note: this property holds for v's upper boundary m+ too.
Expand Down Expand Up @@ -548,7 +548,7 @@ static void InitialScaledStartValuesNegativeExponentNegativePower(
//
// Let ep == estimated_power, then the returned values will satisfy:
// v / 10^ep = numerator / denominator.
// v's boundarys m- and m+:
// v's boundaries m- and m+:
// m- / 10^ep == v / 10^ep - delta_minus / denominator
// m+ / 10^ep == v / 10^ep + delta_plus / denominator
// Or in other words:
Expand Down
7 changes: 4 additions & 3 deletions Siv3D/src/ThirdParty/double-conversion/bignum.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ void Bignum::AssignHexString(Vector<const char> value) {
DOUBLE_CONVERSION_ASSERT(sizeof(uint64_t) * 8 >= kBigitSize + 4); // TODO: static_assert
// Accumulates converted hex digits until at least kBigitSize bits.
// Works with non-factor-of-four kBigitSizes.
uint64_t tmp = 0; // Accumulates converted hex digits until at least
uint64_t tmp = 0;
for (int cnt = 0; !value.is_empty(); value.pop_back()) {
tmp |= (HexCharValue(value.last()) << cnt);
if ((cnt += 4) >= kBigitSize) {
Expand All @@ -146,7 +146,8 @@ void Bignum::AssignHexString(Vector<const char> value) {
}
}
if (tmp > 0) {
RawBigit(used_bigits_++) = static_cast<Bignum::Chunk>(tmp);
DOUBLE_CONVERSION_ASSERT(tmp <= kBigitMask);
RawBigit(used_bigits_++) = static_cast<Bignum::Chunk>(tmp & kBigitMask);
}
Clamp();
}
Expand Down Expand Up @@ -203,7 +204,7 @@ void Bignum::AddBignum(const Bignum& other) {
carry = sum >> kBigitSize;
++bigit_pos;
}
used_bigits_ = static_cast<int16_t>((std::max)(bigit_pos, static_cast<int>(used_bigits_)));
used_bigits_ = static_cast<int16_t>(std::max(bigit_pos, static_cast<int>(used_bigits_)));
DOUBLE_CONVERSION_ASSERT(IsClamped());
}

Expand Down
34 changes: 0 additions & 34 deletions Siv3D/src/ThirdParty/double-conversion/double-conversion.h

This file was deleted.

43 changes: 31 additions & 12 deletions Siv3D/src/ThirdParty/double-conversion/double-to-string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ bool DoubleToStringConverter::HandleSpecialValues(
StringBuilder* result_builder) const {
Double double_inspect(value);
if (double_inspect.IsInfinite()) {
if (infinity_symbol_ == NULL) return false;
if (infinity_symbol_ == DOUBLE_CONVERSION_NULLPTR) return false;
if (value < 0) {
result_builder->AddCharacter('-');
}
result_builder->AddString(infinity_symbol_);
return true;
}
if (double_inspect.IsNan()) {
if (nan_symbol_ == NULL) return false;
if (nan_symbol_ == DOUBLE_CONVERSION_NULLPTR) return false;
result_builder->AddString(nan_symbol_);
return true;
}
Expand All @@ -79,7 +79,14 @@ void DoubleToStringConverter::CreateExponentialRepresentation(
StringBuilder* result_builder) const {
DOUBLE_CONVERSION_ASSERT(length != 0);
result_builder->AddCharacter(decimal_digits[0]);
if (length != 1) {
if (length == 1) {
if ((flags_ & EMIT_TRAILING_DECIMAL_POINT_IN_EXPONENTIAL) != 0) {
result_builder->AddCharacter('.');
if ((flags_ & EMIT_TRAILING_ZERO_AFTER_POINT_IN_EXPONENTIAL) != 0) {
result_builder->AddCharacter('0');
}
}
} else {
result_builder->AddCharacter('.');
result_builder->AddSubstring(&decimal_digits[1], length-1);
}
Expand All @@ -92,19 +99,19 @@ void DoubleToStringConverter::CreateExponentialRepresentation(
result_builder->AddCharacter('+');
}
}
if (exponent == 0) {
result_builder->AddCharacter('0');
return;
}
DOUBLE_CONVERSION_ASSERT(exponent < 1e4);
// Changing this constant requires updating the comment of DoubleToStringConverter constructor
const int kMaxExponentLength = 5;
char buffer[kMaxExponentLength + 1];
buffer[kMaxExponentLength] = '\0';
int first_char_pos = kMaxExponentLength;
while (exponent > 0) {
buffer[--first_char_pos] = '0' + (exponent % 10);
exponent /= 10;
if (exponent == 0) {
buffer[--first_char_pos] = '0';
} else {
while (exponent > 0) {
buffer[--first_char_pos] = '0' + (exponent % 10);
exponent /= 10;
}
}
// Add prefix '0' to make exponent width >= min(min_exponent_with_, kMaxExponentLength)
// For example: convert 1e+9 -> 1e+09, if min_exponent_with_ is set to 2
Expand Down Expand Up @@ -327,9 +334,21 @@ bool DoubleToStringConverter::ToPrecision(double value,
int exponent = decimal_point - 1;

int extra_zero = ((flags_ & EMIT_TRAILING_ZERO_AFTER_POINT) != 0) ? 1 : 0;
if ((-decimal_point + 1 > max_leading_padding_zeroes_in_precision_mode_) ||
bool as_exponential =
(-decimal_point + 1 > max_leading_padding_zeroes_in_precision_mode_) ||
(decimal_point - precision + extra_zero >
max_trailing_padding_zeroes_in_precision_mode_)) {
max_trailing_padding_zeroes_in_precision_mode_);
if ((flags_ & NO_TRAILING_ZERO) != 0) {
// Truncate trailing zeros that occur after the decimal point (if exponential,
// that is everything after the first digit).
int stop = as_exponential ? 1 : std::max(1, decimal_point);
while (decimal_rep_length > stop && decimal_rep[decimal_rep_length - 1] == '0') {
--decimal_rep_length;
}
// Clamp precision to avoid the code below re-adding the zeros.
precision = std::min(precision, decimal_rep_length);
}
if (as_exponential) {
// Fill buffer to contain 'precision' digits.
// Usually the buffer is already at the correct length, but 'DoubleToAscii'
// is allowed to return less characters.
Expand Down
Loading

0 comments on commit ee9371a

Please sign in to comment.