Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1262)

Unified Diff: content/common/android/address_parser.cc

Issue 10456007: [Android] Split the address parser from AddressDetector for WebView use. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/common/android/address_parser.cc
diff --git a/content/renderer/android/address_detector.cc b/content/common/android/address_parser.cc
similarity index 91%
copy from content/renderer/android/address_detector.cc
copy to content/common/android/address_parser.cc
index 891ac34d57ded46a0763585fe952b03a8c2130f8..f5444e99c46318ee184091c69747cfd659d1bab1 100644
--- a/content/renderer/android/address_detector.cc
+++ b/content/common/android/address_parser.cc
@@ -2,7 +2,7 @@
// 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 "content/common/android/address_parser.h"
#include <bitset>
@@ -10,16 +10,9 @@
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "net/base/escape.h"
-#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
namespace {
-// Prefix used for geographical address intent URIs.
-static const char kAddressSchemaPrefix[] = "geo:0,0?q=";
-
-// Maximum text length to be searched for address detection.
-static const size_t kMaxAddressLength = 250;
-
// Minimum number of words in an address after the house number
// before a state is expected to be found.
// A value too high can miss short addresses.
@@ -101,34 +94,23 @@ bool LowerCaseEqualsASCIIWithPlural(string16::const_iterator word_begin,
namespace content {
-AddressDetector::AddressDetector() {
-}
-
-AddressDetector::~AddressDetector() {
-}
-
-std::string AddressDetector::GetContentText(const string16& text) {
- // Get the address and replace unicode bullets with commas.
- 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);
+AddressParser::AddressParser() {
}
-GURL AddressDetector::GetIntentURL(const std::string& content_text) {
- return GURL(kAddressSchemaPrefix +
- net::EscapeQueryParamValue(content_text, true));
-}
-
-size_t AddressDetector::GetMaximumContentLength() {
- return kMaxAddressLength;
+bool AddressParser::FindAddress(const string16& text, string16* address) {
+ size_t start, end;
+ if (FindContent(text.begin(), text.end(), &start, &end)) {
+ address->assign(text.substr(start, end));
+ return true;
+ } else {
+ return false;
+ }
}
-bool AddressDetector::FindContent(const string16::const_iterator& begin,
- const string16::const_iterator& end,
- size_t* start_pos,
- size_t* end_pos,
- std::string* content_text) {
+bool AddressParser::FindContent(const string16::const_iterator& begin,
+ const string16::const_iterator& end,
+ size_t* start_pos,
+ size_t* end_pos) {
HouseNumberParser house_number_parser;
// Keep going through the input string until a potential house number is
@@ -147,7 +129,7 @@ bool AddressDetector::FindContent(const string16::const_iterator& begin,
String16Tokenizer tokenizer(house_number.end, end, delimiters);
tokenizer.set_options(String16Tokenizer::RETURN_DELIMS);
- std::vector<Word> words;
+ WordList words;
words.push_back(house_number);
bool found_location_name = false;
@@ -263,8 +245,6 @@ bool AddressDetector::FindContent(const string16::const_iterator& begin,
*start_pos = words[0].begin - begin;
*end_pos = words[zip_word].end - begin;
- content_text->assign(GetContentText(string16(words[0].begin,
- words[zip_word].end)));
return true;
}
}
@@ -284,45 +264,43 @@ bool AddressDetector::FindContent(const string16::const_iterator& begin,
return false;
}
-AddressDetector::Word::Word(const string16::const_iterator& begin,
- const string16::const_iterator& end)
+AddressParser::Word::Word(const string16::const_iterator& begin,
+ const string16::const_iterator& end)
: begin(begin),
end(end) {
DCHECK(begin <= end);
}
-bool AddressDetector::HouseNumberParser::IsPreDelimiter(
- char16 character) {
+bool AddressParser::HouseNumberParser::IsPreDelimiter(char16 character) {
return character == ':' || IsPostDelimiter(character);
}
-bool AddressDetector::HouseNumberParser::IsPostDelimiter(
- char16 character) {
+bool AddressParser::HouseNumberParser::IsPostDelimiter(char16 character) {
return IsWhitespace(character) || strchr(",\"'", character);
}
-void AddressDetector::HouseNumberParser::RestartOnNextDelimiter() {
+void AddressParser::HouseNumberParser::RestartOnNextDelimiter() {
ResetState();
for (; it_ != end_ && !IsPreDelimiter(*it_); ++it_) {}
}
-void AddressDetector::HouseNumberParser::AcceptChars(size_t num_chars) {
+void AddressParser::HouseNumberParser::AcceptChars(size_t num_chars) {
size_t offset = std::min(static_cast<size_t>(std::distance(it_, end_)),
num_chars);
it_ += offset;
result_chars_ += offset;
}
-void AddressDetector::HouseNumberParser::SkipChars(size_t num_chars) {
+void AddressParser::HouseNumberParser::SkipChars(size_t num_chars) {
it_ += std::min(static_cast<size_t>(std::distance(it_, end_)), num_chars);
}
-void AddressDetector::HouseNumberParser::ResetState() {
+void AddressParser::HouseNumberParser::ResetState() {
num_digits_ = 0;
result_chars_ = 0;
}
-bool AddressDetector::HouseNumberParser::CheckFinished(Word* word) const {
+bool AddressParser::HouseNumberParser::CheckFinished(Word* word) const {
// There should always be a number after a hyphen.
if (result_chars_ == 0 || SafePreviousChar(it_, begin_) == '-')
return false;
@@ -334,7 +312,7 @@ bool AddressDetector::HouseNumberParser::CheckFinished(Word* word) const {
return true;
}
-bool AddressDetector::HouseNumberParser::Parse(
+bool AddressParser::HouseNumberParser::Parse(
const string16::const_iterator& begin,
const string16::const_iterator& end, Word* word) {
it_ = begin_ = begin;
@@ -466,7 +444,7 @@ bool AddressDetector::HouseNumberParser::Parse(
return CheckFinished(word);
}
-bool AddressDetector::FindStateStartingInWord(
+bool AddressParser::FindStateStartingInWord(
WordList* words,
size_t state_first_word,
size_t* state_last_word,
@@ -633,7 +611,7 @@ bool AddressDetector::FindStateStartingInWord(
return false;
}
-bool AddressDetector::IsZipValid(const Word& word, size_t state_index) {
+bool AddressParser::IsZipValid(const Word& word, size_t state_index) {
size_t length = word.end - word.begin;
if (length != kZipDigits && length != kZipPlus4Digits + 1)
return false;
@@ -647,7 +625,7 @@ bool AddressDetector::IsZipValid(const Word& word, size_t state_index) {
return IsZipValidForState(word, state_index);
}
-bool AddressDetector::IsZipValidForState(const Word& word, size_t state_index) {
+bool AddressParser::IsZipValidForState(const Word& word, size_t state_index) {
// List of valid zip code ranges.
static const struct {
char low;
@@ -731,7 +709,7 @@ bool AddressDetector::IsZipValidForState(const Word& word, size_t state_index) {
return false;
}
-bool AddressDetector::IsValidLocationName(const Word& word) {
+bool AddressParser::IsValidLocationName(const Word& word) {
// Supported location names sorted alphabetically and grouped by first letter.
static const struct LocationNameInfo {
const char* string;

Powered by Google App Engine
This is Rietveld 408576698