summaryrefslogtreecommitdiffstats
path: root/ext/protozero/include/protozero/data_view.hpp
blob: 3ec87af34e4c4c9bb3b5bee4602419a9ad52cc63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#ifndef PROTOZERO_DATA_VIEW_HPP
#define PROTOZERO_DATA_VIEW_HPP

/*****************************************************************************

protozero - Minimalistic protocol buffer decoder and encoder in C++.

This file is from https://github.com/mapbox/protozero where you can find more
documentation.

*****************************************************************************/

/**
 * @file data_view.hpp
 *
 * @brief Contains the implementation of the data_view class.
 */

#include "config.hpp"

#include <algorithm>
#include <cstddef>
#include <cstring>
#include <string>
#include <utility>

namespace protozero {

#ifdef PROTOZERO_USE_VIEW
using data_view = PROTOZERO_USE_VIEW;
#else

/**
 * Holds a pointer to some data and a length.
 *
 * This class is supposed to be compatible with the std::string_view
 * that will be available in C++17.
 */
class data_view {

    const char* m_data = nullptr;
    std::size_t m_size = 0;

public:

    /**
     * Default constructor. Construct an empty data_view.
     */
    constexpr data_view() noexcept = default;

    /**
     * Create data_view from pointer and size.
     *
     * @param ptr Pointer to the data.
     * @param length Length of the data.
     */
    constexpr data_view(const char* ptr, std::size_t length) noexcept
        : m_data{ptr},
          m_size{length} {
    }

    /**
     * Create data_view from string.
     *
     * @param str String with the data.
     */
    data_view(const std::string& str) noexcept // NOLINT(google-explicit-constructor, hicpp-explicit-conversions)
        : m_data{str.data()},
          m_size{str.size()} {
    }

    /**
     * Create data_view from zero-terminated string.
     *
     * @param ptr Pointer to the data.
     */
    data_view(const char* ptr) noexcept // NOLINT(google-explicit-constructor, hicpp-explicit-conversions)
        : m_data{ptr},
          m_size{std::strlen(ptr)} {
    }

    /**
     * Swap the contents of this object with the other.
     *
     * @param other Other object to swap data with.
     */
    void swap(data_view& other) noexcept {
        using std::swap;
        swap(m_data, other.m_data);
        swap(m_size, other.m_size);
    }

    /// Return pointer to data.
    constexpr const char* data() const noexcept {
        return m_data;
    }

    /// Return length of data in bytes.
    constexpr std::size_t size() const noexcept {
        return m_size;
    }

    /// Returns true if size is 0.
    constexpr bool empty() const noexcept {
        return m_size == 0;
    }

#ifndef PROTOZERO_STRICT_API
    /**
     * Convert data view to string.
     *
     * @pre Must not be default constructed data_view.
     *
     * @deprecated to_string() is not available in C++17 string_view so it
     *             should not be used to make conversion to that class easier
     *             in the future.
     */
    std::string to_string() const {
        protozero_assert(m_data);
        return {m_data, m_size};
    }
#endif

    /**
     * Convert data view to string.
     *
     * @pre Must not be default constructed data_view.
     */
    explicit operator std::string() const {
        protozero_assert(m_data);
        return {m_data, m_size};
    }

    /**
     * Compares the contents of this object with the given other object.
     *
     * @returns 0 if they are the same, <0 if this object is smaller than
     *          the other or >0 if it is larger. If both objects have the
     *          same size returns <0 if this object is lexicographically
     *          before the other, >0 otherwise.
     *
     * @pre Must not be default constructed data_view.
     */
    int compare(data_view other) const noexcept {
        assert(m_data && other.m_data);
        const int cmp = std::memcmp(data(), other.data(),
                                    std::min(size(), other.size()));
        if (cmp == 0) {
            if (size() == other.size()) {
                return 0;
            }
            return size() < other.size() ? -1 : 1;
        }
        return cmp;
    }

}; // class data_view

/**
 * Swap two data_view objects.
 *
 * @param lhs First object.
 * @param rhs Second object.
 */
inline void swap(data_view& lhs, data_view& rhs) noexcept {
    lhs.swap(rhs);
}

/**
 * Two data_view instances are equal if they have the same size and the
 * same content.
 *
 * @param lhs First object.
 * @param rhs Second object.
 */
inline constexpr bool operator==(const data_view lhs, const data_view rhs) noexcept {
    return lhs.size() == rhs.size() &&
           std::equal(lhs.data(), lhs.data() + lhs.size(), rhs.data());
}

/**
 * Two data_view instances are not equal if they have different sizes or the
 * content differs.
 *
 * @param lhs First object.
 * @param rhs Second object.
 */
inline constexpr bool operator!=(const data_view lhs, const data_view rhs) noexcept {
    return !(lhs == rhs);
}

/**
 * Returns true if lhs.compare(rhs) < 0.
 *
 * @param lhs First object.
 * @param rhs Second object.
 */
inline bool operator<(const data_view lhs, const data_view rhs) noexcept {
    return lhs.compare(rhs) < 0;
}

/**
 * Returns true if lhs.compare(rhs) <= 0.
 *
 * @param lhs First object.
 * @param rhs Second object.
 */
inline bool operator<=(const data_view lhs, const data_view rhs) noexcept {
    return lhs.compare(rhs) <= 0;
}

/**
 * Returns true if lhs.compare(rhs) > 0.
 *
 * @param lhs First object.
 * @param rhs Second object.
 */
inline bool operator>(const data_view lhs, const data_view rhs) noexcept {
    return lhs.compare(rhs) > 0;
}

/**
 * Returns true if lhs.compare(rhs) >= 0.
 *
 * @param lhs First object.
 * @param rhs Second object.
 */
inline bool operator>=(const data_view lhs, const data_view rhs) noexcept {
    return lhs.compare(rhs) >= 0;
}

#endif

} // end namespace protozero

#endif // PROTOZERO_DATA_VIEW_HPP