simdjson 4.2.3
Ridiculously Fast JSON
Loading...
Searching...
No Matches
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
9namespace simdjson {
10
11inline 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
17inline 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
22inline 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
27inline padded_string_view::padded_string_view(const std::string &s) noexcept
28 : std::string_view(s), _capacity(s.capacity())
29{
30}
31
32inline 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
38inline bool padded_string_view::has_sufficient_padding() const noexcept {
39 if (padding() >= SIMDJSON_PADDING) {
40 return true;
41 }
42 size_t missing_padding = SIMDJSON_PADDING - padding();
43 if(length() < missing_padding) { return false; }
44
45 for (size_t i = length() - missing_padding; i < length(); i++) {
46 char c = data()[i];
47 if (c != ' ' && c != '\t' && c != '\n' && c != '\r') {
48 return false;
49 }
50 }
51 return true;
52}
53
54inline size_t padded_string_view::capacity() const noexcept { return _capacity; }
55
56inline size_t padded_string_view::padding() const noexcept { return capacity() - length(); }
57
59 if(length() < 3) { return false; }
60 if (std::memcmp(data(), "\xEF\xBB\xBF", 3) == 0) {
61 remove_prefix(3);
62 _capacity -= 3;
63 return true;
64 }
65 return false;
66}
67
68#if SIMDJSON_EXCEPTIONS
69inline std::ostream& operator<<(std::ostream& out, simdjson_result<padded_string_view> &s) noexcept(false) { return out << s.value(); }
70#endif
71
72inline padded_string_view pad(std::string& s) noexcept {
73 size_t existing_padding = 0;
74 for (size_t i = s.size(); i > 0; i--) {
75 char c = s[i - 1];
76 if (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
77 existing_padding++;
78 } else {
79 break;
80 }
81 }
82 size_t needed_padding = 0;
83 if (existing_padding < SIMDJSON_PADDING) {
84 needed_padding = SIMDJSON_PADDING - existing_padding;
85 s.append(needed_padding, ' ');
86 }
87
88 return padded_string_view(s.data(), s.size() - needed_padding, s.size());
89}
90
91inline padded_string_view pad_with_reserve(std::string& s) noexcept {
92 if (s.capacity() - s.size() < SIMDJSON_PADDING) {
93 s.reserve(s.size() + SIMDJSON_PADDING );
94 }
95 return padded_string_view(s.data(), s.size(), s.capacity());
96}
97
98
99
100} // namespace simdjson
101
102
103#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.
bool has_sufficient_padding() const noexcept
check that the view has sufficient padding
The top level simdjson namespace, containing everything the library provides.
Definition base.h:8
padded_string_view pad_with_reserve(std::string &s) noexcept
Create a padded_string_view from a string.
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:33
The result of a simdjson operation that could fail.
Definition error.h:280