1#ifndef SIMDJSON_PADDED_STRING_VIEW_INL_H
2#define SIMDJSON_PADDED_STRING_VIEW_INL_H
4#include "simdjson/padded_string_view.h"
5#include "simdjson/error-inl.h"
10#if SIMDJSON_HAS_UNISTD_H
12 #if defined(__APPLE__)
13 #include <sys/sysctl.h>
21 : std::string_view(s, len), _capacity(capacity)
23 if(_capacity < len) { _capacity = len; }
37 : std::string_view(s), _capacity(s.capacity())
42 : std::string_view(s), _capacity(capacity)
44 if(_capacity < s.length()) { _capacity = s.length(); }
52 if(length() < missing_padding) {
return false; }
54 for (
size_t i = length() - missing_padding; i < length(); i++) {
56 if (c !=
' ' && c !=
'\t' && c !=
'\n' && c !=
'\r') {
68 if(length() < 3) {
return false; }
69 if (std::memcmp(data(),
"\xEF\xBB\xBF", 3) == 0) {
77#if SIMDJSON_EXCEPTIONS
82 size_t existing_padding = 0;
83 for (
size_t i = s.size(); i > 0; i--) {
85 if (c ==
' ' || c ==
'\t' || c ==
'\n' || c ==
'\r') {
91 size_t needed_padding = 0;
94 s.append(needed_padding,
' ');
108#if defined(_WINDOWS_)
110 static const uint32_t cached = []() -> uint32_t {
113 return static_cast<std::uint32_t
>(si.dwPageSize);
116#elif SIMDJSON_HAS_UNISTD_H
117 static const uint32_t cached = []() -> uint32_t {
118 long page_size = sysconf(_SC_PAGESIZE);
120 return static_cast<uint32_t
>(page_size);
129#if SIMDJSON_CPLUSPLUS17
131inline padded_input::padded_input(std::string_view sv)
132 : storage(
simdjson::padded_string_view{}) {
133 if (needs_allocation(sv.data(), sv.size())) {
141inline padded_input::padded_input(
const char *data,
size_t length)
142 : storage(
simdjson::padded_string_view{}) {
143 if (needs_allocation(data, length)) {
151inline padded_input::padded_input(
const std::string &s)
152 : storage(
simdjson::padded_string_view{}) {
153 const size_t len = s.size();
154 const size_t cap = s.capacity();
159 if (needed_padding > 0 && needs_allocation(s.data(), cap, needed_padding)) {
167inline bool padded_input::is_view() const noexcept {
168 return std::holds_alternative<simdjson::padded_string_view>(storage);
177inline bool padded_input::needs_allocation(
const char* buf,
size_t len,
size_t padding)
noexcept {
178 if(len == 0) {
return false; }
180 return ((
reinterpret_cast<uintptr_t
>(buf + len - 1) % page_size)
181 + padding >=
static_cast<uintptr_t
>(page_size));
186 size_t block_size,
size_t index)
noexcept {
187 if (block_size == 0 || index > data.size() / block_size) {
return {}; }
188 const size_t raw_begin = index * block_size;
189 if (raw_begin >= data.size()) {
return {}; }
191 auto snap = [&](
size_t want) ->
size_t {
192 if (want >= data.size()) {
return data.size(); }
193 const void *p = std::memchr(data.data() + want, delimiter, data.size() - want);
194 return p ? size_t(
static_cast<const char *
>(p) - data.data()) + 1 : data.size();
197 const size_t begin = (raw_begin == 0) ? 0 : snap(raw_begin);
198 const size_t end = snap(raw_begin + block_size);
199 if (begin >= end) {
return {}; }
201 data.capacity() - begin);
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.
padded_string_view slice_at(padded_string_view data, char delimiter, size_t block_size, size_t index) noexcept
Return the index-th document-aligned slice of a delimited stream.
padded_string_view pad_with_reserve(std::string &s) noexcept
Create a padded_string_view from a string.
uint32_t get_page_size() noexcept
Get the system's memory page size.
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.
String with extra allocation for ease of use with parser::parse()
The result of a simdjson operation that could fail.