File: address_rewriter.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (129 lines) | stat: -rw-r--r-- 4,755 bytes parent folder | download | duplicates (3)
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
// Copyright 2016 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 "components/autofill/core/browser/address_rewriter.h"

#include <memory>
#include <unordered_map>

#include "base/i18n/case_conversion.h"
#include "base/memory/singleton.h"
#include "base/strings/utf_string_conversions.h"
#include "third_party/re2/src/re2/re2.h"

namespace autofill {
namespace {

// Import in the internal rule table symbols. The data is defined in
// components/autofill/core/browser/address_rewriter_rules.cc
using internal::Rule;
using internal::RegionInfo;
using internal::kRuleTable;
using internal::kRuleTableSize;

// Aliases for the types used by the compiled rules cache.
using CompiledRule = std::pair<std::unique_ptr<re2::RE2>, re2::StringPiece>;
using CompiledRuleVector = std::vector<CompiledRule>;
using CompiledRuleCache = std::unordered_map<std::string, CompiledRuleVector>;

// Helper function to find the rules associated with |region|. Note that this
// requires that kRuleTable be sorted by region.
static const RegionInfo* GetRegionInfo(const base::StringPiece& region) {
  const RegionInfo* begin = kRuleTable;
  const RegionInfo* end = kRuleTable + kRuleTableSize;
  const RegionInfo* iter = std::lower_bound(begin, end, region);
  if (iter != end && region == iter->region)
    return iter;
  return nullptr;
}

// The cache of compiled string replacement rules, keyed by region. This class
// is a singleton that compiles the rules for a given region the first time
// they are requested.
class Cache {
 public:
  // Return the singleton instance of the cache.
  static Cache* GetInstance() { return base::Singleton<Cache>::get(); }

  // If the rules for |region| have already been compiled and cached, return a
  // pointer to them. Otherwise, find the rules for |region| (returning nullptr
  // if there are no such rules exist), compile them, cache them, and return a
  // pointer to the cached rules.
  const CompiledRuleVector* GetRulesForRegion(const std::string& region) {
    // Take the lock so that we don't update the data cache concurrently. Note
    // that the returned data is const and can be concurrently accessed, just
    // not the data cache.
    base::AutoLock auto_lock(lock_);

    // If we find a cached set of rules, return a pointer to the data.
    CompiledRuleCache::iterator cache_iter = data_.find(region);
    if (cache_iter != data_.end())
      return &cache_iter->second;

    // Cache miss. Look for the raw rules. If none, then return nullptr.
    const RegionInfo* region_info = GetRegionInfo(region);
    if (region_info == nullptr)
      return nullptr;

    // Add a new rule vector the the cache and populate it with compiled rules.
    re2::RE2::Options options;
    options.set_utf8(true);
    options.set_word_boundary(true);
    CompiledRuleVector& compiled_rules = data_[region];
    compiled_rules.reserve(region_info->num_rules);
    for (size_t i = 0; i < region_info->num_rules; ++i) {
      const Rule& rule = region_info->rules[i];
      std::unique_ptr<re2::RE2> pattern(new re2::RE2(rule.pattern, options));
      re2::StringPiece rewrite(rule.rewrite);
      compiled_rules.emplace_back(std::move(pattern), std::move(rewrite));
    }

    // Return a pointer to the data.
    return &compiled_rules;
  }

 private:
  Cache() {}

  // Synchronizes access to |data_|, ensuring that a given set of rules is
  // only compiled once.
  base::Lock lock_;

  // The cache of compiled rules, keyed by region.
  CompiledRuleCache data_;

  friend struct base::DefaultSingletonTraits<Cache>;
  DISALLOW_COPY_AND_ASSIGN(Cache);
};

}  // namespace

AddressRewriter AddressRewriter::ForCountryCode(
    const base::string16& country_code) {
  const std::string region =
      base::UTF16ToUTF8(base::i18n::ToUpper(country_code));
  const CompiledRuleVector* rules =
      Cache::GetInstance()->GetRulesForRegion(region);
  AddressRewriter rewriter;
  rewriter.impl_ = rules;
  return rewriter;
}

base::string16 AddressRewriter::Rewrite(const base::string16& text) const {
  if (impl_ == nullptr)
    return base::CollapseWhitespace(text, true);

  // Apply all of the string replacement rules. We don't have to worry about
  // whitespace during these passes because the patterns are all whitespace
  // tolerant regular expressions.
  std::string utf8_text = base::UTF16ToUTF8(text);
  for (const auto& rule : *static_cast<const CompiledRuleVector*>(impl_)) {
    RE2::GlobalReplace(&utf8_text, *rule.first, rule.second);
  }

  // Collapse whitespace before returning the final value.
  return base::UTF8ToUTF16(base::CollapseWhitespaceASCII(utf8_text, true));
}

}  // namespace autofill