Skip to content

Commit

Permalink
[#75] fix(Vfs): allow trailing whitespace in find and resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
lmichaelis committed Jul 26, 2023
1 parent 97b7cba commit 9e8458e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
16 changes: 15 additions & 1 deletion source/Vfs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,18 @@ namespace phoenix {
return std::get<std::vector<VfsNode>>(_m_data);
}

std::string_view trim_trailing_whitespace(std::string_view s) {
while (std::isspace(s.back())) {
s = s.substr(0, s.size() - 1);
}

return s;
}

VfsNode const* VfsNode::child(std::string_view name) const {
auto& children = std::get<std::vector<VfsNode>>(_m_data);

name = trim_trailing_whitespace(name);
auto it = std::lower_bound(children.begin(), children.end(), name, VfsNodeComparator {});
if (it == children.end() || !iequals(it->name(), name))
return nullptr;
Expand All @@ -53,6 +63,8 @@ namespace phoenix {

VfsNode* VfsNode::child(std::string_view name) {
auto& children = std::get<std::vector<VfsNode>>(_m_data);

name = trim_trailing_whitespace(name);
auto it = std::lower_bound(children.begin(), children.end(), name, VfsNodeComparator {});
if (it == children.end() || !iequals(it->name(), name))
return nullptr;
Expand All @@ -68,6 +80,8 @@ namespace phoenix {

bool VfsNode::remove(std::string_view name) {
auto& children = std::get<std::vector<VfsNode>>(_m_data);

name = trim_trailing_whitespace(name);
auto it = std::lower_bound(children.begin(), children.end(), name, VfsNodeComparator {});
if (it == children.end() || !iequals(it->name(), name))
return false;
Expand Down Expand Up @@ -408,4 +422,4 @@ namespace phoenix {
}
}

} // namespace phoenix
} // namespace phoenix
4 changes: 4 additions & 0 deletions tests/TestVfs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ void check_vfs(Vfs const& vdf) {
CHECK_NE(vdf.resolve("/LICENSES"), nullptr);
CHECK_NE(vdf.resolve(""), nullptr);
CHECK_NE(vdf.resolve("/"), nullptr);

// Ignores trailing whitespace (see #75)
CHECK_NE(vdf.find("config.yml "), nullptr);
CHECK_NE(vdf.resolve("licEnSES /GPL/gpl-3.0.md "), nullptr);
}

TEST_SUITE("Vfs") {
Expand Down

0 comments on commit 9e8458e

Please sign in to comment.