1 #ifndef SIMDJSON_PADDED_STRING_INL_H
2 #define SIMDJSON_PADDED_STRING_INL_H
4 #include "simdjson/padded_string.h"
5 #include "simdjson/padded_string_view.h"
7 #include "simdjson/error-inl.h"
8 #include "simdjson/padded_string_view-inl.h"
21 inline char *allocate_padded_buffer(
size_t length) noexcept {
23 if(totalpaddedlength<length) {
27 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
29 if (totalpaddedlength>(1UL<<20)) {
34 char *padded_buffer =
new (std::nothrow)
char[totalpaddedlength];
35 if (padded_buffer ==
nullptr) {
40 std::memset(padded_buffer + length, 0, totalpaddedlength - length);
49 : viable_size(length), data_ptr(internal::allocate_padded_buffer(length)) {
52 : viable_size(length), data_ptr(internal::allocate_padded_buffer(length)) {
53 if ((data !=
nullptr) && (data_ptr !=
nullptr)) {
54 std::memcpy(data_ptr, data, length);
56 if (data_ptr ==
nullptr) {
62 : viable_size(length), data_ptr(internal::allocate_padded_buffer(length)) {
63 if ((data !=
nullptr) && (data_ptr !=
nullptr)) {
64 std::memcpy(data_ptr,
reinterpret_cast<const char *
>(data), length);
66 if (data_ptr ==
nullptr) {
73 : viable_size(str_.size()), data_ptr(internal::allocate_padded_buffer(str_.size())) {
74 if (data_ptr ==
nullptr) {
77 std::memcpy(data_ptr, str_.data(), str_.size());
82 : viable_size(sv_.size()), data_ptr(internal::allocate_padded_buffer(sv_.size())) {
83 if(simdjson_unlikely(!data_ptr)) {
89 std::memcpy(data_ptr, sv_.data(), sv_.size());
93 : viable_size(o.viable_size), data_ptr(o.data_ptr) {
100 data_ptr = o.data_ptr;
101 viable_size = o.viable_size;
102 o.data_ptr =
nullptr;
108 size_t tmp_viable_size = viable_size;
109 char *tmp_data_ptr = data_ptr;
110 viable_size = o.viable_size;
111 data_ptr = o.data_ptr;
112 o.data_ptr = tmp_data_ptr;
113 o.viable_size = tmp_viable_size;
116 inline padded_string::~padded_string() noexcept {
128 inline padded_string::operator std::string_view()
const {
return std::string_view(data(), length()); }
136 SIMDJSON_PUSH_DISABLE_WARNINGS
137 SIMDJSON_DISABLE_DEPRECATED_WARNING
138 std::FILE *fp = std::fopen(filename.data(),
"rb");
139 SIMDJSON_POP_DISABLE_WARNINGS
147 #if SIMDJSON_VISUAL_STUDIO && !SIMDJSON_IS_32BITS
148 ret = _fseeki64(fp, 0, SEEK_END);
150 ret = std::fseek(fp, 0, SEEK_END);
156 #if SIMDJSON_VISUAL_STUDIO && !SIMDJSON_IS_32BITS
157 __int64 llen = _ftelli64(fp);
163 long llen = std::ftell(fp);
164 if((llen < 0) || (llen == LONG_MAX)) {
171 size_t len =
static_cast<size_t>(llen);
173 if (s.
data() ==
nullptr) {
180 size_t bytes_read = std::fread(s.
data(), 1, len, fp);
181 if (std::fclose(fp) != 0 || bytes_read != len) {
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.
@ MEMALLOC
Error allocating memory, most likely out of memory.
@ IO_ERROR
Error reading a file.
constexpr size_t SIMDJSON_PADDING
The amount of padding needed in a buffer to parse JSON.
String with extra allocation for ease of use with parser::parse()
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.
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.