annotate win32-mingw/include/kj/string.h @ 163:5cc1366da2e9

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