Skip to content

Commit

Permalink
Add test with too many tabs and fix code.
Browse files Browse the repository at this point in the history
`(.+)` is a bit to greedy and matches `\t`.
  • Loading branch information
mgautierfr committed Jul 28, 2023
1 parent fd88187 commit 9225b40
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/zimwriterfs/zimcreatorfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void parse_redirectArticles(std::istream& in_stream, redirect_handler handler) {
std::string line;
int line_number = 1;
while (std::getline(in_stream, line)) {
std::regex line_regex("(.+)\\t(.+)\\t(.+)");
std::regex line_regex("^([^\\t]+)\\t([^\\t]+)\\t([^\\t]+)$");
std::smatch matches;
if (!std::regex_search(line, matches, line_regex) || matches.size() != 4) {
throw std::runtime_error(
Expand Down
12 changes: 12 additions & 0 deletions test/zimwriterfs-zimcreatorfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,16 @@ TEST(ZimCreatorFSTest, ParseRedirect)
);
}, std::runtime_error);
}

{
std::stringstream ss;
ss << "A/path\ttitle\ttarget\tOups, to many tabs\n";
EXPECT_THROW({
parse_redirectArticles(
ss,
[&](Redirect redirect)
{}
);
}, std::runtime_error);
}
}

0 comments on commit 9225b40

Please sign in to comment.