#include <iostream>
#include <set>
#include <cassert>
std::set<std::string, std::less<> > extract_prefix(const std::set<std::string, std::less<> > &all, const std::string_view &prefix)
{
assert( prefix[prefix.size()-1] == '/');
// replace / with next ascii char higher
std::string upper { prefix.substr(0,prefix.size()-1) };
upper += std::string(1, prefix[prefix.size()-1] + 1);
std::set<std::string, std::less<> > res;
for(auto iter=all.lower_bound(prefix); iter!=all.upper_bound(upper); ++iter)
{
auto &entry = *iter;
if(entry == prefix)
continue;
std::string_view remainder( entry.c_str()+prefix.size(), entry.size()-prefix.size());
auto pos = remainder.find('/');
if(pos == std::string_view::npos || pos == remainder.size()-1)
res.emplace(*iter);
}
return std::move(res);
}
int main() {
std::set<std::string, std::less<> > names =
{
"/",
"/dir1/",
"/dir1/file1",
"/file2",
"/dir1/file2",
"/dir1/dir2/",
"/dir1/dir3/",
"/dir1/dir3/file3"
};
auto res = extract_prefix(names, "/dir1/");
for(auto &s: res)
fprintf(stderr,"%s\n", s.c_str());
names = res;
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: