Coverage Report

Created: 2026-06-10 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aspell/common/strtonum.cpp
Line
Count
Source
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
185
  {
16
185
    double x = 0.0;
17
185
    double y = 0.0;
18
185
    double decimal = 1.0;
19
185
    int negative = 0;
20
185
    const char * str = nptr;
21
22
185
    while (asc_isspace(*str))
23
0
      str++;
24
185
    if (!*str)
25
0
      goto END_STRTODBL_C;
26
185
    if (*str == '-') {
27
0
      negative = 1;
28
0
      str++;
29
185
    } else if (*str == '+')
30
0
      str++;
31
185
    if (!*str)
32
0
      goto END_STRTODBL_C;
33
370
    while (*str >= '0' && *str <= '9') {
34
185
      x = x * 10.0 + (*str - '0');
35
185
      str++;
36
185
    }
37
185
    if (!*str || *str != '.')
38
0
      goto END_STRTODBL_C;
39
185
    str++;  
40
185
    decimal = 1.0;
41
555
    while (*str >= '0' && *str <= '9') {
42
370
      decimal *= 0.1;
43
370
      y = y + (*str - '0')*decimal;
44
370
      str++;
45
370
    }
46
185
  END_STRTODBL_C:
47
185
    if (endptr)
48
185
      *endptr = (char *) str;
49
185
    return negative ? -(x + y) : (x + y);
50
185
  }
51
52
  double strtod_c(const char * nptr, const char ** endptr)
53
185
  {
54
185
    double x;
55
185
    const char * eptr;
56
185
    x = strtodbl_c(nptr, &eptr);
57
185
    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
185
    if (endptr)
70
0
      *endptr = eptr;
71
185
    return x;
72
185
  }
73
74
1.48k
  long strtoi_c(const char * npter, const char ** endptr) {
75
76
1.48k
    char * str = (char*)npter;
77
1.48k
    long num = 0;
78
1.48k
    long sign = 1;
79
 
80
1.48k
    *endptr = str;
81
1.48k
    while (asc_isspace(*str)) {
82
0
      str++;
83
0
    }
84
1.48k
    if (*str == '-' || *str == '+') {
85
0
      sign = *(str++) == '-' ? -1 : 1;
86
0
    }
87
3.65k
    while (*str >= '0' && *str <= '9' ) {
88
2.17k
      num = num * 10 + (long)(*str - '0');
89
2.17k
      str++;
90
2.17k
    }
91
1.48k
    *endptr = str;
92
1.48k
    return num;
93
1.48k
  }
94
}
95