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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
base / strings / utf_offset_string_conversions_unittest.cc [blame]
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/strings/utf_offset_string_conversions.h"
#include <stddef.h>
#include <algorithm>
#include <array>
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace {
static const size_t kNpos = std::u16string::npos;
} // namespace
TEST(UTFOffsetStringConversionsTest, AdjustOffset) {
struct UTF8ToUTF16Case {
const char* utf8;
size_t input_offset;
size_t output_offset;
} utf8_to_utf16_cases[] = {
{"", 0, 0},
{"", kNpos, kNpos},
{"\xe4\xbd\xa0\xe5\xa5\xbd", 1, kNpos},
{"\xe4\xbd\xa0\xe5\xa5\xbd", 3, 1},
{"\xed\xb0\x80z", 3, 3},
{"A\xF0\x90\x8C\x80z", 1, 1},
{"A\xF0\x90\x8C\x80z", 2, kNpos},
{"A\xF0\x90\x8C\x80z", 5, 3},
{"A\xF0\x90\x8C\x80z", 6, 4},
{"A\xF0\x90\x8C\x80z", kNpos, kNpos},
};
for (const auto& i : utf8_to_utf16_cases) {
const size_t offset = i.input_offset;
std::vector<size_t> offsets;
offsets.push_back(offset);
UTF8ToUTF16AndAdjustOffsets(i.utf8, &offsets);
EXPECT_EQ(i.output_offset, offsets[0]);
}
struct UTF16ToUTF8Case {
char16_t utf16[10];
size_t input_offset;
size_t output_offset;
};
auto utf16_to_utf8_cases = std::to_array<UTF16ToUTF8Case>({
{{}, 0, 0},
// Converted to 3-byte utf-8 sequences
{{0x5909, 0x63DB}, 3, kNpos},
{{0x5909, 0x63DB}, 2, 6},
{{0x5909, 0x63DB}, 1, 3},
{{0x5909, 0x63DB}, 0, 0},
// Converted to 2-byte utf-8 sequences
{{'A', 0x00bc, 0x00be, 'z'}, 1, 1},
{{'A', 0x00bc, 0x00be, 'z'}, 2, 3},
{{'A', 0x00bc, 0x00be, 'z'}, 3, 5},
{{'A', 0x00bc, 0x00be, 'z'}, 4, 6},
// Surrogate pair
{{'A', 0xd800, 0xdf00, 'z'}, 1, 1},
{{'A', 0xd800, 0xdf00, 'z'}, 2, kNpos},
{{'A', 0xd800, 0xdf00, 'z'}, 3, 5},
{{'A', 0xd800, 0xdf00, 'z'}, 4, 6},
});
for (size_t i = 0; i < std::size(utf16_to_utf8_cases); ++i) {
size_t offset = utf16_to_utf8_cases[i].input_offset;
std::vector<size_t> offsets;
offsets.push_back(offset);
UTF16ToUTF8AndAdjustOffsets(utf16_to_utf8_cases[i].utf16, &offsets);
EXPECT_EQ(utf16_to_utf8_cases[i].output_offset, offsets[0]) << i;
}
}
TEST(UTFOffsetStringConversionsTest, LimitOffsets) {
const OffsetAdjuster::Adjustments kNoAdjustments;
const size_t kLimit = 10;
const size_t kItems = 20;
std::vector<size_t> size_ts;
for (size_t t = 0; t < kItems; ++t) {
size_ts.push_back(t);
OffsetAdjuster::AdjustOffset(kNoAdjustments, &size_ts.back(), kLimit);
}
size_t unlimited_count = 0;
for (auto ti : size_ts) {
if (ti != kNpos)
++unlimited_count;
}
EXPECT_EQ(11U, unlimited_count);
// Reverse the values in the vector and try again.
size_ts.clear();
for (size_t t = kItems; t > 0; --t) {
size_ts.push_back(t - 1);
OffsetAdjuster::AdjustOffset(kNoAdjustments, &size_ts.back(), kLimit);
}
unlimited_count = 0;
for (auto ti : size_ts) {
if (ti != kNpos)
++unlimited_count;
}
EXPECT_EQ(11U, unlimited_count);
}
TEST(UTFOffsetStringConversionsTest, AdjustOffsets) {
// Imagine we have strings as shown in the following cases where the
// X's represent encoded characters.
// 1: abcXXXdef ==> abcXdef
{
std::vector<size_t> offsets;
for (size_t t = 0; t <= 9; ++t)
offsets.push_back(t);
OffsetAdjuster::Adjustments adjustments;
adjustments.push_back(OffsetAdjuster::Adjustment(3, 3, 1));
OffsetAdjuster::AdjustOffsets(adjustments, &offsets);
auto expected_1 =
std::to_array<size_t>({0, 1, 2, 3, kNpos, kNpos, 4, 5, 6, 7});
EXPECT_EQ(offsets.size(), std::size(expected_1));
for (size_t i = 0; i < std::size(expected_1); ++i)
EXPECT_EQ(expected_1[i], offsets[i]);
}
// 2: XXXaXXXXbcXXXXXXXdefXXX ==> XaXXbcXXXXdefX
{
std::vector<size_t> offsets;
for (size_t t = 0; t <= 23; ++t)
offsets.push_back(t);
OffsetAdjuster::Adjustments adjustments;
adjustments.push_back(OffsetAdjuster::Adjustment(0, 3, 1));
adjustments.push_back(OffsetAdjuster::Adjustment(4, 4, 2));
adjustments.push_back(OffsetAdjuster::Adjustment(10, 7, 4));
adjustments.push_back(OffsetAdjuster::Adjustment(20, 3, 1));
OffsetAdjuster::AdjustOffsets(adjustments, &offsets);
auto expected_2 = std::to_array<size_t>({
0, kNpos, kNpos, 1, 2, kNpos, kNpos, kNpos,
4, 5, 6, kNpos, kNpos, kNpos, kNpos, kNpos,
kNpos, 10, 11, 12, 13, kNpos, kNpos, 14,
});
EXPECT_EQ(offsets.size(), std::size(expected_2));
for (size_t i = 0; i < std::size(expected_2); ++i)
EXPECT_EQ(expected_2[i], offsets[i]);
}
// 3: XXXaXXXXbcdXXXeXX ==> aXXXXbcdXXXe
{
std::vector<size_t> offsets;
for (size_t t = 0; t <= 17; ++t)
offsets.push_back(t);
OffsetAdjuster::Adjustments adjustments;
adjustments.push_back(OffsetAdjuster::Adjustment(0, 3, 0));
adjustments.push_back(OffsetAdjuster::Adjustment(4, 4, 4));
adjustments.push_back(OffsetAdjuster::Adjustment(11, 3, 3));
adjustments.push_back(OffsetAdjuster::Adjustment(15, 2, 0));
OffsetAdjuster::AdjustOffsets(adjustments, &offsets);
auto expected_3 = std::to_array<size_t>({
0,
kNpos,
kNpos,
0,
1,
kNpos,
kNpos,
kNpos,
5,
6,
7,
8,
kNpos,
kNpos,
11,
12,
kNpos,
12,
});
EXPECT_EQ(offsets.size(), std::size(expected_3));
for (size_t i = 0; i < std::size(expected_3); ++i)
EXPECT_EQ(expected_3[i], offsets[i]);
}
}
TEST(UTFOffsetStringConversionsTest, UnadjustOffsets) {
// Imagine we have strings as shown in the following cases where the
// X's represent encoded characters.
// 1: abcXXXdef ==> abcXdef
{
std::vector<size_t> offsets;
for (size_t t = 0; t <= 7; ++t)
offsets.push_back(t);
OffsetAdjuster::Adjustments adjustments;
adjustments.push_back(OffsetAdjuster::Adjustment(3, 3, 1));
OffsetAdjuster::UnadjustOffsets(adjustments, &offsets);
auto expected_1 = std::to_array<size_t>({0, 1, 2, 3, 6, 7, 8, 9});
EXPECT_EQ(offsets.size(), std::size(expected_1));
for (size_t i = 0; i < std::size(expected_1); ++i)
EXPECT_EQ(expected_1[i], offsets[i]);
}
// 2: XXXaXXXXbcXXXXXXXdefXXX ==> XaXXbcXXXXdefX
{
std::vector<size_t> offsets;
for (size_t t = 0; t <= 14; ++t)
offsets.push_back(t);
OffsetAdjuster::Adjustments adjustments;
adjustments.push_back(OffsetAdjuster::Adjustment(0, 3, 1));
adjustments.push_back(OffsetAdjuster::Adjustment(4, 4, 2));
adjustments.push_back(OffsetAdjuster::Adjustment(10, 7, 4));
adjustments.push_back(OffsetAdjuster::Adjustment(20, 3, 1));
OffsetAdjuster::UnadjustOffsets(adjustments, &offsets);
auto expected_2 = std::to_array<size_t>({
0,
3,
4,
kNpos,
8,
9,
10,
kNpos,
kNpos,
kNpos,
17,
18,
19,
20,
23,
});
EXPECT_EQ(offsets.size(), std::size(expected_2));
for (size_t i = 0; i < std::size(expected_2); ++i)
EXPECT_EQ(expected_2[i], offsets[i]);
}
// 3: XXXaXXXXbcdXXXeXX ==> aXXXXbcdXXXe
{
std::vector<size_t> offsets;
for (size_t t = 0; t <= 12; ++t)
offsets.push_back(t);
OffsetAdjuster::Adjustments adjustments;
adjustments.push_back(OffsetAdjuster::Adjustment(0, 3, 0));
adjustments.push_back(OffsetAdjuster::Adjustment(4, 4, 4));
adjustments.push_back(OffsetAdjuster::Adjustment(11, 3, 3));
adjustments.push_back(OffsetAdjuster::Adjustment(15, 2, 0));
OffsetAdjuster::UnadjustOffsets(adjustments, &offsets);
auto expected_3 = std::to_array<size_t>({
0, // this could just as easily be 3
4, kNpos, kNpos, kNpos, 8, 9, 10, 11, kNpos, kNpos, 14,
15, // this could just as easily be 17
});
EXPECT_EQ(offsets.size(), std::size(expected_3));
for (size_t i = 0; i < std::size(expected_3); ++i)
EXPECT_EQ(expected_3[i], offsets[i]);
}
}
// MergeSequentialAdjustments is used by net/base/escape.{h,cc} and
// net/base/net_util.{h,cc}. The two tests EscapeTest.AdjustOffset and
// NetUtilTest.FormatUrlWithOffsets test its behavior extensively. This
// is simply a short, additional test.
TEST(UTFOffsetStringConversionsTest, MergeSequentialAdjustments) {
// Pretend the input string is "abcdefghijklmnopqrstuvwxyz".
// Set up |first_adjustments| to
// - remove the leading "a"
// - combine the "bc" into one character (call it ".")
// - remove the "f"
// - remove the "tuv"
// The resulting string should be ".deghijklmnopqrswxyz".
OffsetAdjuster::Adjustments first_adjustments;
first_adjustments.push_back(OffsetAdjuster::Adjustment(0, 1, 0));
first_adjustments.push_back(OffsetAdjuster::Adjustment(1, 2, 1));
first_adjustments.push_back(OffsetAdjuster::Adjustment(5, 1, 0));
first_adjustments.push_back(OffsetAdjuster::Adjustment(19, 3, 0));
// Set up |adjustments_on_adjusted_string| to
// - combine the "." character that replaced "bc" with "d" into one character
// (call it "?")
// - remove the "egh"
// - expand the "i" into two characters (call them "12")
// - combine the "jkl" into one character (call it "@")
// - expand the "z" into two characters (call it "34")
// The resulting string should be "?12@mnopqrswxy34".
OffsetAdjuster::Adjustments adjustments_on_adjusted_string;
adjustments_on_adjusted_string.push_back(OffsetAdjuster::Adjustment(
0, 2, 1));
adjustments_on_adjusted_string.push_back(OffsetAdjuster::Adjustment(
2, 3, 0));
adjustments_on_adjusted_string.push_back(OffsetAdjuster::Adjustment(
5, 1, 2));
adjustments_on_adjusted_string.push_back(OffsetAdjuster::Adjustment(
6, 3, 1));
adjustments_on_adjusted_string.push_back(OffsetAdjuster::Adjustment(
19, 1, 2));
// Now merge the adjustments and check the results.
OffsetAdjuster::MergeSequentialAdjustments(first_adjustments,
&adjustments_on_adjusted_string);
// The merged adjustments should look like
// - combine abcd into "?"
// - note: it's also reasonable for the Merge function to instead produce
// two adjustments instead of this, one to remove a and another to
// combine bcd into "?". This test verifies the current behavior.
// - remove efgh
// - expand i into "12"
// - combine jkl into "@"
// - remove tuv
// - expand z into "34"
ASSERT_EQ(6u, adjustments_on_adjusted_string.size());
EXPECT_EQ(0u, adjustments_on_adjusted_string[0].original_offset);
EXPECT_EQ(4u, adjustments_on_adjusted_string[0].original_length);
EXPECT_EQ(1u, adjustments_on_adjusted_string[0].output_length);
EXPECT_EQ(4u, adjustments_on_adjusted_string[1].original_offset);
EXPECT_EQ(4u, adjustments_on_adjusted_string[1].original_length);
EXPECT_EQ(0u, adjustments_on_adjusted_string[1].output_length);
EXPECT_EQ(8u, adjustments_on_adjusted_string[2].original_offset);
EXPECT_EQ(1u, adjustments_on_adjusted_string[2].original_length);
EXPECT_EQ(2u, adjustments_on_adjusted_string[2].output_length);
EXPECT_EQ(9u, adjustments_on_adjusted_string[3].original_offset);
EXPECT_EQ(3u, adjustments_on_adjusted_string[3].original_length);
EXPECT_EQ(1u, adjustments_on_adjusted_string[3].output_length);
EXPECT_EQ(19u, adjustments_on_adjusted_string[4].original_offset);
EXPECT_EQ(3u, adjustments_on_adjusted_string[4].original_length);
EXPECT_EQ(0u, adjustments_on_adjusted_string[4].output_length);
EXPECT_EQ(25u, adjustments_on_adjusted_string[5].original_offset);
EXPECT_EQ(1u, adjustments_on_adjusted_string[5].original_length);
EXPECT_EQ(2u, adjustments_on_adjusted_string[5].output_length);
}
} // namespace base