summaryrefslogtreecommitdiffstats
path: root/src/basic/percent-util.c
blob: cab9d0eaeac055e889adb75441b95c4aed8e2606 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "percent-util.h"
#include "string-util.h"
#include "parse-util.h"

static int parse_parts_value_whole(const char *p, const char *symbol) {
        const char *pc, *n;
        int r, v;

        pc = endswith(p, symbol);
        if (!pc)
                return -EINVAL;

        n = strndupa_safe(p, pc - p);
        r = safe_atoi(n, &v);
        if (r < 0)
                return r;
        if (v < 0)
                return -ERANGE;

        return v;
}

static int parse_parts_value_with_tenths_place(const char *p, const char *symbol) {
        const char *pc, *dot, *n;
        int r, q, v;

        pc = endswith(p, symbol);
        if (!pc)
                return -EINVAL;

        dot = memchr(p, '.', pc - p);
        if (dot) {
                if (dot + 2 != pc)
                        return -EINVAL;
                if (dot[1] < '0' || dot[1] > '9')
                        return -EINVAL;
                q = dot[1] - '0';
                n = strndupa_safe(p, dot - p);
        } else {
                q = 0;
                n = strndupa_safe(p, pc - p);
        }
        r = safe_atoi(n, &v);
        if (r < 0)
                return r;
        if (v < 0)
                return -ERANGE;
        if (v > (INT_MAX - q) / 10)
                return -ERANGE;

        v = v * 10 + q;
        return v;
}

static int parse_parts_value_with_hundredths_place(const char *p, const char *symbol) {
        const char *pc, *dot, *n;
        int r, q, v;

        pc = endswith(p, symbol);
        if (!pc)
                return -EINVAL;

        dot = memchr(p, '.', pc - p);
        if (dot) {
                if (dot + 3 == pc) {
                        /* Support two places after the dot */

                        if (dot[1] < '0' || dot[1] > '9' || dot[2] < '0' || dot[2] > '9')
                                return -EINVAL;
                        q = (dot[1] - '0') * 10 + (dot[2] - '0');

                } else if (dot + 2 == pc) {
                        /* Support one place after the dot */

                        if (dot[1] < '0' || dot[1] > '9')
                                return -EINVAL;
                        q = (dot[1] - '0') * 10;
                } else
                        /* We do not support zero or more than two places */
                        return -EINVAL;

                n = strndupa_safe(p, dot - p);
        } else {
                q = 0;
                n = strndupa_safe(p, pc - p);
        }
        r = safe_atoi(n, &v);
        if (r < 0)
                return r;
        if (v < 0)
                return -ERANGE;
        if (v > (INT_MAX - q) / 100)
                return -ERANGE;

        v = v * 100 + q;
        return v;
}

int parse_percent_unbounded(const char *p) {
        return parse_parts_value_whole(p, "%");
}

int parse_percent(const char *p) {
        int v;

        v = parse_percent_unbounded(p);
        if (v > 100)
                return -ERANGE;

        return v;
}

int parse_permille_unbounded(const char *p) {
        const char *pm;

        pm = endswith(p, "‰");
        if (pm)
                return parse_parts_value_whole(p, "‰");

        return parse_parts_value_with_tenths_place(p, "%");
}

int parse_permille(const char *p) {
        int v;

        v = parse_permille_unbounded(p);
        if (v > 1000)
                return -ERANGE;

        return v;
}

int parse_permyriad_unbounded(const char *p) {
        const char *pm;

        pm = endswith(p, "‱");
        if (pm)
                return parse_parts_value_whole(p, "‱");

        pm = endswith(p, "‰");
        if (pm)
                return parse_parts_value_with_tenths_place(p, "‰");

        return parse_parts_value_with_hundredths_place(p, "%");
}

int parse_permyriad(const char *p) {
        int v;

        v = parse_permyriad_unbounded(p);
        if (v > 10000)
                return -ERANGE;

        return v;
}