simdjson  0.3.1
Ridiculously Fast JSON
padded_string.h
1 #ifndef SIMDJSON_INLINE_PADDED_STRING_H
2 #define SIMDJSON_INLINE_PADDED_STRING_H
3 
4 #include "simdjson/portability.h"
5 #include "simdjson/common_defs.h" // for SIMDJSON_PADDING
6 
7 #include <climits>
8 #include <cstring>
9 #include <memory>
10 #include <string>
11 
12 namespace simdjson::internal {
13 
14 // low-level function to allocate memory with padding so we can read past the
15 // "length" bytes safely. if you must provide a pointer to some data, create it
16 // with this function: length is the max. size in bytes of the string caller is
17 // responsible to free the memory (free(...))
18 inline char *allocate_padded_buffer(size_t length) noexcept {
19  // we could do a simple malloc
20  // return (char *) malloc(length + SIMDJSON_PADDING);
21  // However, we might as well align to cache lines...
22  size_t totalpaddedlength = length + SIMDJSON_PADDING;
23  char *padded_buffer = aligned_malloc_char(64, totalpaddedlength);
24 #ifndef NDEBUG
25  if (padded_buffer == nullptr) {
26  return nullptr;
27  }
28 #endif // NDEBUG
29  memset(padded_buffer + length, 0, totalpaddedlength - length);
30  return padded_buffer;
31 } // allocate_padded_buffer()
32 
33 } // namespace simdjson::internal
34 
35 namespace simdjson {
36 
37 inline padded_string::padded_string() noexcept : viable_size(0), data_ptr(nullptr) {}
38 inline padded_string::padded_string(size_t length) noexcept
39  : viable_size(length), data_ptr(internal::allocate_padded_buffer(length)) {
40  if (data_ptr != nullptr)
41  data_ptr[length] = '\0'; // easier when you need a c_str
42 }
43 inline padded_string::padded_string(const char *data, size_t length) noexcept
44  : viable_size(length), data_ptr(internal::allocate_padded_buffer(length)) {
45  if ((data != nullptr) and (data_ptr != nullptr)) {
46  memcpy(data_ptr, data, length);
47  data_ptr[length] = '\0'; // easier when you need a c_str
48  }
49 }
50 // note: do not pass std::string arguments by value
51 inline padded_string::padded_string(const std::string & str_ ) noexcept
52  : viable_size(str_.size()), data_ptr(internal::allocate_padded_buffer(str_.size())) {
53  if (data_ptr != nullptr) {
54  memcpy(data_ptr, str_.data(), str_.size());
55  data_ptr[str_.size()] = '\0'; // easier when you need a c_str
56  }
57 }
58 // note: do pass std::string_view arguments by value
59 inline padded_string::padded_string(std::string_view sv_) noexcept
60  : viable_size(sv_.size()), data_ptr(internal::allocate_padded_buffer(sv_.size())) {
61  if (data_ptr != nullptr) {
62  memcpy(data_ptr, sv_.data(), sv_.size());
63  data_ptr[sv_.size()] = '\0'; // easier when you need a c_str
64  }
65 }
67  : viable_size(o.viable_size), data_ptr(o.data_ptr) {
68  o.data_ptr = nullptr; // we take ownership
69 }
70 
72  aligned_free_char(data_ptr);
73  data_ptr = o.data_ptr;
74  viable_size = o.viable_size;
75  o.data_ptr = nullptr; // we take ownership
76  o.viable_size = 0;
77  return *this;
78 }
79 
80 inline void padded_string::swap(padded_string &o) noexcept {
81  size_t tmp_viable_size = viable_size;
82  char *tmp_data_ptr = data_ptr;
83  viable_size = o.viable_size;
84  data_ptr = o.data_ptr;
85  o.data_ptr = tmp_data_ptr;
86  o.viable_size = tmp_viable_size;
87 }
88 
89 inline padded_string::~padded_string() noexcept {
90  aligned_free_char(data_ptr);
91 }
92 
93 inline size_t padded_string::size() const noexcept { return viable_size; }
94 
95 inline size_t padded_string::length() const noexcept { return viable_size; }
96 
97 inline const char *padded_string::data() const noexcept { return data_ptr; }
98 
99 inline char *padded_string::data() noexcept { return data_ptr; }
100 
101 inline padded_string::operator std::string_view() const { return std::string_view(data(), length()); }
102 
103 inline simdjson_result<padded_string> padded_string::load(const std::string &filename) noexcept {
104  // Open the file
105  std::FILE *fp = std::fopen(filename.c_str(), "rb");
106  if (fp == nullptr) {
107  return IO_ERROR;
108  }
109 
110  // Get the file size
111  if(std::fseek(fp, 0, SEEK_END) < 0) {
112  std::fclose(fp);
113  return IO_ERROR;
114  }
115  long llen = std::ftell(fp);
116  if((llen < 0) || (llen == LONG_MAX)) {
117  std::fclose(fp);
118  return IO_ERROR;
119  }
120 
121  // Allocate the padded_string
122  size_t len = (size_t) llen;
123  padded_string s(len);
124  if (s.data() == nullptr) {
125  std::fclose(fp);
126  return MEMALLOC;
127  }
128 
129  // Read the padded_string
130  std::rewind(fp);
131  size_t bytes_read = std::fread(s.data(), 1, len, fp);
132  if (std::fclose(fp) != 0 || bytes_read != len) {
133  return IO_ERROR;
134  }
135 
136  return s;
137 }
138 
139 } // namespace simdjson
140 
141 #endif // SIMDJSON_INLINE_PADDED_STRING_H
simdjson::padded_string::data
const char * data() const noexcept
The string data.
Definition: padded_string.h:97
simdjson::padded_string::length
size_t length() const noexcept
The length of the string.
Definition: padded_string.h:95
simdjson::padded_string::operator=
padded_string & operator=(padded_string &&o) noexcept
Move one padded string into another.
Definition: padded_string.h:71
simdjson::padded_string::load
static simdjson_result< padded_string > load(const std::string &path) noexcept
Load this padded string from a file.
Definition: padded_string.h:103
simdjson::simdjson_result
The result of a simdjson operation that could fail.
Definition: error.h:166
simdjson::padded_string::size
size_t size() const noexcept
The length of the string.
Definition: padded_string.h:93
simdjson::padded_string::padded_string
padded_string() noexcept
Create a new, empty padded string.
Definition: padded_string.h:37
simdjson::padded_string
String with extra allocation for ease of use with parser::parse()
Definition: padded_string.h:18