blob: 13104341b149540585eeb02c5e8d7f955265e596 [file] [log] [blame] [edit]
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include "upb/lex/round_trip.h"
#include <float.h>
#include <stdlib.h>
// Must be last.
#include "upb/port/def.inc"
/* Miscellaneous utilities ****************************************************/
static void upb_FixLocale(char* p) {
/* printf() is dependent on locales; sadly there is no easy and portable way
* to avoid this. This little post-processing step will translate 1,2 -> 1.2
* since JSON needs the latter. Arguably a hack, but it is simple and the
* alternatives are far more complicated, platform-dependent, and/or larger
* in code size. */
for (; *p; p++) {
if (*p == ',') *p = '.';
}
}
void _upb_EncodeRoundTripDouble(double val, char* buf, size_t size) {
assert(size >= kUpb_RoundTripBufferSize);
snprintf(buf, size, "%.*g", DBL_DIG, val);
if (strtod(buf, NULL) != val) {
snprintf(buf, size, "%.*g", DBL_DIG + 2, val);
assert(strtod(buf, NULL) == val);
}
upb_FixLocale(buf);
}
void _upb_EncodeRoundTripFloat(float val, char* buf, size_t size) {
assert(size >= kUpb_RoundTripBufferSize);
snprintf(buf, size, "%.*g", FLT_DIG, val);
if (strtof(buf, NULL) != val) {
snprintf(buf, size, "%.*g", FLT_DIG + 3, val);
assert(strtof(buf, NULL) == val);
}
upb_FixLocale(buf);
}