annotate win32-mingw/include/kj/string.h @ 146:206f0eb279b8

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