Skip to content

Commit 61aafe7

Browse files
committed
widen() and narrow() are now implemented on non-win32 platforms
These functions have previously only been implemented on WIN32. This commit adds an implementation for the other platforms.
1 parent e3189d0 commit 61aafe7

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

src/util/unicode.cpp

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Author: Daniel Kroening, kroening@kroening.com
2929
# include <windows.h>
3030
#endif
3131

32+
static void utf8_append_code(unsigned int c, std::string &);
33+
3234
std::string narrow(const wchar_t *s)
3335
{
3436
#ifdef _WIN32
@@ -41,16 +43,7 @@ std::string narrow(const wchar_t *s)
4143
return r;
4244

4345
#else
44-
// dummy conversion
45-
std::string r;
46-
r.reserve(wcslen(s));
47-
while(*s != 0)
48-
{
49-
r += static_cast<char>(*s);
50-
s++;
51-
}
52-
53-
return r;
46+
return narrow(std::wstring(s));
5447
#endif
5548
}
5649

@@ -65,16 +58,7 @@ std::wstring widen(const char *s)
6558
return r;
6659

6760
#else
68-
// dummy conversion
69-
std::wstring r;
70-
r.reserve(strlen(s));
71-
while(*s != 0)
72-
{
73-
r += wchar_t(*s);
74-
s++;
75-
}
76-
77-
return r;
61+
return widen(std::string(s));
7862
#endif
7963
}
8064

@@ -90,8 +74,14 @@ std::string narrow(const std::wstring &s)
9074
return r;
9175

9276
#else
93-
// dummy conversion
94-
return std::string(s.begin(), s.end());
77+
std::string result;
78+
79+
result.reserve(s.size()); // at least that long
80+
81+
for(const auto codepoint : s)
82+
utf8_append_code(codepoint, result);
83+
84+
return result;
9585
#endif
9686
}
9787

@@ -106,8 +96,13 @@ std::wstring widen(const std::string &s)
10696
return r;
10797

10898
#else
109-
// dummy conversion
110-
return std::wstring(s.begin(), s.end());
99+
auto utf32 = utf8_to_utf32(std::string(s));
100+
101+
std::wstring r;
102+
r.reserve(utf32.size());
103+
for(auto codepoint : utf32)
104+
r += codepoint;
105+
return r;
111106
#endif
112107
}
113108

0 commit comments

Comments
 (0)