Skip to content

Commit 9cb2910

Browse files
committed
add in #define for old method
1 parent 82e5bf4 commit 9cb2910

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

include/flatbuffers/util.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,27 +157,43 @@ inline std::string NumToString<char>(char t) {
157157

158158
// definitions in util.cpp
159159
template <typename Float>
160-
std::string format_fixed_dragonbox(Float value);
160+
std::string FloatToStringImpl(Float value);
161161

162162
// Special versions for floats/doubles.
163163
template <typename T>
164164
std::string FloatToString(T t, [[maybe_unused]] int precision) {
165165
// clang-format off
166166

167167
#ifndef FLATBUFFERS_PREFER_PRINTF
168-
return format_fixed_dragonbox(t);
168+
#ifndef FLATBUFFERS_PREFER_OLD_FLOATTOSTRING
169+
return FloatToStringImpl(t);
170+
#else // FLATBUFFERS_PREFER_OLD_FLOATTOSTRING
171+
// to_string() prints different numbers of digits for floats depending on
172+
// platform and isn't available on Android, so we use stringstream
173+
std::stringstream ss;
174+
// Use std::fixed to suppress scientific notation.
175+
ss << std::fixed;
176+
// Default precision is 6, we want that to be higher for doubles.
177+
ss << std::setprecision(precision);
178+
ss << t;
179+
auto s = ss.str();
180+
#endif // FLATBUFFERS_PREFER_OLD_FLOATTOSTRING
169181
#else // FLATBUFFERS_PREFER_PRINTF
170182
auto v = static_cast<double>(t);
171183
auto s = NumToStringImplWrapper(v, "%0.*f", precision);
172-
// clang-format on
184+
#endif // FLATBUFFERS_PREFER_PRINTF
185+
186+
#if defined(FLATBUFFERS_PREFER_OLD_FLOATTOSTRING) || defined(FLATBUFFERS_PREFER_PRINTF)
173187
// Sadly, std::fixed turns "1" into "1.00000", so here we undo that.
174188
auto p = s.find_last_not_of('0');
175189
if (p != std::string::npos) {
176190
// Strip trailing zeroes. If it is a whole number, keep one zero.
177191
s.resize(p + (s[p] == '.' ? 2 : 1));
178192
}
193+
179194
return s;
180-
#endif // FLATBUFFERS_PREFER_PRINTF
195+
#endif
196+
// clang-format on
181197
}
182198

183199
template <>

src/util.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ std::string ConvertCase(const std::string& input, Case output_case,
492492
}
493493

494494
template <typename Float>
495-
std::string format_fixed_dragonbox(Float value) {
495+
std::string FloatToStringImpl(Float value) {
496496
// Handle NaN
497497
if (std::isnan(value)) {
498498
return "nan";
@@ -543,7 +543,7 @@ std::string format_fixed_dragonbox(Float value) {
543543
return out;
544544
}
545545

546-
template std::string format_fixed_dragonbox<float>(float value);
547-
template std::string format_fixed_dragonbox<double>(double value);
546+
template std::string FloatToStringImpl<float>(float value);
547+
template std::string FloatToStringImpl<double>(double value);
548548

549549
} // namespace flatbuffers

0 commit comments

Comments
 (0)