OpenTTD Source 15.0-beta2
utf8.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10#include "../stdafx.h"
11
12#include "../3rdparty/catch2/catch.hpp"
13
14#include "../core/utf8.hpp"
15
16#include "../safeguards.h"
17
18using namespace std::literals;
19
20TEST_CASE("Utf8View - empty")
21{
22 Utf8View view;
23 auto begin = view.begin();
24 auto end = view.end();
25 CHECK(begin == end);
26 CHECK(begin.GetByteOffset() == 0);
27}
28
29TEST_CASE("Utf8View - invalid")
30{
31 Utf8View view("\u1234\x80\x80""a\xFF\x80\x80\x80\x80\x80""b\xF0");
32 auto begin = view.begin();
33 auto end = view.end();
34 CHECK(begin < end);
35 auto it = begin;
36 CHECK(it == begin);
37 CHECK(it.GetByteOffset() == 0);
38 CHECK(*it == 0x1234);
39 ++it;
40 CHECK(begin < it);
41 CHECK(it < end);
42 CHECK(it.GetByteOffset() == 5);
43 CHECK(*it == 'a');
44 ++it;
45 CHECK(begin < it);
46 CHECK(it < end);
47 CHECK(it.GetByteOffset() == 6);
48 CHECK(*it == '?');
49 ++it;
50 CHECK(begin < it);
51 CHECK(it < end);
52 CHECK(it.GetByteOffset() == 12);
53 CHECK(*it == 'b');
54 ++it;
55 CHECK(begin < it);
56 CHECK(it < end);
57 CHECK(it.GetByteOffset() == 13);
58 CHECK(*it == '?');
59 ++it;
60 CHECK(it.GetByteOffset() == 14);
61 CHECK(begin < it);
62 CHECK(it == end);
63 --it;
64 CHECK(begin < it);
65 CHECK(it < end);
66 CHECK(it.GetByteOffset() == 13);
67 CHECK(*it == '?');
68 --it;
69 CHECK(begin < it);
70 CHECK(it < end);
71 CHECK(it.GetByteOffset() == 12);
72 CHECK(*it == 'b');
73 --it;
74 CHECK(begin < it);
75 CHECK(it < end);
76 CHECK(it.GetByteOffset() == 6);
77 CHECK(*it == '?');
78 --it;
79 CHECK(begin < it);
80 CHECK(it < end);
81 CHECK(it.GetByteOffset() == 5);
82 CHECK(*it == 'a');
83 --it;
84 CHECK(it == begin);
85 CHECK(it.GetByteOffset() == 0);
86 CHECK(*it == 0x1234);
87}
88
89TEST_CASE("Utf8View - iterate")
90{
91 Utf8View view("\u1234a\0b\U00012345"sv);
92 auto begin = view.begin();
93 auto end = view.end();
94 CHECK(begin < end);
95 auto it = begin;
96 CHECK(it == begin);
97 CHECK(it.GetByteOffset() == 0);
98 CHECK(std::distance(begin, it) == 0);
99 CHECK(std::distance(it, end) == 5);
100 CHECK(*it == 0x1234);
101 CHECK(it == view.GetIterAtByte(0));
102 CHECK(it == view.GetIterAtByte(1));
103 CHECK(it == view.GetIterAtByte(2));
104 ++it;
105 CHECK(begin < it);
106 CHECK(it < end);
107 CHECK(it.GetByteOffset() == 3);
108 CHECK(std::distance(begin, it) == 1);
109 CHECK(std::distance(it, end) == 4);
110 CHECK(*it == 'a');
111 CHECK(it == view.GetIterAtByte(3));
112 ++it;
113 CHECK(it.GetByteOffset() == 4);
114 CHECK(std::distance(begin, it) == 2);
115 CHECK(std::distance(it, end) == 3);
116 CHECK(*it == 0);
117 CHECK(it == view.GetIterAtByte(4));
118 ++it;
119 CHECK(it.GetByteOffset() == 5);
120 CHECK(std::distance(begin, it) == 3);
121 CHECK(std::distance(it, end) == 2);
122 CHECK(*it == 'b');
123 CHECK(it == view.GetIterAtByte(5));
124 ++it;
125 CHECK(begin < it);
126 CHECK(it < end);
127 CHECK(it.GetByteOffset() == 6);
128 CHECK(std::distance(begin, it) == 4);
129 CHECK(std::distance(it, end) == 1);
130 CHECK(*it == 0x00012345);
131 CHECK(it == view.GetIterAtByte(6));
132 CHECK(it == view.GetIterAtByte(7));
133 CHECK(it == view.GetIterAtByte(8));
134 CHECK(it == view.GetIterAtByte(9));
135 ++it;
136 CHECK(begin < it);
137 CHECK(it.GetByteOffset() == 10);
138 CHECK(std::distance(begin, it) == 5);
139 CHECK(std::distance(it, end) == 0);
140 CHECK(it == end);
141 CHECK(it == view.GetIterAtByte(10));
142 --it;
143 CHECK(begin < it);
144 CHECK(it < end);
145 CHECK(it.GetByteOffset() == 6);
146 CHECK(*it == 0x00012345);
147 --it;
148 CHECK(begin < it);
149 CHECK(it < end);
150 CHECK(it.GetByteOffset() == 5);
151 CHECK(*it == 'b');
152}
Constant span of UTF-8 encoded data.
Definition utf8.hpp:24
iterator GetIterAtByte(size_t offset) const
Create iterator pointing at codepoint, which occupies the byte position "offset".
Definition utf8.cpp:83