#include <iostream>
#include <string>
#include <vector>
bool ends_with(std::string const & str, std::string const & suffix)
{
// If the string is smaller than the suffix then there is no match.
if (str.size() < suffix.size()) { return false; }
return 0 == str.compare(str.size() - suffix.size(),
suffix.size(),
suffix,
0,
suffix.size());
}
void ensure_ends_with_txt(std::string & str)
{
if (!ends_with(str, ".txt")) {
str += ".txt";
}
}
int main() {
std::vector<std::string> tests = {
"a.txt",
"b.txt.zip",
"c"
};
for (auto i = tests.begin(); i != tests.end(); ++i) {
std::cout << *i << " --> ";
ensure_ends_with_txt(*i);
std::cout << *i << std::endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8dmVjdG9yPgoKYm9vbCBlbmRzX3dpdGgoc3RkOjpzdHJpbmcgY29uc3QgJiBzdHIsIHN0ZDo6c3RyaW5nIGNvbnN0ICYgc3VmZml4KQp7CiAgICAvLyBJZiB0aGUgc3RyaW5nIGlzIHNtYWxsZXIgdGhhbiB0aGUgc3VmZml4IHRoZW4gdGhlcmUgaXMgbm8gbWF0Y2guCiAgICBpZiAoc3RyLnNpemUoKSA8IHN1ZmZpeC5zaXplKCkpIHsgcmV0dXJuIGZhbHNlOyB9CgogICAgcmV0dXJuIDAgPT0gc3RyLmNvbXBhcmUoc3RyLnNpemUoKSAtIHN1ZmZpeC5zaXplKCksCiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzdWZmaXguc2l6ZSgpLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgc3VmZml4LAogICAgICAgICAgICAgICAgICAgICAgICAgICAgMCwKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHN1ZmZpeC5zaXplKCkpOwp9Cgp2b2lkIGVuc3VyZV9lbmRzX3dpdGhfdHh0KHN0ZDo6c3RyaW5nICYgc3RyKQp7CglpZiAoIWVuZHNfd2l0aChzdHIsICIudHh0IikpIHsKCQlzdHIgKz0gIi50eHQiOwoJfQp9CgppbnQgbWFpbigp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