Skip to content

Commit e4fe5f1

Browse files
committed
Introduced debug parameter in floating point to string conversion
1 parent 1ed9da8 commit e4fe5f1

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

stdlib/public/core/Runtime.swift.gyb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,19 +355,20 @@ struct _Buffer72 {
355355
@_silgen_name("swift_float${bits}ToString")
356356
func _float${bits}ToStringImpl(
357357
buffer: UnsafeMutablePointer<UTF8.CodeUnit>,
358-
_ bufferLength: UInt, _ value: Float${bits}
358+
_ bufferLength: UInt, _ value: Float${bits},
359+
_ debug: Bool
359360
) -> UInt
360361

361362
@warn_unused_result
362-
func _float${bits}ToString(value: Float${bits}) -> String {
363+
func _float${bits}ToString(value: Float${bits}, debug: Bool) -> String {
363364
_sanityCheck(sizeof(_Buffer32.self) == 32)
364365
_sanityCheck(sizeof(_Buffer72.self) == 72)
365366

366367
var buffer = _Buffer32()
367368
return withUnsafeMutablePointer(&buffer) {
368369
(bufferPtr) in
369370
let bufferUTF8Ptr = UnsafeMutablePointer<UTF8.CodeUnit>(bufferPtr)
370-
let actualLength = _float${bits}ToStringImpl(bufferUTF8Ptr, 32, value)
371+
let actualLength = _float${bits}ToStringImpl(bufferUTF8Ptr, 32, value, debug)
371372
return String._fromWellFormedCodeUnitSequence(
372373
UTF8.self,
373374
input: UnsafeBufferPointer(

stdlib/public/stubs/Stubs.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,15 @@ static int swift_snprintf_l(char *Str, size_t StrSize, locale_t Locale,
141141

142142
template <typename T>
143143
static uint64_t swift_floatingPointToString(char *Buffer, size_t BufferLength,
144-
T Value, const char *Format) {
144+
T Value, const char *Format, bool Debug) {
145145
if (BufferLength < 32)
146146
swift::crash("swift_floatingPointToString: insufficient buffer size");
147147

148-
const int Precision = std::numeric_limits<T>::max_digits10;
149-
148+
int Precision = std::numeric_limits<T>::digits10;
149+
if (Debug) {
150+
Precision = std::numeric_limits<T>::max_digits10;
151+
}
152+
150153
// Pass a null locale to use the C locale.
151154
int i = swift_snprintf_l(Buffer, BufferLength, /*locale=*/nullptr, Format,
152155
Precision, Value);
@@ -170,21 +173,21 @@ static uint64_t swift_floatingPointToString(char *Buffer, size_t BufferLength,
170173
}
171174

172175
extern "C" uint64_t swift_float32ToString(char *Buffer, size_t BufferLength,
173-
float Value) {
176+
float Value, bool Debug) {
174177
return swift_floatingPointToString<float>(Buffer, BufferLength, Value,
175-
"%0.*g");
178+
"%0.*g", Debug);
176179
}
177180

178181
extern "C" uint64_t swift_float64ToString(char *Buffer, size_t BufferLength,
179-
double Value) {
182+
double Value, bool Debug) {
180183
return swift_floatingPointToString<double>(Buffer, BufferLength, Value,
181-
"%0.*g");
184+
"%0.*g", Debug);
182185
}
183186

184187
extern "C" uint64_t swift_float80ToString(char *Buffer, size_t BufferLength,
185-
long double Value) {
188+
long double Value, bool Debug) {
186189
return swift_floatingPointToString<long double>(Buffer, BufferLength, Value,
187-
"%0.*Lg");
190+
"%0.*Lg", Debug);
188191
}
189192

190193
/// \param[out] LinePtr Replaced with the pointer to the malloc()-allocated

0 commit comments

Comments
 (0)