annotate win32-mingw/include/kj/string.h @ 72:7b5216b54e42

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