| 1 | // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors |
| 2 | // Licensed under the MIT License: |
| 3 | // |
| 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | // of this software and associated documentation files (the "Software"), to deal |
| 6 | // in the Software without restriction, including without limitation the rights |
| 7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | // copies of the Software, and to permit persons to whom the Software is |
| 9 | // furnished to do so, subject to the following conditions: |
| 10 | // |
| 11 | // The above copyright notice and this permission notice shall be included in |
| 12 | // all copies or substantial portions of the Software. |
| 13 | // |
| 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | // THE SOFTWARE. |
| 21 | |
| 22 | #pragma once |
| 23 | |
| 24 | #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS) |
| 25 | #pragma GCC system_header |
| 26 | #endif |
| 27 | |
| 28 | #include <kj/common.h> |
| 29 | #include <kj/string.h> |
| 30 | #include "common.h" |
| 31 | #include <string.h> |
| 32 | |
| 33 | namespace capnp { |
| 34 | |
| 35 | struct Data { |
| 36 | Data() = delete; |
| 37 | class Reader; |
| 38 | class Builder; |
| 39 | class Pipeline {}; |
| 40 | }; |
| 41 | |
| 42 | struct Text { |
| 43 | Text() = delete; |
| 44 | class Reader; |
| 45 | class Builder; |
| 46 | class Pipeline {}; |
| 47 | }; |
| 48 | |
| 49 | class Data::Reader: public kj::ArrayPtr<const byte> { |
| 50 | // Points to a blob of bytes. The usual Reader rules apply -- Data::Reader behaves like a simple |
| 51 | // pointer which does not own its target, can be passed by value, etc. |
| 52 | |
| 53 | public: |
| 54 | typedef Data Reads; |
| 55 | |
| 56 | Reader() = default; |
| 57 | inline Reader(decltype(nullptr)): ArrayPtr<const byte>(nullptr) {} |
| 58 | inline Reader(const byte* value, size_t size): ArrayPtr<const byte>(value, size) {} |
| 59 | inline Reader(const kj::Array<const byte>& value): ArrayPtr<const byte>(value) {} |
| 60 | inline Reader(const ArrayPtr<const byte>& value): ArrayPtr<const byte>(value) {} |
| 61 | inline Reader(const kj::Array<byte>& value): ArrayPtr<const byte>(value) {} |
| 62 | inline Reader(const ArrayPtr<byte>& value): ArrayPtr<const byte>(value) {} |
| 63 | }; |
| 64 | |
| 65 | class Text::Reader: public kj::StringPtr { |
| 66 | // Like Data::Reader, but points at NUL-terminated UTF-8 text. The NUL terminator is not counted |
| 67 | // in the size but must be present immediately after the last byte. |
| 68 | // |
| 69 | // Text::Reader's interface contract is that its data MUST be NUL-terminated. The producer of |
| 70 | // the Text::Reader must guarantee this, so that the consumer need not check. The data SHOULD |
| 71 | // also be valid UTF-8, but this is NOT guaranteed -- the consumer must verify if it cares. |
| 72 | |
| 73 | public: |
| 74 | typedef Text Reads; |
| 75 | |
| 76 | Reader() = default; |
| 77 | inline Reader(decltype(nullptr)): StringPtr(nullptr) {} |
| 78 | inline Reader(const char* value): StringPtr(value) {} |
| 79 | inline Reader(const char* value, size_t size): StringPtr(value, size) {} |
| 80 | inline Reader(const kj::String& value): StringPtr(value) {} |
| 81 | inline Reader(const StringPtr& value): StringPtr(value) {} |
| 82 | |
| 83 | #if KJ_COMPILER_SUPPORTS_STL_STRING_INTEROP |
| 84 | template <typename T, typename = decltype(kj::instance<T>().c_str())> |
| 85 | inline Reader(const T& t): StringPtr(t) {} |
| 86 | // Allow implicit conversion from any class that has a c_str() method (namely, std::string). |
| 87 | // We use a template trick to detect std::string in order to avoid including the header for |
| 88 | // those who don't want it. |
| 89 | #endif |
| 90 | }; |
| 91 | |
| 92 | class Data::Builder: public kj::ArrayPtr<byte> { |
| 93 | // Like Data::Reader except the pointers aren't const. |
| 94 | |
| 95 | public: |
| 96 | typedef Data Builds; |
| 97 | |
| 98 | Builder() = default; |
| 99 | inline Builder(decltype(nullptr)): ArrayPtr<byte>(nullptr) {} |
| 100 | inline Builder(byte* value, size_t size): ArrayPtr<byte>(value, size) {} |
| 101 | inline Builder(kj::Array<byte>& value): ArrayPtr<byte>(value) {} |
| 102 | inline Builder(ArrayPtr<byte> value): ArrayPtr<byte>(value) {} |
| 103 | |
| 104 | inline Data::Reader asReader() const { |
| 105 | return Data::Reader(kj::implicitCast<const kj::ArrayPtr<byte>&>(*this)); |
| 106 | } |
| 107 | inline operator Reader() const { return asReader(); } |
| 108 | }; |
| 109 | |
| 110 | class Text::Builder: public kj::DisallowConstCopy { |
| 111 | // Basically identical to kj::StringPtr, except that the contents are non-const. |
| 112 | |
| 113 | public: |
| 114 | inline Builder(): content(nulstr, 1) {} |
| 115 | inline Builder(decltype(nullptr)): content(nulstr, 1) {} |
| 116 | inline Builder(char* value): content(value, strlen(value) + 1) {} |
| 117 | inline Builder(char* value, size_t size): content(value, size + 1) { |
| 118 | KJ_IREQUIRE(value[size] == '\0', "StringPtr must be NUL-terminated." ); |
| 119 | } |
| 120 | |
| 121 | inline Reader asReader() const { return Reader(content.begin(), content.size() - 1); } |
| 122 | inline operator Reader() const { return asReader(); } |
| 123 | |
| 124 | inline operator kj::ArrayPtr<char>(); |
| 125 | inline kj::ArrayPtr<char> asArray(); |
| 126 | inline operator kj::ArrayPtr<const char>() const; |
| 127 | inline kj::ArrayPtr<const char> asArray() const; |
| 128 | inline kj::ArrayPtr<byte> asBytes() { return asArray().asBytes(); } |
| 129 | inline kj::ArrayPtr<const byte> asBytes() const { return asArray().asBytes(); } |
| 130 | // Result does not include NUL terminator. |
| 131 | |
| 132 | inline operator kj::StringPtr() const; |
| 133 | inline kj::StringPtr asString() const; |
| 134 | |
| 135 | inline const char* cStr() const { return content.begin(); } |
| 136 | // Returns NUL-terminated string. |
| 137 | |
| 138 | inline size_t size() const { return content.size() - 1; } |
| 139 | // Result does not include NUL terminator. |
| 140 | |
| 141 | inline char operator[](size_t index) const { return content[index]; } |
| 142 | inline char& operator[](size_t index) { return content[index]; } |
| 143 | |
| 144 | inline char* begin() { return content.begin(); } |
| 145 | inline char* end() { return content.end() - 1; } |
| 146 | inline const char* begin() const { return content.begin(); } |
| 147 | inline const char* end() const { return content.end() - 1; } |
| 148 | |
| 149 | inline bool operator==(decltype(nullptr)) const { return content.size() <= 1; } |
| 150 | inline bool operator!=(decltype(nullptr)) const { return content.size() > 1; } |
| 151 | |
| 152 | inline bool operator==(Builder other) const { return asString() == other.asString(); } |
| 153 | inline bool operator!=(Builder other) const { return asString() != other.asString(); } |
| 154 | inline bool operator< (Builder other) const { return asString() < other.asString(); } |
| 155 | inline bool operator> (Builder other) const { return asString() > other.asString(); } |
| 156 | inline bool operator<=(Builder other) const { return asString() <= other.asString(); } |
| 157 | inline bool operator>=(Builder other) const { return asString() >= other.asString(); } |
| 158 | |
| 159 | inline kj::StringPtr slice(size_t start) const; |
| 160 | inline kj::ArrayPtr<const char> slice(size_t start, size_t end) const; |
| 161 | inline Builder slice(size_t start); |
| 162 | inline kj::ArrayPtr<char> slice(size_t start, size_t end); |
| 163 | // A string slice is only NUL-terminated if it is a suffix, so slice() has a one-parameter |
| 164 | // version that assumes end = size(). |
| 165 | |
| 166 | private: |
| 167 | inline explicit Builder(kj::ArrayPtr<char> content): content(content) {} |
| 168 | |
| 169 | kj::ArrayPtr<char> content; |
| 170 | |
| 171 | static char nulstr[1]; |
| 172 | }; |
| 173 | |
| 174 | inline kj::StringPtr KJ_STRINGIFY(Text::Builder builder) { |
| 175 | return builder.asString(); |
| 176 | } |
| 177 | |
| 178 | inline bool operator==(const char* a, const Text::Builder& b) { return a == b.asString(); } |
| 179 | inline bool operator!=(const char* a, const Text::Builder& b) { return a != b.asString(); } |
| 180 | |
| 181 | inline Text::Builder::operator kj::StringPtr() const { |
| 182 | return kj::StringPtr(content.begin(), content.size() - 1); |
| 183 | } |
| 184 | |
| 185 | inline kj::StringPtr Text::Builder::asString() const { |
| 186 | return kj::StringPtr(content.begin(), content.size() - 1); |
| 187 | } |
| 188 | |
| 189 | inline Text::Builder::operator kj::ArrayPtr<char>() { |
| 190 | return content.slice(0, content.size() - 1); |
| 191 | } |
| 192 | |
| 193 | inline kj::ArrayPtr<char> Text::Builder::asArray() { |
| 194 | return content.slice(0, content.size() - 1); |
| 195 | } |
| 196 | |
| 197 | inline Text::Builder::operator kj::ArrayPtr<const char>() const { |
| 198 | return content.slice(0, content.size() - 1); |
| 199 | } |
| 200 | |
| 201 | inline kj::ArrayPtr<const char> Text::Builder::asArray() const { |
| 202 | return content.slice(0, content.size() - 1); |
| 203 | } |
| 204 | |
| 205 | inline kj::StringPtr Text::Builder::slice(size_t start) const { |
| 206 | return asReader().slice(start); |
| 207 | } |
| 208 | inline kj::ArrayPtr<const char> Text::Builder::slice(size_t start, size_t end) const { |
| 209 | return content.slice(start, end); |
| 210 | } |
| 211 | |
| 212 | inline Text::Builder Text::Builder::slice(size_t start) { |
| 213 | return Text::Builder(content.slice(start, content.size())); |
| 214 | } |
| 215 | inline kj::ArrayPtr<char> Text::Builder::slice(size_t start, size_t end) { |
| 216 | return content.slice(start, end); |
| 217 | } |
| 218 | |
| 219 | } // namespace capnp |
| 220 | |