• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

afxres / binary-cxx / 12989139270

27 Jan 2025 12:35PM UTC coverage: 94.521% (+0.2%) from 94.291%
12989139270

push

github

afxres
Add shared exception functions

17 of 19 new or added lines in 6 files covered. (89.47%)

483 of 511 relevant lines covered (94.52%)

65.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.5
/next/binary/src/internal/Length.cpp
1
#include "binary/internal/Length.hpp"
2

3
#include <cassert>
4
#include <cstdint>
5
#include <stdexcept>
6

7
#include "binary/internal/Endian.hpp"
8
#include "binary/internal/Exception.hpp"
9

10
namespace binary::internal {
11
const std::byte* EnsureLength(const std::span<const std::byte>& span, const size_t length) {
67✔
12
    if (span.size() < length) {
67✔
NEW
13
        internal::ThrowNotEnoughBytes();
×
14
    }
15
    return span.data();
67✔
16
}
17

18
void EnsureLengthPrefixLength(const size_t number) {
41✔
19
    if (number > INT32_MAX) {
41✔
20
        throw std::invalid_argument("number > INT32_MAX");
4✔
21
    }
22
}
37✔
23

24
size_t EncodeLengthPrefixLength(const size_t number) {
88✔
25
    assert(number <= INT32_MAX);
88✔
26
    if ((static_cast<uint32_t>(number) >> 7) == 0) {
88✔
27
        return 1;
61✔
28
    } else {
29
        return 4;
27✔
30
    }
31
}
32

33
size_t DecodeLengthPrefixLength(const std::byte header) {
×
34
    if ((static_cast<uint32_t>(header) & 0x80U) == 0) {
×
35
        return 1;
×
36
    } else {
37
        return 4;
×
38
    }
39
}
40

41
void EncodeLengthPrefix(std::byte* buffer, const size_t number, const size_t length) {
51✔
42
    assert(number <= INT32_MAX);
51✔
43
    assert(length == 1 || length == 4);
51✔
44
    assert(length >= EncodeLengthPrefixLength(number));
51✔
45
    if (length == 1) {
51✔
46
        EncodeBigEndian<uint8_t>(buffer, static_cast<uint8_t>(number));
40✔
47
    } else {
48
        EncodeBigEndian<uint32_t>(buffer, static_cast<uint32_t>(number) | 0x8000'0000);
11✔
49
    }
50
}
51✔
51

52
size_t DecodeLengthPrefix(const std::byte* buffer, size_t& offset, const size_t limits) {
69✔
53
    assert(limits >= offset);
69✔
54
    if (limits == offset) {
69✔
55
        internal::ThrowNotEnoughBytes();
2✔
56
    }
57
    const std::byte* source = buffer + offset;
67✔
58
    uint32_t header = static_cast<uint32_t>(DecodeBigEndian<uint8_t>(source));
67✔
59
    offset += 1;
67✔
60
    if ((header & 0x80U) == 0U) {
67✔
61
        return header;
50✔
62
    }
63
    assert(limits >= offset);
17✔
64
    if (limits < offset + 3U) {
17✔
65
        internal::ThrowNotEnoughBytes();
6✔
66
    }
67
    uint32_t result = DecodeBigEndian<uint32_t>(source);
11✔
68
    offset += 3;
11✔
69
    return result & 0x7FFF'FFFU;
11✔
70
}
71
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc