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 #endif
12
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
31 namespace torch::jit {
32
33 #ifdef _MSC_VER
strtod_c(const char * nptr,char ** endptr)34 double strtod_c(const char* nptr, char** endptr) {
35 static _locale_t loc = _create_locale(LC_ALL, "C");
36 return _strtod_l(nptr, endptr, loc);
37 }
38 #else
39 double strtod_c(const char* nptr, char** endptr) {
40 /// NOLINTNEXTLINE(hicpp-signed-bitwise)
41 static locale_t loc = newlocale(LC_ALL_MASK, "C", nullptr);
42 return strtod_l(nptr, endptr, loc);
43 }
44 #endif
45
strtof_c(const char * nptr,char ** endptr)46 float strtof_c(const char* nptr, char** endptr) {
47 return (float)strtod_c(nptr, endptr);
48 }
49
50 } // namespace torch::jit
51