1/*
2 * Copyright (c) 2017, Matias Fontanini
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30#include <algorithm>
31#include <iostream>
32#include <iomanip>
33#include "buffer.h"
34
35using std::string;
36using std::equal;
37using std::lexicographical_compare;
38using std::ostream;
39using std::hex;
40using std::dec;
41
42namespace cppkafka {
43
44Buffer::Buffer()
45: data_(nullptr), size_(0) {
46
47}
48
49Buffer::Buffer(const string& data)
50: Buffer(data.data(), data.size()) {
51
52}
53
54const Buffer::DataType* Buffer::get_data() const {
55 return data_;
56}
57
58size_t Buffer::get_size() const {
59 return size_;
60}
61
62Buffer::const_iterator Buffer::begin() const {
63 return data_;
64}
65
66Buffer::const_iterator Buffer::end() const {
67 return data_ + size_;
68}
69
70Buffer::operator bool() const {
71 return size_ != 0;
72}
73
74Buffer::operator string() const {
75 return string(data_, data_ + size_);
76}
77
78ostream& operator<<(ostream& output, const Buffer& rhs) {
79 for (const uint8_t value : rhs) {
80 if (value >= 0x20 && value < 0x7f) {
81 output << value;
82 }
83 else {
84 output << "\\x";
85 if (value < 16) {
86 output << '0';
87 }
88 output << hex << static_cast<int>(value) << dec;
89 }
90 }
91 return output;
92}
93
94bool operator==(const Buffer& lhs, const Buffer& rhs) {
95 if (lhs.get_size() != rhs.get_size()) {
96 return false;
97 }
98 return equal(lhs.get_data(), lhs.get_data() + lhs.get_size(), rhs.get_data());
99}
100
101bool operator!=(const Buffer& lhs, const Buffer& rhs) {
102 return !(lhs == rhs);
103}
104
105bool operator<(const Buffer& lhs, const Buffer& rhs) {
106 return lexicographical_compare(lhs.get_data(), lhs.get_data() + lhs.get_size(),
107 rhs.get_data(), rhs.get_data() + rhs.get_size());
108}
109
110bool operator>(const Buffer& lhs, const Buffer& rhs) {
111 return lexicographical_compare(rhs.get_data(), rhs.get_data() + rhs.get_size(),
112 lhs.get_data(), lhs.get_data() + lhs.get_size());
113}
114
115bool operator<=(const Buffer& lhs, const Buffer& rhs) {
116 return !(lhs > rhs);
117}
118
119bool operator>=(const Buffer& lhs, const Buffer& rhs) {
120 return !(lhs < rhs);
121}
122
123} // cppkafka
124