1 |
786 |
skrzyp |
// This file is part of the uSTL library, an STL implementation.
|
2 |
|
|
//
|
3 |
|
|
// Copyright (c) 2005-2009 by Mike Sharov <msharov@users.sourceforge.net>
|
4 |
|
|
// This file is free software, distributed under the MIT License.
|
5 |
|
|
|
6 |
|
|
#include "stdtest.h"
|
7 |
|
|
|
8 |
|
|
static void Widen (const string& str, vector<wchar_t>& result)
|
9 |
|
|
{
|
10 |
|
|
result.clear();
|
11 |
|
|
result.resize (str.length());
|
12 |
|
|
copy (str.utf8_begin(), str.utf8_end(), result.begin());
|
13 |
|
|
}
|
14 |
|
|
|
15 |
|
|
static void DumpWchars (const vector<wchar_t>& v)
|
16 |
|
|
{
|
17 |
|
|
foreach (vector<wchar_t>::const_iterator, i, v)
|
18 |
|
|
cout.format (" %u", uint32_t(*i));
|
19 |
|
|
}
|
20 |
|
|
|
21 |
|
|
void TestUTF8 (void)
|
22 |
|
|
{
|
23 |
|
|
cout << "Generating Unicode characters ";
|
24 |
|
|
vector<wchar_t> srcChars;
|
25 |
|
|
srcChars.resize (0xFFFF);
|
26 |
|
|
iota (srcChars.begin(), srcChars.end(), 0);
|
27 |
|
|
cout.format ("%zu - %zu\n", size_t(srcChars[0]), size_t(srcChars.back()));
|
28 |
|
|
|
29 |
|
|
cout << "Encoding to utf8.\n";
|
30 |
|
|
string encoded;
|
31 |
|
|
encoded.reserve (srcChars.size() * 4);
|
32 |
|
|
copy (srcChars, utf8out (back_inserter(encoded)));
|
33 |
|
|
static const char c_ProperEncoding[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
|
34 |
|
|
if (encoded.compare (encoded.begin(), encoded.begin() + VectorSize(c_ProperEncoding), VectorRange(c_ProperEncoding))) {
|
35 |
|
|
cout << "Encoding failed: ";
|
36 |
|
|
for (string::const_iterator i = encoded.begin(); i != encoded.begin() + VectorSize(c_ProperEncoding); ++ i)
|
37 |
|
|
cout << uint32_t(*i);
|
38 |
|
|
cout << endl;
|
39 |
|
|
}
|
40 |
|
|
|
41 |
|
|
cout << "Decoding back.\n";
|
42 |
|
|
vector<wchar_t> decChars;
|
43 |
|
|
Widen (encoded, decChars);
|
44 |
|
|
|
45 |
|
|
cout.format ("Comparing.\nsrc = %zu chars, encoded = %zu chars, decoded = %zu\n", srcChars.size(), encoded.size(), decChars.size());
|
46 |
|
|
size_t nDiffs = 0;
|
47 |
|
|
for (uoff_t i = 0; i < min (srcChars.size(), decChars.size()); ++ i) {
|
48 |
|
|
if (srcChars[i] != decChars[i]) {
|
49 |
|
|
cout.format ("%u != %u\n", uint32_t(srcChars[i]), uint32_t(decChars[i]));
|
50 |
|
|
++ nDiffs;
|
51 |
|
|
}
|
52 |
|
|
}
|
53 |
|
|
cout.format ("%zu differences between src and decoded.\n", nDiffs);
|
54 |
|
|
|
55 |
|
|
cout << "Testing wide character string::insert\n";
|
56 |
|
|
string ws ("1234567890", 10);
|
57 |
|
|
|
58 |
|
|
ws.insert (ws.find('1'), wchar_t(1234));
|
59 |
|
|
static const wchar_t c_WChars[2] = { 3456, 4567 };
|
60 |
|
|
ws.insert (ws.find('3'), VectorRange(c_WChars), 2);
|
61 |
|
|
ws.insert (ws.find('3'), wchar_t(2345));
|
62 |
|
|
ws.insert (ws.size(), wchar_t(5678));
|
63 |
|
|
cout.format ("Values[%zu]:", ws.length());
|
64 |
|
|
for (string::utf8_iterator j = ws.utf8_begin(); j < ws.utf8_end(); ++ j)
|
65 |
|
|
cout.format (" %u", uint32_t(*j));
|
66 |
|
|
cout << endl;
|
67 |
|
|
|
68 |
|
|
cout << "Character offsets:";
|
69 |
|
|
for (string::utf8_iterator k = ws.utf8_begin(); k < ws.utf8_end(); ++ k)
|
70 |
|
|
cout.format (" %zu", distance (ws.begin(), k.base()));
|
71 |
|
|
cout << endl;
|
72 |
|
|
|
73 |
|
|
cout.format ("Erasing character %zu: ", ws.length() - 1);
|
74 |
|
|
ws.erase (ws.wiat(ws.length() - 1), ws.end());
|
75 |
|
|
Widen (ws, decChars);
|
76 |
|
|
DumpWchars (decChars);
|
77 |
|
|
cout << endl;
|
78 |
|
|
|
79 |
|
|
cout << "Erasing 2 characters after '2': ";
|
80 |
|
|
ws.erase (ws.find('2')+1, Utf8Bytes(VectorRange(c_WChars)));
|
81 |
|
|
Widen (ws, decChars);
|
82 |
|
|
DumpWchars (decChars);
|
83 |
|
|
cout << endl;
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
StdBvtMain (TestUTF8)
|