Coverage Report

Created: 2026-02-26 06:53

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