-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed issues with reading/writing JSON floats
* Reading/writing floating point was accidentally locale-dependent, which caused errors in locales that don't use '.' as the decimal point. (Fixes #54) * Improved round-trip accuracy of Fleece->JSON->Fleece float/double conversions, by writing the optimal number of decimal places. To do this we brought in a small subcomponent of Swift. (Fixes #55)
- Loading branch information
Showing
11 changed files
with
2,694 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// | ||
// NumConversion.cc | ||
// | ||
// Copyright © 2019 Couchbase. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
#include "NumConversion.hh" | ||
#include "SwiftDtoa.h" | ||
#include <locale.h> | ||
#include <stdlib.h> | ||
#ifndef _MSC_VER | ||
#include <xlocale.h> | ||
#endif | ||
|
||
namespace fleece { | ||
|
||
double ParseDouble(const char *str) noexcept { | ||
// strtod is locale-aware, so in some locales it will not interpret '.' as a decimal point. | ||
// To work around that, use the C locale explicitly. | ||
#ifdef LC_C_LOCALE // Apple & BSD | ||
return strtod_l(str, nullptr, LC_C_LOCALE); | ||
#elif defined(_MSC_VER) // Windows | ||
static _locale_t kCLocale = _create_locale(LC_ALL, "C"); | ||
return _strtod_l(str, nullptr, kCLocale); | ||
#else // Linux | ||
static locale_t kCLocale = newlocale(LC_ALL_MASK, NULL, NULL); | ||
return strtod_l(str, nullptr, kCLocale); | ||
#endif | ||
} | ||
|
||
|
||
size_t WriteFloat(float n, char *dst, size_t capacity) { | ||
return swift_format_float(n, dst, capacity); | ||
} | ||
|
||
|
||
size_t WriteFloat(double n, char *dst, size_t capacity) { | ||
return swift_format_double(n, dst, capacity); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// NumConversion.hh | ||
// | ||
// Copyright © 2019 Couchbase. All rights reserved. | ||
// | ||
|
||
#pragma once | ||
#include "PlatformCompat.hh" | ||
#include <stddef.h> | ||
|
||
namespace fleece { | ||
|
||
/// Parse `str` as a floating-point number, reading as many digits as possible. | ||
/// (I.e. non-numeric characters after the digits are not treated as an error.) | ||
double ParseDouble(const char *str NONNULL) noexcept; | ||
|
||
/// Format a 64-bit-floating point number to a string. | ||
size_t WriteFloat(double n, char *dst, size_t capacity); | ||
|
||
/// Format a 32-bit floating-point number to a string. | ||
size_t WriteFloat(float n, char *dst, size_t capacity); | ||
|
||
/// Alternative syntax for formatting a 64-bit-floating point number to a string. | ||
static inline size_t WriteDouble(double n, char *dst, size_t c) {return WriteFloat(n, dst, c);} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.