Header lexy/input/string_input.hpp

Inputs that read a string.

Input lexy::string_input

lexy/input/string_input.hpp
namespace lexy
{
    template <encoding Encoding = default_encoding>
    class string_input
    {
    public:
        using encoding  = Encoding;
        using char_type = typename encoding::char_type;

        //=== constructors ===//
        constexpr string_input() noexcept;

        template <typename CharT>
        constexpr string_input(const CharT data, std::size_t size) noexcept;
        template <typename CharT>
        constexpr string_input(const CharT begin, const CharT end) noexcept;
        template <typename View>
        constexpr explicit string_input(const View& view) noexcept;

        //=== access ===//
        constexpr const char_type data() const noexcept;
        constexpr std::size_t      size() const noexcept;

        constexpr reader auto reader() const& noexcept;
    };
}

The class string_input uses a string as input.

It is a lightweight view and does not own any of the data. Use lexy::buffer  if you need to own the contents of the string.

Example 1. Use a byte array as input
int main()
{
    unsigned char array[] = {'H', 'i'};

    // Create the input, deducing the encoding.
    auto input = lexy::string_input(array, array + 2);

    // Use the input.
    if (!lexy::match<production>(input))
    {
        std::puts("Error!\n");
        return 1;
    }
}

Pointer constructors

lexy/input/string_input.hpp
template <typename CharT>
constexpr string_input(const CharT data, std::size_t size) noexcept;  (1)
template <typename CharT>
constexpr string_input(const CharT begin, const CharT end) noexcept; (2)

template <typename CharT>
string_input(const CharT data, std::size_t size)
  -> string_input<deduce_encoding<CharT>>;
template <typename CharT>
string_input(const CharT begin, const CharT end)
  -> string_input<deduce_encoding<CharT>>;
  1. Use the contiguous range [data, data + size) as input.

  2. Use the contiguous range [begin, end) as input.

CharT must be the primary or secondary character type of the encoding. CTAD can be used to deduce the encoding from the character type.

View constructor

lexy/input/string_input.hpp
template <typename View>
    requires requires (View view) {
        view.data();
        view.size();
    }
constexpr explicit string_input(const View& view) noexcept;

template <typename View>
string_input(const View&)
  -> string_input<deduce-encoding>;

Use the contiguous range [view.data(), view.data() + view.size()) as input. Its character type must be the primary or secondary character type of the encoding. CTAD can be used to deduce the encoding from the character type.

Input lexy::zstring_input

lexy/input/string_input.hpp
namespace lexy
{
    template <encoding Encoding, typename CharT>
    constexpr auto zstring_input(const CharT str) noexcept
      -> string_input<Encoding>;

    template <typename CharT>
    constexpr auto zstring_input(const CharT str) noexcept
      -> string_input<deduce_encoding<CharT>>;
}

The function zstring_input uses a null-terminated string as input.

It returns the lexy::string_input  for the range [str, str + std::strlen(str)). The second overload deduces the encoding from the character type.

Example 2. Use a string literal as input
int main()
{
    // Create the input, deducing the encoding.
    auto input = lexy::zstring_input("Hi");

    // Use the input.
    if (!lexy::match<production>(input))
    {
        std::puts("Error!\n");
        return 1;
    }
}
Tip
This input is useful for testing your grammar.

Convenience typedefs

lexy/input/string_input.hpp
namespace lexy
{
    template <encoding Encoding = default_encoding>
    using string_lexeme = lexeme_for<string_input<Encoding>>;

    template <typename Tag, encoding Encoding = default_encoding>
    using string_error = error_for<string_input<Encoding>, Tag>;

    template <encoding Encoding = default_encoding>
    using string_error_context = error_context<string_input<Encoding>>;
}

Convenience typedefs for string inputs.

See also