Skip to content

Commit b49d07f

Browse files
committed
Rework utf16-to-ascii to avoid snprintf
There isn't a standard snprintf in MS toolchains until VS 2015, so use C++ iostreams stuff instead.
1 parent ef5f3ca commit b49d07f

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/util/unicode.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Author: Daniel Kroening, [email protected]
99
#include <cstring>
1010
#include <locale>
1111
#include <codecvt>
12+
#include <sstream>
13+
#include <iomanip>
1214

1315
#include "unicode.h"
1416

@@ -277,19 +279,20 @@ std::wstring utf8_to_utf16_little_endian(const std::string& in)
277279

278280
std::string utf16_little_endian_to_ascii(const std::wstring& in)
279281
{
280-
std::string result;
282+
std::ostringstream result;
281283
std::locale loc;
282284
for(const auto c : in)
283285
{
284286
if(c<=255 && isprint(c, loc))
285-
result+=(unsigned char)c;
287+
result << (unsigned char)c;
286288
else
287289
{
288-
result+="\\u";
289-
char hex[5];
290-
snprintf(hex, sizeof(hex), "%04x", (wchar_t)c);
291-
result+=hex;
290+
result << "\\u"
291+
<< std::hex
292+
<< std::setw(4)
293+
<< std::setfill('0')
294+
<< (unsigned int)c;
292295
}
293296
}
294-
return result;
297+
return result.str();
295298
}

0 commit comments

Comments
 (0)