| 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 | |
| 35 | using std::string; |
| 36 | using std::equal; |
| 37 | using std::lexicographical_compare; |
| 38 | using std::ostream; |
| 39 | using std::hex; |
| 40 | using std::dec; |
| 41 | |
| 42 | namespace cppkafka { |
| 43 | |
| 44 | Buffer::Buffer() |
| 45 | : data_(nullptr), size_(0) { |
| 46 | |
| 47 | } |
| 48 | |
| 49 | Buffer::Buffer(const string& data) |
| 50 | : Buffer(data.data(), data.size()) { |
| 51 | |
| 52 | } |
| 53 | |
| 54 | const Buffer::DataType* Buffer::get_data() const { |
| 55 | return data_; |
| 56 | } |
| 57 | |
| 58 | size_t Buffer::get_size() const { |
| 59 | return size_; |
| 60 | } |
| 61 | |
| 62 | Buffer::const_iterator Buffer::begin() const { |
| 63 | return data_; |
| 64 | } |
| 65 | |
| 66 | Buffer::const_iterator Buffer::end() const { |
| 67 | return data_ + size_; |
| 68 | } |
| 69 | |
| 70 | Buffer::operator bool() const { |
| 71 | return size_ != 0; |
| 72 | } |
| 73 | |
| 74 | Buffer::operator string() const { |
| 75 | return string(data_, data_ + size_); |
| 76 | } |
| 77 | |
| 78 | ostream& 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 | |
| 94 | bool 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 | |
| 101 | bool operator!=(const Buffer& lhs, const Buffer& rhs) { |
| 102 | return !(lhs == rhs); |
| 103 | } |
| 104 | |
| 105 | bool 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 | |
| 110 | bool 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 | |
| 115 | bool operator<=(const Buffer& lhs, const Buffer& rhs) { |
| 116 | return !(lhs > rhs); |
| 117 | } |
| 118 | |
| 119 | bool operator>=(const Buffer& lhs, const Buffer& rhs) { |
| 120 | return !(lhs < rhs); |
| 121 | } |
| 122 | |
| 123 | } // cppkafka |
| 124 | |