annotate win32-mingw/include/kj/string.h @ 51:bbebe9a28170

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