summaryrefslogtreecommitdiffstats
path: root/content/renderer/android/address_detector.cc
blob: fdc70b136aebd6f9878609a95beee2c7568809d6 (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
58
59
60
61
// Copyright (c) 2012 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 "content/renderer/android/address_detector.h"

#include <bitset>

#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "content/common/android/address_parser.h"
#include "content/public/renderer/android_content_detection_prefixes.h"
#include "net/base/escape.h"

namespace {

// Maximum text length to be searched for address detection.
static const size_t kMaxAddressLength = 250;

}  // anonymous namespace

namespace content {

AddressDetector::AddressDetector() {
}

AddressDetector::~AddressDetector() {
}

GURL AddressDetector::GetIntentURL(const std::string& content_text) {
  return GURL(kAddressPrefix +
      net::EscapeQueryParamValue(content_text, true));
}

size_t AddressDetector::GetMaximumContentLength() {
  return kMaxAddressLength;
}

std::string AddressDetector::GetContentText(const base::string16& text) {
  // Get the address and replace unicode bullets with commas.
  base::string16 address_16 = CollapseWhitespace(text, false);
  std::replace(address_16.begin(), address_16.end(),
      static_cast<char16>(0x2022), static_cast<char16>(','));
  return UTF16ToUTF8(address_16);
}

bool AddressDetector::FindContent(
    const base::string16::const_iterator& begin,
    const base::string16::const_iterator& end,
    size_t* start_pos,
    size_t* end_pos,
    std::string* content_text) {
  if (address_parser::FindAddress(begin, end, start_pos, end_pos)) {
    content_text->assign(
        GetContentText(base::string16(begin + *start_pos, begin + *end_pos)));
    return true;
  }
  return false;
}

}  // namespace content