simdjson  3.11.0
Ridiculously Fast JSON
padded_string_view-inl.h
1 #ifndef SIMDJSON_PADDED_STRING_VIEW_INL_H
2 #define SIMDJSON_PADDED_STRING_VIEW_INL_H
3 
4 #include "simdjson/padded_string_view.h"
5 #include "simdjson/error-inl.h"
6 
7 #include <cstring> /* memcmp */
8 
9 namespace simdjson {
10 
11 inline padded_string_view::padded_string_view(const char* s, size_t len, size_t capacity) noexcept
12  : std::string_view(s, len), _capacity(capacity)
13 {
14  if(_capacity < len) { _capacity = len; }
15 }
16 
17 inline padded_string_view::padded_string_view(const uint8_t* s, size_t len, size_t capacity) noexcept
18  : padded_string_view(reinterpret_cast<const char*>(s), len, capacity)
19 {
20 }
21 #ifdef __cpp_char8_t
22 inline padded_string_view::padded_string_view(const char8_t* s, size_t len, size_t capacity) noexcept
23  : padded_string_view(reinterpret_cast<const char*>(s), len, capacity)
24 {
25 }
26 #endif
27 inline padded_string_view::padded_string_view(const std::string &s) noexcept
28  : std::string_view(s), _capacity(s.capacity())
29 {
30 }
31 
32 inline padded_string_view::padded_string_view(std::string_view s, size_t capacity) noexcept
33  : std::string_view(s), _capacity(capacity)
34 {
35  if(_capacity < s.length()) { _capacity = s.length(); }
36 }
37 
38 inline size_t padded_string_view::capacity() const noexcept { return _capacity; }
39 
40 inline size_t padded_string_view::padding() const noexcept { return capacity() - length(); }
41 
42 inline bool padded_string_view::remove_utf8_bom() noexcept {
43  if(length() < 3) { return false; }
44  if (std::memcmp(data(), "\xEF\xBB\xBF", 3) == 0) {
45  remove_prefix(3);
46  _capacity -= 3;
47  return true;
48  }
49  return false;
50 }
51 
52 #if SIMDJSON_EXCEPTIONS
53 inline std::ostream& operator<<(std::ostream& out, simdjson_result<padded_string_view> &s) noexcept(false) { return out << s.value(); }
54 #endif
55 
56 inline padded_string_view pad(std::string& s) noexcept {
57  const auto len = s.size();
58  s.append(SIMDJSON_PADDING, ' ');
59  return padded_string_view(s.data(), len, s.size());
60 }
61 } // namespace simdjson
62 
63 
64 #endif // SIMDJSON_PADDED_STRING_VIEW_INL_H
User-provided string that promises it has extra padded bytes at the end for use with parser::parse().
bool remove_utf8_bom() noexcept
Remove the UTF-8 Byte Order Mark (BOM) if it exists.
size_t padding() const noexcept
The amount of padding on the string (capacity() - length())
size_t capacity() const noexcept
The number of allocated bytes.
padded_string_view() noexcept=default
Create an empty padded_string_view.
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
padded_string_view pad(std::string &s) noexcept
Create a padded_string_view from a string.
constexpr size_t SIMDJSON_PADDING
The amount of padding needed in a buffer to parse JSON.
Definition: base.h:32
The result of a simdjson operation that could fail.
Definition: error.h:215