• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The V8 Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // This file is V8 specific. It's not rolled from the upstream project.
6 
7 #include "json_platform.h"
8 
9 #include <cmath>
10 #include "../../../src/base/vector.h"
11 #include "../../../src/numbers/conversions.h"
12 
13 namespace v8_crdtp {
14 namespace json {
15 namespace platform {
16 // Parses |str| into |result|. Returns false iff there are
17 // leftover characters or parsing errors.
StrToD(const char * str,double * result)18 bool StrToD(const char* str, double* result) {
19   *result =
20       v8::internal::StringToDouble(str, v8::internal::NO_CONVERSION_FLAGS);
21   return std::isfinite(*result);
22 }
23 
24 // Prints |value| in a format suitable for JSON.
DToStr(double value)25 std::string DToStr(double value) {
26   v8::base::ScopedVector<char> buffer(
27       v8::internal::kDoubleToCStringMinBufferSize);
28   const char* str = v8::internal::DoubleToCString(value, buffer);
29   return (str == nullptr) ? "" : std::string(str);
30 }
31 }  // namespace platform
32 }  // namespace json
33 }  // namespace v8_crdtp
34