File tree Expand file tree Collapse file tree 1 file changed +19
-24
lines changed
Expand file tree Collapse file tree 1 file changed +19
-24
lines changed Original file line number Diff line number Diff 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+
3234std::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
You can’t perform that action at this time.
0 commit comments