Skip to content

Commit e433cd4

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 0541fcd commit e433cd4

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/util/unicode.cpp

+10-7
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 <iomanip>
13+
#include <sstream>
1214

1315
#include "unicode.h"
1416

@@ -282,19 +284,20 @@ std::wstring utf8_to_utf16_little_endian(const std::string& in)
282284

283285
std::string utf16_little_endian_to_ascii(const std::wstring& in)
284286
{
285-
std::string result;
287+
std::ostringstream result;
286288
std::locale loc;
287289
for(const auto c : in)
288290
{
289291
if(c<=255 && isprint(c, loc))
290-
result+=(unsigned char)c;
292+
result << (unsigned char)c;
291293
else
292294
{
293-
result+="\\u";
294-
char hex[5];
295-
snprintf(hex, sizeof(hex), "%04x", (wchar_t)c);
296-
result+=hex;
295+
result << "\\u"
296+
<< std::hex
297+
<< std::setw(4)
298+
<< std::setfill('0')
299+
<< (unsigned int)c;
297300
}
298301
}
299-
return result;
302+
return result.str();
300303
}

0 commit comments

Comments
 (0)