summaryrefslogtreecommitdiff
path: root/chromium/components/url_formatter/top_domains/trie_entry.cc
blob: 459d9378b278588621b4306277ff1b884d8edc26 (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
47
48
49
50
51
52
53
54
55
56
57
// Copyright 2018 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/url_formatter/top_domains/trie_entry.h"
#include "base/strings/string_util.h"
#include "net/tools/huffman_trie/trie/trie_bit_buffer.h"
#include "net/tools/huffman_trie/trie/trie_writer.h"

namespace url_formatter {

namespace top_domains {

TopDomainTrieEntry::TopDomainTrieEntry(
    const net::huffman_trie::HuffmanRepresentationTable& huffman_table,
    net::huffman_trie::HuffmanBuilder* huffman_builder,
    TopDomainEntry* entry)
    : huffman_table_(huffman_table),
      huffman_builder_(huffman_builder),
      entry_(entry) {}

TopDomainTrieEntry::~TopDomainTrieEntry() {}

std::string TopDomainTrieEntry::name() const {
  return entry_->skeleton;
}

bool TopDomainTrieEntry::WriteEntry(
    net::huffman_trie::TrieBitBuffer* writer) const {
  if (entry_->skeleton == entry_->top_domain) {
    writer->WriteBit(1);
    return true;
  }
  writer->WriteBit(0);

  std::string top_domain = entry_->top_domain;
  // With the current top 10,000 domains, this optimization reduces the
  // additional binary size required for the trie from 71 kB to 59 kB.
  if (base::EndsWith(top_domain, ".com",
                     base::CompareCase::INSENSITIVE_ASCII)) {
    writer->WriteBit(1);
    top_domain = top_domain.substr(0, top_domain.size() - 4);
  } else {
    writer->WriteBit(0);
  }

  for (const auto& c : top_domain) {
    writer->WriteChar(c, huffman_table_, huffman_builder_);
  }
  writer->WriteChar(net::huffman_trie::kEndOfTableValue, huffman_table_,
                    huffman_builder_);
  return true;
}

}  // namespace top_domains

}  // namespace url_formatter