annotate osx/include/kj/string.h @ 49:3ab5a40c4e3b

Add Capnp and KJ builds for OSX
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 25 Oct 2016 14:48:23 +0100
parents
children 0994c39f1e94
rev   line source
cannam@49 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
cannam@49 2 // Licensed under the MIT License:
cannam@49 3 //
cannam@49 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@49 5 // of this software and associated documentation files (the "Software"), to deal
cannam@49 6 // in the Software without restriction, including without limitation the rights
cannam@49 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@49 8 // copies of the Software, and to permit persons to whom the Software is
cannam@49 9 // furnished to do so, subject to the following conditions:
cannam@49 10 //
cannam@49 11 // The above copyright notice and this permission notice shall be included in
cannam@49 12 // all copies or substantial portions of the Software.
cannam@49 13 //
cannam@49 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@49 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@49 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@49 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@49 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@49 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@49 20 // THE SOFTWARE.
cannam@49 21
cannam@49 22 #ifndef KJ_STRING_H_
cannam@49 23 #define KJ_STRING_H_
cannam@49 24
cannam@49 25 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
cannam@49 26 #pragma GCC system_header
cannam@49 27 #endif
cannam@49 28
cannam@49 29 #include <initializer_list>
cannam@49 30 #include "array.h"
cannam@49 31 #include <string.h>
cannam@49 32
cannam@49 33 namespace kj {
cannam@49 34
cannam@49 35 class StringPtr;
cannam@49 36 class String;
cannam@49 37
cannam@49 38 class StringTree; // string-tree.h
cannam@49 39
cannam@49 40 // Our STL string SFINAE trick does not work with GCC 4.7, but it works with Clang and GCC 4.8, so
cannam@49 41 // we'll just preprocess it out if not supported.
cannam@49 42 #if __clang__ || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || _MSC_VER
cannam@49 43 #define KJ_COMPILER_SUPPORTS_STL_STRING_INTEROP 1
cannam@49 44 #endif
cannam@49 45
cannam@49 46 // =======================================================================================
cannam@49 47 // StringPtr -- A NUL-terminated ArrayPtr<const char> containing UTF-8 text.
cannam@49 48 //
cannam@49 49 // NUL bytes are allowed to appear before the end of the string. The only requirement is that
cannam@49 50 // a NUL byte appear immediately after the last byte of the content. This terminator byte is not
cannam@49 51 // counted in the string's size.
cannam@49 52
cannam@49 53 class StringPtr {
cannam@49 54 public:
cannam@49 55 inline StringPtr(): content("", 1) {}
cannam@49 56 inline StringPtr(decltype(nullptr)): content("", 1) {}
cannam@49 57 inline StringPtr(const char* value): content(value, strlen(value) + 1) {}
cannam@49 58 inline StringPtr(const char* value, size_t size): content(value, size + 1) {
cannam@49 59 KJ_IREQUIRE(value[size] == '\0', "StringPtr must be NUL-terminated.");
cannam@49 60 }
cannam@49 61 inline StringPtr(const char* begin, const char* end): StringPtr(begin, end - begin) {}
cannam@49 62 inline StringPtr(const String& value);
cannam@49 63
cannam@49 64 #if KJ_COMPILER_SUPPORTS_STL_STRING_INTEROP
cannam@49 65 template <typename T, typename = decltype(instance<T>().c_str())>
cannam@49 66 inline StringPtr(const T& t): StringPtr(t.c_str()) {}
cannam@49 67 // Allow implicit conversion from any class that has a c_str() method (namely, std::string).
cannam@49 68 // We use a template trick to detect std::string in order to avoid including the header for
cannam@49 69 // those who don't want it.
cannam@49 70
cannam@49 71 template <typename T, typename = decltype(instance<T>().c_str())>
cannam@49 72 inline operator T() const { return cStr(); }
cannam@49 73 // Allow implicit conversion to any class that has a c_str() method (namely, std::string).
cannam@49 74 // We use a template trick to detect std::string in order to avoid including the header for
cannam@49 75 // those who don't want it.
cannam@49 76 #endif
cannam@49 77
cannam@49 78 inline operator ArrayPtr<const char>() const;
cannam@49 79 inline ArrayPtr<const char> asArray() const;
cannam@49 80 inline ArrayPtr<const byte> asBytes() const { return asArray().asBytes(); }
cannam@49 81 // Result does not include NUL terminator.
cannam@49 82
cannam@49 83 inline const char* cStr() const { return content.begin(); }
cannam@49 84 // Returns NUL-terminated string.
cannam@49 85
cannam@49 86 inline size_t size() const { return content.size() - 1; }
cannam@49 87 // Result does not include NUL terminator.
cannam@49 88
cannam@49 89 inline char operator[](size_t index) const { return content[index]; }
cannam@49 90
cannam@49 91 inline const char* begin() const { return content.begin(); }
cannam@49 92 inline const char* end() const { return content.end() - 1; }
cannam@49 93
cannam@49 94 inline bool operator==(decltype(nullptr)) const { return content.size() <= 1; }
cannam@49 95 inline bool operator!=(decltype(nullptr)) const { return content.size() > 1; }
cannam@49 96
cannam@49 97 inline bool operator==(const StringPtr& other) const;
cannam@49 98 inline bool operator!=(const StringPtr& other) const { return !(*this == other); }
cannam@49 99 inline bool operator< (const StringPtr& other) const;
cannam@49 100 inline bool operator> (const StringPtr& other) const { return other < *this; }
cannam@49 101 inline bool operator<=(const StringPtr& other) const { return !(other < *this); }
cannam@49 102 inline bool operator>=(const StringPtr& other) const { return !(*this < other); }
cannam@49 103
cannam@49 104 inline StringPtr slice(size_t start) const;
cannam@49 105 inline ArrayPtr<const char> slice(size_t start, size_t end) const;
cannam@49 106 // A string slice is only NUL-terminated if it is a suffix, so slice() has a one-parameter
cannam@49 107 // version that assumes end = size().
cannam@49 108
cannam@49 109 inline bool startsWith(const StringPtr& other) const;
cannam@49 110 inline bool endsWith(const StringPtr& other) const;
cannam@49 111
cannam@49 112 inline Maybe<size_t> findFirst(char c) const;
cannam@49 113 inline Maybe<size_t> findLast(char c) const;
cannam@49 114
cannam@49 115 template <typename T>
cannam@49 116 T parseAs() const;
cannam@49 117 // Parse string as template number type.
cannam@49 118 // Integer numbers prefixed by "0x" and "0X" are parsed in base 16 (like strtoi with base 0).
cannam@49 119 // Integer numbers prefixed by "0" are parsed in base 10 (unlike strtoi with base 0).
cannam@49 120 // Overflowed integer numbers throw exception.
cannam@49 121 // Overflowed floating numbers return inf.
cannam@49 122
cannam@49 123 private:
cannam@49 124 inline StringPtr(ArrayPtr<const char> content): content(content) {}
cannam@49 125
cannam@49 126 ArrayPtr<const char> content;
cannam@49 127 };
cannam@49 128
cannam@49 129 inline bool operator==(const char* a, const StringPtr& b) { return b == a; }
cannam@49 130 inline bool operator!=(const char* a, const StringPtr& b) { return b != a; }
cannam@49 131
cannam@49 132 template <> char StringPtr::parseAs<char>() const;
cannam@49 133 template <> signed char StringPtr::parseAs<signed char>() const;
cannam@49 134 template <> unsigned char StringPtr::parseAs<unsigned char>() const;
cannam@49 135 template <> short StringPtr::parseAs<short>() const;
cannam@49 136 template <> unsigned short StringPtr::parseAs<unsigned short>() const;
cannam@49 137 template <> int StringPtr::parseAs<int>() const;
cannam@49 138 template <> unsigned StringPtr::parseAs<unsigned>() const;
cannam@49 139 template <> long StringPtr::parseAs<long>() const;
cannam@49 140 template <> unsigned long StringPtr::parseAs<unsigned long>() const;
cannam@49 141 template <> long long StringPtr::parseAs<long long>() const;
cannam@49 142 template <> unsigned long long StringPtr::parseAs<unsigned long long>() const;
cannam@49 143 template <> float StringPtr::parseAs<float>() const;
cannam@49 144 template <> double StringPtr::parseAs<double>() const;
cannam@49 145
cannam@49 146 // =======================================================================================
cannam@49 147 // String -- A NUL-terminated Array<char> containing UTF-8 text.
cannam@49 148 //
cannam@49 149 // NUL bytes are allowed to appear before the end of the string. The only requirement is that
cannam@49 150 // a NUL byte appear immediately after the last byte of the content. This terminator byte is not
cannam@49 151 // counted in the string's size.
cannam@49 152 //
cannam@49 153 // To allocate a String, you must call kj::heapString(). We do not implement implicit copying to
cannam@49 154 // the heap because this hides potential inefficiency from the developer.
cannam@49 155
cannam@49 156 class String {
cannam@49 157 public:
cannam@49 158 String() = default;
cannam@49 159 inline String(decltype(nullptr)): content(nullptr) {}
cannam@49 160 inline String(char* value, size_t size, const ArrayDisposer& disposer);
cannam@49 161 // Does not copy. `size` does not include NUL terminator, but `value` must be NUL-terminated.
cannam@49 162 inline explicit String(Array<char> buffer);
cannam@49 163 // Does not copy. Requires `buffer` ends with `\0`.
cannam@49 164
cannam@49 165 inline operator ArrayPtr<char>();
cannam@49 166 inline operator ArrayPtr<const char>() const;
cannam@49 167 inline ArrayPtr<char> asArray();
cannam@49 168 inline ArrayPtr<const char> asArray() const;
cannam@49 169 inline ArrayPtr<byte> asBytes() { return asArray().asBytes(); }
cannam@49 170 inline ArrayPtr<const byte> asBytes() const { return asArray().asBytes(); }
cannam@49 171 // Result does not include NUL terminator.
cannam@49 172
cannam@49 173 inline const char* cStr() const;
cannam@49 174
cannam@49 175 inline size_t size() const;
cannam@49 176 // Result does not include NUL terminator.
cannam@49 177
cannam@49 178 inline char operator[](size_t index) const;
cannam@49 179 inline char& operator[](size_t index);
cannam@49 180
cannam@49 181 inline char* begin();
cannam@49 182 inline char* end();
cannam@49 183 inline const char* begin() const;
cannam@49 184 inline const char* end() const;
cannam@49 185
cannam@49 186 inline bool operator==(decltype(nullptr)) const { return content.size() <= 1; }
cannam@49 187 inline bool operator!=(decltype(nullptr)) const { return content.size() > 1; }
cannam@49 188
cannam@49 189 inline bool operator==(const StringPtr& other) const { return StringPtr(*this) == other; }
cannam@49 190 inline bool operator!=(const StringPtr& other) const { return StringPtr(*this) != other; }
cannam@49 191 inline bool operator< (const StringPtr& other) const { return StringPtr(*this) < other; }
cannam@49 192 inline bool operator> (const StringPtr& other) const { return StringPtr(*this) > other; }
cannam@49 193 inline bool operator<=(const StringPtr& other) const { return StringPtr(*this) <= other; }
cannam@49 194 inline bool operator>=(const StringPtr& other) const { return StringPtr(*this) >= other; }
cannam@49 195
cannam@49 196 inline bool startsWith(const StringPtr& other) const { return StringPtr(*this).startsWith(other);}
cannam@49 197 inline bool endsWith(const StringPtr& other) const { return StringPtr(*this).endsWith(other); }
cannam@49 198
cannam@49 199 inline StringPtr slice(size_t start) const { return StringPtr(*this).slice(start); }
cannam@49 200 inline ArrayPtr<const char> slice(size_t start, size_t end) const {
cannam@49 201 return StringPtr(*this).slice(start, end);
cannam@49 202 }
cannam@49 203
cannam@49 204 inline Maybe<size_t> findFirst(char c) const { return StringPtr(*this).findFirst(c); }
cannam@49 205 inline Maybe<size_t> findLast(char c) const { return StringPtr(*this).findLast(c); }
cannam@49 206
cannam@49 207 template <typename T>
cannam@49 208 T parseAs() const { return StringPtr(*this).parseAs<T>(); }
cannam@49 209 // Parse as number
cannam@49 210
cannam@49 211 private:
cannam@49 212 Array<char> content;
cannam@49 213 };
cannam@49 214
cannam@49 215 inline bool operator==(const char* a, const String& b) { return b == a; }
cannam@49 216 inline bool operator!=(const char* a, const String& b) { return b != a; }
cannam@49 217
cannam@49 218 String heapString(size_t size);
cannam@49 219 // Allocate a String of the given size on the heap, not including NUL terminator. The NUL
cannam@49 220 // terminator will be initialized automatically but the rest of the content is not initialized.
cannam@49 221
cannam@49 222 String heapString(const char* value);
cannam@49 223 String heapString(const char* value, size_t size);
cannam@49 224 String heapString(StringPtr value);
cannam@49 225 String heapString(const String& value);
cannam@49 226 String heapString(ArrayPtr<const char> value);
cannam@49 227 // Allocates a copy of the given value on the heap.
cannam@49 228
cannam@49 229 // =======================================================================================
cannam@49 230 // Magic str() function which transforms parameters to text and concatenates them into one big
cannam@49 231 // String.
cannam@49 232
cannam@49 233 namespace _ { // private
cannam@49 234
cannam@49 235 inline size_t sum(std::initializer_list<size_t> nums) {
cannam@49 236 size_t result = 0;
cannam@49 237 for (auto num: nums) {
cannam@49 238 result += num;
cannam@49 239 }
cannam@49 240 return result;
cannam@49 241 }
cannam@49 242
cannam@49 243 inline char* fill(char* ptr) { return ptr; }
cannam@49 244
cannam@49 245 template <typename... Rest>
cannam@49 246 char* fill(char* __restrict__ target, const StringTree& first, Rest&&... rest);
cannam@49 247 // Make str() work with stringifiers that return StringTree by patching fill().
cannam@49 248 //
cannam@49 249 // Defined in string-tree.h.
cannam@49 250
cannam@49 251 template <typename First, typename... Rest>
cannam@49 252 char* fill(char* __restrict__ target, const First& first, Rest&&... rest) {
cannam@49 253 auto i = first.begin();
cannam@49 254 auto end = first.end();
cannam@49 255 while (i != end) {
cannam@49 256 *target++ = *i++;
cannam@49 257 }
cannam@49 258 return fill(target, kj::fwd<Rest>(rest)...);
cannam@49 259 }
cannam@49 260
cannam@49 261 template <typename... Params>
cannam@49 262 String concat(Params&&... params) {
cannam@49 263 // Concatenate a bunch of containers into a single Array. The containers can be anything that
cannam@49 264 // is iterable and whose elements can be converted to `char`.
cannam@49 265
cannam@49 266 String result = heapString(sum({params.size()...}));
cannam@49 267 fill(result.begin(), kj::fwd<Params>(params)...);
cannam@49 268 return result;
cannam@49 269 }
cannam@49 270
cannam@49 271 inline String concat(String&& arr) {
cannam@49 272 return kj::mv(arr);
cannam@49 273 }
cannam@49 274
cannam@49 275 struct Stringifier {
cannam@49 276 // This is a dummy type with only one instance: STR (below). To make an arbitrary type
cannam@49 277 // stringifiable, define `operator*(Stringifier, T)` to return an iterable container of `char`.
cannam@49 278 // The container type must have a `size()` method. Be sure to declare the operator in the same
cannam@49 279 // namespace as `T` **or** in the global scope.
cannam@49 280 //
cannam@49 281 // A more usual way to accomplish what we're doing here would be to require that you define
cannam@49 282 // a function like `toString(T)` and then rely on argument-dependent lookup. However, this has
cannam@49 283 // the problem that it pollutes other people's namespaces and even the global namespace. For
cannam@49 284 // example, some other project may already have functions called `toString` which do something
cannam@49 285 // different. Declaring `operator*` with `Stringifier` as the left operand cannot conflict with
cannam@49 286 // anything.
cannam@49 287
cannam@49 288 inline ArrayPtr<const char> operator*(ArrayPtr<const char> s) const { return s; }
cannam@49 289 inline ArrayPtr<const char> operator*(ArrayPtr<char> s) const { return s; }
cannam@49 290 inline ArrayPtr<const char> operator*(const Array<const char>& s) const { return s; }
cannam@49 291 inline ArrayPtr<const char> operator*(const Array<char>& s) const { return s; }
cannam@49 292 template<size_t n>
cannam@49 293 inline ArrayPtr<const char> operator*(const CappedArray<char, n>& s) const { return s; }
cannam@49 294 template<size_t n>
cannam@49 295 inline ArrayPtr<const char> operator*(const FixedArray<char, n>& s) const { return s; }
cannam@49 296 inline ArrayPtr<const char> operator*(const char* s) const { return arrayPtr(s, strlen(s)); }
cannam@49 297 inline ArrayPtr<const char> operator*(const String& s) const { return s.asArray(); }
cannam@49 298 inline ArrayPtr<const char> operator*(const StringPtr& s) const { return s.asArray(); }
cannam@49 299
cannam@49 300 inline Range<char> operator*(const Range<char>& r) const { return r; }
cannam@49 301 inline Repeat<char> operator*(const Repeat<char>& r) const { return r; }
cannam@49 302
cannam@49 303 inline FixedArray<char, 1> operator*(char c) const {
cannam@49 304 FixedArray<char, 1> result;
cannam@49 305 result[0] = c;
cannam@49 306 return result;
cannam@49 307 }
cannam@49 308
cannam@49 309 StringPtr operator*(decltype(nullptr)) const;
cannam@49 310 StringPtr operator*(bool b) const;
cannam@49 311
cannam@49 312 CappedArray<char, 5> operator*(signed char i) const;
cannam@49 313 CappedArray<char, 5> operator*(unsigned char i) const;
cannam@49 314 CappedArray<char, sizeof(short) * 3 + 2> operator*(short i) const;
cannam@49 315 CappedArray<char, sizeof(unsigned short) * 3 + 2> operator*(unsigned short i) const;
cannam@49 316 CappedArray<char, sizeof(int) * 3 + 2> operator*(int i) const;
cannam@49 317 CappedArray<char, sizeof(unsigned int) * 3 + 2> operator*(unsigned int i) const;
cannam@49 318 CappedArray<char, sizeof(long) * 3 + 2> operator*(long i) const;
cannam@49 319 CappedArray<char, sizeof(unsigned long) * 3 + 2> operator*(unsigned long i) const;
cannam@49 320 CappedArray<char, sizeof(long long) * 3 + 2> operator*(long long i) const;
cannam@49 321 CappedArray<char, sizeof(unsigned long long) * 3 + 2> operator*(unsigned long long i) const;
cannam@49 322 CappedArray<char, 24> operator*(float f) const;
cannam@49 323 CappedArray<char, 32> operator*(double f) const;
cannam@49 324 CappedArray<char, sizeof(const void*) * 3 + 2> operator*(const void* s) const;
cannam@49 325
cannam@49 326 template <typename T>
cannam@49 327 String operator*(ArrayPtr<T> arr) const;
cannam@49 328 template <typename T>
cannam@49 329 String operator*(const Array<T>& arr) const;
cannam@49 330
cannam@49 331 #if KJ_COMPILER_SUPPORTS_STL_STRING_INTEROP // supports expression SFINAE?
cannam@49 332 template <typename T, typename Result = decltype(instance<T>().toString())>
cannam@49 333 inline Result operator*(T&& value) const { return kj::fwd<T>(value).toString(); }
cannam@49 334 #endif
cannam@49 335 };
cannam@49 336 static KJ_CONSTEXPR(const) Stringifier STR = Stringifier();
cannam@49 337
cannam@49 338 } // namespace _ (private)
cannam@49 339
cannam@49 340 template <typename T>
cannam@49 341 auto toCharSequence(T&& value) -> decltype(_::STR * kj::fwd<T>(value)) {
cannam@49 342 // Returns an iterable of chars that represent a textual representation of the value, suitable
cannam@49 343 // for debugging.
cannam@49 344 //
cannam@49 345 // Most users should use str() instead, but toCharSequence() may occasionally be useful to avoid
cannam@49 346 // heap allocation overhead that str() implies.
cannam@49 347 //
cannam@49 348 // To specialize this function for your type, see KJ_STRINGIFY.
cannam@49 349
cannam@49 350 return _::STR * kj::fwd<T>(value);
cannam@49 351 }
cannam@49 352
cannam@49 353 CappedArray<char, sizeof(unsigned char) * 2 + 1> hex(unsigned char i);
cannam@49 354 CappedArray<char, sizeof(unsigned short) * 2 + 1> hex(unsigned short i);
cannam@49 355 CappedArray<char, sizeof(unsigned int) * 2 + 1> hex(unsigned int i);
cannam@49 356 CappedArray<char, sizeof(unsigned long) * 2 + 1> hex(unsigned long i);
cannam@49 357 CappedArray<char, sizeof(unsigned long long) * 2 + 1> hex(unsigned long long i);
cannam@49 358
cannam@49 359 template <typename... Params>
cannam@49 360 String str(Params&&... params) {
cannam@49 361 // Magic function which builds a string from a bunch of arbitrary values. Example:
cannam@49 362 // str(1, " / ", 2, " = ", 0.5)
cannam@49 363 // returns:
cannam@49 364 // "1 / 2 = 0.5"
cannam@49 365 // To teach `str` how to stringify a type, see `Stringifier`.
cannam@49 366
cannam@49 367 return _::concat(toCharSequence(kj::fwd<Params>(params))...);
cannam@49 368 }
cannam@49 369
cannam@49 370 inline String str(String&& s) { return mv(s); }
cannam@49 371 // Overload to prevent redundant allocation.
cannam@49 372
cannam@49 373 template <typename T>
cannam@49 374 String strArray(T&& arr, const char* delim) {
cannam@49 375 size_t delimLen = strlen(delim);
cannam@49 376 KJ_STACK_ARRAY(decltype(_::STR * arr[0]), pieces, kj::size(arr), 8, 32);
cannam@49 377 size_t size = 0;
cannam@49 378 for (size_t i = 0; i < kj::size(arr); i++) {
cannam@49 379 if (i > 0) size += delimLen;
cannam@49 380 pieces[i] = _::STR * arr[i];
cannam@49 381 size += pieces[i].size();
cannam@49 382 }
cannam@49 383
cannam@49 384 String result = heapString(size);
cannam@49 385 char* pos = result.begin();
cannam@49 386 for (size_t i = 0; i < kj::size(arr); i++) {
cannam@49 387 if (i > 0) {
cannam@49 388 memcpy(pos, delim, delimLen);
cannam@49 389 pos += delimLen;
cannam@49 390 }
cannam@49 391 pos = _::fill(pos, pieces[i]);
cannam@49 392 }
cannam@49 393 return result;
cannam@49 394 }
cannam@49 395
cannam@49 396 namespace _ { // private
cannam@49 397
cannam@49 398 template <typename T>
cannam@49 399 inline String Stringifier::operator*(ArrayPtr<T> arr) const {
cannam@49 400 return strArray(arr, ", ");
cannam@49 401 }
cannam@49 402
cannam@49 403 template <typename T>
cannam@49 404 inline String Stringifier::operator*(const Array<T>& arr) const {
cannam@49 405 return strArray(arr, ", ");
cannam@49 406 }
cannam@49 407
cannam@49 408 } // namespace _ (private)
cannam@49 409
cannam@49 410 #define KJ_STRINGIFY(...) operator*(::kj::_::Stringifier, __VA_ARGS__)
cannam@49 411 // Defines a stringifier for a custom type. Example:
cannam@49 412 //
cannam@49 413 // class Foo {...};
cannam@49 414 // inline StringPtr KJ_STRINGIFY(const Foo& foo) { return foo.name(); }
cannam@49 415 //
cannam@49 416 // This allows Foo to be passed to str().
cannam@49 417 //
cannam@49 418 // The function should be declared either in the same namespace as the target type or in the global
cannam@49 419 // namespace. It can return any type which is an iterable container of chars.
cannam@49 420
cannam@49 421 // =======================================================================================
cannam@49 422 // Inline implementation details.
cannam@49 423
cannam@49 424 inline StringPtr::StringPtr(const String& value): content(value.begin(), value.size() + 1) {}
cannam@49 425
cannam@49 426 inline StringPtr::operator ArrayPtr<const char>() const {
cannam@49 427 return content.slice(0, content.size() - 1);
cannam@49 428 }
cannam@49 429
cannam@49 430 inline ArrayPtr<const char> StringPtr::asArray() const {
cannam@49 431 return content.slice(0, content.size() - 1);
cannam@49 432 }
cannam@49 433
cannam@49 434 inline bool StringPtr::operator==(const StringPtr& other) const {
cannam@49 435 return content.size() == other.content.size() &&
cannam@49 436 memcmp(content.begin(), other.content.begin(), content.size() - 1) == 0;
cannam@49 437 }
cannam@49 438
cannam@49 439 inline bool StringPtr::operator<(const StringPtr& other) const {
cannam@49 440 bool shorter = content.size() < other.content.size();
cannam@49 441 int cmp = memcmp(content.begin(), other.content.begin(),
cannam@49 442 shorter ? content.size() : other.content.size());
cannam@49 443 return cmp < 0 || (cmp == 0 && shorter);
cannam@49 444 }
cannam@49 445
cannam@49 446 inline StringPtr StringPtr::slice(size_t start) const {
cannam@49 447 return StringPtr(content.slice(start, content.size()));
cannam@49 448 }
cannam@49 449 inline ArrayPtr<const char> StringPtr::slice(size_t start, size_t end) const {
cannam@49 450 return content.slice(start, end);
cannam@49 451 }
cannam@49 452
cannam@49 453 inline bool StringPtr::startsWith(const StringPtr& other) const {
cannam@49 454 return other.content.size() <= content.size() &&
cannam@49 455 memcmp(content.begin(), other.content.begin(), other.size()) == 0;
cannam@49 456 }
cannam@49 457 inline bool StringPtr::endsWith(const StringPtr& other) const {
cannam@49 458 return other.content.size() <= content.size() &&
cannam@49 459 memcmp(end() - other.size(), other.content.begin(), other.size()) == 0;
cannam@49 460 }
cannam@49 461
cannam@49 462 inline Maybe<size_t> StringPtr::findFirst(char c) const {
cannam@49 463 const char* pos = reinterpret_cast<const char*>(memchr(content.begin(), c, size()));
cannam@49 464 if (pos == nullptr) {
cannam@49 465 return nullptr;
cannam@49 466 } else {
cannam@49 467 return pos - content.begin();
cannam@49 468 }
cannam@49 469 }
cannam@49 470
cannam@49 471 inline Maybe<size_t> StringPtr::findLast(char c) const {
cannam@49 472 for (size_t i = size(); i > 0; --i) {
cannam@49 473 if (content[i-1] == c) {
cannam@49 474 return i-1;
cannam@49 475 }
cannam@49 476 }
cannam@49 477 return nullptr;
cannam@49 478 }
cannam@49 479
cannam@49 480 inline String::operator ArrayPtr<char>() {
cannam@49 481 return content == nullptr ? ArrayPtr<char>(nullptr) : content.slice(0, content.size() - 1);
cannam@49 482 }
cannam@49 483 inline String::operator ArrayPtr<const char>() const {
cannam@49 484 return content == nullptr ? ArrayPtr<const char>(nullptr) : content.slice(0, content.size() - 1);
cannam@49 485 }
cannam@49 486
cannam@49 487 inline ArrayPtr<char> String::asArray() {
cannam@49 488 return content == nullptr ? ArrayPtr<char>(nullptr) : content.slice(0, content.size() - 1);
cannam@49 489 }
cannam@49 490 inline ArrayPtr<const char> String::asArray() const {
cannam@49 491 return content == nullptr ? ArrayPtr<const char>(nullptr) : content.slice(0, content.size() - 1);
cannam@49 492 }
cannam@49 493
cannam@49 494 inline const char* String::cStr() const { return content == nullptr ? "" : content.begin(); }
cannam@49 495
cannam@49 496 inline size_t String::size() const { return content == nullptr ? 0 : content.size() - 1; }
cannam@49 497
cannam@49 498 inline char String::operator[](size_t index) const { return content[index]; }
cannam@49 499 inline char& String::operator[](size_t index) { return content[index]; }
cannam@49 500
cannam@49 501 inline char* String::begin() { return content == nullptr ? nullptr : content.begin(); }
cannam@49 502 inline char* String::end() { return content == nullptr ? nullptr : content.end() - 1; }
cannam@49 503 inline const char* String::begin() const { return content == nullptr ? nullptr : content.begin(); }
cannam@49 504 inline const char* String::end() const { return content == nullptr ? nullptr : content.end() - 1; }
cannam@49 505
cannam@49 506 inline String::String(char* value, size_t size, const ArrayDisposer& disposer)
cannam@49 507 : content(value, size + 1, disposer) {
cannam@49 508 KJ_IREQUIRE(value[size] == '\0', "String must be NUL-terminated.");
cannam@49 509 }
cannam@49 510
cannam@49 511 inline String::String(Array<char> buffer): content(kj::mv(buffer)) {
cannam@49 512 KJ_IREQUIRE(content.size() > 0 && content.back() == '\0', "String must be NUL-terminated.");
cannam@49 513 }
cannam@49 514
cannam@49 515 inline String heapString(const char* value) {
cannam@49 516 return heapString(value, strlen(value));
cannam@49 517 }
cannam@49 518 inline String heapString(StringPtr value) {
cannam@49 519 return heapString(value.begin(), value.size());
cannam@49 520 }
cannam@49 521 inline String heapString(const String& value) {
cannam@49 522 return heapString(value.begin(), value.size());
cannam@49 523 }
cannam@49 524 inline String heapString(ArrayPtr<const char> value) {
cannam@49 525 return heapString(value.begin(), value.size());
cannam@49 526 }
cannam@49 527
cannam@49 528 } // namespace kj
cannam@49 529
cannam@49 530 #endif // KJ_STRING_H_