simdjson 5.0.1
Ridiculously Fast JSON
Loading...
Searching...
No Matches
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
14namespace simdjson {
15
16class padded_string_view;
17
23struct 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;
42#ifdef __cpp_char8_t
43 explicit inline padded_string(const char8_t *data, size_t length) noexcept;
44#endif
50 inline padded_string(const std::string & str_ ) noexcept;
56 inline padded_string(std::string_view sv_) noexcept;
64 inline padded_string(padded_string &&o) noexcept;
72 inline padded_string &operator=(padded_string &&o) noexcept;
73 inline void swap(padded_string &o) noexcept;
74 ~padded_string() noexcept;
75
81 size_t size() const noexcept;
82
88 size_t length() const noexcept;
89
93 const char *data() const noexcept;
94 const uint8_t *u8data() const noexcept { return static_cast<const uint8_t*>(static_cast<const void*>(data_ptr));}
95
99 char *data() noexcept;
100
109 inline bool append(const char *data, size_t length) noexcept;
110
114 operator std::string_view() const;
115
119 operator padded_string_view() const noexcept;
120
138 inline static simdjson_result<padded_string> load(std::string_view path) noexcept;
139
140 #if defined(_WIN32) && SIMDJSON_CPLUSPLUS17
150 inline static simdjson_result<padded_string> load(std::wstring_view path) noexcept;
151 #endif
152
153private:
154 friend class padded_string_builder;
155 padded_string &operator=(const padded_string &o) = delete;
156 padded_string(const padded_string &o) = delete;
157
158 size_t viable_size{0};
159 char *data_ptr{nullptr};
160
161}; // padded_string
162
169public:
173 inline padded_string_builder() noexcept;
174
180 inline padded_string_builder(size_t capacity) noexcept;
181
185 inline padded_string_builder(padded_string_builder &&o) noexcept;
186
190 inline padded_string_builder &operator=(padded_string_builder &&o) noexcept;
191
196
200 padded_string_builder &operator=(const padded_string_builder &) = delete;
201
205 inline ~padded_string_builder() noexcept;
206
214 inline bool append(const char *newdata, size_t length) noexcept;
215
222 inline bool append(std::string_view sv) noexcept;
223
227 inline size_t length() const noexcept;
228
235 inline padded_string build() const noexcept;
236
243 inline padded_string convert() noexcept;
244private:
245 size_t size{0};
246 size_t capacity{0};
247 char *data{nullptr};
248
255 inline bool reserve(size_t additional) noexcept;
256};
257
265inline std::ostream& operator<<(std::ostream& out, const padded_string& s) { return out << s.data(); }
266
267#if SIMDJSON_EXCEPTIONS
277inline std::ostream& operator<<(std::ostream& out, simdjson_result<padded_string> &s) noexcept(false) { return out << s.value(); }
278#endif
279
280#if SIMDJSON_HAS_PADDED_MEMORY_MAP
301class padded_memory_map {
302public:
316 simdjson_inline padded_memory_map(const char *filename) noexcept;
320 simdjson_inline ~padded_memory_map() noexcept;
321
322 // lifetime of the view is tied to the memory map, so we can return a view
323 // directly
334 simdjson_inline simdjson::padded_string_view view() const noexcept simdjson_lifetime_bound;
340 simdjson_inline bool is_valid() const noexcept;
341
342private:
343 padded_memory_map() = delete;
344 padded_memory_map(const padded_memory_map &) = delete;
345 padded_memory_map &operator=(const padded_memory_map &) = delete;
346 const char *data{nullptr};
347 size_t size{0};
348#ifdef _WIN32
349 // When the file ends near an allocation-granularity boundary, we use the
350 // placeholder API to append zero-filled padding pages. This pointer tracks
351 // that region so the destructor can release it with VirtualFree.
352 void *padding_view_{nullptr};
353#endif
354};
355#endif // SIMDJSON_HAS_PADDED_MEMORY_MAP
356
357
358
359} // namespace simdjson
360
361// This is deliberately outside of simdjson so that people get it without having to use the namespace
362inline simdjson::padded_string operator ""_padded(const char *str, size_t len);
363#ifdef __cpp_char8_t
364inline simdjson::padded_string operator ""_padded(const char8_t *str, size_t len);
365#endif
366
367namespace simdjson {
368namespace internal {
369
370// The allocate_padded_buffer function is a low-level function to allocate memory
371// with padding so we can read past the "length" bytes safely. It is used by
372// the padded_string class automatically. It returns nullptr in case
373// of error: the caller should check for a null pointer.
374// The length parameter is the maximum size in bytes of the string.
375// The caller is responsible to free the memory (e.g., delete[] (...)).
376inline char *allocate_padded_buffer(size_t length) noexcept;
377
378} // namespace internal
379} // namespace simdjson
380
381#endif // SIMDJSON_PADDED_STRING_H
Builder for constructing padded_string incrementally.
padded_string convert() noexcept
Convert the current content into a padded_string.
size_t length() const noexcept
Get the current length of the built string.
padded_string build() const noexcept
Build a padded_string from the current content.
bool append(const char *newdata, size_t length) noexcept
Append data to the builder.
padded_string_builder() noexcept
Create a new, empty padded string builder.
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
String with extra allocation for ease of use with parser::parse()
size_t size() const noexcept
The length of the string.
operator std::string_view() const
Create a std::string_view with the same content.
bool append(const char *data, size_t length) noexcept
Append data to the padded string.
size_t length() const noexcept
The length of the string.
padded_string() noexcept
Create a new, empty padded string.
padded_string & operator=(padded_string &&o) noexcept
Move one padded string into another.
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:281