MCPcopy Create free account
hub / github.com/Kitware/CMake / ToNarrow

Method ToNarrow

Source/kwsys/EncodingCXX.cxx:159–191  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

157}
158
159std::string Encoding::ToNarrow(std::wstring const& str)
160{
161 std::string nstr;
162#if defined(_WIN32)
163 int length =
164 WideCharToMultiByte(KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str.c_str(),
165 int(str.size()), nullptr, 0, nullptr, nullptr);
166 if (length > 0) {
167 char* data = new char[length];
168 int r =
169 WideCharToMultiByte(KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str.c_str(),
170 int(str.size()), data, length, nullptr, nullptr);
171 if (r > 0) {
172 nstr = std::string(data, length);
173 }
174 delete[] data;
175 }
176#else
177 size_t pos = 0;
178 size_t nullPos = 0;
179 do {
180 if (pos < str.size() && str.at(pos) != '\0') {
181 nstr += ToNarrow(str.c_str() + pos);
182 }
183 nullPos = str.find(wchar_t('\0'), pos);
184 if (nullPos != std::string::npos) {
185 pos = nullPos + 1;
186 nstr += '\0';
187 }
188 } while (nullPos != std::string::npos);
189#endif
190 return nstr;
191}
192
193std::wstring Encoding::ToWide(char const* cstr)
194{

Callers

nothing calls this directly

Calls 6

kwsysEncoding_wcstombsFunction · 0.85
c_strMethod · 0.80
sizeMethod · 0.45
atMethod · 0.45
findMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected