simdjson  3.1.6
Ridiculously Fast JSON
padded_string.h
1 #ifndef SIMDJSON_PADDED_STRING_H
2 #define SIMDJSON_PADDED_STRING_H
3 
4 #include "simdjson/portability.h"
5 #include "simdjson/common_defs.h" // for SIMDJSON_PADDING
6 #include "simdjson/error.h"
7 #include <cstring>
8 #include <memory>
9 #include <string>
10 #include <ostream>
11 
12 namespace simdjson {
13 
14 class padded_string_view;
15 
21 struct padded_string final {
22 
26  explicit inline padded_string() noexcept;
32  explicit inline padded_string(size_t length) noexcept;
39  explicit inline padded_string(const char *data, size_t length) noexcept;
45  inline padded_string(const std::string & str_ ) noexcept;
51  inline padded_string(std::string_view sv_) noexcept;
59  inline padded_string(padded_string &&o) noexcept;
67  inline padded_string &operator=(padded_string &&o) noexcept;
68  inline void swap(padded_string &o) noexcept;
69  ~padded_string() noexcept;
70 
76  size_t size() const noexcept;
77 
83  size_t length() const noexcept;
84 
88  const char *data() const noexcept;
89  const uint8_t *u8data() const noexcept { return static_cast<const uint8_t*>(static_cast<const void*>(data_ptr));}
90 
94  char *data() noexcept;
95 
99  operator std::string_view() const;
100 
104  operator padded_string_view() const noexcept;
105 
114  inline static simdjson_result<padded_string> load(std::string_view path) noexcept;
115 
116 private:
117  padded_string &operator=(const padded_string &o) = delete;
118  padded_string(const padded_string &o) = delete;
119 
120  size_t viable_size{0};
121  char *data_ptr{nullptr};
122 
123 }; // padded_string
124 
132 inline std::ostream& operator<<(std::ostream& out, const padded_string& s) { return out << s.data(); }
133 
134 #if SIMDJSON_EXCEPTIONS
144 inline std::ostream& operator<<(std::ostream& out, simdjson_result<padded_string> &s) noexcept(false) { return out << s.value(); }
145 #endif
146 
147 } // namespace simdjson
148 
149 // This is deliberately outside of simdjson so that people get it without having to use the namespace
150 inline simdjson::padded_string operator "" _padded(const char *str, size_t len) {
151  return simdjson::padded_string(str, len);
152 }
153 
154 namespace simdjson {
155 namespace internal {
156 
157 // The allocate_padded_buffer function is a low-level function to allocate memory
158 // with padding so we can read past the "length" bytes safely. It is used by
159 // the padded_string class automatically. It returns nullptr in case
160 // of error: the caller should check for a null pointer.
161 // The length parameter is the maximum size in bytes of the string.
162 // The caller is responsible to free the memory (e.g., delete[] (...)).
163 inline char *allocate_padded_buffer(size_t length) noexcept;
164 
165 } // namespace internal
166 } // namespace simdjson
167 
168 #endif // SIMDJSON_PADDED_STRING_H
User-provided string that promises it has extra padded bytes at the end for use with parser::parse().
We want to support argument-dependent lookup (ADL).
std::ostream & operator<<(std::ostream &out, error_code error) noexcept
Write the error message to the output stream.
Definition: error-inl.h:36
String with extra allocation for ease of use with parser::parse()
Definition: padded_string.h:21
size_t size() const noexcept
The length of the string.
size_t length() const noexcept
The length of the string.
padded_string() noexcept
Create a new, empty padded string.
const char * data() const noexcept
The string data.
static simdjson_result< padded_string > load(std::string_view path) noexcept
Load this padded string from a file.
The result of a simdjson operation that could fail.
Definition: error.h:205