Simple coroutine string parser
In this section, we will implement our last example: a simple string parser. The coroutine will wait for the input, a std::string
object, and will yield the output, a number, after parsing the input string. To simplify the example, we will assume that the string representation of the number doesn’t have any errors and that the end of a number is represented by the hash character, #
. We will also assume that the number type is int64_t
and that the string won’t contain any values out of that integer type range.
The parsing algorithm
Let’s see how to convert a string representing an integer into a number. For example, the string "-12321#"
represents the number -12321. To convert the string into a number, we can write a function like this:
int64_t parse_string(const std::string& str) { int64_t num{ 0 }; int64_t sign { 1 }; std::size_t c = 0...