EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
utfUtils.cpp File Reference
#include "utfUtils.h"
Include dependency graph for utfUtils.cpp:

Go to the source code of this file.

Classes

class  UTFCodeParser
 

Functions

std::string codeToUTF8 (char32_t code)
 
std::u16string codeToUTF16 (char32_t code)
 
std::u16string utf8to16 (std::string &str)
 
std::string utf16to8 (std::u16string &str)
 

Function Documentation

std::u16string codeToUTF16 ( char32_t  code)

Convert a 32 bit character code to a UTF16 string

Parameters
codeThe UTF32 character.
Returns
The UTF16 string. Returns an empty string on failure.

Definition at line 85 of file utfUtils.cpp.

Referenced by UTFCodeParser::getUTF16().

86 {
87  std::u16string out;
88  if (code < 0x010000)
89  {
90  out += (char16_t) code;
91  return out;
92  }
93  char32_t ch = code - 0x010000;
94  char32_t cl = ch & 0x03FF;
95  ch >>= 10;
96  out += (0xD800 + ch);
97  out += (0xDC00 + cl);
98  return out;
99 }

Here is the caller graph for this function:

std::string codeToUTF8 ( char32_t  code)

Convert a 32 bit character code to a UTF8 string

Parameters
codeThe UTF32 character.
Returns
The UTF8 string. Returns an empty string on failure.

Definition at line 33 of file utfUtils.cpp.

Referenced by UTFCodeParser::getUTF8().

34 {
35  std::string out;
36  if (code < 0x80)
37  {
38  out += (char) code;
39  return out;
40  }
41  if (code < 0x800)
42  {
43  out += (char) (0xC0 + ((code >> 6) & 0x1f));
44  out += (char) (0x80 + (code & 0x3f));
45  return out;
46  }
47  if (code < 0x10000)
48  {
49  out += (char) (0xE0 + ((code >> 12) & 0x1f));
50  out += (char) (0x80 + ((code >> 6) & 0x3f));
51  out += (char) (0x80 + (code & 0x3f));
52  return out;
53  }
54  if (code < 0x200000)
55  {
56  out += (char) (0xF0 + ((code >> 18) & 0x1f));
57  out += (char) (0x80 + ((code >> 12) & 0x3f));
58  out += (char) (0x80 + ((code >> 6) & 0x3f));
59  out += (char) (0x80 + (code & 0x3f));
60  return out;
61  }
62  if (code < 0x4000000)
63  {
64  out += (char) (0xF8 + ((code >> 24) & 0x1f));
65  out += (char) (0x80 + ((code >> 18) & 0x3f));
66  out += (char) (0x80 + ((code >> 12) & 0x3f));
67  out += (char) (0x80 + ((code >> 6) & 0x3f));
68  out += (char) (0x80 + (code & 0x3f));
69  return out;
70  }
71  out += (char) (0xFC + ((code >> 30) & 0x1f));
72  out += (char) (0x80 + ((code >> 24) & 0x3f));
73  out += (char) (0x80 + ((code >> 18) & 0x3f));
74  out += (char) (0x80 + ((code >> 12) & 0x3f));
75  out += (char) (0x80 + ((code >> 6) & 0x3f));
76  out += (char) (0x80 + (code & 0x3f));
77  return out;
78 }

Here is the caller graph for this function:

std::string utf16to8 ( std::u16string &  str)

Convert a UTF16 string to a UTF8 string

Parameters
strThe UTF16 string.
Returns
The UTF8 string. Returns an empty string on failure.

Definition at line 309 of file utfUtils.cpp.

References UTFCodeParser::addChar(), and UTFCodeParser::getUTF8().

Referenced by UnmarshalStream::LoadWStringUCS2(), and UnmarshalStream::LoadWStringUCS2Char().

310 {
311  std::string out;
312  UTFCodeParser code;
313  for (char c : str)
314  {
315  int res = code.addChar(c);
316  if (res == -1)
317  {
318  // There was an error.
319  return "";
320  }
321  if (res == 0)
322  {
323  out += code.getUTF8();
324  }
325  }
326  return out;
327 }
std::string getUTF8()
Definition: utfUtils.cpp:218
int addChar(char c)
Definition: utfUtils.cpp:116

Here is the call graph for this function:

Here is the caller graph for this function:

std::u16string utf8to16 ( std::string &  str)

Convert a UTF8 string to a UTF16 string

Parameters
strThe UTF8 string.
Returns
The UTF16 string. Returns an empty string on failure.

Definition at line 289 of file utfUtils.cpp.

References UTFCodeParser::addChar(), and UTFCodeParser::getUTF16().

Referenced by PasswordModule::GeneratePassHash().

290 {
291  std::u16string out;
292  UTFCodeParser code;
293  for (char16_t c : str)
294  {
295  int res = code.addChar(c);
296  if (res == -1)
297  {
298  // There was an error.
299  return u"";
300  }
301  if (res == 0)
302  {
303  out += code.getUTF16();
304  }
305  }
306  return out;
307 }
int addChar(char c)
Definition: utfUtils.cpp:116
std::u16string getUTF16()
Definition: utfUtils.cpp:231

Here is the call graph for this function:

Here is the caller graph for this function: