Skip to content

Commit 75718fc

Browse files
author
Daniel Kroening
committed
added utf16_to_utf8
1 parent ce035c4 commit 75718fc

File tree

2 files changed

+63
-28
lines changed

2 files changed

+63
-28
lines changed

src/util/unicode.cpp

Lines changed: 62 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,43 @@ std::wstring widen(const std::string &s)
146146

147147
/*******************************************************************\
148148
149-
Function:
149+
Function: utf32_to_utf8
150+
151+
Inputs:
152+
153+
Outputs:
154+
155+
Purpose:
156+
157+
\*******************************************************************/
158+
159+
void utf32_to_utf8(unsigned int c, std::string &result)
160+
{
161+
if(c<=0x7f)
162+
result+=char(c);
163+
else if(c<=0x7ff)
164+
{
165+
result+=char((c >> 6) | 0xc0);
166+
result+=char((c & 0x3f) | 0x80);
167+
}
168+
else if(c<=0xffff)
169+
{
170+
result+=char((c >> 12) | 0xe0);
171+
result+=char(((c >> 6) & 0x3f) | 0x80);
172+
result+=char((c & 0x3f) | 0x80);
173+
}
174+
else
175+
{
176+
result+=char((c >> 18) | 0xf0);
177+
result+=char(((c >> 12) & 0x3f)| 0x80);
178+
result+=char(((c >> 6) & 0x3f) | 0x80);
179+
result+=char((c & 0x3f) | 0x80);
180+
}
181+
}
182+
183+
/*******************************************************************\
184+
185+
Function: utf32_to_utf8
150186
151187
Inputs:
152188
@@ -162,34 +198,32 @@ std::string utf32_to_utf8(const std::basic_string<unsigned int> &s)
162198

163199
result.reserve(s.size()); // at least that long
164200

165-
for(std::basic_string<unsigned int>::const_iterator
166-
it=s.begin();
167-
it!=s.end();
168-
it++)
169-
{
170-
unsigned int c=*it;
201+
for(const auto it : s)
202+
utf32_to_utf8(it, result);
203+
204+
return result;
205+
}
206+
207+
/*******************************************************************\
208+
209+
Function: utf16_to_utf8
210+
211+
Inputs:
212+
213+
Outputs:
214+
215+
Purpose:
216+
217+
\*******************************************************************/
218+
219+
std::string utf16_to_utf8(const std::basic_string<unsigned short int> &s)
220+
{
221+
std::string result;
171222

172-
if(c<=0x7f)
173-
result+=char(c);
174-
else if(c<=0x7ff)
175-
{
176-
result+=char((c >> 6) | 0xc0);
177-
result+=char((c & 0x3f) | 0x80);
178-
}
179-
else if(c<=0xffff)
180-
{
181-
result+=char((c >> 12) | 0xe0);
182-
result+=char(((c >> 6) & 0x3f) | 0x80);
183-
result+=char((c & 0x3f) | 0x80);
184-
}
185-
else
186-
{
187-
result+=char((c >> 18) | 0xf0);
188-
result+=char(((c >> 12) & 0x3f)| 0x80);
189-
result+=char(((c >> 6) & 0x3f) | 0x80);
190-
result+=char((c & 0x3f) | 0x80);
191-
}
192-
}
223+
result.reserve(s.size()); // at least that long
224+
225+
for(const auto it : s)
226+
utf32_to_utf8(it, result);
193227

194228
return result;
195229
}

src/util/unicode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ std::string narrow(const std::wstring &s);
2020
std::wstring widen(const std::string &s);
2121

2222
std::string utf32_to_utf8(const std::basic_string<unsigned int> &s);
23+
std::string utf16_to_utf8(const std::basic_string<unsigned short int> &s);
2324

2425
const char **narrow_argv(int argc, const wchar_t **argv_wide);
2526

0 commit comments

Comments
 (0)