Coverage Report

Created: 2025-11-09 07:04

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