annotate osx/include/kj/string.h @ 169:223a55898ab9 tip default

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