/src/aspell/common/strtonum.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // This file is part of The New Aspell |
2 | | // Copyright (C) 2002 by Melvin Hadasht and Kevin Atkinson under the |
3 | | // GNU LGPL license version 2.0 or 2.1. You should have received a |
4 | | // copy of the LGPL license along with this library if you did not you |
5 | | // can find it at http://www.gnu.org/. |
6 | | |
7 | | #include <stdlib.h> |
8 | | |
9 | | #include "strtonum.hpp" |
10 | | #include "asc_ctype.hpp" |
11 | | |
12 | | namespace acommon { |
13 | | |
14 | | static double strtodbl_c(const char * nptr, const char ** endptr) |
15 | 79 | { |
16 | 79 | double x = 0.0; |
17 | 79 | double y = 0.0; |
18 | 79 | double decimal = 1.0; |
19 | 79 | int negative = 0; |
20 | 79 | const char * str = nptr; |
21 | | |
22 | 79 | while (asc_isspace(*str)) |
23 | 0 | str++; |
24 | 79 | if (!*str) |
25 | 0 | goto END_STRTODBL_C; |
26 | 79 | if (*str == '-') { |
27 | 0 | negative = 1; |
28 | 0 | str++; |
29 | 79 | } else if (*str == '+') |
30 | 0 | str++; |
31 | 79 | if (!*str) |
32 | 0 | goto END_STRTODBL_C; |
33 | 158 | while (*str >= '0' && *str <= '9') { |
34 | 79 | x = x * 10.0 + (*str - '0'); |
35 | 79 | str++; |
36 | 79 | } |
37 | 79 | if (!*str || *str != '.') |
38 | 0 | goto END_STRTODBL_C; |
39 | 79 | str++; |
40 | 79 | decimal = 1.0; |
41 | 237 | while (*str >= '0' && *str <= '9') { |
42 | 158 | decimal *= 0.1; |
43 | 158 | y = y + (*str - '0')*decimal; |
44 | 158 | str++; |
45 | 158 | } |
46 | 79 | END_STRTODBL_C: |
47 | 79 | if (endptr) |
48 | 79 | *endptr = (char *) str; |
49 | 79 | return negative ? -(x + y) : (x + y); |
50 | 79 | } |
51 | | |
52 | | double strtod_c(const char * nptr, const char ** endptr) |
53 | 79 | { |
54 | 79 | double x; |
55 | 79 | const char * eptr; |
56 | 79 | x = strtodbl_c(nptr, &eptr); |
57 | 79 | if (*eptr == 'E' || *eptr == 'e') { |
58 | 0 | const char *nptr2 = eptr; |
59 | 0 | long int y, i; |
60 | 0 | double e = 1.0; |
61 | 0 | nptr2++; |
62 | 0 | y = strtol(nptr2, (char **)&eptr, 10); |
63 | 0 | if (y) { |
64 | 0 | for (i=0; i < ( y < 0 ? -y : y); i++) |
65 | 0 | e *= 10.0; |
66 | 0 | x = (y < 0) ? x / e : x * e; |
67 | 0 | } |
68 | 0 | } |
69 | 79 | if (endptr) |
70 | 0 | *endptr = eptr; |
71 | 79 | return x; |
72 | 79 | } |
73 | | |
74 | 6.91k | long strtoi_c(const char * npter, const char ** endptr) { |
75 | | |
76 | 6.91k | char * str = (char*)npter; |
77 | 6.91k | long num = 0; |
78 | 6.91k | long sign = 1; |
79 | | |
80 | 6.91k | *endptr = str; |
81 | 6.91k | while (asc_isspace(*str)) { |
82 | 0 | str++; |
83 | 0 | } |
84 | 6.91k | if (*str == '-' || *str == '+') { |
85 | 0 | sign = *(str++) == '-' ? -1 : 1; |
86 | 0 | } |
87 | 16.9k | while (*str >= '0' && *str <= '9' ) { |
88 | 10.0k | num = num * 10 + (long)(*str - '0'); |
89 | 10.0k | str++; |
90 | 10.0k | } |
91 | 6.91k | *endptr = str; |
92 | 6.91k | return num; |
93 | 6.91k | } |
94 | | } |
95 | | |