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

tstack / lnav / 11872214087-1756

16 Nov 2024 06:12PM UTC coverage: 70.243% (+0.5%) from 69.712%
11872214087-1756

push

github

tstack
[build] disable regex101

46266 of 65866 relevant lines covered (70.24%)

467515.63 hits per line

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

89.09
/src/byte_array.hh
1
/**
2
 * Copyright (c) 2007-2013, Timothy Stack
3
 *
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
8
 *
9
 * * Redistributions of source code must retain the above copyright notice, this
10
 * list of conditions and the following disclaimer.
11
 * * Redistributions in binary form must reproduce the above copyright notice,
12
 * this list of conditions and the following disclaimer in the documentation
13
 * and/or other materials provided with the distribution.
14
 * * Neither the name of Timothy Stack nor the names of its contributors
15
 * may be used to endorse or promote products derived from this software
16
 * without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29
#ifndef byte_array_hh
30
#define byte_array_hh
31

32
#include <optional>
33
#include <ostream>
34
#include <string>
35

36
#include <stdio.h>
37
#include <string.h>
38
#include <sys/types.h>
39

40
#include "base/lnav_log.hh"
41
#include "fmt/format.h"
42

43
template<size_t COUNT, typename T = unsigned char>
44
struct byte_array {
45
    static constexpr size_t BYTE_COUNT = COUNT * sizeof(T);
46
    static constexpr size_t STRING_SIZE = BYTE_COUNT * 2 + 1;
47

48
    byte_array() = default;
763✔
49

50
    static byte_array from(std::initializer_list<T> bytes)
1✔
51
    {
52
        byte_array retval;
1✔
53
        size_t index = 0;
1✔
54

55
        for (const auto by : bytes) {
7✔
56
            retval.ba_data[index++] = by;
6✔
57
        }
58
        return retval;
1✔
59
    }
60

61
    byte_array(const byte_array& other)
62
    {
63
        memcpy(this->ba_data, other.ba_data, BYTE_COUNT);
64
    }
65

66
    bool operator<(const byte_array& other) const
67
    {
68
        return memcmp(this->ba_data, other.ba_data, BYTE_COUNT) < 0;
69
    }
70

71
    bool operator!=(const byte_array& other) const
49✔
72
    {
73
        return memcmp(this->ba_data, other.ba_data, BYTE_COUNT) != 0;
49✔
74
    }
75

76
    bool operator==(const byte_array& other) const
77
    {
78
        return memcmp(this->ba_data, other.ba_data, BYTE_COUNT) == 0;
79
    }
80

81
    void clear() { memset(this->ba_data, 0, BYTE_COUNT); }
15✔
82

83
    template<typename OutputIt>
84
    void to_string(OutputIt out,
1,533✔
85
                   std::optional<char> separator = std::nullopt) const
86
    {
87
        for (size_t lpc = 0; lpc < BYTE_COUNT; lpc++) {
26,051✔
88
            if (lpc > 0 && separator) {
24,518✔
89
                *out = separator.value();
20✔
90
            }
91
            fmt::format_to(out, FMT_STRING("{:02x}"), this->ba_data[lpc]);
98,072✔
92
        }
24,512✔
93
    }
24,513✔
94

1✔
95
    std::string to_uuid_string() const
1,532✔
96
    {
97
        return fmt::format(
7✔
98
            FMT_STRING("{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-"
6✔
99
                       "{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}"),
5✔
100
            this->ba_data[0 % BYTE_COUNT],
101
            this->ba_data[1 % BYTE_COUNT],
12✔
102
            this->ba_data[2 % BYTE_COUNT],
103
            this->ba_data[3 % BYTE_COUNT],
1✔
104
            this->ba_data[4 % BYTE_COUNT],
×
105
            this->ba_data[5 % BYTE_COUNT],
106
            this->ba_data[6 % BYTE_COUNT],
107
            this->ba_data[7 % BYTE_COUNT],
×
108
            this->ba_data[8 % BYTE_COUNT],
×
109
            this->ba_data[9 % BYTE_COUNT],
×
110
            this->ba_data[10 % BYTE_COUNT],
111
            this->ba_data[11 % BYTE_COUNT],
×
112
            this->ba_data[12 % BYTE_COUNT],
113
            this->ba_data[13 % BYTE_COUNT],
114
            this->ba_data[14 % BYTE_COUNT],
115
            this->ba_data[15 % BYTE_COUNT]);
3✔
116
    }
117

118
    std::string to_string(std::optional<char> separator = std::nullopt) const
6✔
119
    {
3✔
120
        std::string retval;
1,491✔
121

122
        retval.reserve(STRING_SIZE);
1,491✔
123
        this->to_string(std::back_inserter(retval), separator);
3✔
124
        return retval;
1,491✔
125
    }
1,491✔
126

1,491✔
127
    const unsigned char* in() const { return this->ba_data; }
74✔
128

3✔
129
    T* out(int offset = 0)
3✔
130
    {
3✔
131
        T* ptr = (T*) this->ba_data;
3,931✔
132

3✔
133
        return &ptr[offset];
3,931✔
134
    }
3✔
135

3,931✔
136
    unsigned char ba_data[BYTE_COUNT]{};
3✔
137
};
6✔
138

139
template<size_t COUNT, typename T = unsigned char>
140
std::ostream&
1✔
141
operator<<(std::ostream& os, const byte_array<COUNT, T>& ba)
142
{
1✔
143
    os << ba.to_string();
144
    return os;
1✔
145
}
1✔
146

1✔
147
#endif
×
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