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 #include <kj/common.h>
25 #include <kj/string.h>
26 #include "common.h"
27 #include <string.h>
28 
29 CAPNP_BEGIN_HEADER
30 
31 namespace capnp {
32 
33 struct Data {
34   Data() = delete;
35   class Reader;
36   class Builder;
37   class Pipeline {};
38 };
39 
40 struct Text {
41   Text() = delete;
42   class Reader;
43   class Builder;
44   class Pipeline {};
45 };
46 
47 class Data::Reader: public kj::ArrayPtr<const byte> {
48   // Points to a blob of bytes.  The usual Reader rules apply -- Data::Reader behaves like a simple
49   // pointer which does not own its target, can be passed by value, etc.
50 
51 public:
52   typedef Data Reads;
53 
54   Reader() = default;
Reader(decltype (nullptr))55   inline Reader(decltype(nullptr)): ArrayPtr<const byte>(nullptr) {}
Reader(const byte * value,size_t size)56   inline Reader(const byte* value, size_t size): ArrayPtr<const byte>(value, size) {}
Reader(const kj::Array<const byte> & value)57   inline Reader(const kj::Array<const byte>& value): ArrayPtr<const byte>(value) {}
Reader(const ArrayPtr<const byte> & value)58   inline Reader(const ArrayPtr<const byte>& value): ArrayPtr<const byte>(value) {}
Reader(const kj::Array<byte> & value)59   inline Reader(const kj::Array<byte>& value): ArrayPtr<const byte>(value) {}
Reader(const ArrayPtr<byte> & value)60   inline Reader(const ArrayPtr<byte>& value): ArrayPtr<const byte>(value) {}
61 };
62 
63 class Text::Reader: public kj::StringPtr {
64   // Like Data::Reader, but points at NUL-terminated UTF-8 text.  The NUL terminator is not counted
65   // in the size but must be present immediately after the last byte.
66   //
67   // Text::Reader's interface contract is that its data MUST be NUL-terminated.  The producer of
68   // the Text::Reader must guarantee this, so that the consumer need not check.  The data SHOULD
69   // also be valid UTF-8, but this is NOT guaranteed -- the consumer must verify if it cares.
70 
71 public:
72   typedef Text Reads;
73 
74   Reader() = default;
Reader(decltype (nullptr))75   inline Reader(decltype(nullptr)): StringPtr(nullptr) {}
Reader(const char * value)76   inline Reader(const char* value): StringPtr(value) {}
Reader(const char * value,size_t size)77   inline Reader(const char* value, size_t size): StringPtr(value, size) {}
Reader(const kj::String & value)78   inline Reader(const kj::String& value): StringPtr(value) {}
Reader(const StringPtr & value)79   inline Reader(const StringPtr& value): StringPtr(value) {}
80 
81 #if KJ_COMPILER_SUPPORTS_STL_STRING_INTEROP
82   template <typename T, typename = decltype(kj::instance<T>().c_str())>
Reader(const T & t)83   inline Reader(const T& t): StringPtr(t) {}
84   // Allow implicit conversion from any class that has a c_str() method (namely, std::string).
85   // We use a template trick to detect std::string in order to avoid including the header for
86   // those who don't want it.
87 #endif
88 };
89 
90 class Data::Builder: public kj::ArrayPtr<byte> {
91   // Like Data::Reader except the pointers aren't const.
92 
93 public:
94   typedef Data Builds;
95 
96   Builder() = default;
Builder(decltype (nullptr))97   inline Builder(decltype(nullptr)): ArrayPtr<byte>(nullptr) {}
Builder(byte * value,size_t size)98   inline Builder(byte* value, size_t size): ArrayPtr<byte>(value, size) {}
Builder(kj::Array<byte> & value)99   inline Builder(kj::Array<byte>& value): ArrayPtr<byte>(value) {}
Builder(ArrayPtr<byte> value)100   inline Builder(ArrayPtr<byte> value): ArrayPtr<byte>(value) {}
101 
asReader()102   inline Data::Reader asReader() const {
103     return Data::Reader(kj::implicitCast<const kj::ArrayPtr<byte>&>(*this));
104   }
Reader()105   inline operator Reader() const { return asReader(); }
106 };
107 
108 class Text::Builder: public kj::DisallowConstCopy {
109   // Basically identical to kj::StringPtr, except that the contents are non-const.
110 
111 public:
Builder()112   inline Builder(): content(nulstr, 1) {}
Builder(decltype (nullptr))113   inline Builder(decltype(nullptr)): content(nulstr, 1) {}
Builder(char * value)114   inline Builder(char* value): content(value, strlen(value) + 1) {}
Builder(char * value,size_t size)115   inline Builder(char* value, size_t size): content(value, size + 1) {
116     KJ_IREQUIRE(value[size] == '\0', "StringPtr must be NUL-terminated.");
117   }
118 
asReader()119   inline Reader asReader() const { return Reader(content.begin(), content.size() - 1); }
Reader()120   inline operator Reader() const { return asReader(); }
121 
122   inline operator kj::ArrayPtr<char>();
123   inline kj::ArrayPtr<char> asArray();
124   inline operator kj::ArrayPtr<const char>() const;
125   inline kj::ArrayPtr<const char> asArray() const;
asBytes()126   inline kj::ArrayPtr<byte> asBytes() { return asArray().asBytes(); }
asBytes()127   inline kj::ArrayPtr<const byte> asBytes() const { return asArray().asBytes(); }
128   // Result does not include NUL terminator.
129 
130   inline operator kj::StringPtr() const;
131   inline kj::StringPtr asString() const;
132 
cStr()133   inline const char* cStr() const { return content.begin(); }
134   // Returns NUL-terminated string.
135 
size()136   inline size_t size() const { return content.size() - 1; }
137   // Result does not include NUL terminator.
138 
139   inline char operator[](size_t index) const { return content[index]; }
140   inline char& operator[](size_t index) { return content[index]; }
141 
begin()142   inline char* begin() { return content.begin(); }
end()143   inline char* end() { return content.end() - 1; }
begin()144   inline const char* begin() const { return content.begin(); }
end()145   inline const char* end() const { return content.end() - 1; }
146 
147   inline bool operator==(decltype(nullptr)) const { return content.size() <= 1; }
148   inline bool operator!=(decltype(nullptr)) const { return content.size() > 1; }
149 
150   inline bool operator==(Builder other) const { return asString() == other.asString(); }
151   inline bool operator!=(Builder other) const { return asString() != other.asString(); }
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 
157   inline kj::StringPtr slice(size_t start) const;
158   inline kj::ArrayPtr<const char> slice(size_t start, size_t end) const;
159   inline Builder slice(size_t start);
160   inline kj::ArrayPtr<char> slice(size_t start, size_t end);
161   // A string slice is only NUL-terminated if it is a suffix, so slice() has a one-parameter
162   // version that assumes end = size().
163 
164 private:
Builder(kj::ArrayPtr<char> content)165   inline explicit Builder(kj::ArrayPtr<char> content): content(content) {}
166 
167   kj::ArrayPtr<char> content;
168 
169   static char nulstr[1];
170 };
171 
KJ_STRINGIFY(Text::Builder builder)172 inline kj::StringPtr KJ_STRINGIFY(Text::Builder builder) {
173   return builder.asString();
174 }
175 
176 inline bool operator==(const char* a, const Text::Builder& b) { return a == b.asString(); }
177 inline bool operator!=(const char* a, const Text::Builder& b) { return a != b.asString(); }
178 
StringPtr()179 inline Text::Builder::operator kj::StringPtr() const {
180   return kj::StringPtr(content.begin(), content.size() - 1);
181 }
182 
asString()183 inline kj::StringPtr Text::Builder::asString() const {
184   return kj::StringPtr(content.begin(), content.size() - 1);
185 }
186 
187 inline Text::Builder::operator kj::ArrayPtr<char>() {
188   return content.slice(0, content.size() - 1);
189 }
190 
asArray()191 inline kj::ArrayPtr<char> Text::Builder::asArray() {
192   return content.slice(0, content.size() - 1);
193 }
194 
195 inline Text::Builder::operator kj::ArrayPtr<const char>() const {
196   return content.slice(0, content.size() - 1);
197 }
198 
asArray()199 inline kj::ArrayPtr<const char> Text::Builder::asArray() const {
200   return content.slice(0, content.size() - 1);
201 }
202 
slice(size_t start)203 inline kj::StringPtr Text::Builder::slice(size_t start) const {
204   return asReader().slice(start);
205 }
slice(size_t start,size_t end)206 inline kj::ArrayPtr<const char> Text::Builder::slice(size_t start, size_t end) const {
207   return content.slice(start, end);
208 }
209 
slice(size_t start)210 inline Text::Builder Text::Builder::slice(size_t start) {
211   return Text::Builder(content.slice(start, content.size()));
212 }
slice(size_t start,size_t end)213 inline kj::ArrayPtr<char> Text::Builder::slice(size_t start, size_t end) {
214   return content.slice(start, end);
215 }
216 
217 }  // namespace capnp
218 
219 CAPNP_END_HEADER
220