-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move NumberUtils to an ABI stable, statically linked library
Signed-off-by: L. E. Segovia <13498015+amyspark@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
193 additions
and
179 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
// Copyright Contributors to the OpenColorIO Project. | ||
|
||
#include "NumberUtils.h" | ||
|
||
#ifdef USE_FAST_FLOAT | ||
#include <fast_float/fast_float.h> | ||
#endif | ||
|
||
#include <cstdlib> | ||
#include <locale> | ||
|
||
namespace NumberUtils | ||
{ | ||
|
||
struct Locale | ||
{ | ||
#ifdef _WIN32 | ||
Locale() : local(_create_locale(LC_ALL, "C")) | ||
{ | ||
} | ||
~Locale() | ||
{ | ||
_free_locale(local); | ||
} | ||
_locale_t local; | ||
#else | ||
Locale() : local(newlocale(LC_ALL_MASK, "C", NULL)) | ||
{ | ||
} | ||
~Locale() | ||
{ | ||
freelocale(local); | ||
} | ||
locale_t local; | ||
#endif | ||
}; | ||
|
||
static const Locale loc; | ||
|
||
#ifdef USE_FAST_FLOAT | ||
from_chars_result from_chars(const char *first, const char *last, float &value) noexcept | ||
{ | ||
// By design that's not supported by from_chars() so handle it here. | ||
if (first && *first == '+') | ||
++first; | ||
|
||
const auto ret = fast_float::from_chars(first, last, value); | ||
return {ret.ptr, ret.ec}; | ||
} | ||
|
||
from_chars_result from_chars(const char *first, const char *last, double &value) noexcept | ||
{ | ||
// By design that's not supported by from_chars() so handle it here. | ||
if (first && *first == '+') | ||
++first; | ||
|
||
const auto ret = fast_float::from_chars(first, last, value); | ||
return {ret.ptr, ret.ec}; | ||
} | ||
|
||
#else | ||
from_chars_result from_chars(const char *first, const char *last, double &value) noexcept | ||
{ | ||
errno = 0; | ||
if (!first || !last || first == last) | ||
{ | ||
return {first, std::errc::invalid_argument}; | ||
} | ||
|
||
char *endptr = nullptr; | ||
|
||
double | ||
#ifdef _WIN32 | ||
tempval = _strtod_l(first, &endptr, loc.local); | ||
#else | ||
tempval = ::strtod_l(first, &endptr, loc.local); | ||
#endif | ||
|
||
if (errno != 0) | ||
{ | ||
return {first + (endptr - first), std::errc::result_out_of_range}; | ||
} | ||
else if (endptr == first) | ||
{ | ||
return {first, std::errc::invalid_argument}; | ||
} | ||
else if (endptr <= last) | ||
{ | ||
value = tempval; | ||
return {first + (endptr - first), {}}; | ||
} | ||
else | ||
{ | ||
return {first, std::errc::argument_out_of_domain}; | ||
} | ||
} | ||
|
||
from_chars_result from_chars(const char *first, const char *last, float &value) noexcept | ||
{ | ||
errno = 0; | ||
if (!first || !last || first == last) | ||
{ | ||
return {first, std::errc::invalid_argument}; | ||
} | ||
|
||
char *endptr = nullptr; | ||
|
||
float | ||
#ifdef _WIN32 | ||
tempval = _strtof_l(first, &endptr, loc.local); | ||
#elif __APPLE__ | ||
// On OSX, strtod_l is for some reason drastically faster than strtof_l. | ||
tempval = static_cast<float>(::strtod_l(first, &endptr, loc.local)); | ||
#else | ||
tempval = ::strtof_l(first, &endptr, loc.local); | ||
#endif | ||
|
||
if (errno != 0) | ||
{ | ||
return {first + (endptr - first), std::errc::result_out_of_range}; | ||
} | ||
else if (endptr == first) | ||
{ | ||
return {first, std::errc::invalid_argument}; | ||
} | ||
else if (endptr <= last) | ||
{ | ||
value = tempval; | ||
return {first + (endptr - first), {}}; | ||
} | ||
else | ||
{ | ||
return {first, std::errc::argument_out_of_domain}; | ||
} | ||
} | ||
#endif // USE_FAST_FLOAT | ||
|
||
from_chars_result from_chars(const char *first, const char *last, long int &value) noexcept | ||
{ | ||
errno = 0; | ||
if (!first || !last || first == last) | ||
{ | ||
return {first, std::errc::invalid_argument}; | ||
} | ||
|
||
char *endptr = nullptr; | ||
|
||
long int | ||
#ifdef _WIN32 | ||
tempval = _strtol_l(first, &endptr, 0, loc.local); | ||
#else | ||
tempval = ::strtol_l(first, &endptr, 0, loc.local); | ||
#endif | ||
|
||
if (errno != 0) | ||
{ | ||
return {first + (endptr - first), std::errc::result_out_of_range}; | ||
} | ||
else if (endptr == first) | ||
{ | ||
return {first, std::errc::invalid_argument}; | ||
} | ||
else if (endptr <= last) | ||
{ | ||
value = tempval; | ||
return {first + (endptr - first), {}}; | ||
} | ||
else | ||
{ | ||
return {first, std::errc::argument_out_of_domain}; | ||
} | ||
} | ||
} // namespace NumberUtils |
Oops, something went wrong.