-
Notifications
You must be signed in to change notification settings - Fork 217
Expand file tree
/
Copy pathAscii.cpp
More file actions
66 lines (52 loc) · 1.21 KB
/
Ascii.cpp
File metadata and controls
66 lines (52 loc) · 1.21 KB
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
58
59
60
61
62
63
64
65
66
#include <hxcpp.h>
using namespace cpp::marshal;
bool cpp::encoding::Ascii::isEncoded(const String& string)
{
if (null() == string)
{
hx::NullReference("String", false);
}
return string.isAsciiEncoded();
}
int64_t cpp::encoding::Ascii::encode(const String& string, View<uint8_t> buffer)
{
if (null() == string)
{
hx::NullReference("String", false);
}
if (string.isUTF16Encoded())
{
hx::Throw(HX_CSTRING("String cannot be encoded to ASCII"));
}
auto src = cpp::marshal::View<char>(string.raw_ptr(), string.length).reinterpret<uint8_t>();
if (src.tryCopyTo(buffer))
{
return src.length;
}
else
{
return hx::Throw(HX_CSTRING("Buffer too small"));
}
}
String cpp::encoding::Ascii::decode(View<uint8_t> view)
{
if (view.isEmpty())
{
return hx::Throw(HX_CSTRING("View is empty"));
}
auto bytes = int64_t{ 0 };
auto i = int64_t{ 0 };
auto chars = view.reinterpret<char>();
while (i < chars.length && 0 != chars.ptr[i])
{
bytes += sizeof(char);
i++;
}
if (0 == bytes)
{
return String::emptyString;
}
auto backing = hx::NewGCPrivate(0, bytes + sizeof(char));
std::memcpy(backing, view.ptr.ptr, bytes);
return String(static_cast<const char*>(backing), bytes / sizeof(char));
}