-
Notifications
You must be signed in to change notification settings - Fork 5
/
Strings.cpp
57 lines (49 loc) · 1.19 KB
/
Strings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "Strings.hpp"
#include "MemoryFile.hpp"
#include "deps/utf8.h"
#include <cstring>
#include <cstdio>
BEGIN_INANITY
std::string Strings::Unicode2UTF8(const std::wstring& str)
{
std::string res;
utf8::unchecked::utf16to8(str.begin(), str.end(), std::back_inserter(res));
return res;
}
std::wstring Strings::UTF82Unicode(const std::string& str)
{
std::wstring res;
utf8::unchecked::utf8to16(str.begin(), str.end(), std::back_inserter(res));
return res;
}
ptr<File> Strings::String2File(const std::string& str)
{
ptr<File> file = NEW(MemoryFile(str.length()));
memcpy(file->GetData(), str.c_str(), file->GetSize());
return file;
}
std::string Strings::File2String(ptr<File> file)
{
char* str = (char*)file->GetData();
return std::string(str, str + file->GetSize());
}
String Strings::ToString(int number)
{
char str[11];
sprintf(str, "%d", number);
return str;
}
String Strings::ToHex(const void* data, size_t size)
{
static const char hex[] = "0123456789abcdef";
String str(size * 2, ' ');
size_t j = 0;
for(size_t i = 0; i < size; ++i)
{
unsigned char byte = ((const unsigned char*)data)[i];
str[j++] = hex[byte >> 4];
str[j++] = hex[byte & 15];
}
return str;
}
END_INANITY