annotate win64-msvc/include/kj/string.h @ 55:284acf908dcd

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