]> git.sesse.net Git - ffmpeg/blob - compat/strtod.c
Merge commit 'd58dd4b5b5d31cfd4092e38a5f2c894eee2ab078'
[ffmpeg] / compat / strtod.c
1 /*
2  * C99-compatible strtod() implementation
3  * Copyright (c) 2012 Ronald S. Bultje <rsbultje@gmail.com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include <ctype.h>
23 #include <limits.h>
24
25 #include "libavutil/avstring.h"
26 #include "libavutil/mathematics.h"
27 #include "strtod.h"
28
29 static char *check_nan_suffix(char *s)
30 {
31     char *start = s;
32
33     if (*s++ != '(')
34         return start;
35
36     while ((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') ||
37            (*s >= '0' && *s <= '9') ||  *s == '_')
38         s++;
39
40     return *s == ')' ? s + 1 : start;
41 }
42
43 #undef strtod
44 double avpriv_strtod(char *restrict nptr, char **restrict endptr)
45 {
46     char *end;
47     double res;
48
49     /* Skip leading spaces */
50     while (isspace(*nptr))
51         nptr++;
52
53     if (!av_strncasecmp(nptr, "infinity", 8)) {
54         end = nptr + 8;
55         res = INFINITY;
56     } else if (!av_strncasecmp(nptr, "inf", 3)) {
57         end = nptr + 3;
58         res = INFINITY;
59     } else if (!av_strncasecmp(nptr, "+infinity", 9)) {
60         end = nptr + 9;
61         res = INFINITY;
62     } else if (!av_strncasecmp(nptr, "+inf", 4)) {
63         end = nptr + 4;
64         res = INFINITY;
65     } else if (!av_strncasecmp(nptr, "-infinity", 9)) {
66         end = nptr + 9;
67         res = -INFINITY;
68     } else if (!av_strncasecmp(nptr, "-inf", 4)) {
69         end = nptr + 4;
70         res = -INFINITY;
71     } else if (!av_strncasecmp(nptr, "nan", 3)) {
72         end = check_nan_suffix(nptr + 3);
73         res = NAN;
74     } else if (!av_strncasecmp(nptr, "+nan", 4) ||
75                !av_strncasecmp(nptr, "-nan", 4)) {
76         end = check_nan_suffix(nptr + 4);
77         res = NAN;
78     } else if (!av_strncasecmp(nptr, "0x", 2) ||
79                !av_strncasecmp(nptr, "-0x", 3) ||
80                !av_strncasecmp(nptr, "+0x", 3)) {
81         /* FIXME this doesn't handle exponents, non-integers (float/double)
82          * and numbers too large for long long */
83         res = strtoll(nptr, &end, 16);
84     } else {
85         res = strtod(nptr, &end);
86     }
87
88     if (endptr)
89         *endptr = end;
90
91     return res;
92 }