From 540728706239a8a3a625cf0548423a424351d106 Mon Sep 17 00:00:00 2001 From: Tony Kelman Date: Wed, 8 Feb 2017 01:14:56 -0800 Subject: [PATCH] fix strtod indentation and mention it in LICENSE.md ref https://github.com/JuliaLang/julia/pull/5988 --- LICENSE.md | 1 + src/support/strtod.c | 167 ++++++++++++++++++++----------------------- 2 files changed, 79 insertions(+), 89 deletions(-) diff --git a/LICENSE.md b/LICENSE.md index e601fa09f6be3..1d812d2a7c300 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -35,6 +35,7 @@ Julia includes code from the following projects, which have their own licenses: - [MUSL](http://git.musl-libc.org/cgit/musl/tree/COPYRIGHT) (for getopt implementation on Windows) [MIT] - [MINGW](https://sourceforge.net/p/mingw/mingw-org-wsl/ci/legacy/tree/mingwrt/mingwex/dirname.c) (for dirname implementation on Windows) [MIT] - [NetBSD](http://www.netbsd.org/about/redistribution.html) (for setjmp, longjmp, and strptime implementations on Windows) [BSD-3] +- [Python](https://docs.python.org/2/license.html) (for strtod implementation on Windows) [BSD-3, effectively] - [randmtzig.c](https://github.com/JuliaLang/julia/blob/master/test/perf/micro/randmtzig.c) for Gaussian random number generation (for C benchmarks only) [BSD-3] The Julia language links to the following external libraries, which have their diff --git a/src/support/strtod.c b/src/support/strtod.c index 1dc2b961576ca..fe617da2490d2 100644 --- a/src/support/strtod.c +++ b/src/support/strtod.c @@ -20,22 +20,21 @@ static locale_t c_locale; locale_t get_c_locale(void) { - if(!c_locale_initialized) - { - c_locale_initialized = 1; - c_locale = newlocale(LC_ALL_MASK, "C", NULL); - } - return c_locale; + if (!c_locale_initialized) { + c_locale_initialized = 1; + c_locale = newlocale(LC_ALL_MASK, "C", NULL); + } + return c_locale; } JL_DLLEXPORT double jl_strtod_c(const char *nptr, char **endptr) { - return strtod_l(nptr, endptr, get_c_locale()); + return strtod_l(nptr, endptr, get_c_locale()); } JL_DLLEXPORT float jl_strtof_c(const char *nptr, char **endptr) { - return strtof_l(nptr, endptr, get_c_locale()); + return strtof_l(nptr, endptr, get_c_locale()); } @@ -58,7 +57,7 @@ JL_DLLEXPORT float jl_strtof_c(const char *nptr, char **endptr) int case_insensitive_match(const char *s, const char *t) { - while(*t && tolower(*s) == *t) { + while (*t && tolower(*s) == *t) { s++; t++; } @@ -147,84 +146,75 @@ JL_DLLEXPORT double jl_strtod_c(const char *nptr, char **endptr) } /* This code path is used for hex floats */ - if (*p == '0' && (*(p+1) == 'x' || *(p+1) == 'X')) - { - digits_pos = p; - p += 2; - /* Check that what's left begins with a digit or decimal point */ - if (!isxdigit(*p) && *p != '.') - goto invalid_string; - - - if (decimal_point[0] != '.' || - decimal_point[1] != 0) - { - /* Look for a '.' in the input; if present, it'll need to be - swapped for the current locale's decimal point before we - call strtod. On the other hand, if we find the current - locale's decimal point then the input is invalid. */ - while (isxdigit(*p)) - p++; - - if (*p == '.') - { - decimal_point_pos = p++; - - /* locate end of number */ - while (isxdigit(*p)) - p++; - - if (*p == 'p' || *p == 'P') - p++; - if (*p == '+' || *p == '-') - p++; - while (isdigit(*p)) - p++; - end = p; - } - else if (strncmp(p, decimal_point, decimal_point_len) == 0) - goto invalid_string; - /* For the other cases, we need not convert the decimal - point */ - } - } else - { - /* Check that what's left begins with a digit or decimal point */ - if (!isdigit(*p) && *p != '.') - goto invalid_string; - - digits_pos = p; - if (decimal_point[0] != '.' || - decimal_point[1] != 0) - { - /* Look for a '.' in the input; if present, it'll need to be - swapped for the current locale's decimal point before we - call strtod. On the other hand, if we find the current - locale's decimal point then the input is invalid. */ - while (isdigit(*p)) - p++; - - if (*p == '.') - { - decimal_point_pos = p++; - - /* locate end of number */ - while (isdigit(*p)) - p++; - - if (*p == 'e' || *p == 'E') - p++; - if (*p == '+' || *p == '-') - p++; - while (isdigit(*p)) - p++; - end = p; - } - else if (strncmp(p, decimal_point, decimal_point_len) == 0) + if (*p == '0' && (*(p+1) == 'x' || *(p+1) == 'X')) { + digits_pos = p; + p += 2; + /* Check that what's left begins with a digit or decimal point */ + if (!isxdigit(*p) && *p != '.') + goto invalid_string; + + + if (decimal_point[0] != '.' || decimal_point[1] != 0) { + /* Look for a '.' in the input; if present, it'll need to be + swapped for the current locale's decimal point before we + call strtod. On the other hand, if we find the current + locale's decimal point then the input is invalid. */ + while (isxdigit(*p)) + p++; + + if (*p == '.') { + decimal_point_pos = p++; + + /* locate end of number */ + while (isxdigit(*p)) + p++; + + if (*p == 'p' || *p == 'P') + p++; + if (*p == '+' || *p == '-') + p++; + while (isdigit(*p)) + p++; + end = p; + } + else if (strncmp(p, decimal_point, decimal_point_len) == 0) + goto invalid_string; + /* For the other cases, we need not convert the decimal point */ + } + } + else { + /* Check that what's left begins with a digit or decimal point */ + if (!isdigit(*p) && *p != '.') goto invalid_string; - /* For the other cases, we need not convert the decimal - point */ - } + + digits_pos = p; + if (decimal_point[0] != '.' || decimal_point[1] != 0) { + /* Look for a '.' in the input; if present, it'll need to be + swapped for the current locale's decimal point before we + call strtod. On the other hand, if we find the current + locale's decimal point then the input is invalid. */ + while (isdigit(*p)) + p++; + + if (*p == '.') { + decimal_point_pos = p++; + + /* locate end of number */ + while (isdigit(*p)) + p++; + + if (*p == 'e' || *p == 'E') + p++; + if (*p == '+' || *p == '-') + p++; + while (isdigit(*p)) + p++; + end = p; + } + else if (strncmp(p, decimal_point, decimal_point_len) == 0) + goto invalid_string; + /* For the other cases, we need not convert the decimal point */ + } } if (decimal_point_pos) { @@ -263,7 +253,6 @@ JL_DLLEXPORT double jl_strtod_c(const char *nptr, char **endptr) } free(copy); - } else { val = strtod(digits_pos, &fail_pos); @@ -278,7 +267,7 @@ JL_DLLEXPORT double jl_strtod_c(const char *nptr, char **endptr) return val; - invalid_string: +invalid_string: *endptr = (char*)nptr; errno = EINVAL; return -1.0; @@ -287,7 +276,7 @@ JL_DLLEXPORT double jl_strtod_c(const char *nptr, char **endptr) JL_DLLEXPORT float jl_strtof_c(const char *nptr, char **endptr) { - return (float) jl_strtod_c(nptr, endptr); + return (float) jl_strtod_c(nptr, endptr); } #endif