strtoint128.h

filter: Statementstmt 70.4% 31 / 44 Branchbran 42.8% 24 / 56 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 55.0% 55 / 100 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« Int128.xs ↑ Summary blib/lib/Math/Int128.pm »
1 /*
2 * Converts a string into a int128_t
3 *
4 * based on OpenBSD: strtoll.c,v 1.6 2005/11/10 10:00:17 espie Exp $
5 */
6
7 /*-
8 * Copyright (c) 1992 The Regents of the University of California.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <ctype.h>
37
38 /*
39 * Convert a string to an int128_t/uint128_t.
40 */
41
42 static uint128_t
43 18 strtoint128(pTHX_ const char *s, int base, int sign) {
44 18 uint128_t acc = 0;
45 18 int c, neg, between = 0;
46
47 18 uint128_t upper_mul_limit = 0;
48
49 /*
50 * Skip white space and pick up leading +/- sign if any.
51 * If base is 0, allow 0x for hex and 0 for octal, else
52 * assume decimal; if base is already 16, allow 0x.
53 */
54 do {
55 18 c = (unsigned char) *s++;
56 18 ▶ } while (isspace(c));
Branch: 1 branch
BranchTrueFalseTotal
} while (isspace(c)); 0 18 18
57 18 ▶ if (c == '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '-') { 3 15 18
58 3 neg = 1;
59 3 c = *s++;
60 3 ▶ if (!sign) overflow(aTHX_ "negative sign found when parsing unsigned number");
Branch: 1 branch
BranchTrueFalseTotal
if (!sign) overflow(aTHX_ "negative sign found when parsing unsigned number"); 0 3 3
61 } else {
62 15 neg = 0;
63 15 ▶ if (c == '+')
Branch: 1 branch
BranchTrueFalseTotal
if (c == '+') 0 15 15
64 0 c = *s++;
65 }
66 18 ▶ if ((base == 0 || base == 16) &&
Branch: 3 branches
BranchTrueFalseTotal
if ((base == 0 || base == 16) && 9 9 18
if ((base == 0 || base == 16) && 0 9 9
if ((base == 0 || base == 16) && 9 0 9
67 9 ▶ c == '0' && (*s == 'x' || *s == 'X')) {
Branch: 2 branches
BranchTrueFalseTotal
c == '0' && (*s == 'x' || *s == 'X')) { 0 9 9
c == '0' && (*s == 'x' || *s == 'X')) { 0 0 0
68 9 c = s[1];
69 9 s += 2;
70 9 base = 16;
71 }
72 18 ▶ if (base == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (base == 0) 0 18 18
73 0 ▶ base = c == '0' ? 8 : 10;
Branch: 1 branch
BranchTrueFalseTotal
base = c == '0' ? 8 : 10; 0 0 0
74
75 512 for (;; c = (unsigned char) *s++) {
76 530 ▶ if (isdigit(c))
Branch: 1 branch
BranchTrueFalseTotal
if (isdigit(c)) 260 270 530
77 260 c -= '0';
78 270 ▶ else if (isalpha(c))
Branch: 1 branch
BranchTrueFalseTotal
else if (isalpha(c)) 189 81 270
79 189 ▶ c -= isupper(c) ? 'A' - 10 : 'a' - 10;
Branch: 1 branch
BranchTrueFalseTotal
c -= isupper(c) ? 'A' - 10 : 'a' - 10; 0 189 189
80 81 ▶ else if ((c == '_') && between)
Branch: 2 branches
BranchTrueFalseTotal
else if ((c == '_') && between) 63 18 81
else if ((c == '_') && between) 63 0 63
81 63 continue; /* ignore underscores as Perl does */
82 else
83 break;
84 449 ▶ if (c >= base)
Branch: 1 branch
BranchTrueFalseTotal
if (c >= base) 0 449 449
85 0 break;
86 449 ▶ if (may_die_on_overflow) {
Branch: 1 branch
BranchTrueFalseTotal
if (may_die_on_overflow) { 0 449 449
87 0 redo:
88 0 ▶ if (acc > upper_mul_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (acc > upper_mul_limit) { 0 0 0
89 0 ▶ if (!upper_mul_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (!upper_mul_limit) { 0 0 0
90 0 upper_mul_limit = UINT128_MAX / base;
91 0 goto redo;
92 }
93 0 ▶ overflow(aTHX_ (sign ? out_of_bounds_error_s : out_of_bounds_error_u));
Branch: 1 branch
BranchTrueFalseTotal
overflow(aTHX_ (sign ? out_of_bounds_error_s : out_of_bounds_error_u)); 0 0 0
94 }
95 0 acc *= base;
96 0 ▶ if (UINT128_MAX - acc < c) overflow(aTHX_ (sign ? out_of_bounds_error_s : out_of_bounds_error_u));
Branch: 2 branches
BranchTrueFalseTotal
if (UINT128_MAX - acc < c) overflow(aTHX_ (sign ? out_of_bounds_error_s : out_of_bounds_error_u)); 0 0 0
if (UINT128_MAX - acc < c) overflow(aTHX_ (sign ? out_of_bounds_error_s : out_of_bounds_error_u)); 0 0 0
97 0 acc += c;
98 }
99 else {
100 449 acc = acc * base + c;
101 }
102 449 between = 1;
103 }
104 18 ▶ if ( may_die_on_overflow && sign &&
Branch: 3 branches
BranchTrueFalseTotal
if ( may_die_on_overflow && sign && 0 18 18
if ( may_die_on_overflow && sign && 0 0 0
if ( may_die_on_overflow && sign && 0 0 0
105 0 ▶ ( acc > (neg ? (~(uint128_t)INT128_MIN + 1) : INT128_MAX) ) ) overflow(aTHX_ out_of_bounds_error_s);
Branch: 1 branch
BranchTrueFalseTotal
( acc > (neg ? (~(uint128_t)INT128_MIN + 1) : INT128_MAX) ) ) overflow(aTHX_ out_of_bounds_error_s); 0 0 0
106
107 18 ▶ return (neg ? ~acc + 1 : acc);
Branch: 1 branch
BranchTrueFalseTotal
return (neg ? ~acc + 1 : acc); 3 15 18
108 }
109
« Int128.xs ↑ Summary blib/lib/Math/Int128.pm »