| 1 | #ifndef SIMDJSON_PADDED_STRING_VIEW_H |
| 2 | #define SIMDJSON_PADDED_STRING_VIEW_H |
| 3 | |
| 4 | #include "simdjson/portability.h" |
| 5 | #include "simdjson/common_defs.h" // for SIMDJSON_PADDING |
| 6 | #include "simdjson/error.h" |
| 7 | |
| 8 | #include <cstring> |
| 9 | #include <memory> |
| 10 | #include <string> |
| 11 | #include <ostream> |
| 12 | |
| 13 | namespace simdjson { |
| 14 | |
| 15 | /** |
| 16 | * User-provided string that promises it has extra padded bytes at the end for use with parser::parse(). |
| 17 | */ |
| 18 | class padded_string_view : public std::string_view { |
| 19 | private: |
| 20 | size_t _capacity; |
| 21 | |
| 22 | public: |
| 23 | /** Create an empty padded_string_view. */ |
| 24 | inline padded_string_view() noexcept = default; |
| 25 | |
| 26 | /** |
| 27 | * Promise the given buffer has at least SIMDJSON_PADDING extra bytes allocated to it. |
| 28 | * |
| 29 | * @param s The string. |
| 30 | * @param len The length of the string (not including padding). |
| 31 | * @param capacity The allocated length of the string, including padding. |
| 32 | */ |
| 33 | explicit inline padded_string_view(const char* s, size_t len, size_t capacity) noexcept; |
| 34 | /** overload explicit inline padded_string_view(const char* s, size_t len) noexcept */ |
| 35 | explicit inline padded_string_view(const uint8_t* s, size_t len, size_t capacity) noexcept; |
| 36 | |
| 37 | /** |
| 38 | * Promise the given string has at least SIMDJSON_PADDING extra bytes allocated to it. |
| 39 | * |
| 40 | * The capacity of the string will be used to determine its padding. |
| 41 | * |
| 42 | * @param s The string. |
| 43 | */ |
| 44 | explicit inline padded_string_view(const std::string &s) noexcept; |
| 45 | |
| 46 | /** |
| 47 | * Promise the given string_view has at least SIMDJSON_PADDING extra bytes allocated to it. |
| 48 | * |
| 49 | * @param s The string. |
| 50 | * @param capacity The allocated length of the string, including padding. |
| 51 | */ |
| 52 | explicit inline padded_string_view(std::string_view s, size_t capacity) noexcept; |
| 53 | |
| 54 | /** The number of allocated bytes. */ |
| 55 | inline size_t capacity() const noexcept; |
| 56 | |
| 57 | /** The amount of padding on the string (capacity() - length()) */ |
| 58 | inline size_t padding() const noexcept; |
| 59 | |
| 60 | }; // padded_string_view |
| 61 | |
| 62 | #if SIMDJSON_EXCEPTIONS |
| 63 | /** |
| 64 | * Send padded_string instance to an output stream. |
| 65 | * |
| 66 | * @param out The output stream. |
| 67 | * @param s The padded_string_view. |
| 68 | * @throw simdjson_error if the result being printed has an error. If there is an error with the |
| 69 | * underlying output stream, that error will be propagated (simdjson_error will not be |
| 70 | * thrown). |
| 71 | */ |
| 72 | inline std::ostream& operator<<(std::ostream& out, simdjson_result<padded_string_view> &s) noexcept(false) { return out << s.value(); } |
| 73 | #endif |
| 74 | |
| 75 | } // namespace simdjson |
| 76 | |
| 77 | #endif // SIMDJSON_PADDED_STRING_VIEW_H |
| 78 | |