Go to the documentation of this file.
4 #ifndef LEXY_INPUT_BUFFER_HPP_INCLUDED
5 #define LEXY_INPUT_BUFFER_HPP_INCLUDED
17 template <
typename Encoding>
73 template <
typename Encoding>
76 if constexpr (std::is_same_v<Encoding, lexy::ascii_encoding>)
78 else if constexpr (std::is_same_v<Encoding, lexy::utf8_encoding>)
80 else if constexpr (std::is_same_v<Encoding, lexy::utf8_char_encoding>)
82 else if constexpr (std::is_same_v<Encoding, lexy::utf32_encoding>)
94 template <
typename Encoding = default_encoding,
typename MemoryResource =
void>
97 static_assert(lexy::is_char_encoding<Encoding>);
99 = std::is_same_v<typename Encoding::char_type, typename Encoding::int_type>;
104 static_assert(std::is_trivially_copyable_v<char_type>);
112 MemoryResource* resource = _detail::get_memory_resource<MemoryResource>())
123 std::size_t
size() const noexcept
138 MemoryResource* resource = _detail::get_memory_resource<MemoryResource>())
150 constexpr
explicit buffer(MemoryResource* resource) noexcept
155 MemoryResource* resource = _detail::get_memory_resource<MemoryResource>())
163 MemoryResource* resource = _detail::get_memory_resource<MemoryResource>())
167 template <
typename CharT,
typename = _detail::require_secondary_
char_type<encoding, CharT>>
169 MemoryResource* resource = _detail::get_memory_resource<MemoryResource>())
172 static_assert(
sizeof(CharT) ==
sizeof(
char_type));
174 template <
typename CharT,
typename = _detail::require_secondary_
char_type<encoding, CharT>>
176 MemoryResource* resource = _detail::get_memory_resource<MemoryResource>())
180 static_assert(
sizeof(CharT) ==
sizeof(
char_type));
185 MemoryResource* resource = _detail::get_memory_resource<MemoryResource>())
197 other._data =
nullptr;
251 std::size_t
size() const noexcept
268 return _buffer_reader<encoding>(
_data);
282 for (
auto ptr = memory +
size; ptr != memory + mem_size; ++ptr)
299 template <
typename CharT>
301 template <
typename CharT>
303 template <
typename View>
306 template <
typename CharT,
typename MemoryResource>
307 buffer(
const CharT*,
const CharT*, MemoryResource*)
309 template <
typename CharT,
typename MemoryResource>
310 buffer(
const CharT*, std::size_t, MemoryResource*)
312 template <
typename View,
typename MemoryResource>
313 buffer(
const View&, MemoryResource*)
317 template <
typename Encoding, encoding_endianness Endian>
320 template <
typename MemoryResource =
void>
322 MemoryResource* resource = _detail::get_memory_resource<MemoryResource>())
const
324 constexpr
auto native_endianness
329 auto memory =
static_cast<const unsigned char*
>(_memory);
331 if constexpr (
sizeof(
char_type) == 1 || Endian == native_endianness)
344 const auto end = memory +
size;
345 for (
auto dest = builder.
data(); memory !=
end; memory +=
sizeof(
char_type))
347 constexpr
auto is_char16 = std::is_same_v<char_type, char16_t>;
348 constexpr
auto is_char32 = std::is_same_v<char_type, char32_t>;
352 *dest++ =
static_cast<char_type>((memory[0] << 0) | (memory[1] << 8));
354 *dest++ =
static_cast<char_type>((memory[0] << 0) | (memory[1] << 8)
355 | (memory[2] << 16) | (memory[3] << 24));
357 *dest++ =
static_cast<char_type>((memory[0] << 8) | (memory[1] << 0));
359 *dest++ =
static_cast<char_type>((memory[0] << 24) | (memory[1] << 16)
360 | (memory[2] << 8) | (memory[3] << 0));
362 static_assert(_detail::error<Encoding>,
"unhandled encoding/endianness");
372 template <
typename MemoryResource =
void>
374 MemoryResource* resource = _detail::get_memory_resource<MemoryResource>())
const
376 auto memory =
static_cast<const unsigned char*
>(_memory);
379 if (
size >= 3 && memory[0] == 0xEF && memory[1] == 0xBB && memory[2] == 0xBF)
391 template <
typename MemoryResource =
void>
393 MemoryResource* resource = _detail::get_memory_resource<MemoryResource>())
const
395 auto memory =
static_cast<const unsigned char*
>(_memory);
398 if (
size >= 3 && memory[0] == 0xEF && memory[1] == 0xBB && memory[2] == 0xBF)
410 template <
typename MemoryResource =
void>
412 MemoryResource* resource = _detail::get_memory_resource<MemoryResource>())
const
416 auto memory =
static_cast<const unsigned char*
>(_memory);
419 return utf16_big(memory,
size, resource);
420 if (memory[0] == 0xFF && memory[1] == 0xFE)
421 return utf16_little(memory + 2,
size - 2, resource);
422 else if (memory[0] == 0xFE && memory[1] == 0xFF)
423 return utf16_big(memory + 2,
size - 2, resource);
425 return utf16_big(memory,
size, resource);
431 template <
typename MemoryResource =
void>
433 MemoryResource* resource = _detail::get_memory_resource<MemoryResource>())
const
437 auto memory =
static_cast<const unsigned char*
>(_memory);
441 if (memory[0] == 0xFF && memory[1] == 0xFE && memory[2] == 0x00 && memory[3] == 0x00)
442 return utf32_little(memory + 4,
size - 4, resource);
443 else if (memory[0] == 0x00 && memory[1] == 0x00 && memory[2] == 0xFE && memory[3])
444 return utf32_big(memory + 4,
size - 4, resource);
447 return utf32_big(memory,
size, resource);
452 template <
typename Encoding, encoding_endianness Endianness>
456 template <
typename Input>
459 template <
typename Input,
typename MemoryResource =
void>
461 MemoryResource* resource
462 = _detail::get_memory_resource<MemoryResource>())
466 if constexpr (_detail::is_detected<_detect_input_data, Input>)
468 return type(input.data(), input.size(), resource);
472 auto reader = input.reader();
473 auto begin = reader.position();
476 auto end = reader.position();
478 if constexpr (std::is_pointer_v<decltype(
begin)>)
485 typename type::builder builder(
size, resource);
486 auto dest = builder.data();
487 for (
auto cur =
begin; cur !=
end; ++cur)
496 template <
typename Encoding = default_encoding,
typename MemoryResource =
void>
499 template <
typename Tag,
typename Encoding = default_encoding,
typename MemoryResource =
void>
502 template <
typename Encoding = default_encoding,
typename MemoryResource =
void>
506 #endif // LEXY_INPUT_BUFFER_HPP_INCLUDED
constexpr auto size(const C &c) -> decltype(c.size())
encoding_endianness
The endianness used by an encoding.
marker current() const noexcept
LEXY_EMPTY_MEMBER _detail::memory_resource_ptr< MemoryResource > _resource
LEXY_INSTANTIATION_NEWTYPE(_pr, _rr, Encoding, const typename Encoding::char_type *)
std::conditional_t< std::is_void_v< MemoryResource >, _memory_resource_ptr_empty< default_memory_resource >, std::conditional_t< std::is_empty_v< MemoryResource >, _memory_resource_ptr_empty< MemoryResource >, _memory_resource_ptr< MemoryResource > >> memory_resource_ptr
string_view::value_type char_type
decltype(LEXY_DECLVAL(Input &).data()) _detect_input_data
constexpr MemoryResource * get_memory_resource()
iterator position() const noexcept
constexpr auto make_buffer_from_input(const Input &input, MemoryResource *resource=_detail::get_memory_resource< MemoryResource >()) -> buffer< typename input_reader< Input >::encoding, MemoryResource >
char_type * allocate(std::size_t size) const
buffer(const char_type *begin, const char_type *end, MemoryResource *resource=_detail::get_memory_resource< MemoryResource >())
constexpr std::size_t round_size_for_swar(std::size_t size_in_bytes)
auto operator()(const void *_memory, std::size_t size, MemoryResource *resource=_detail::get_memory_resource< MemoryResource >()) const
Contains information about the context of an error, production is type-erased.
buffer(buffer &&other) noexcept
#define LEXY_PRECONDITION(Expr)
buffer(const char_type *data, std::size_t size, MemoryResource *resource=_detail::get_memory_resource< MemoryResource >())
An encoding where the input is assumed to be valid UTF-32.
constexpr auto end(const C &c) -> decltype(c.end())
auto operator()(const void *_memory, std::size_t size, MemoryResource *resource=_detail::get_memory_resource< MemoryResource >()) const
typename _deduce_encoding< CharT >::type deduce_encoding
buffer(const buffer &other)
auto operator()(const void *_memory, std::size_t size, MemoryResource *resource=_detail::get_memory_resource< MemoryResource >()) const
const typename Encoding::char_type * iterator
auto operator()(const void *_memory, std::size_t size, MemoryResource *resource=_detail::get_memory_resource< MemoryResource >()) const
buffer(const CharT *data, std::size_t size, MemoryResource *resource=_detail::get_memory_resource< MemoryResource >())
_br(iterator begin) noexcept
buffer(const CharT *, const CharT *) -> buffer< deduce_encoding< CharT >>
constexpr void swap(T &lhs, T &rhs)
auto reader() const &noexcept
constexpr buffer(MemoryResource *resource) noexcept
buffer & operator=(const buffer &other)
constexpr auto bom
The BOM for that particular encoding.
char_type * data() const noexcept
constexpr buffer() noexcept
const char_type * release() &&noexcept
#define LEXY_DECAY_DECLTYPE(...)
An encoding where the input is assumed to be valid UTF-8.
const char_type * data() const noexcept
typename encoding::char_type char_type
constexpr std::size_t range_size(Iterator begin, Sentinel end)
buffer finish() &&noexcept
An encoding where the input is some 8bit encoding (ASCII, UTF-8, extended ASCII etc....
An encoding where the input is assumed to be valid UTF-8, but the char type is char.
auto get(const nlohmann::detail::iteration_proxy_value< IteratorType > &i) -> decltype(i.key())
static constexpr auto _has_sentinel
constexpr auto begin(const C &c) -> decltype(c.begin())
An encoding where the input is assumed to be valid UTF-16.
buffer(const View &view, MemoryResource *resource=_detail::get_memory_resource< MemoryResource >())
constexpr auto _buffer_reader(const typename Encoding::char_type *data)
constexpr auto make_buffer_from_raw
Creates a buffer with the specified encoding/endianness from raw memory.
buffer(const CharT *begin, const CharT *end, MemoryResource *resource=_detail::get_memory_resource< MemoryResource >())
auto peek() const noexcept
constexpr iterator position() const noexcept
std::size_t size() const noexcept
buffer(const buffer &other, MemoryResource *resource)
builder(std::size_t size, MemoryResource *resource=_detail::get_memory_resource< MemoryResource >())
static buffer adopt(const char_type *data, std::size_t size, MemoryResource *resource=_detail::get_memory_resource< MemoryResource >())
Allows the creation of an uninitialized buffer that is then filled by the user.
std::size_t size() const noexcept
buffer & operator=(buffer &&other) noexcept(std::is_empty_v< MemoryResource >)
#define LEXY_EMPTY_MEMBER
#define LEXY_DECLVAL(...)
void reset(marker m) noexcept
constexpr auto eof
Matches EOF.
auto operator()(const void *_memory, std::size_t size, MemoryResource *resource=_detail::get_memory_resource< MemoryResource >()) const
decltype(LEXY_DECLVAL(Input).reader()) input_reader