Skip to content

Commit 97c0877

Browse files
committed
removing span as it doesn't seem to be on GCC.
1 parent 2925a49 commit 97c0877

File tree

4 files changed

+56
-36
lines changed

4 files changed

+56
-36
lines changed

src/core/CalChartFileFormat.h

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,19 @@ namespace Parser {
366366
class Reader
367367
{
368368
public:
369-
Reader(std::span<uint8_t const> data, uint32_t version = kVersion) : data(data), version(version) {}
369+
Reader(std::pair<uint8_t const*, size_t> data, uint32_t version = kVersion) : data(data), version(version) {}
370370

371-
auto size() const { return data.size(); }
372-
auto first(std::size_t n) const { auto copy = *this; copy.data = copy.data.first(n); return copy; }
373-
auto subspan(std::size_t n) const { auto copy = *this; copy.data = copy.data.subspan(n); return copy; }
371+
auto size() const { return data.second; }
372+
auto first(std::size_t n) const
373+
{
374+
if (size() < n) {
375+
throw std::runtime_error(std::string("not enough data for first. Need ") + std::to_string(n) + ", currently have " + std::to_string(size()));
376+
}
377+
auto copy = *this;
378+
copy.data.second = n;
379+
return copy;
380+
}
381+
auto subspan(std::size_t n) const { auto copy = *this; copy.subspanImpl(n); return copy; }
374382

375383
template <typename T>
376384
T Peek() const;
@@ -379,54 +387,54 @@ class Reader
379387
T Get()
380388
{
381389
auto result = Peek<T>();
382-
data = data.subspan(sizeof(T));
390+
subspanImpl(sizeof(T));
383391
return result;
384392
}
385393

386394
template <>
387395
uint8_t Peek<uint8_t>() const
388396
{
389-
if (data.size() < 1) {
390-
throw std::runtime_error(std::string("not enough data for uint8_t. Need 1, currently have ") + std::to_string(data.size()));
397+
if (size() < 1) {
398+
throw std::runtime_error(std::string("not enough data for uint8_t. Need 1, currently have ") + std::to_string(size()));
391399
}
392-
return (data[0] & 0xFF);
400+
return (data.first[0] & 0xFF);
393401
}
394402

395403
template <>
396404
uint16_t Peek<uint16_t>() const
397405
{
398-
if (data.size() < 2) {
399-
throw std::runtime_error(std::string("not enough data for uint16_t. Need 2, currently have ") + std::to_string(data.size()));
406+
if (size() < 2) {
407+
throw std::runtime_error(std::string("not enough data for uint16_t. Need 2, currently have ") + std::to_string(size()));
400408
}
401-
return ((data[0] & 0xFF) << 8) | (data[1] & 0xFF);
409+
return ((data.first[0] & 0xFF) << 8) | (data.first[1] & 0xFF);
402410
}
403411

404412
template <>
405413
uint32_t Peek<uint32_t>() const
406414
{
407-
if (data.size() < 4) {
408-
throw std::runtime_error(std::string("not enough data for uint32_t. Need 4, currently have ") + std::to_string(data.size()));
415+
if (size() < 4) {
416+
throw std::runtime_error(std::string("not enough data for uint32_t. Need 4, currently have ") + std::to_string(size()));
409417
}
410-
return ((data[0] & 0xFF) << 24) | ((data[1] & 0xFF) << 16) | ((data[2] & 0xFF) << 8) | ((data[3] & 0xFF));
418+
return ((data.first[0] & 0xFF) << 24) | ((data.first[1] & 0xFF) << 16) | ((data.first[2] & 0xFF) << 8) | ((data.first[3] & 0xFF));
411419
}
412420

413421
template <>
414422
int32_t Peek<int32_t>() const
415423
{
416-
if (data.size() < 4) {
417-
throw std::runtime_error(std::string("not enough data for int32_t. Need 4, currently have ") + std::to_string(data.size()));
424+
if (size() < 4) {
425+
throw std::runtime_error(std::string("not enough data for int32_t. Need 4, currently have ") + std::to_string(size()));
418426
}
419-
return ((data[0] & 0xFF) << 24) | ((data[1] & 0xFF) << 16) | ((data[2] & 0xFF) << 8) | ((data[3] & 0xFF));
427+
return ((data.first[0] & 0xFF) << 24) | ((data.first[1] & 0xFF) << 16) | ((data.first[2] & 0xFF) << 8) | ((data.first[3] & 0xFF));
420428
}
421429

422430
template <>
423431
float Peek<float>() const
424432
{
425-
if (data.size() < sizeof(float)) {
426-
throw std::runtime_error(std::string("not enough data for float. Need " + std::to_string(sizeof(float)) + ", currently have ") + std::to_string(data.size()));
433+
if (size() < sizeof(float)) {
434+
throw std::runtime_error(std::string("not enough data for float. Need " + std::to_string(sizeof(float)) + ", currently have ") + std::to_string(size()));
427435
}
428436
unsigned char rawd[sizeof(float)];
429-
std::copy(data.data(), data.data() + sizeof(float), rawd);
437+
std::copy(data.first, data.first + sizeof(float), rawd);
430438
float result = 0.f;
431439
void* fptr = &result;
432440
std::memcpy(fptr, rawd, sizeof(float));
@@ -436,41 +444,41 @@ class Reader
436444
template <typename T>
437445
std::vector<T> GetVector()
438446
{
439-
if (data.size() < 4) {
440-
throw std::runtime_error(std::string("not enough data for vector size. Need 4, currently have ") + std::to_string(data.size()));
447+
if (size() < 4) {
448+
throw std::runtime_error(std::string("not enough data for vector size. Need 4, currently have ") + std::to_string(size()));
441449
}
442450
auto size = Get<uint32_t>();
443-
auto result = std::vector<T>(data.data(), data.data()+size);
444-
data = data.subspan(size);
451+
auto result = std::vector<T>(data.first, data.first+size);
452+
subspanImpl(size);
445453
return result;
446454
}
447455

448456
template <>
449457
std::string Get<std::string>()
450458
{
451-
auto result = std::string(reinterpret_cast<char const*>(data.data()));
452-
if (data.size() < (result.size() + 1)) {
453-
throw std::runtime_error(std::string("not enough data for string. Need " + std::to_string(result.size() + 1) + ", currently have ") + std::to_string(data.size()));
459+
auto result = std::string(reinterpret_cast<char const*>(data.first));
460+
if (size() < (result.size() + 1)) {
461+
throw std::runtime_error(std::string("not enough data for string. Need " + std::to_string(result.size() + 1) + ", currently have ") + std::to_string(size()));
454462
}
455-
data = data.subspan(result.size()+1); // +1 for the null terminator
463+
subspanImpl(result.size()+1); // +1 for the null terminator
456464
return result;
457465
}
458466

459467
std::vector<std::tuple<uint32_t, Reader>> ParseOutLabels()
460468
{
461469
std::vector<std::tuple<uint32_t, Reader>> result;
462-
while (data.size()) {
463-
auto length = data.size();
470+
while (size()) {
471+
auto length = size();
464472
if (length < 8) {
465473
return result;
466474
}
467475
auto name = Get<uint32_t>();
468476
auto size = Get<uint32_t>();
469-
if (data.size() < size + 8) {
477+
if (data.second < size + 8) {
470478
return result;
471479
}
472480
auto reader = first(size);
473-
data = data.subspan(size);
481+
subspanImpl(size);
474482
auto end = Get<uint32_t>();
475483
auto end_name = Get<uint32_t>();
476484
if ((end != INGL_END) || (end_name != name)) {
@@ -525,7 +533,19 @@ class Reader
525533
}
526534

527535
private:
528-
std::span<uint8_t const> data;
536+
537+
// privateSubspan
538+
void subspanImpl(std::size_t n)
539+
{
540+
if (data.second < n) {
541+
throw std::runtime_error(std::string("not enough data for subspan. Need ") + std::to_string(n) + ", currently have " + std::to_string(size()));
542+
}
543+
data.first += n;
544+
data.second -= n;
545+
}
546+
547+
548+
std::pair<uint8_t const*, size_t> data;
529549
uint32_t version;
530550
};
531551

src/core/CalChartSheet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ Sheet::Sheet(Version_3_3_and_earlier, size_t numPoints, Reader& reader, ParseErr
168168
throw CC_FileException("bad POS chunk");
169169
}
170170
{
171-
auto reader = CalChart::Reader(data);
171+
auto reader = CalChart::Reader({data.data(), data.size()});
172172
for (unsigned i = 0; i < mPoints.size(); ++i) {
173173
auto c = Coord(reader.Get<uint16_t>(), reader.Get<uint16_t>());
174174
for (unsigned j = 0; j <= Point::kNumRefPoints; j++) {

src/core/CalChartShowMode.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ void ShowMode_UnitTests()
193193
{
194194
auto uut1 = ShowMode::GetDefaultShowMode();
195195
auto data = uut1.Serialize();
196-
auto uut2 = ShowMode::CreateShowMode(CalChart::Reader({data.data(), data.data()+data.size()}));
196+
auto uut2 = ShowMode::CreateShowMode(CalChart::Reader({data.data(), data.size()}));
197197
assert(uut1 == uut2);
198198
}
199199

tests/CalChartFileFormatTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
TEST_CASE( "CalChartParserBasics" ) {
55
auto a = std::vector<uint8_t>{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
66

7-
auto reader = CalChart::Reader(std::span{a});
7+
auto reader = CalChart::Reader({a.data(), a.size()});
88
CHECK(reader.Peek<uint16_t>() == 0x0001);
99
CHECK(reader.Peek<uint32_t>() == 0x00010203);
1010
CHECK(reader.Peek<uint16_t>() == 0x0001);

0 commit comments

Comments
 (0)