Skip to content

Commit b97e74b

Browse files
authored
Merge pull request #659 from ckormanyos/issue658
Fix #658 via Issue658 branch
2 parents b79cbc0 + 0ecc2ab commit b97e74b

1 file changed

Lines changed: 31 additions & 11 deletions

File tree

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,58 @@
11
/////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 2018 - 2023.
2+
// Copyright Christopher Kormanyos 2018 - 2025.
33
// Distributed under the Boost Software License,
44
// Version 1.0. (See accompanying file LICENSE_1_0.txt
55
// or copy at http://www.boost.org/LICENSE_1_0.txt)
66
//
77

8+
// Repaired in: https://github.com/ckormanyos/real-time-cpp/issues/658
9+
810
// chapter03_21-001_span.cpp
911

12+
#include <algorithm>
1013
#include <cstdint>
14+
#include <iomanip>
1115
#include <iostream>
1216
#include <iterator>
1317
#include <span>
18+
#include <sstream>
1419

15-
void clear_buffer(std::span<std::uint8_t> pb)
20+
namespace
1621
{
17-
for(auto u : pb)
22+
void clear_buffer(std::span<std::uint8_t> buf)
1823
{
19-
u = static_cast<std::uint8_t>(UINT8_C(0));
20-
21-
static_cast<void>(u);
24+
for(auto& next_byte : buf)
25+
{
26+
next_byte = static_cast<std::uint8_t>(UINT8_C(0));
27+
}
2228
}
2329
}
2430

25-
#define LEGACY_BUFFER_LENGTH 64U
31+
#define LEGACY_BUF_LEN 64U
2632

27-
std::uint8_t legacy_buffer[LEGACY_BUFFER_LENGTH];
33+
std::uint8_t legacy_buffer[LEGACY_BUF_LEN];
2834

2935
void do_something()
3036
{
31-
clear_buffer(legacy_buffer);
37+
std::span<std::uint8_t> buf_span(legacy_buffer);
38+
39+
// First off, fill the legacy buffer with a pattern.
40+
std::fill(buf_span.begin(), buf_span.end(), UINT8_C(0x5A));
41+
42+
std::stringstream strm { };
43+
44+
// Ensure that the buffer has the pattern.
45+
strm << "filled: " << std::hex << std::uppercase << std::setw(std::streamsize { 2 }) << std::setfill('0') << unsigned(buf_span.back());
46+
47+
// Now clear the buffer and the buffer has been cleared to 0.
48+
clear_buffer(buf_span);
49+
50+
strm << "\ncleared: " << std::hex << std::uppercase << std::setw(std::streamsize { 2 }) << std::setfill('0') << unsigned(buf_span.back());
51+
52+
std::cout << strm.str() << std::endl;
3253
}
3354

3455
int main()
3556
{
36-
// 0
37-
std::cout << unsigned(*(std::cend(legacy_buffer) - 1U)) << std::endl;
57+
do_something();
3858
}

0 commit comments

Comments
 (0)