Skip to content

Commit ef11481

Browse files
committed
Add constants for the integer base system
On multiple locations in the code base, magic numbers are used to mark the base of an intenger conversion. This commit introduces constants and replaces the magic numbers in some places.
1 parent f6d4505 commit ef11481

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

src/util/mp_arith.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ Author: Daniel Kroening, [email protected]
1818
#include "optional.h"
1919
#include "deprecate.h"
2020

21+
const int DECIMAL_SYSTEM = 10;
22+
const int OCTAL_SYSTEM = 8;
23+
const int BINARY_SYSTEM = 2;
24+
const int HEXADECIMAL_SYSTEM = 16;
25+
2126
// NOLINTNEXTLINE(readability/identifiers)
2227
typedef BigInt mp_integer;
2328

@@ -47,8 +52,11 @@ mp_integer rotate_right(
4752
mp_integer rotate_left(
4853
const mp_integer &, const mp_integer &, std::size_t true_size);
4954

50-
const std::string integer2string(const mp_integer &, unsigned base=10);
51-
const mp_integer string2integer(const std::string &, unsigned base=10);
55+
const std::string
56+
integer2string(const mp_integer &, unsigned base = DECIMAL_SYSTEM);
57+
const mp_integer
58+
string2integer(const std::string &, unsigned base = DECIMAL_SYSTEM);
59+
5260
const std::string integer2binary(const mp_integer &, std::size_t width);
5361
const mp_integer binary2integer(const std::string &, bool is_signed);
5462

src/util/string2int.h

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,35 @@ Author: Michael Tautschnig, [email protected]
66
77
\*******************************************************************/
88

9-
109
#ifndef CPROVER_UTIL_STRING2INT_H
1110
#define CPROVER_UTIL_STRING2INT_H
1211

12+
#include "mp_arith.h"
13+
1314
#include <string>
1415

1516
// These check that the string is indeed a valid number,
1617
// and fail an assertion otherwise.
1718
// We use those for data types that C++11's std::stoi etc. do not
1819
// cover.
19-
unsigned safe_string2unsigned(const std::string &str, int base=10);
20-
std::size_t safe_string2size_t(const std::string &str, int base=10);
20+
unsigned
21+
safe_string2unsigned(const std::string &str, int base = DECIMAL_SYSTEM);
22+
std::size_t
23+
safe_string2size_t(const std::string &str, int base = DECIMAL_SYSTEM);
2124

2225
// The below mimic C's atoi/atol: any errors are silently ignored.
2326
// They are meant to replace atoi/atol.
24-
int unsafe_string2int(const std::string &str, int base=10);
25-
unsigned unsafe_string2unsigned(const std::string &str, int base=10);
26-
std::size_t unsafe_string2size_t(const std::string &str, int base=10);
27+
int unsafe_string2int(const std::string &str, int base = DECIMAL_SYSTEM);
28+
unsigned
29+
unsafe_string2unsigned(const std::string &str, int base = DECIMAL_SYSTEM);
30+
std::size_t
31+
unsafe_string2size_t(const std::string &str, int base = DECIMAL_SYSTEM);
2732

2833
// Same for atoll
29-
long long int unsafe_string2signedlonglong(const std::string &str, int base=10);
34+
long long int
35+
unsafe_string2signedlonglong(const std::string &str, int base = DECIMAL_SYSTEM);
3036
long long unsigned int unsafe_string2unsignedlonglong(
31-
const std::string &str, int base=10);
37+
const std::string &str,
38+
int base = DECIMAL_SYSTEM);
3239

3340
#endif // CPROVER_UTIL_STRING2INT_H

0 commit comments

Comments
 (0)