Skip to content

Commit 3dbceb9

Browse files
author
Aidan Lee
committed
Add new view extension cstring function tests
1 parent 485e8b7 commit 3dbceb9

File tree

8 files changed

+106
-80
lines changed

8 files changed

+106
-80
lines changed

src/cpp/encoding/Utf16.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,42 @@ namespace
1919
{
2020
return codepoint >= 0xd800 && codepoint < 0xdc00;
2121
}
22+
23+
bool isAsciiBuffer(View<uint8_t> buffer)
24+
{
25+
while (buffer.isEmpty() == false)
26+
{
27+
auto p = cpp::encoding::Utf16::codepoint(buffer);
28+
29+
if (p > 127)
30+
{
31+
return false;
32+
}
33+
34+
buffer = buffer.slice(cpp::encoding::Utf16::getByteCount(p));
35+
}
36+
37+
return true;
38+
}
39+
40+
String toAsciiString(View<uint8_t> buffer)
41+
{
42+
auto bytes = buffer.length / sizeof(char16_t);
43+
auto chars = View<char>(hx::InternalNew(bytes + 1, false), bytes * sizeof(char));
44+
auto output = chars.reinterpret<uint8_t>();
45+
46+
while (buffer.isEmpty() == false)
47+
{
48+
auto p = cpp::encoding::Utf16::codepoint(buffer);
49+
50+
output[0] = static_cast<uint8_t>(p);
51+
52+
buffer = buffer.slice(cpp::encoding::Utf16::getByteCount(p));
53+
output = output.slice(1);
54+
}
55+
56+
return String(chars.ptr.ptr, chars.length);
57+
}
2258
}
2359

2460
bool cpp::encoding::Utf16::isEncoded(const String& string)
@@ -174,6 +210,11 @@ String cpp::encoding::Utf16::decode(cpp::marshal::View<uint8_t> buffer)
174210
return String::emptyString;
175211
}
176212

213+
if (isAsciiBuffer(buffer))
214+
{
215+
return toAsciiString(buffer);
216+
}
217+
177218
auto chars = int64_t{ 0 };
178219
auto i = int64_t{ 0 };
179220
while (i < buffer.length)

src/cpp/encoding/Utf8.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ using namespace cpp::marshal;
55

66
namespace
77
{
8-
bool isAsciiBuffer(View<uint8_t>& buffer)
8+
bool isAsciiBuffer(View<uint8_t> buffer)
99
{
10-
for (auto i = int64_t{ 0 }; i < buffer.length; i++)
10+
while (buffer.isEmpty() == false)
1111
{
12-
if (buffer.ptr[i] > 127)
12+
auto p = cpp::encoding::Utf8::codepoint(buffer);
13+
14+
if (p > 127)
1315
{
1416
return false;
1517
}
18+
19+
buffer = buffer.slice(cpp::encoding::Utf8::getByteCount(p));
1620
}
1721

1822
return true;

test/native/cpp/encoding/Ascii.hx

Lines changed: 0 additions & 15 deletions
This file was deleted.

test/native/cpp/encoding/Utf16.hx

Lines changed: 0 additions & 22 deletions
This file was deleted.

test/native/cpp/encoding/Utf8.hx

Lines changed: 0 additions & 20 deletions
This file was deleted.

test/native/tests/encoding/TestUtf16.hx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,23 +141,17 @@ class TestUtf16 extends Test {
141141
}
142142

143143
public function test_decode_codepoint() {
144-
var codepoint : cpp.Char32 = 0;
145-
146144
var bytes = Bytes.ofHex('6100');
147-
Assert.equals(2i64, Utf16.decode(bytes.asView(), codepoint));
148-
Assert.equals('a'.code, cast codepoint);
145+
Assert.equals('a'.code, Utf16.codepoint(bytes.asView()));
149146

150147
var bytes = Bytes.ofHex('8501');
151-
Assert.equals(2i64, Utf16.decode(bytes.asView(), codepoint));
152-
Assert.equals('ƅ'.code, cast codepoint);
148+
Assert.equals('ƅ'.code, Utf16.codepoint(bytes.asView()));
153149

154150
var bytes = Bytes.ofHex('D030');
155-
Assert.equals(2i64, Utf16.decode(bytes.asView(), codepoint));
156-
Assert.equals(''.code, cast codepoint);
151+
Assert.equals(''.code, Utf16.codepoint(bytes.asView()));
157152

158153
var bytes = Bytes.ofHex('34D833DD');
159-
Assert.equals(4i64, Utf16.decode(bytes.asView(), codepoint));
160-
Assert.equals('𝄳'.code, cast codepoint);
154+
Assert.equals('𝄳'.code, Utf16.codepoint(bytes.asView()));
161155
}
162156

163157
public function test_decode_string() {

test/native/tests/encoding/TestUtf8.hx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,23 +118,17 @@ class TestUtf8 extends Test {
118118
}
119119

120120
public function test_decode_codepoint() {
121-
var codepoint : cpp.Char32 = 0;
122-
123121
var bytes = Bytes.ofHex('61');
124-
Assert.equals(1i64, Utf8.decode(bytes.asView(), codepoint));
125-
Assert.equals('a'.code, cast codepoint);
122+
Assert.equals('a'.code, Utf8.codepoint(bytes.asView()));
126123

127124
var bytes = Bytes.ofHex('c685');
128-
Assert.equals(2i64, Utf8.decode(bytes.asView(), codepoint));
129-
Assert.equals('ƅ'.code, cast codepoint);
125+
Assert.equals('ƅ'.code, Utf8.codepoint(bytes.asView()));
130126

131127
var bytes = Bytes.ofHex('e38390');
132-
Assert.equals(3i64, Utf8.decode(bytes.asView(), codepoint));
133-
Assert.equals(''.code, cast codepoint);
128+
Assert.equals(''.code, Utf8.codepoint(bytes.asView()));
134129

135130
var bytes = Bytes.ofHex('f09d84b3');
136-
Assert.equals(4i64, Utf8.decode(bytes.asView(), codepoint));
137-
Assert.equals('𝄳'.code, cast codepoint);
131+
Assert.equals('𝄳'.code, Utf8.codepoint(bytes.asView()));
138132
}
139133

140134
public function test_decode_string() {

test/native/tests/marshalling/view/TestViewExtensions.hx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,54 @@ class TestViewExtensions extends Test {
162162
}
163163
}
164164
}
165+
166+
function test_szToString_char_no_null() {
167+
final vec = new Vector<cpp.Char>(4);
168+
vec[0] = 't'.code;
169+
vec[1] = 'e'.code;
170+
vec[2] = 's'.code;
171+
vec[3] = 't'.code;
172+
173+
Assert.equals("test", vec.asView().szToString());
174+
}
175+
176+
function test_szToString_char() {
177+
final vec = new Vector<cpp.Char>(9);
178+
vec[0] = 't'.code;
179+
vec[1] = 'e'.code;
180+
vec[2] = 's'.code;
181+
vec[3] = 't'.code;
182+
vec[4] = 0;
183+
vec[5] = 't'.code;
184+
vec[6] = 'e'.code;
185+
vec[7] = 's'.code;
186+
vec[8] = 't'.code;
187+
188+
Assert.equals("test", vec.asView().szToString());
189+
}
190+
191+
function test_szToString_char16_no_null() {
192+
final vec = new Vector<cpp.Char16>(4);
193+
vec[0] = 't'.code;
194+
vec[1] = 'e'.code;
195+
vec[2] = 's'.code;
196+
vec[3] = 't'.code;
197+
198+
Assert.equals("test", vec.asView().szToString());
199+
}
200+
201+
function test_szToString16_char() {
202+
final vec = new Vector<cpp.Char16>(9);
203+
vec[0] = 't'.code;
204+
vec[1] = 'e'.code;
205+
vec[2] = 's'.code;
206+
vec[3] = 't'.code;
207+
vec[4] = 0;
208+
vec[5] = 't'.code;
209+
vec[6] = 'e'.code;
210+
vec[7] = 's'.code;
211+
vec[8] = 't'.code;
212+
213+
Assert.equals("test", vec.asView().szToString());
214+
}
165215
}

0 commit comments

Comments
 (0)