fast_io icon indicating copy to clipboard operation
fast_io copied to clipboard

How to write custom input devices to work with scan()?

Open MartyMcFlyInTheSky opened this issue 1 year ago • 1 comments

I'm enjoying fast_io so far. Everything is quite intuitive. However, I have multiple questions:

  1. I tried to implement input devices for printing to string and reading from std::string. I succeeded at the former, but failed with the later. I can't wrap my head around how you intended custom reader to work? Can you give me an example? This is my code so far:
#include<fast_io_device.h>
#include<fast_io.h>
#include<string>

/* custom output */

class string_writer
{
public:
	using char_type = char;
	std::string& ref_;
};

template<std::contiguous_iterator Iter>
constexpr void write(string_writer str, Iter begin, Iter end)
{
	str.ref_.append(begin, end);
}

/* custom input */

class string_reader
{
public:
	using char_type = char;
	const std::string& ref_;
};

template<std::contiguous_iterator Iter>
constexpr Iter read(string_reader, Iter begin, Iter end)
{
	/* How to access ryu fp parsing and int parsing?
	.. and where to save the read-out values? */
	return begin; // EOF
}

int main()
{
	// output
	std::string ostr;
	print(string_writer{ostr}, "Hello ", 4, " ever!");
	print(ostr);

	// input (doesn't work yet)
	// std::string istr = "2, 3";
	// int a,b;
	// scan(string_reader{istr}, a, b); <-- make this work
	// print(a,b);
}

/*
g++ -o compile compile.cc -Wall -Ofast -std=c++20 -s -flto -march=native -I../../include
*/
  1. How can get the next character like std::istream::get() and peek the next character like std::istream::peek() with fast_io?

MartyMcFlyInTheSky avatar Sep 01 '22 09:09 MartyMcFlyInTheSky

  1. you do not need to write a custom device for std::string in fast_io. Just use ibuffer_view https://github.com/cppfastio/fast_io/blob/8ba73cb3d1dfcbed79214e0a11f5d863fc16664f/tests/0002.printscan/scan.cc
fast_io::to<std::uint_least64_t>("214124214")

is another way to work, except it does not need ibuffer_view and more optimizations are applied. Like

fast_io::to<std::uint_least64_t>("214124214","5235235")

can work without concat two strings. 4. The input model in fast_io is NOT character model. It is a state-machine model. You cannot unget a character and it is absolutely pointless. The state-machine model is fairly complex due to herbception is not available. That is why i didn't write parser for different types.

trcrsired avatar Sep 02 '22 14:09 trcrsired