Skip to content

Commit

Permalink
fix strtod indentation and mention it in LICENSE.md
Browse files Browse the repository at this point in the history
  • Loading branch information
tkelman committed Mar 16, 2017
1 parent 028a4b1 commit 5407287
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 89 deletions.
1 change: 1 addition & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Julia includes code from the following projects, which have their own licenses:
- [MUSL](http:https://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:https://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
Expand Down
167 changes: 78 additions & 89 deletions src/support/strtod.c
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}


Expand All @@ -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++;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -263,7 +253,6 @@ JL_DLLEXPORT double jl_strtod_c(const char *nptr, char **endptr)
}

free(copy);

}
else {
val = strtod(digits_pos, &fail_pos);
Expand All @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit 5407287

Please sign in to comment.