| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/coreutils/src/gl/lib/strnumcmp-in.h |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /* Compare numeric strings. This is an internal include file. | - | ||||||||||||
| 2 | - | |||||||||||||
| 3 | Copyright (C) 1988-2018 Free Software Foundation, Inc. | - | ||||||||||||
| 4 | - | |||||||||||||
| 5 | This program is free software: you can redistribute it and/or modify | - | ||||||||||||
| 6 | it under the terms of the GNU General Public License as published by | - | ||||||||||||
| 7 | the Free Software Foundation, either version 3 of the License, or | - | ||||||||||||
| 8 | (at your option) any later version. | - | ||||||||||||
| 9 | - | |||||||||||||
| 10 | This program is distributed in the hope that it will be useful, | - | ||||||||||||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | - | ||||||||||||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | - | ||||||||||||
| 13 | GNU General Public License for more details. | - | ||||||||||||
| 14 | - | |||||||||||||
| 15 | You should have received a copy of the GNU General Public License | - | ||||||||||||
| 16 | along with this program. If not, see <https://www.gnu.org/licenses/>. */ | - | ||||||||||||
| 17 | - | |||||||||||||
| 18 | /* Written by Mike Haertel. */ | - | ||||||||||||
| 19 | - | |||||||||||||
| 20 | #ifndef STRNUMCMP_IN_H | - | ||||||||||||
| 21 | # define STRNUMCMP_IN_H 1 | - | ||||||||||||
| 22 | - | |||||||||||||
| 23 | # include "strnumcmp.h" | - | ||||||||||||
| 24 | - | |||||||||||||
| 25 | # include <stddef.h> | - | ||||||||||||
| 26 | - | |||||||||||||
| 27 | # define NEGATION_SIGN '-' | - | ||||||||||||
| 28 | # define NUMERIC_ZERO '0' | - | ||||||||||||
| 29 | - | |||||||||||||
| 30 | /* ISDIGIT differs from isdigit, as follows: | - | ||||||||||||
| 31 | - Its arg may be any int or unsigned int; it need not be an unsigned char | - | ||||||||||||
| 32 | or EOF. | - | ||||||||||||
| 33 | - It's typically faster. | - | ||||||||||||
| 34 | POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to | - | ||||||||||||
| 35 | isdigit unless it's important to use the locale's definition | - | ||||||||||||
| 36 | of 'digit' even when the host does not conform to POSIX. */ | - | ||||||||||||
| 37 | # define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9) | - | ||||||||||||
| 38 | - | |||||||||||||
| 39 | - | |||||||||||||
| 40 | /* Compare strings A and B containing decimal fractions < 1. | - | ||||||||||||
| 41 | DECIMAL_POINT is the decimal point. Each string | - | ||||||||||||
| 42 | should begin with a decimal point followed immediately by the digits | - | ||||||||||||
| 43 | of the fraction. Strings not of this form are treated as zero. */ | - | ||||||||||||
| 44 | - | |||||||||||||
| 45 | /* The goal here, is to take two numbers a and b... compare these | - | ||||||||||||
| 46 | in parallel. Instead of converting each, and then comparing the | - | ||||||||||||
| 47 | outcome. Most likely stopping the comparison before the conversion | - | ||||||||||||
| 48 | is complete. The algorithm used, in the old "sort" utility: | - | ||||||||||||
| 49 | - | |||||||||||||
| 50 | Algorithm: fraccompare | - | ||||||||||||
| 51 | Action : compare two decimal fractions | - | ||||||||||||
| 52 | accepts : char *a, char *b | - | ||||||||||||
| 53 | returns : -1 if a<b, 0 if a=b, 1 if a>b. | - | ||||||||||||
| 54 | implement: | - | ||||||||||||
| 55 | - | |||||||||||||
| 56 | if *a == decimal_point AND *b == decimal_point | - | ||||||||||||
| 57 | find first character different in a and b. | - | ||||||||||||
| 58 | if both are digits, return the difference *a - *b. | - | ||||||||||||
| 59 | if *a is a digit | - | ||||||||||||
| 60 | skip past zeros | - | ||||||||||||
| 61 | if digit return 1, else 0 | - | ||||||||||||
| 62 | if *b is a digit | - | ||||||||||||
| 63 | skip past zeros | - | ||||||||||||
| 64 | if digit return -1, else 0 | - | ||||||||||||
| 65 | if *a is a decimal_point | - | ||||||||||||
| 66 | skip past decimal_point and zeros | - | ||||||||||||
| 67 | if digit return 1, else 0 | - | ||||||||||||
| 68 | if *b is a decimal_point | - | ||||||||||||
| 69 | skip past decimal_point and zeros | - | ||||||||||||
| 70 | if digit return -1, else 0 | - | ||||||||||||
| 71 | return 0 */ | - | ||||||||||||
| 72 | - | |||||||||||||
| 73 | static inline int _GL_ATTRIBUTE_PURE | - | ||||||||||||
| 74 | fraccompare (char const *a, char const *b, char decimal_point) | - | ||||||||||||
| 75 | { | - | ||||||||||||
| 76 | if (*a == decimal_point && *b == decimal_point)
| 10-354219 | ||||||||||||
| 77 | { | - | ||||||||||||
| 78 | while (*++a == *++b)
| 315868-354201 | ||||||||||||
| 79 | if (! ISDIGIT (*a))
| 0-315868 | ||||||||||||
| 80 | return 0; never executed: return 0; | 0 | ||||||||||||
| 81 | if (ISDIGIT (*a) && ISDIGIT (*b))
| 14-354176 | ||||||||||||
| 82 | return *a - *b; executed 354162 times by 1 test: return *a - *b;Executed by:
| 354162 | ||||||||||||
| 83 | if (ISDIGIT (*a))
| 14-25 | ||||||||||||
| 84 | goto a_trailing_nonzero; executed 14 times by 1 test: goto a_trailing_nonzero;Executed by:
| 14 | ||||||||||||
| 85 | if (ISDIGIT (*b))
| 12-13 | ||||||||||||
| 86 | goto b_trailing_nonzero; executed 13 times by 1 test: goto b_trailing_nonzero;Executed by:
| 13 | ||||||||||||
| 87 | return 0; executed 12 times by 1 test: return 0;Executed by:
| 12 | ||||||||||||
| 88 | } | - | ||||||||||||
| 89 | else if (*a++ == decimal_point)
| 10-18 | ||||||||||||
| 90 | { | - | ||||||||||||
| 91 | a_trailing_nonzero: | - | ||||||||||||
| 92 | while (*a == NUMERIC_ZERO)
| 18-32 | ||||||||||||
| 93 | a++; executed 18 times by 1 test: a++;Executed by:
| 18 | ||||||||||||
| 94 | return ISDIGIT (*a); executed 32 times by 1 test: return ((unsigned int) (*a) - '0' <= 9);Executed by:
| 32 | ||||||||||||
| 95 | } | - | ||||||||||||
| 96 | else if (*b++ == decimal_point)
| 0-10 | ||||||||||||
| 97 | { | - | ||||||||||||
| 98 | b_trailing_nonzero: | - | ||||||||||||
| 99 | while (*b == NUMERIC_ZERO)
| 14-23 | ||||||||||||
| 100 | b++; executed 14 times by 1 test: b++;Executed by:
| 14 | ||||||||||||
| 101 | return - ISDIGIT (*b); executed 23 times by 1 test: return - ((unsigned int) (*b) - '0' <= 9);Executed by:
| 23 | ||||||||||||
| 102 | } | - | ||||||||||||
| 103 | return 0; never executed: return 0; | 0 | ||||||||||||
| 104 | } | - | ||||||||||||
| 105 | - | |||||||||||||
| 106 | /* Compare strings A and B as numbers without explicitly converting | - | ||||||||||||
| 107 | them to machine numbers, to avoid overflow problems and perhaps | - | ||||||||||||
| 108 | improve performance. DECIMAL_POINT is the decimal point and | - | ||||||||||||
| 109 | THOUSANDS_SEP the thousands separator. A DECIMAL_POINT of -1 | - | ||||||||||||
| 110 | causes comparisons to act as if there is no decimal point | - | ||||||||||||
| 111 | character, and likewise for THOUSANDS_SEP. */ | - | ||||||||||||
| 112 | - | |||||||||||||
| 113 | static inline int _GL_ATTRIBUTE_PURE | - | ||||||||||||
| 114 | numcompare (char const *a, char const *b, | - | ||||||||||||
| 115 | int decimal_point, int thousands_sep) | - | ||||||||||||
| 116 | { | - | ||||||||||||
| 117 | unsigned char tmpa = *a; | - | ||||||||||||
| 118 | unsigned char tmpb = *b; | - | ||||||||||||
| 119 | int tmp; | - | ||||||||||||
| 120 | size_t log_a; | - | ||||||||||||
| 121 | size_t log_b; | - | ||||||||||||
| 122 | - | |||||||||||||
| 123 | if (tmpa == NEGATION_SIGN)
| 259010-297842 | ||||||||||||
| 124 | { | - | ||||||||||||
| 125 | do | - | ||||||||||||
| 126 | tmpa = *++a; executed 285926 times by 3 tests: tmpa = *++a;Executed by:
| 285926 | ||||||||||||
| 127 | while (tmpa == NUMERIC_ZERO || tmpa == thousands_sep);
| 0-259010 | ||||||||||||
| 128 | if (tmpb != NEGATION_SIGN)
| 16413-242597 | ||||||||||||
| 129 | { | - | ||||||||||||
| 130 | if (tmpa == decimal_point)
| 4477-11936 | ||||||||||||
| 131 | do | - | ||||||||||||
| 132 | tmpa = *++a; executed 5366 times by 1 test: tmpa = *++a;Executed by:
| 5366 | ||||||||||||
| 133 | while (tmpa == NUMERIC_ZERO);
| 889-4477 | ||||||||||||
| 134 | if (ISDIGIT (tmpa))
| 4-16409 | ||||||||||||
| 135 | return -1; executed 16409 times by 3 tests: return -1;Executed by:
| 16409 | ||||||||||||
| 136 | while (tmpb == NUMERIC_ZERO || tmpb == thousands_sep)
| 0-4 | ||||||||||||
| 137 | tmpb = *++b; never executed: tmpb = *++b; | 0 | ||||||||||||
| 138 | if (tmpb == decimal_point)
| 0-4 | ||||||||||||
| 139 | do | - | ||||||||||||
| 140 | tmpb = *++b; never executed: tmpb = *++b; | 0 | ||||||||||||
| 141 | while (tmpb == NUMERIC_ZERO);
| 0 | ||||||||||||
| 142 | return - ISDIGIT (tmpb); executed 4 times by 1 test: return - ((unsigned int) (tmpb) - '0' <= 9);Executed by:
| 4 | ||||||||||||
| 143 | } | - | ||||||||||||
| 144 | do | - | ||||||||||||
| 145 | tmpb = *++b; executed 264666 times by 2 tests: tmpb = *++b;Executed by:
| 264666 | ||||||||||||
| 146 | while (tmpb == NUMERIC_ZERO || tmpb == thousands_sep);
| 0-242597 | ||||||||||||
| 147 | - | |||||||||||||
| 148 | while (tmpa == tmpb && ISDIGIT (tmpa))
| 65541-336920 | ||||||||||||
| 149 | { | - | ||||||||||||
| 150 | do | - | ||||||||||||
| 151 | tmpa = *++a; executed 159864 times by 1 test: tmpa = *++a;Executed by:
| 159864 | ||||||||||||
| 152 | while (tmpa == thousands_sep);
| 0-159864 | ||||||||||||
| 153 | do | - | ||||||||||||
| 154 | tmpb = *++b; executed 159864 times by 1 test: tmpb = *++b;Executed by:
| 159864 | ||||||||||||
| 155 | while (tmpb == thousands_sep);
| 0-159864 | ||||||||||||
| 156 | } executed 159864 times by 1 test: end of blockExecuted by:
| 159864 | ||||||||||||
| 157 | - | |||||||||||||
| 158 | if ((tmpa == decimal_point && !ISDIGIT (tmpb))
| 4813-181871 | ||||||||||||
| 159 | || (tmpb == decimal_point && !ISDIGIT (tmpa)))
| 6-61087 | ||||||||||||
| 160 | return fraccompare (b, a, decimal_point); executed 177064 times by 1 test: return fraccompare (b, a, decimal_point);Executed by:
| 177064 | ||||||||||||
| 161 | - | |||||||||||||
| 162 | tmp = tmpb - tmpa; | - | ||||||||||||
| 163 | - | |||||||||||||
| 164 | for (log_a = 0; ISDIGIT (tmpa); ++log_a)
| 63542-65533 | ||||||||||||
| 165 | do | - | ||||||||||||
| 166 | tmpa = *++a; executed 63542 times by 2 tests: tmpa = *++a;Executed by:
| 63542 | ||||||||||||
| 167 | while (tmpa == thousands_sep);
| 0-63542 | ||||||||||||
| 168 | - | |||||||||||||
| 169 | for (log_b = 0; ISDIGIT (tmpb); ++log_b)
| 64296-65533 | ||||||||||||
| 170 | do | - | ||||||||||||
| 171 | tmpb = *++b; executed 64296 times by 2 tests: tmpb = *++b;Executed by:
| 64296 | ||||||||||||
| 172 | while (tmpb == thousands_sep);
| 0-64296 | ||||||||||||
| 173 | - | |||||||||||||
| 174 | if (log_a != log_b)
| 9515-56018 | ||||||||||||
| 175 | return log_a < log_b ? 1 : -1; executed 9515 times by 1 test: return log_a < log_b ? 1 : -1;Executed by:
| 9515 | ||||||||||||
| 176 | - | |||||||||||||
| 177 | if (!log_a)
| 2-56016 | ||||||||||||
| 178 | return 0; executed 2 times by 1 test: return 0;Executed by:
| 2 | ||||||||||||
| 179 | - | |||||||||||||
| 180 | return tmp; executed 56016 times by 2 tests: return tmp;Executed by:
| 56016 | ||||||||||||
| 181 | } | - | ||||||||||||
| 182 | else if (tmpb == NEGATION_SIGN)
| 21127-276715 | ||||||||||||
| 183 | { | - | ||||||||||||
| 184 | do | - | ||||||||||||
| 185 | tmpb = *++b; executed 26080 times by 2 tests: tmpb = *++b;Executed by:
| 26080 | ||||||||||||
| 186 | while (tmpb == NUMERIC_ZERO || tmpb == thousands_sep);
| 0-21127 | ||||||||||||
| 187 | if (tmpb == decimal_point)
| 4952-16175 | ||||||||||||
| 188 | do | - | ||||||||||||
| 189 | tmpb = *++b; executed 5840 times by 1 test: tmpb = *++b;Executed by:
| 5840 | ||||||||||||
| 190 | while (tmpb == NUMERIC_ZERO);
| 888-4952 | ||||||||||||
| 191 | if (ISDIGIT (tmpb))
| 5-21122 | ||||||||||||
| 192 | return 1; executed 21122 times by 2 tests: return 1;Executed by:
| 21122 | ||||||||||||
| 193 | while (tmpa == NUMERIC_ZERO || tmpa == thousands_sep)
| 0-5 | ||||||||||||
| 194 | tmpa = *++a; never executed: tmpa = *++a; | 0 | ||||||||||||
| 195 | if (tmpa == decimal_point)
| 0-5 | ||||||||||||
| 196 | do | - | ||||||||||||
| 197 | tmpa = *++a; never executed: tmpa = *++a; | 0 | ||||||||||||
| 198 | while (tmpa == NUMERIC_ZERO);
| 0 | ||||||||||||
| 199 | return ISDIGIT (tmpa); executed 5 times by 1 test: return ((unsigned int) (tmpa) - '0' <= 9);Executed by:
| 5 | ||||||||||||
| 200 | } | - | ||||||||||||
| 201 | else | - | ||||||||||||
| 202 | { | - | ||||||||||||
| 203 | while (tmpa == NUMERIC_ZERO || tmpa == thousands_sep)
| 0-276715 | ||||||||||||
| 204 | tmpa = *++a; executed 22692 times by 2 tests: tmpa = *++a;Executed by:
| 22692 | ||||||||||||
| 205 | while (tmpb == NUMERIC_ZERO || tmpb == thousands_sep)
| 0-276715 | ||||||||||||
| 206 | tmpb = *++b; executed 23382 times by 2 tests: tmpb = *++b;Executed by:
| 23382 | ||||||||||||
| 207 | - | |||||||||||||
| 208 | while (tmpa == tmpb && ISDIGIT (tmpa))
| 85382-406877 | ||||||||||||
| 209 | { | - | ||||||||||||
| 210 | do | - | ||||||||||||
| 211 | tmpa = *++a; executed 215544 times by 3 tests: tmpa = *++a;Executed by:
| 215544 | ||||||||||||
| 212 | while (tmpa == thousands_sep);
| 0-215544 | ||||||||||||
| 213 | do | - | ||||||||||||
| 214 | tmpb = *++b; executed 215544 times by 3 tests: tmpb = *++b;Executed by:
| 215544 | ||||||||||||
| 215 | while (tmpb == thousands_sep);
| 0-215544 | ||||||||||||
| 216 | } executed 215544 times by 3 tests: end of blockExecuted by:
| 215544 | ||||||||||||
| 217 | - | |||||||||||||
| 218 | if ((tmpa == decimal_point && !ISDIGIT (tmpb))
| 4475-181632 | ||||||||||||
| 219 | || (tmpb == decimal_point && !ISDIGIT (tmpa)))
| 8-94813 | ||||||||||||
| 220 | return fraccompare (a, b, decimal_point); executed 177165 times by 1 test: return fraccompare (a, b, decimal_point);Executed by:
| 177165 | ||||||||||||
| 221 | - | |||||||||||||
| 222 | tmp = tmpa - tmpb; | - | ||||||||||||
| 223 | - | |||||||||||||
| 224 | for (log_a = 0; ISDIGIT (tmpa); ++log_a)
| 88283-99550 | ||||||||||||
| 225 | do | - | ||||||||||||
| 226 | tmpa = *++a; executed 88283 times by 3 tests: tmpa = *++a;Executed by:
| 88283 | ||||||||||||
| 227 | while (tmpa == thousands_sep);
| 0-88283 | ||||||||||||
| 228 | - | |||||||||||||
| 229 | for (log_b = 0; ISDIGIT (tmpb); ++log_b)
| 88223-99550 | ||||||||||||
| 230 | do | - | ||||||||||||
| 231 | tmpb = *++b; executed 88223 times by 3 tests: tmpb = *++b;Executed by:
| 88223 | ||||||||||||
| 232 | while (tmpb == thousands_sep);
| 0-88223 | ||||||||||||
| 233 | - | |||||||||||||
| 234 | if (log_a != log_b)
| 11162-88388 | ||||||||||||
| 235 | return log_a < log_b ? -1 : 1; executed 11162 times by 3 tests: return log_a < log_b ? -1 : 1;Executed by:
| 11162 | ||||||||||||
| 236 | - | |||||||||||||
| 237 | if (!log_a)
| 14188-74200 | ||||||||||||
| 238 | return 0; executed 14188 times by 2 tests: return 0;Executed by:
| 14188 | ||||||||||||
| 239 | - | |||||||||||||
| 240 | return tmp; executed 74200 times by 3 tests: return tmp;Executed by:
| 74200 | ||||||||||||
| 241 | } | - | ||||||||||||
| 242 | } | - | ||||||||||||
| 243 | - | |||||||||||||
| 244 | #endif | - | ||||||||||||
| Source code | Switch to Preprocessed file |