summaryrefslogtreecommitdiffstats
path: root/base/i18n/string_search.cc
blob: f5fe95cce26882186c47d010883564666aeaaa6d (plain)
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
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// 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 "unicode/usearch.h"

namespace {

bool CollationSensitiveStringSearch(const string16& find_this,
                                    const string16& in_this,
                                    UCollationStrength strength) {
  UErrorCode status = U_ZERO_ERROR;

  UStringSearch* search = usearch_open(find_this.data(), -1, in_this.data(), -1,
                                       uloc_getDefault(), NULL, &status);

  // Default to basic substring search if usearch fails. According to
  // http://icu-project.org/apiref/icu4c/usearch_8h.html, usearch_open will fail
  // if either |find_this| or |in_this| are empty. In either case basic
  // substring search will give the correct return value.
  if (!U_SUCCESS(status))
    return in_this.find(find_this) != string16::npos;

  UCollator* collator = usearch_getCollator(search);
  ucol_setStrength(collator, strength);
  usearch_reset(search);

  bool result = usearch_first(search, &status) != USEARCH_DONE;
  usearch_close(search);
  return result;
}

}  // namespace

namespace base {
namespace i18n {

bool StringSearchIgnoringCaseAndAccents(const string16& find_this,
                                        const string16& in_this) {
  return CollationSensitiveStringSearch(find_this, in_this, UCOL_PRIMARY);
}

}  // namespace i18n
}  // namespace base