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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
|
// 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/i18n/string_search.h"
#include <stddef.h>
#include <string>
#include <vector>
#include "base/i18n/rtl.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/icu/source/i18n/unicode/usearch.h"
namespace base {
namespace i18n {
#define EXPECT_MATCH_IGNORE_CASE(find_this, in_this, ex_start, ex_len) \
{ \
size_t index = 0; \
size_t length = 0; \
EXPECT_TRUE(StringSearchIgnoringCaseAndAccents(find_this, in_this, &index, \
&length)); \
EXPECT_EQ(ex_start, index); \
EXPECT_EQ(ex_len, length); \
index = 0; \
length = 0; \
EXPECT_TRUE( \
StringSearch(find_this, in_this, &index, &length, false, true)); \
EXPECT_EQ(ex_start, index); \
EXPECT_EQ(ex_len, length); \
}
#define EXPECT_MATCH_SENSITIVE(find_this, in_this, ex_start, ex_len) \
{ \
size_t index = 0; \
size_t length = 0; \
EXPECT_TRUE( \
StringSearch(find_this, in_this, &index, &length, true, true)); \
EXPECT_EQ(ex_start, index); \
EXPECT_EQ(ex_len, length); \
}
#define EXPECT_MATCH_IGNORE_CASE_BACKWARDS(find_this, in_this, ex_start, \
ex_len) \
{ \
size_t index = 0; \
size_t length = 0; \
EXPECT_TRUE( \
StringSearch(find_this, in_this, &index, &length, false, false)); \
EXPECT_EQ(ex_start, index); \
EXPECT_EQ(ex_len, length); \
}
#define EXPECT_MATCH_SENSITIVE_BACKWARDS(find_this, in_this, ex_start, ex_len) \
{ \
size_t index = 0; \
size_t length = 0; \
EXPECT_TRUE( \
StringSearch(find_this, in_this, &index, &length, true, false)); \
EXPECT_EQ(ex_start, index); \
EXPECT_EQ(ex_len, length); \
}
#define EXPECT_MISS_IGNORE_CASE(find_this, in_this) \
{ \
size_t index = 0; \
size_t length = 0; \
EXPECT_FALSE(StringSearchIgnoringCaseAndAccents(find_this, in_this, \
&index, &length)); \
index = 0; \
length = 0; \
EXPECT_FALSE( \
StringSearch(find_this, in_this, &index, &length, false, true)); \
}
#define EXPECT_MISS_SENSITIVE(find_this, in_this) \
{ \
size_t index = 0; \
size_t length = 0; \
EXPECT_FALSE( \
StringSearch(find_this, in_this, &index, &length, true, true)); \
}
#define EXPECT_MISS_IGNORE_CASE_BACKWARDS(find_this, in_this) \
{ \
size_t index = 0; \
size_t length = 0; \
EXPECT_FALSE( \
StringSearch(find_this, in_this, &index, &length, false, false)); \
}
#define EXPECT_MISS_SENSITIVE_BACKWARDS(find_this, in_this) \
{ \
size_t index = 0; \
size_t length = 0; \
EXPECT_FALSE( \
StringSearch(find_this, in_this, &index, &length, true, false)); \
}
// Note on setting default locale for testing: The current default locale on
// the Mac trybot is en_US_POSIX, with which primary-level collation strength
// string search is case-sensitive, when normally it should be
// case-insensitive. In other locales (including en_US which English speakers
// in the U.S. use), this search would be case-insensitive as expected.
TEST(StringSearchTest, ASCII) {
std::string default_locale(uloc_getDefault());
bool locale_is_posix = (default_locale == "en_US_POSIX");
if (locale_is_posix)
SetICUDefaultLocale("en_US");
EXPECT_MATCH_IGNORE_CASE(u"hello", u"hello world", 0U, 5U);
EXPECT_MISS_IGNORE_CASE(u"h e l l o", u"h e l l o");
EXPECT_MATCH_IGNORE_CASE(u"aabaaa", u"aaabaabaaa", 4U, 6U);
EXPECT_MISS_IGNORE_CASE(u"searching within empty string", std::u16string());
EXPECT_MATCH_IGNORE_CASE(std::u16string(), u"searching for empty string", 0U,
0U);
EXPECT_MATCH_IGNORE_CASE(u"case insensitivity", u"CaSe InSeNsItIvItY", 0U,
18U);
EXPECT_MATCH_SENSITIVE(u"aabaaa", u"aaabaabaaa", 4U, 6U);
EXPECT_MISS_SENSITIVE(u"searching within empty string", std::u16string());
EXPECT_MATCH_SENSITIVE(std::u16string(), u"searching for empty string", 0U,
0U);
EXPECT_MISS_SENSITIVE(u"case insensitivity", u"CaSe InSeNsItIvItY");
if (locale_is_posix)
SetICUDefaultLocale(default_locale.data());
}
TEST(StringSearchTest, UnicodeLocaleIndependent) {
// Base characters
const std::u16string e_base = u"e";
const std::u16string E_base = u"E";
const std::u16string a_base = u"a";
// Composed characters
const std::u16string e_with_acute_accent = u"\u00e9";
const std::u16string E_with_acute_accent = u"\u00c9";
const std::u16string e_with_grave_accent = u"\u00e8";
const std::u16string E_with_grave_accent = u"\u00c8";
const std::u16string a_with_acute_accent = u"\u00e1";
// Decomposed characters
const std::u16string e_with_acute_combining_mark = u"e\u0301";
const std::u16string E_with_acute_combining_mark = u"E\u0301";
const std::u16string e_with_grave_combining_mark = u"e\u0300";
const std::u16string E_with_grave_combining_mark = u"E\u0300";
const std::u16string a_with_acute_combining_mark = u"a\u0301";
std::string default_locale(uloc_getDefault());
bool locale_is_posix = (default_locale == "en_US_POSIX");
if (locale_is_posix)
SetICUDefaultLocale("en_US");
EXPECT_MATCH_IGNORE_CASE(e_base, e_with_acute_accent, 0U,
e_with_acute_accent.size());
EXPECT_MATCH_IGNORE_CASE(e_with_acute_accent, e_base, 0U, e_base.size());
EXPECT_MATCH_IGNORE_CASE(e_base, e_with_acute_combining_mark, 0U,
e_with_acute_combining_mark.size());
EXPECT_MATCH_IGNORE_CASE(e_with_acute_combining_mark, e_base, 0U,
e_base.size());
EXPECT_MATCH_IGNORE_CASE(e_with_acute_combining_mark, e_with_acute_accent, 0U,
e_with_acute_accent.size());
EXPECT_MATCH_IGNORE_CASE(e_with_acute_accent, e_with_acute_combining_mark, 0U,
e_with_acute_combining_mark.size());
EXPECT_MATCH_IGNORE_CASE(e_with_acute_combining_mark,
e_with_grave_combining_mark, 0U,
e_with_grave_combining_mark.size());
EXPECT_MATCH_IGNORE_CASE(e_with_grave_combining_mark,
e_with_acute_combining_mark, 0U,
e_with_acute_combining_mark.size());
EXPECT_MATCH_IGNORE_CASE(e_with_acute_combining_mark, e_with_grave_accent, 0U,
e_with_grave_accent.size());
EXPECT_MATCH_IGNORE_CASE(e_with_grave_accent, e_with_acute_combining_mark, 0U,
e_with_acute_combining_mark.size());
EXPECT_MATCH_IGNORE_CASE(E_with_acute_accent, e_with_acute_accent, 0U,
e_with_acute_accent.size());
EXPECT_MATCH_IGNORE_CASE(E_with_grave_accent, e_with_acute_accent, 0U,
e_with_acute_accent.size());
EXPECT_MATCH_IGNORE_CASE(E_with_acute_combining_mark, e_with_grave_accent, 0U,
e_with_grave_accent.size());
EXPECT_MATCH_IGNORE_CASE(E_with_grave_combining_mark, e_with_acute_accent, 0U,
e_with_acute_accent.size());
EXPECT_MATCH_IGNORE_CASE(E_base, e_with_grave_accent, 0U,
e_with_grave_accent.size());
EXPECT_MISS_IGNORE_CASE(a_with_acute_accent, e_with_acute_accent);
EXPECT_MISS_IGNORE_CASE(a_with_acute_combining_mark,
e_with_acute_combining_mark);
EXPECT_MISS_SENSITIVE(e_base, e_with_acute_accent);
EXPECT_MISS_SENSITIVE(e_with_acute_accent, e_base);
EXPECT_MISS_SENSITIVE(e_base, e_with_acute_combining_mark);
EXPECT_MISS_SENSITIVE(e_with_acute_combining_mark, e_base);
EXPECT_MATCH_SENSITIVE(e_with_acute_combining_mark, e_with_acute_accent, 0U,
1U);
EXPECT_MATCH_SENSITIVE(e_with_acute_accent, e_with_acute_combining_mark, 0U,
2U);
EXPECT_MISS_SENSITIVE(e_with_acute_combining_mark,
e_with_grave_combining_mark);
EXPECT_MISS_SENSITIVE(e_with_grave_combining_mark,
e_with_acute_combining_mark);
EXPECT_MISS_SENSITIVE(e_with_acute_combining_mark, e_with_grave_accent);
EXPECT_MISS_SENSITIVE(e_with_grave_accent, e_with_acute_combining_mark);
EXPECT_MISS_SENSITIVE(E_with_acute_accent, e_with_acute_accent);
EXPECT_MISS_SENSITIVE(E_with_grave_accent, e_with_acute_accent);
EXPECT_MISS_SENSITIVE(E_with_acute_combining_mark, e_with_grave_accent);
EXPECT_MISS_SENSITIVE(E_with_grave_combining_mark, e_with_acute_accent);
EXPECT_MISS_SENSITIVE(E_base, e_with_grave_accent);
EXPECT_MISS_SENSITIVE(a_with_acute_accent, e_with_acute_accent);
EXPECT_MISS_SENSITIVE(a_with_acute_combining_mark,
e_with_acute_combining_mark);
EXPECT_MATCH_SENSITIVE(a_with_acute_combining_mark,
a_with_acute_combining_mark, 0U, 2U);
if (locale_is_posix)
SetICUDefaultLocale(default_locale.data());
}
TEST(StringSearchTest, UnicodeLocaleDependent) {
// Base characters
const std::u16string a_base = u"a";
// Composed characters
const std::u16string a_with_ring = u"\u00e5";
EXPECT_TRUE(StringSearchIgnoringCaseAndAccents(a_base, a_with_ring, nullptr,
nullptr));
EXPECT_TRUE(StringSearch(a_base, a_with_ring, nullptr, nullptr, false, true));
const char* default_locale = uloc_getDefault();
SetICUDefaultLocale("da");
EXPECT_FALSE(StringSearchIgnoringCaseAndAccents(a_base, a_with_ring, nullptr,
nullptr));
EXPECT_FALSE(
StringSearch(a_base, a_with_ring, nullptr, nullptr, false, true));
SetICUDefaultLocale(default_locale);
}
TEST(StringSearchTest, SearchBackwards) {
std::string default_locale(uloc_getDefault());
bool locale_is_posix = (default_locale == "en_US_POSIX");
if (locale_is_posix)
SetICUDefaultLocale("en_US");
EXPECT_MATCH_IGNORE_CASE_BACKWARDS(u"ab", u"ABAB", 2U, 2U);
EXPECT_MATCH_SENSITIVE_BACKWARDS(u"ab", u"abab", 2U, 2U);
EXPECT_MISS_SENSITIVE_BACKWARDS(u"ab", u"ABAB");
if (locale_is_posix)
SetICUDefaultLocale(default_locale.data());
}
TEST(StringSearchTest, FixedPatternMultipleSearch) {
std::string default_locale(uloc_getDefault());
bool locale_is_posix = (default_locale == "en_US_POSIX");
if (locale_is_posix)
SetICUDefaultLocale("en_US");
size_t index = 0;
size_t length = 0;
// Search "foo" over multiple texts.
FixedPatternStringSearch query1(u"foo", true);
EXPECT_TRUE(query1.Search(u"12foo34", &index, &length, true));
EXPECT_EQ(2U, index);
EXPECT_EQ(3U, length);
EXPECT_FALSE(query1.Search(u"bye", &index, &length, true));
EXPECT_FALSE(query1.Search(u"FOO", &index, &length, true));
EXPECT_TRUE(query1.Search(u"foobarfoo", &index, &length, true));
EXPECT_EQ(0U, index);
EXPECT_EQ(3U, length);
EXPECT_TRUE(query1.Search(u"foobarfoo", &index, &length, false));
EXPECT_EQ(6U, index);
EXPECT_EQ(3U, length);
// Search "hello" over multiple texts.
FixedPatternStringSearchIgnoringCaseAndAccents query2(u"hello");
EXPECT_TRUE(query2.Search(u"12hello34", &index, &length));
EXPECT_EQ(2U, index);
EXPECT_EQ(5U, length);
EXPECT_FALSE(query2.Search(u"bye", &index, &length));
EXPECT_TRUE(query2.Search(u"hELLo", &index, &length));
EXPECT_EQ(0U, index);
EXPECT_EQ(5U, length);
if (locale_is_posix)
SetICUDefaultLocale(default_locale.data());
}
TEST(StringSearchTest, RepeatingStringSearch) {
struct MatchResult {
int match_index;
int match_length;
};
std::string default_locale(uloc_getDefault());
bool locale_is_posix = (default_locale == "en_US_POSIX");
if (locale_is_posix)
SetICUDefaultLocale("en_US");
const char16_t kPattern[] = u"fox";
const char16_t kTarget[] = u"The quick brown fox jumped over the lazy Fox";
// Case sensitive.
{
const MatchResult kExpectation[] = {{16, 3}};
RepeatingStringSearch searcher(kPattern, kTarget, /*case_sensitive=*/true);
std::vector<MatchResult> results;
int match_index;
int match_length;
while (searcher.NextMatchResult(match_index, match_length)) {
results.push_back(
{.match_index = match_index, .match_length = match_length});
}
ASSERT_EQ(std::size(kExpectation), results.size());
for (size_t i = 0; i < results.size(); ++i) {
EXPECT_EQ(results[i].match_index, kExpectation[i].match_index);
EXPECT_EQ(results[i].match_length, kExpectation[i].match_length);
}
}
// Case insensitive.
{
const MatchResult kExpectation[] = {{16, 3}, {41, 3}};
RepeatingStringSearch searcher(kPattern, kTarget, /*case_sensitive=*/false);
std::vector<MatchResult> results;
int match_index;
int match_length;
while (searcher.NextMatchResult(match_index, match_length)) {
results.push_back(
{.match_index = match_index, .match_length = match_length});
}
ASSERT_EQ(std::size(kExpectation), results.size());
for (size_t i = 0; i < results.size(); ++i) {
EXPECT_EQ(results[i].match_index, kExpectation[i].match_index);
EXPECT_EQ(results[i].match_length, kExpectation[i].match_length);
}
}
if (locale_is_posix)
SetICUDefaultLocale(default_locale.data());
}
} // namespace i18n
} // namespace base
|