pytorch
1// Taken from
2// https://github.com/JuliaLang/julia/blob/v1.1.0/src/support/strtod.c
3#include <torch/csrc/jit/frontend/strtod.h>4
5#include <c10/macros/Macros.h>6#include <clocale>7#include <cstdlib>8
9#if defined(__APPLE__) || defined(__FreeBSD__)10#include <xlocale.h>11#endif12
13// The following code is derived from the Python function _PyOS_ascii_strtod
14// see http://hg.python.org/cpython/file/default/Python/pystrtod.c
15//
16// Copyright © 2001-2014 Python Software Foundation; All Rights Reserved
17//
18// The following modifications have been made:
19// - Leading spaces are ignored
20// - Parsing of hex floats is supported in the derived version
21// - Python functions for tolower, isdigit and malloc have been replaced by the
22// respective
23// C stdlib functions
24
25#include <cctype>26#include <cerrno>27#include <cmath>28#include <cstring>29#include <locale>30
31namespace torch::jit {32
33#ifdef _MSC_VER34double strtod_c(const char* nptr, char** endptr) {35static _locale_t loc = _create_locale(LC_ALL, "C");36return _strtod_l(nptr, endptr, loc);37}
38#else39double strtod_c(const char* nptr, char** endptr) {40/// NOLINTNEXTLINE(hicpp-signed-bitwise)41static locale_t loc = newlocale(LC_ALL_MASK, "C", nullptr);42return strtod_l(nptr, endptr, loc);43}
44#endif45
46float strtof_c(const char* nptr, char** endptr) {47return (float)strtod_c(nptr, endptr);48}
49
50} // namespace torch::jit51