1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "content/renderer/android/address_detector.h"
6
7#include <bitset>
8
9#include "base/strings/string_util.h"
10#include "base/strings/utf_string_conversions.h"
11#include "content/common/android/address_parser.h"
12#include "content/public/renderer/android_content_detection_prefixes.h"
13#include "net/base/escape.h"
14
15namespace {
16
17// Maximum text length to be searched for address detection.
18static const size_t kMaxAddressLength = 250;
19
20}  // anonymous namespace
21
22namespace content {
23
24AddressDetector::AddressDetector() {
25}
26
27AddressDetector::~AddressDetector() {
28}
29
30GURL AddressDetector::GetIntentURL(const std::string& content_text) {
31  return GURL(kAddressPrefix +
32      net::EscapeQueryParamValue(content_text, true));
33}
34
35size_t AddressDetector::GetMaximumContentLength() {
36  return kMaxAddressLength;
37}
38
39std::string AddressDetector::GetContentText(const string16& text) {
40  // Get the address and replace unicode bullets with commas.
41  string16 address_16 = CollapseWhitespace(text, false);
42  std::replace(address_16.begin(), address_16.end(),
43      static_cast<char16>(0x2022), static_cast<char16>(','));
44  return UTF16ToUTF8(address_16);
45}
46
47bool AddressDetector::FindContent(const string16::const_iterator& begin,
48    const string16::const_iterator& end, size_t* start_pos, size_t* end_pos,
49    std::string* content_text) {
50  if (address_parser::FindAddress(begin, end, start_pos, end_pos)) {
51    content_text->assign(
52        GetContentText(string16(begin + *start_pos, begin + *end_pos)));
53    return true;
54  }
55  return false;
56}
57
58}  // namespace content
59