pytorch/c10/util/StringUtil.cpp
Xiang Gao 15c7486416 Canonicalize includes in c10, and add tests for it (#36299)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/36299

Test Plan: Imported from OSS

Differential Revision: D20943005

Pulled By: ezyang

fbshipit-source-id: 9dd0a58824bd0f1b5ad259942f92954ba1f63eae
2020-04-10 12:07:52 -07:00

44 lines
1.0 KiB
C++

#include <c10/util/StringUtil.h>
#include <c10/util/Exception.h>
#include <cstring>
#include <string>
namespace c10 {
namespace detail {
std::string StripBasename(const std::string& full_path) {
const char kSeparator = '/';
size_t pos = full_path.rfind(kSeparator);
if (pos != std::string::npos) {
return full_path.substr(pos + 1, std::string::npos);
} else {
return full_path;
}
}
} // namespace detail
std::ostream& operator<<(std::ostream& out, const SourceLocation& loc) {
out << loc.function << " at " << loc.file << ":" << loc.line;
return out;
}
size_t ReplaceAll(std::string& s, const char* from, const char* to) {
TORCH_CHECK(from && *from, "");
TORCH_CHECK(to, "");
size_t numReplaced = 0;
std::string::size_type lenFrom = std::strlen(from);
std::string::size_type lenTo = std::strlen(to);
for (auto pos = s.find(from); pos != std::string::npos;
pos = s.find(from, pos + lenTo)) {
s.replace(pos, lenFrom, to);
numReplaced++;
}
return numReplaced;
}
} // namespace c10