Sha256: 0697f64ec9388710a9c5a6eff65a10b507b89dff28d17133c9f6a085ce3a824c

Contents?: true

Size: 1.49 KB

Versions: 22

Compression:

Stored size: 1.49 KB

Contents

// 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);
}

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
grpc-1.67.0 third_party/upb/upb/lex/round_trip.c
grpc-1.67.0.pre1 third_party/upb/upb/lex/round_trip.c
grpc-1.66.0 third_party/upb/upb/lex/round_trip.c
grpc-1.66.0.pre5 third_party/upb/upb/lex/round_trip.c
grpc-1.66.0.pre3 third_party/upb/upb/lex/round_trip.c
grpc-1.60.2 third_party/upb/upb/lex/round_trip.c
grpc-1.61.3 third_party/upb/upb/lex/round_trip.c
grpc-1.62.3 third_party/upb/upb/lex/round_trip.c
grpc-1.63.2 third_party/upb/upb/lex/round_trip.c
grpc-1.64.3 third_party/upb/upb/lex/round_trip.c
grpc-1.65.2 third_party/upb/upb/lex/round_trip.c
grpc-1.65.1 third_party/upb/upb/lex/round_trip.c
grpc-1.65.0 third_party/upb/upb/lex/round_trip.c
grpc-1.65.0.pre2 third_party/upb/upb/lex/round_trip.c
grpc-1.65.0.pre1 third_party/upb/upb/lex/round_trip.c
grpc-1.63.0 third_party/upb/upb/lex/round_trip.c
grpc-1.62.0 third_party/upb/upb/lex/round_trip.c
grpc-1.62.0.pre1 third_party/upb/upb/lex/round_trip.c
grpc-1.61.0 third_party/upb/upb/lex/round_trip.c
grpc-1.61.0.pre2 third_party/upb/upb/lex/round_trip.c