Skip to content

Commit 5e8c9a6

Browse files
Lawrence Nahumjgriffiths
authored andcommitted
improvements from BlockchainCommons#13
1 parent e1fe6da commit 5e8c9a6

File tree

11 files changed

+49
-49
lines changed

11 files changed

+49
-49
lines changed

src/bytewords.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,21 +99,21 @@ static bool decode_word(const string& word, size_t word_len, uint8_t& output) {
9999
return true;
100100
}
101101

102-
static const string get_word(uint8_t index) {
103-
auto p = &bytewords[index * 4];
104-
return string(p, p + 4);
102+
static string get_word(uint8_t index) {
103+
const auto* p = &bytewords[index * 4];
104+
return {p, p + 4};
105105
}
106106

107-
static const string get_minimal_word(uint8_t index) {
107+
static string get_minimal_word(uint8_t index) {
108108
string word;
109109
word.reserve(2);
110-
auto p = &bytewords[index * 4];
110+
const auto* p = &bytewords[index * 4];
111111
word.push_back(*p);
112112
word.push_back(*(p + 3));
113113
return word;
114114
}
115115

116-
static const string encode(const ByteVector& buf, const string& separator) {
116+
static string encode(const ByteVector& buf, const string& separator) {
117117
auto len = buf.size();
118118
StringVector words;
119119
words.reserve(len);
@@ -124,19 +124,19 @@ static const string encode(const ByteVector& buf, const string& separator) {
124124
return join(words, separator);
125125
}
126126

127-
static const ByteVector add_crc(const ByteVector& buf) {
127+
static ByteVector add_crc(const ByteVector& buf) {
128128
auto crc_buf = crc32_bytes(buf);
129129
auto result = buf;
130130
append(result, crc_buf);
131131
return result;
132132
}
133133

134-
static const string encode_with_separator(const ByteVector& buf, const string& separator) {
134+
static string encode_with_separator(const ByteVector& buf, const string& separator) {
135135
auto crc_buf = add_crc(buf);
136136
return encode(crc_buf, separator);
137137
}
138138

139-
static const string encode_minimal(const ByteVector& buf) {
139+
static string encode_minimal(const ByteVector& buf) {
140140
string result;
141141
auto crc_buf = add_crc(buf);
142142
auto len = crc_buf.size();
@@ -147,7 +147,7 @@ static const string encode_minimal(const ByteVector& buf) {
147147
return result;
148148
}
149149

150-
static const ByteVector _decode(const string& s, char separator, size_t word_len) {
150+
static ByteVector _decode(const string& s, char separator, size_t word_len) {
151151
StringVector words;
152152
if(word_len == 4) {
153153
words = split(s, separator);
@@ -186,9 +186,9 @@ string Bytewords::encode(style style, const ByteVector& bytes) {
186186
return encode_with_separator(bytes, "-");
187187
case minimal:
188188
return encode_minimal(bytes);
189-
default:
190-
assert(false);
191189
}
190+
assert(false);
191+
return string();
192192
}
193193

194194
ByteVector Bytewords::decode(style style, const string& string) {
@@ -199,9 +199,9 @@ ByteVector Bytewords::decode(style style, const string& string) {
199199
return _decode(string, '-', 4);
200200
case minimal:
201201
return _decode(string, 0, 2);
202-
default:
203-
assert(false);
204202
}
203+
assert(false);
204+
return ByteVector();
205205
}
206206

207207
}

src/fountain-encoder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77

88
#include "fountain-encoder.hpp"
9-
#include <assert.h>
9+
#include <cassert>
1010
#include <cmath>
1111
#include <optional>
1212
#include <vector>

src/random-sampler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "random-sampler.hpp"
99
#include <numeric>
1010
#include <algorithm>
11-
#include <assert.h>
11+
#include <cassert>
1212

1313
using namespace std;
1414

src/ur-decoder.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
namespace ur {
2020

21-
class URDecoder final {
21+
class URDecoder {
2222
public:
2323
typedef std::optional<std::variant<UR, std::exception> > Result;
2424

@@ -63,4 +63,4 @@ class URDecoder final {
6363

6464
}
6565

66-
#endif // BC_UR_DECODER_HPP
66+
#endif // BC_UR_DECODER_HPP

src/ur-encoder.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
namespace ur {
1717

18-
class UREncoder final {
18+
class UREncoder {
1919
public:
2020
// Encode a single-part UR.
2121
static std::string encode(const UR& ur);
@@ -49,4 +49,4 @@ class UREncoder final {
4949

5050
}
5151

52-
#endif // BC_UR_ENCODER_HPP
52+
#endif // BC_UR_ENCODER_HPP

src/ur.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ using namespace std;
1212

1313
namespace ur {
1414

15-
UR::UR(const std::string &type, const ByteVector &cbor)
16-
: type_(type), cbor_(cbor)
15+
UR::UR(std::string type, ByteVector cbor)
16+
: type_(std::move(type)), cbor_(std::move(cbor))
1717
{}
1818

1919
bool UR::is_valid() const {

src/ur.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace ur {
1616

17-
class UR final {
17+
class UR {
1818
private:
1919
std::string type_;
2020
ByteVector cbor_;
@@ -25,11 +25,11 @@ class UR final {
2525
const ByteVector& cbor() const { return cbor_; }
2626
bool is_valid() const;
2727

28-
UR(const std::string& type, const ByteVector& cbor);
28+
UR(std::string type, ByteVector cbor);
2929
};
3030

3131
bool operator==(const UR& lhs, const UR& rhs);
3232

3333
}
3434

35-
#endif // BC_UR_UR_HPP
35+
#endif // BC_UR_UR_HPP

src/utils.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ extern "C" {
1313

1414
}
1515

16+
#include <array>
1617
#include <vector>
1718
#include <algorithm>
1819
#include <cctype>
@@ -26,34 +27,34 @@ namespace ur {
2627
#define SHA256_LEN 32
2728

2829
ByteVector sha256(const ByteVector &buf) {
29-
uint8_t digest[SHA256_LEN];
30+
array<uint8_t,SHA256_LEN> digest;
3031
mbedtls_sha256_context ctx;
3132
mbedtls_sha256_init(&ctx);
3233
mbedtls_sha256_starts(&ctx, 0);
33-
mbedtls_sha256_update(&ctx, &buf[0], buf.size());
34-
mbedtls_sha256_finish(&ctx, digest);
34+
mbedtls_sha256_update(&ctx, buf.data(), buf.size());
35+
mbedtls_sha256_finish(&ctx, digest.data());
3536
mbedtls_sha256_free(&ctx);
3637

37-
return ByteVector(digest, digest + SHA256_LEN);
38+
return {digest.begin(), digest.end()};
3839
}
3940

4041
ByteVector crc32_bytes(const ByteVector &buf) {
41-
uint32_t checksum = ur_crc32n(&buf[0], buf.size());
42-
auto cbegin = (uint8_t*)&checksum;
43-
auto cend = cbegin + sizeof(uint32_t);
44-
return ByteVector(cbegin, cend);
42+
uint32_t checksum = ur_crc32n(buf.data(), buf.size());
43+
auto *cbegin = (uint8_t*)&checksum;
44+
auto *cend = cbegin + sizeof(uint32_t);
45+
return {cbegin, cend};
4546
}
4647

4748
uint32_t crc32_int(const ByteVector &buf) {
48-
return ur_crc32(&buf[0], buf.size());
49+
return ur_crc32(buf.data(), buf.size());
4950
}
5051

5152
ByteVector string_to_bytes(const string& s) {
52-
return ByteVector(s.begin(), s.end());
53+
return {s.begin(), s.end()};
5354
}
5455

5556
string data_to_hex(const ByteVector& in) {
56-
auto hex = "0123456789abcdef";
57+
const string hex = "0123456789abcdef";
5758
string result;
5859
for(auto c: in) {
5960
result.append(1, hex[(c >> 4) & 0xF]);
@@ -121,7 +122,7 @@ StringVector split(const string& s, char separator) {
121122
}
122123
}
123124

124-
if(buf != "") {
125+
if(!buf.empty()) {
125126
result.push_back(buf);
126127
}
127128

@@ -142,12 +143,12 @@ string take_first(const string &s, size_t count) {
142143
auto first = s.begin();
143144
auto c = min(s.size(), count);
144145
auto last = first + c;
145-
return string(first, last);
146+
return {first, last};
146147
}
147148

148149
string drop_first(const string& s, size_t count) {
149150
if(count >= s.length()) { return ""; }
150-
return string(s.begin() + count, s.end());
151+
return {s.begin() + count, s.end()};
151152
}
152153

153154
void xor_into(ByteVector& target, const ByteVector& source) {

src/utils.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
#include <utility>
1414
#include <string>
1515
#include <array>
16-
#include <assert.h>
16+
#include <cassert>
1717

1818
#include "psram-allocator.hpp"
1919

2020
namespace ur {
2121

22-
typedef std::vector<uint8_t, PSRAMAllocator<uint8_t>> ByteVector;
23-
typedef std::vector<ByteVector, PSRAMAllocator<ByteVector>> ByteVectorVector;
24-
typedef std::vector<std::string, PSRAMAllocator<std::string>> StringVector;
22+
using ByteVector = std::vector<uint8_t, PSRAMAllocator<uint8_t>>;
23+
using ByteVectorVector = std::vector<ByteVector, PSRAMAllocator<ByteVector>>;
24+
using StringVector = std::vector<std::string, PSRAMAllocator<std::string>>;
2525

2626
ByteVector sha256(const ByteVector &buf);
2727
ByteVector crc32_bytes(const ByteVector &buf);

src/xoshiro256.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ void Xoshiro256::jump() {
130130
uint64_t s3 = 0;
131131
for(int i = 0; i < sizeof JUMP / sizeof *JUMP; i++)
132132
for(int b = 0; b < 64; b++) {
133-
if (JUMP[i] & UINT64_C(1) << b) {
133+
if ((JUMP[i] & UINT64_C(1) << b) != 0u) {
134134
s0 ^= s[0];
135135
s1 ^= s[1];
136136
s2 ^= s[2];
@@ -151,13 +151,12 @@ void Xoshiro256::jump() {
151151
subsequences for parallel distributed computations. */
152152

153153
void Xoshiro256::long_jump() {
154-
static const uint64_t LONG_JUMP[] = { 0x76e15d3efefdcbbf, 0xc5004e441c522fb3, 0x77710069854ee241, 0x39109bb02acbe635 };
155-
154+
static const std::array<uint64_t,4> LONG_JUMP = { 0x76e15d3efefdcbbf, 0xc5004e441c522fb3, 0x77710069854ee241, 0x39109bb02acbe635 };
156155
uint64_t s0 = 0;
157156
uint64_t s1 = 0;
158157
uint64_t s2 = 0;
159158
uint64_t s3 = 0;
160-
for(int i = 0; i < sizeof LONG_JUMP / sizeof *LONG_JUMP; i++)
159+
for(int i = 0; i < LONG_JUMP.size(); i++)
161160
for(int b = 0; b < 64; b++) {
162161
if (LONG_JUMP[i] & UINT64_C(1) << b) {
163162
s0 ^= s[0];

0 commit comments

Comments
 (0)