jonathondgebhardt / advent-of-code-cpp

Advent of Code
0 stars 0 forks source link

Don't preserve trailing new line in util::Parse #22

Open jonathondgebhardt opened 10 months ago

jonathondgebhardt commented 10 months ago

The trailing newline is probably useful for manual parsing of the input, but it's not when solving challenges. Extra care must be taken to handle the empty line which is annoying. For example:

struct Foo {
    Foo(const std::string& x) {
        // Parse x but be prepared for x.empty()
    }
};

void SolveChallenge() {
    for(const auto& line : mInput) {
        Foo f{line};
        f.bar();
    }
}

This is not a big deal when manually iterating the input, but if you use an STL algorithm:

std::vector<Foo> foos;

// An extra Foo is made because of trailing new line
std::transform(mInput.begin(), mInput.end(), std::back_inserter(foos), 
    [](const std::string& x) { return Foo{x}; }
);
jonathondgebhardt commented 10 months ago

Will be closed with #changes-during-2023