annotate win64-msvc/include/kj/string.h @ 143:e95e00bdc3eb

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