pytorch/c10/util/StringUtil.cpp
Scott Wolchok 44cc873fba [PyTorch] Autoformat c10 (#56830)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56830

Opt into formatting on GitHub and format everything. This is a trial run before turning on formatting for more and eventually all of the codebase.

Test Plan: CI

Reviewed By: zertosh

Differential Revision: D27979080

fbshipit-source-id: a80f0c48691c08ae8ca0af06377b87e6a2351151
2021-04-30 21:23:28 -07:00

52 lines
1.3 KiB
C++

#include <c10/util/Exception.h>
#include <c10/util/StringUtil.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;
}
}
std::string ExcludeFileExtension(const std::string& file_name) {
const char sep = '.';
auto end_index = file_name.find_last_of(sep) == std::string::npos
? -1
: file_name.find_last_of(sep);
return file_name.substr(0, end_index);
}
} // 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