1 #include <c10/util/StringUtil.h>
2
3 #include <string>
4
5 #ifndef _WIN32
6 #include <codecvt>
7 #include <locale>
8 #else
9 #include <c10/util/Unicode.h>
10 #endif
11
12 namespace c10 {
13
14 namespace detail {
15
StripBasename(const std::string & full_path)16 std::string StripBasename(const std::string& full_path) {
17 #ifdef _WIN32
18 const std::string separators("/\\");
19 #else
20 const std::string separators("/");
21 #endif
22 size_t pos = full_path.find_last_of(separators);
23 if (pos != std::string::npos) {
24 return full_path.substr(pos + 1, std::string::npos);
25 } else {
26 return full_path;
27 }
28 }
29
ExcludeFileExtension(const std::string & file_name)30 std::string ExcludeFileExtension(const std::string& file_name) {
31 const char sep = '.';
32 auto end_index = file_name.find_last_of(sep) == std::string::npos
33 ? -1
34 : file_name.find_last_of(sep);
35 return file_name.substr(0, end_index);
36 }
37
38 // Narrows the wstr argument and then passes it to _str.
39 // Assumes that the input (wide) text is encoded as UTF-16.
40 std::ostream& _strFromWide(std::ostream& ss, const std::wstring& wString);
41
42 #ifndef _WIN32
43
44 #pragma GCC diagnostic push
45 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
46 // TODO (huydhn) https://en.cppreference.com/w/cpp/header/codecvt has been
47 // deprecated in C++17 but there is no alternative yet, so I just ack it
_strFromWide(std::ostream & ss,const std::wstring & wString)48 std::ostream& _strFromWide(std::ostream& ss, const std::wstring& wString) {
49 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
50 return _str(ss, converter.to_bytes(wString));
51 }
52 #pragma GCC diagnostic pop
53
54 #else // #ifndef _WIN32
55 // The WIN32 implementation of wstring_convert leaks memory; see
56 // https://github.com/microsoft/STL/issues/443
57
_strFromWide(std::ostream & ss,const std::wstring & wString)58 std::ostream& _strFromWide(std::ostream& ss, const std::wstring& wString) {
59 return _str(ss, u16u8(wString));
60 }
61
62 #endif // _WIN32
63
_str(std::ostream & ss,const wchar_t * wCStr)64 std::ostream& _str(std::ostream& ss, const wchar_t* wCStr) {
65 return _strFromWide(ss, std::wstring(wCStr));
66 }
_str(std::ostream & ss,const wchar_t & wChar)67 std::ostream& _str(std::ostream& ss, const wchar_t& wChar) {
68 return _strFromWide(ss, std::wstring(1, wChar));
69 }
_str(std::ostream & ss,const std::wstring & wString)70 std::ostream& _str(std::ostream& ss, const std::wstring& wString) {
71 return _strFromWide(ss, wString);
72 }
73
74 } // namespace detail
75
operator <<(std::ostream & out,const SourceLocation & loc)76 std::ostream& operator<<(std::ostream& out, const SourceLocation& loc) {
77 out << loc.function << " at " << loc.file << ":" << loc.line;
78 return out;
79 }
80
ReplaceAll(std::string & s,c10::string_view from,c10::string_view to)81 size_t ReplaceAll(std::string& s, c10::string_view from, c10::string_view to) {
82 if (from.empty()) {
83 return 0;
84 }
85
86 size_t numReplaced = 0;
87 std::string::size_type last_pos = 0u;
88 std::string::size_type cur_pos = 0u;
89 std::string::size_type write_pos = 0u;
90 const c10::string_view input(s);
91
92 if (from.size() >= to.size()) {
93 // If the replacement string is not larger than the original, we
94 // can do the replacement in-place without allocating new storage.
95 char* s_data = &s[0];
96
97 while ((cur_pos = s.find(from.data(), last_pos, from.size())) !=
98 std::string::npos) {
99 ++numReplaced;
100 // Append input between replaced sub-strings
101 if (write_pos != last_pos) {
102 std::copy(s_data + last_pos, s_data + cur_pos, s_data + write_pos);
103 }
104 write_pos += cur_pos - last_pos;
105 // Append the replacement sub-string
106 std::copy(to.begin(), to.end(), s_data + write_pos);
107 write_pos += to.size();
108 // Start search from next character after `from`
109 last_pos = cur_pos + from.size();
110 }
111
112 // Append any remaining input after replaced sub-strings
113 if (write_pos != last_pos) {
114 std::copy(s_data + last_pos, s_data + input.size(), s_data + write_pos);
115 write_pos += input.size() - last_pos;
116 s.resize(write_pos);
117 }
118 return numReplaced;
119 }
120
121 // Otherwise, do an out-of-place replacement in a temporary buffer
122 std::string buffer;
123
124 while ((cur_pos = s.find(from.data(), last_pos, from.size())) !=
125 std::string::npos) {
126 ++numReplaced;
127 // Append input between replaced sub-strings
128 buffer.append(input.begin() + last_pos, input.begin() + cur_pos);
129 // Append the replacement sub-string
130 buffer.append(to.begin(), to.end());
131 // Start search from next character after `from`
132 last_pos = cur_pos + from.size();
133 }
134 if (numReplaced == 0) {
135 // If nothing was replaced, don't modify the input
136 return 0;
137 }
138 // Append any remaining input after replaced sub-strings
139 buffer.append(input.begin() + last_pos, input.end());
140 s = std::move(buffer);
141 return numReplaced;
142 }
143
144 } // namespace c10
145