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