annotate osx/include/kj/string.h @ 144:a0c5ef2265f5

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