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>
59 template <
typename Encoding>
62 if constexpr (std::is_same_v<Encoding, lexy::ascii_encoding>)
64 else if constexpr (std::is_same_v<Encoding, lexy::utf8_encoding>)
66 else if constexpr (std::is_same_v<Encoding, lexy::utf8_char_encoding>)
68 else if constexpr (std::is_same_v<Encoding, lexy::utf32_encoding>)
80 template <
typename Encoding = default_encoding,
typename MemoryResource =
void>
84 = std::is_same_v<typename Encoding::char_type, typename Encoding::int_type>;
89 static_assert(std::is_trivial_v<char_type>);
97 MemoryResource* resource = _detail::get_memory_resource<MemoryResource>())
108 std::size_t
size() const noexcept
124 constexpr
explicit buffer(MemoryResource* resource) noexcept
129 MemoryResource* resource = _detail::get_memory_resource<MemoryResource>())
137 MemoryResource* resource = _detail::get_memory_resource<MemoryResource>())
141 template <
typename CharT,
typename = _detail::require_secondary_
char_type<encoding, CharT>>
143 MemoryResource* resource = _detail::get_memory_resource<MemoryResource>())
146 static_assert(
sizeof(CharT) ==
sizeof(
char_type));
148 template <
typename CharT,
typename = _detail::require_secondary_
char_type<encoding, CharT>>
150 MemoryResource* resource = _detail::get_memory_resource<MemoryResource>())
154 static_assert(
sizeof(CharT) ==
sizeof(
char_type));
159 MemoryResource* resource = _detail::get_memory_resource<MemoryResource>())
171 other._data =
nullptr;
225 std::size_t
size() const noexcept
234 return _buffer_reader<encoding>(
_data);
248 for (
auto ptr = memory +
size; ptr != memory + mem_size; ++ptr)
265 template <
typename CharT>
267 template <
typename CharT>
269 template <
typename View>
272 template <
typename CharT,
typename MemoryResource>
273 buffer(
const CharT*,
const CharT*, MemoryResource*)
275 template <
typename CharT,
typename MemoryResource>
276 buffer(
const CharT*, std::size_t, MemoryResource*)
278 template <
typename View,
typename MemoryResource>
279 buffer(
const View&, MemoryResource*)
283 template <
typename Encoding, encoding_endianness Endian>
286 template <
typename MemoryResource =
void>
288 MemoryResource* resource = _detail::get_memory_resource<MemoryResource>())
const
290 constexpr
auto native_endianness
295 auto memory =
static_cast<const unsigned char*
>(_memory);
297 if constexpr (
sizeof(
char_type) == 1 || Endian == native_endianness)
310 const auto end = memory +
size;
311 for (
auto dest = builder.
data(); memory !=
end; memory +=
sizeof(
char_type))
313 constexpr
auto is_char16 = std::is_same_v<char_type, char16_t>;
314 constexpr
auto is_char32 = std::is_same_v<char_type, char32_t>;
318 *dest++ =
static_cast<char_type>((memory[0] << 0) | (memory[1] << 8));
320 *dest++ =
static_cast<char_type>((memory[0] << 0) | (memory[1] << 8)
321 | (memory[2] << 16) | (memory[3] << 24));
323 *dest++ =
static_cast<char_type>((memory[0] << 8) | (memory[1] << 0));
325 *dest++ =
static_cast<char_type>((memory[0] << 24) | (memory[1] << 16)
326 | (memory[2] << 8) | (memory[3] << 0));
328 static_assert(_detail::error<Encoding>,
"unhandled encoding/endianness");
338 template <
typename MemoryResource =
void>
340 MemoryResource* resource = _detail::get_memory_resource<MemoryResource>())
const
342 auto memory =
static_cast<const unsigned char*
>(_memory);
345 if (
size >= 3 && memory[0] == 0xEF && memory[1] == 0xBB && memory[2] == 0xBF)
357 template <
typename MemoryResource =
void>
359 MemoryResource* resource = _detail::get_memory_resource<MemoryResource>())
const
361 auto memory =
static_cast<const unsigned char*
>(_memory);
364 if (
size >= 3 && memory[0] == 0xEF && memory[1] == 0xBB && memory[2] == 0xBF)
376 template <
typename MemoryResource =
void>
378 MemoryResource* resource = _detail::get_memory_resource<MemoryResource>())
const
382 auto memory =
static_cast<const unsigned char*
>(_memory);
385 return utf16_big(memory,
size, resource);
386 if (memory[0] == 0xFF && memory[1] == 0xFE)
387 return utf16_little(memory + 2,
size - 2, resource);
388 else if (memory[0] == 0xFE && memory[1] == 0xFF)
389 return utf16_big(memory + 2,
size - 2, resource);
391 return utf16_big(memory,
size, resource);
397 template <
typename MemoryResource =
void>
399 MemoryResource* resource = _detail::get_memory_resource<MemoryResource>())
const
403 auto memory =
static_cast<const unsigned char*
>(_memory);
407 if (memory[0] == 0xFF && memory[1] == 0xFE && memory[2] == 0x00 && memory[3] == 0x00)
408 return utf32_little(memory + 4,
size - 4, resource);
409 else if (memory[0] == 0x00 && memory[1] == 0x00 && memory[2] == 0xFE && memory[3])
410 return utf32_big(memory + 4,
size - 4, resource);
413 return utf32_big(memory,
size, resource);
418 template <
typename Encoding, encoding_endianness Endianness>
422 template <
typename Input>
425 template <
typename Input,
typename MemoryResource =
void>
427 MemoryResource* resource
428 = _detail::get_memory_resource<MemoryResource>())
432 if constexpr (_detail::is_detected<_detect_input_data, Input>)
434 return type(input.data(), input.size(), resource);
438 auto reader = input.reader();
439 auto begin = reader.position();
442 auto end = reader.position();
444 if constexpr (std::is_pointer_v<decltype(
begin)>)
451 typename type::builder builder(
size, resource);
452 auto dest = builder.data();
453 for (
auto cur =
begin; cur !=
end; ++cur)
462 template <
typename Encoding = default_encoding,
typename MemoryResource =
void>
465 template <
typename Tag,
typename Encoding = default_encoding,
typename MemoryResource =
void>
468 template <
typename Encoding = default_encoding,
typename MemoryResource =
void>
472 #endif // LEXY_INPUT_BUFFER_HPP_INCLUDED
constexpr auto size(const C &c) -> decltype(c.size())
encoding_endianness
The endianness used by an encoding.
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
void set_position(iterator new_pos) noexcept
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
#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
std::size_t size() const noexcept
buffer(const buffer &other, MemoryResource *resource)
builder(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(...)
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