simdjson  3.3.0
Ridiculously Fast JSON
padded_string.h
1 #ifndef SIMDJSON_PADDED_STRING_H
2 #define SIMDJSON_PADDED_STRING_H
3 
4 #include "simdjson/base.h"
5 #include "simdjson/error.h"
6 
7 #include "simdjson/error-inl.h"
8 
9 #include <cstring>
10 #include <memory>
11 #include <string>
12 #include <ostream>
13 
14 namespace simdjson {
15 
16 class padded_string_view;
17 
23 struct padded_string final {
24 
28  explicit inline padded_string() noexcept;
34  explicit inline padded_string(size_t length) noexcept;
41  explicit inline padded_string(const char *data, size_t length) noexcept;
47  inline padded_string(const std::string & str_ ) noexcept;
53  inline padded_string(std::string_view sv_) noexcept;
61  inline padded_string(padded_string &&o) noexcept;
69  inline padded_string &operator=(padded_string &&o) noexcept;
70  inline void swap(padded_string &o) noexcept;
71  ~padded_string() noexcept;
72 
78  size_t size() const noexcept;
79 
85  size_t length() const noexcept;
86 
90  const char *data() const noexcept;
91  const uint8_t *u8data() const noexcept { return static_cast<const uint8_t*>(static_cast<const void*>(data_ptr));}
92 
96  char *data() noexcept;
97 
101  operator std::string_view() const;
102 
106  operator padded_string_view() const noexcept;
107 
116  inline static simdjson_result<padded_string> load(std::string_view path) noexcept;
117 
118 private:
119  padded_string &operator=(const padded_string &o) = delete;
120  padded_string(const padded_string &o) = delete;
121 
122  size_t viable_size{0};
123  char *data_ptr{nullptr};
124 
125 }; // padded_string
126 
134 inline std::ostream& operator<<(std::ostream& out, const padded_string& s) { return out << s.data(); }
135 
136 #if SIMDJSON_EXCEPTIONS
146 inline std::ostream& operator<<(std::ostream& out, simdjson_result<padded_string> &s) noexcept(false) { return out << s.value(); }
147 #endif
148 
149 } // namespace simdjson
150 
151 // This is deliberately outside of simdjson so that people get it without having to use the namespace
152 inline simdjson::padded_string operator "" _padded(const char *str, size_t len);
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().
The top level simdjson namespace, containing everything the library provides.
Definition: base.h:8
std::ostream & operator<<(std::ostream &out, error_code error) noexcept
Write the error message to the output stream.
Definition: error-inl.h:35
String with extra allocation for ease of use with parser::parse()
Definition: padded_string.h:23
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:207