2006-07-07 11:48:37 +00:00
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <vector>
|
2006-07-18 11:35:09 +00:00
|
|
|
#include "../../source/utf8.h"
|
2006-07-07 11:48:37 +00:00
|
|
|
using namespace utf8;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
//append
|
|
|
|
unsigned char u[5] = {0,0,0,0,0};
|
|
|
|
|
|
|
|
unsigned char* end = append(0x0448, u);
|
|
|
|
assert (u[0] == 0xd1 && u[1] == 0x88 && u[2] == 0 && u[3] == 0 && u[4] == 0);
|
|
|
|
|
|
|
|
end = append(0x65e5, u);
|
|
|
|
assert (u[0] == 0xe6 && u[1] == 0x97 && u[2] == 0xa5 && u[3] == 0 && u[4] == 0);
|
|
|
|
|
2006-07-31 22:04:46 +00:00
|
|
|
end = append(0x3044, u);
|
|
|
|
assert (u[0] == 0xe3 && u[1] == 0x81 && u[2] == 0x84 && u[3] == 0 && u[4] == 0);
|
|
|
|
|
2006-07-07 11:48:37 +00:00
|
|
|
end = append(0x10346, u);
|
|
|
|
assert (u[0] == 0xf0 && u[1] == 0x90 && u[2] == 0x8d && u[3] == 0x86 && u[4] == 0);
|
|
|
|
|
2006-11-04 01:28:38 +00:00
|
|
|
|
|
|
|
|
2006-07-07 11:48:37 +00:00
|
|
|
//next
|
2006-08-10 21:51:28 +00:00
|
|
|
char* twochars = "\xe6\x97\xa5\xd1\x88";
|
|
|
|
char* w = twochars;
|
2006-07-07 11:48:37 +00:00
|
|
|
int cp = next(w, twochars + 6);
|
|
|
|
assert (cp == 0x65e5);
|
|
|
|
assert (w == twochars + 3);
|
|
|
|
|
2006-08-10 21:51:28 +00:00
|
|
|
char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88";
|
2006-07-26 17:39:56 +00:00
|
|
|
w = threechars;
|
|
|
|
cp = next(w, threechars + 9);
|
|
|
|
assert (cp == 0x10346);
|
|
|
|
assert (w == threechars + 4);
|
|
|
|
cp = next(w, threechars + 9);
|
|
|
|
assert (cp == 0x65e5);
|
|
|
|
assert (w == threechars + 7);
|
|
|
|
cp = next(w, threechars + 9);
|
|
|
|
assert (cp == 0x0448);
|
|
|
|
assert (w == threechars + 9);
|
|
|
|
|
2006-11-04 01:28:38 +00:00
|
|
|
//prior
|
|
|
|
w = twochars + 3;
|
|
|
|
cp = prior (w, twochars);
|
|
|
|
assert (cp == 0x65e5);
|
|
|
|
assert (w == twochars);
|
|
|
|
|
|
|
|
w = threechars + 9;
|
|
|
|
cp = prior(w, threechars);
|
|
|
|
assert (cp == 0x0448);
|
|
|
|
assert (w == threechars + 7);
|
|
|
|
cp = prior(w, threechars);
|
|
|
|
assert (cp == 0x65e5);
|
|
|
|
assert (w == threechars + 4);
|
|
|
|
cp = prior(w, threechars);
|
|
|
|
assert (cp == 0x10346);
|
|
|
|
assert (w == threechars);
|
2006-07-26 17:39:56 +00:00
|
|
|
|
2006-11-04 01:28:38 +00:00
|
|
|
//previous (deprecated)
|
2006-07-26 17:39:56 +00:00
|
|
|
w = twochars + 3;
|
2006-07-07 11:48:37 +00:00
|
|
|
cp = previous (w, twochars - 1);
|
|
|
|
assert (cp == 0x65e5);
|
|
|
|
assert (w == twochars);
|
|
|
|
|
2006-07-26 17:57:22 +00:00
|
|
|
w = threechars + 9;
|
|
|
|
cp = previous(w, threechars - 1);
|
|
|
|
assert (cp == 0x0448);
|
|
|
|
assert (w == threechars + 7);
|
|
|
|
cp = previous(w, threechars -1);
|
|
|
|
assert (cp == 0x65e5);
|
|
|
|
assert (w == threechars + 4);
|
|
|
|
cp = previous(w, threechars - 1);
|
|
|
|
assert (cp == 0x10346);
|
|
|
|
assert (w == threechars);
|
|
|
|
|
2006-07-07 11:48:37 +00:00
|
|
|
// advance
|
|
|
|
w = twochars;
|
|
|
|
advance (w, 2, twochars + 6);
|
|
|
|
assert (w == twochars + 5);
|
|
|
|
|
|
|
|
// distance
|
|
|
|
size_t dist = utf8::distance(twochars, twochars + 5);
|
|
|
|
assert (dist == 2);
|
|
|
|
|
|
|
|
// utf32to8
|
|
|
|
int utf32string[] = {0x448, 0x65E5, 0x10346, 0};
|
2006-08-15 01:54:49 +00:00
|
|
|
vector<char> utf8result;
|
2006-07-07 11:48:37 +00:00
|
|
|
utf32to8(utf32string, utf32string + 3, back_inserter(utf8result));
|
|
|
|
assert (utf8result.size() == 9);
|
2006-08-15 01:54:49 +00:00
|
|
|
// try it with the return value;
|
|
|
|
char* utf8_end = utf32to8(utf32string, utf32string + 3, &utf8result[0]);
|
|
|
|
assert (utf8_end == &utf8result[0] + 9);
|
2006-07-07 11:48:37 +00:00
|
|
|
|
|
|
|
//utf8to32
|
|
|
|
vector<int> utf32result;
|
|
|
|
utf8to32(twochars, twochars + 5, back_inserter(utf32result));
|
|
|
|
assert (utf32result.size() == 2);
|
2006-08-15 01:54:49 +00:00
|
|
|
// try it with the return value;
|
|
|
|
int* utf32_end = utf8to32(twochars, twochars + 5, &utf32result[0]);
|
|
|
|
assert (utf32_end == &utf32result[0] + 2);
|
2006-07-07 11:48:37 +00:00
|
|
|
|
|
|
|
//utf16to8
|
|
|
|
unsigned short utf16string[] = {0x41, 0x0448, 0x65e5, 0xd834, 0xdd1e};
|
|
|
|
utf8result.clear();
|
|
|
|
utf16to8(utf16string, utf16string + 5, back_inserter(utf8result));
|
|
|
|
assert (utf8result.size() == 10);
|
2006-08-15 01:54:49 +00:00
|
|
|
// try it with the return value;
|
|
|
|
utf8_end = utf16to8 (utf16string, utf16string + 5, &utf8result[0]);
|
|
|
|
assert (utf8_end == &utf8result[0] + 10);
|
2006-07-07 11:48:37 +00:00
|
|
|
|
|
|
|
//utf8to16
|
2006-08-15 01:54:49 +00:00
|
|
|
char utf8_with_surrogates[] = "\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e";
|
2006-07-07 11:48:37 +00:00
|
|
|
vector <unsigned short> utf16result;
|
|
|
|
utf8to16(utf8_with_surrogates, utf8_with_surrogates + 9, back_inserter(utf16result));
|
|
|
|
assert (utf16result.size() == 4);
|
|
|
|
assert (utf16result[2] == 0xd834);
|
|
|
|
assert (utf16result[3] == 0xdd1e);
|
2006-08-15 01:54:49 +00:00
|
|
|
// try it with the return value;
|
|
|
|
unsigned short* utf16_end = utf8to16 (utf8_with_surrogates, utf8_with_surrogates + 9, &utf16result[0]);
|
|
|
|
assert (utf16_end == &utf16result[0] + 4);
|
2006-07-07 11:48:37 +00:00
|
|
|
|
|
|
|
//find_invalid
|
2006-08-23 21:39:13 +00:00
|
|
|
char utf_invalid[] = "\xe6\x97\xa5\xd1\x88\xfa";
|
|
|
|
char* invalid = find_invalid(utf_invalid, utf_invalid + 6);
|
2006-07-07 11:48:37 +00:00
|
|
|
assert (invalid == utf_invalid + 5);
|
|
|
|
|
|
|
|
//is_valid
|
|
|
|
bool bvalid = is_valid(utf_invalid, utf_invalid + 6);
|
|
|
|
assert (bvalid == false);
|
|
|
|
bvalid = is_valid(utf8_with_surrogates, utf8_with_surrogates + 9);
|
|
|
|
assert (bvalid == true);
|
|
|
|
|
|
|
|
//is_bom
|
|
|
|
unsigned char byte_order_mark[] = {0xef, 0xbb, 0xbf};
|
|
|
|
bool bbom = is_bom(byte_order_mark);
|
|
|
|
assert (bbom == true);
|
2006-10-07 21:25:47 +00:00
|
|
|
|
|
|
|
//replace_invalid
|
|
|
|
char invalid_sequence[] = "a\x80\xe0\xa0\xc0\xaf\xed\xa0\x80z";
|
|
|
|
vector<char> replace_invalid_result;
|
|
|
|
replace_invalid (invalid_sequence, invalid_sequence + sizeof(invalid_sequence), back_inserter(replace_invalid_result), '?');
|
|
|
|
bvalid = is_valid(replace_invalid_result.begin(), replace_invalid_result.end());
|
|
|
|
assert (bvalid);
|
|
|
|
char* fixed_invalid_sequence = "a????z";
|
|
|
|
assert (std::equal(replace_invalid_result.begin(), replace_invalid_result.end(), fixed_invalid_sequence));
|
2006-07-07 11:48:37 +00:00
|
|
|
|
2006-10-28 16:25:52 +00:00
|
|
|
// iterator
|
2006-11-04 01:28:38 +00:00
|
|
|
utf8::iterator<char*> it(threechars, threechars, threechars + 9);
|
2006-10-28 16:25:52 +00:00
|
|
|
utf8::iterator<char*> it2 = it;
|
|
|
|
assert (it2 == it);
|
|
|
|
assert (*it == 0x10346);
|
|
|
|
assert (*(++it) == 0x65e5);
|
|
|
|
assert ((*it++) == 0x65e5);
|
|
|
|
assert (*it == 0x0448);
|
2006-11-04 01:28:38 +00:00
|
|
|
utf8::iterator<char*> endit (threechars + 9, threechars, threechars + 9);
|
2006-10-28 16:25:52 +00:00
|
|
|
assert (++it == endit);
|
|
|
|
assert (*(--it) == 0x0448);
|
|
|
|
assert ((*it--) == 0x0448);
|
|
|
|
assert (*it == 0x65e5);
|
2006-11-04 01:28:38 +00:00
|
|
|
assert (--it == utf8::iterator<char*>(threechars, threechars, threechars + 9));
|
2006-10-28 16:25:52 +00:00
|
|
|
assert (*it == 0x10346);
|
|
|
|
|
2006-07-07 11:48:37 +00:00
|
|
|
//////////////////////////////////////////////////////////
|
|
|
|
//// Unchecked variants
|
|
|
|
//////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
//append
|
|
|
|
memset(u, 0, 5);
|
|
|
|
end = unchecked::append(0x0448, u);
|
|
|
|
assert (u[0] == 0xd1 && u[1] == 0x88 && u[2] == 0 && u[3] == 0 && u[4] == 0);
|
|
|
|
|
|
|
|
end = unchecked::append(0x65e5, u);
|
|
|
|
assert (u[0] == 0xe6 && u[1] == 0x97 && u[2] == 0xa5 && u[3] == 0 && u[4] == 0);
|
|
|
|
|
|
|
|
end = unchecked::append(0x10346, u);
|
|
|
|
assert (u[0] == 0xf0 && u[1] == 0x90 && u[2] == 0x8d && u[3] == 0x86 && u[4] == 0);
|
|
|
|
|
|
|
|
//next
|
|
|
|
w = twochars;
|
|
|
|
cp = unchecked::next(w);
|
|
|
|
assert (cp == 0x65e5);
|
|
|
|
assert (w == twochars + 3);
|
|
|
|
|
2006-07-26 17:39:56 +00:00
|
|
|
w = threechars;
|
|
|
|
cp = unchecked::next(w);
|
|
|
|
assert (cp == 0x10346);
|
|
|
|
assert (w == threechars + 4);
|
|
|
|
cp = unchecked::next(w);
|
|
|
|
assert (cp == 0x65e5);
|
|
|
|
assert (w == threechars + 7);
|
|
|
|
cp = unchecked::next(w);
|
|
|
|
assert (cp == 0x0448);
|
|
|
|
assert (w == threechars + 9);
|
|
|
|
|
|
|
|
|
2006-11-04 01:28:38 +00:00
|
|
|
//previous (calls prior internally)
|
|
|
|
|
2006-07-26 17:39:56 +00:00
|
|
|
w = twochars + 3;
|
2006-07-07 11:48:37 +00:00
|
|
|
cp = unchecked::previous (w);
|
|
|
|
assert (cp == 0x65e5);
|
|
|
|
assert (w == twochars);
|
|
|
|
|
2006-07-26 17:57:22 +00:00
|
|
|
w = threechars + 9;
|
|
|
|
cp = unchecked::previous(w);
|
|
|
|
assert (cp == 0x0448);
|
|
|
|
assert (w == threechars + 7);
|
|
|
|
cp = unchecked::previous(w);
|
|
|
|
assert (cp == 0x65e5);
|
|
|
|
assert (w == threechars + 4);
|
|
|
|
cp = unchecked::previous(w);
|
|
|
|
assert (cp == 0x10346);
|
|
|
|
assert (w == threechars);
|
|
|
|
|
2006-07-07 11:48:37 +00:00
|
|
|
// advance
|
|
|
|
w = twochars;
|
|
|
|
unchecked::advance (w, 2);
|
|
|
|
assert (w == twochars + 5);
|
|
|
|
|
|
|
|
// distance
|
|
|
|
dist = unchecked::distance(twochars, twochars + 5);
|
|
|
|
assert (dist == 2);
|
|
|
|
|
|
|
|
// utf32to8
|
|
|
|
utf8result.clear();
|
|
|
|
unchecked::utf32to8(utf32string, utf32string + 3, back_inserter(utf8result));
|
|
|
|
assert (utf8result.size() == 9);
|
2006-08-15 01:54:49 +00:00
|
|
|
// try it with the return value;
|
|
|
|
utf8_end = utf32to8(utf32string, utf32string + 3, &utf8result[0]);
|
|
|
|
assert(utf8_end == &utf8result[0] + 9);
|
2006-07-07 11:48:37 +00:00
|
|
|
|
|
|
|
//utf8to32
|
|
|
|
utf32result.clear();
|
|
|
|
unchecked::utf8to32(twochars, twochars + 5, back_inserter(utf32result));
|
|
|
|
assert (utf32result.size() == 2);
|
2006-08-15 01:54:49 +00:00
|
|
|
// try it with the return value;
|
|
|
|
utf32_end = utf8to32(twochars, twochars + 5, &utf32result[0]);
|
|
|
|
assert (utf32_end == &utf32result[0] + 2);
|
2006-07-07 11:48:37 +00:00
|
|
|
|
|
|
|
//utf16to8
|
|
|
|
utf8result.clear();
|
|
|
|
unchecked::utf16to8(utf16string, utf16string + 5, back_inserter(utf8result));
|
|
|
|
assert (utf8result.size() == 10);
|
2006-08-15 01:54:49 +00:00
|
|
|
// try it with the return value;
|
|
|
|
utf8_end = utf16to8 (utf16string, utf16string + 5, &utf8result[0]);
|
|
|
|
assert (utf8_end == &utf8result[0] + 10);
|
2006-07-07 11:48:37 +00:00
|
|
|
|
|
|
|
//utf8to16
|
|
|
|
utf16result.clear();
|
|
|
|
unchecked::utf8to16(utf8_with_surrogates, utf8_with_surrogates + 9, back_inserter(utf16result));
|
|
|
|
assert (utf16result.size() == 4);
|
|
|
|
assert (utf16result[2] == 0xd834);
|
|
|
|
assert (utf16result[3] == 0xdd1e);
|
2006-08-15 01:54:49 +00:00
|
|
|
// try it with the return value;
|
|
|
|
utf16_end = utf8to16 (utf8_with_surrogates, utf8_with_surrogates + 9, &utf16result[0]);
|
|
|
|
assert (utf16_end == &utf16result[0] + 4);
|
2006-11-23 18:10:26 +00:00
|
|
|
|
|
|
|
// iterator
|
|
|
|
utf8::unchecked::iterator<char*> un_it(threechars);
|
|
|
|
utf8::unchecked::iterator<char*> un_it2 = un_it;
|
|
|
|
assert (un_it2 == un_it);
|
|
|
|
assert (*un_it == 0x10346);
|
|
|
|
assert (*(++un_it) == 0x65e5);
|
|
|
|
assert ((*un_it++) == 0x65e5);
|
|
|
|
assert (*un_it == 0x0448);
|
|
|
|
utf8::unchecked::iterator<char*> un_endit (threechars + 9);
|
|
|
|
assert (++un_it == un_endit);
|
|
|
|
assert (*(--un_it) == 0x0448);
|
|
|
|
assert ((*un_it--) == 0x0448);
|
|
|
|
assert (*un_it == 0x65e5);
|
|
|
|
assert (--un_it == utf8::unchecked::iterator<char*>(threechars));
|
|
|
|
assert (*un_it == 0x10346);
|
2006-07-07 11:48:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|