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 #ifndef CONTENT_COMMON_ADDRESS_PARSER_INTERNAL_H_ 6 #define CONTENT_COMMON_ADDRESS_PARSER_INTERNAL_H_ 7 8 #include <vector> 9 10 #include "base/strings/string_tokenizer.h" 11 #include "content/common/content_export.h" 12 13 namespace content { 14 15 namespace address_parser { 16 17 // Internal classes and functions for address parsing. 18 namespace internal { 19 20 // Exposed for tests. 21 struct CONTENT_EXPORT Word { 22 base::string16::const_iterator begin; 23 base::string16::const_iterator end; 24 25 Word() {} 26 Word(const base::string16::const_iterator& begin, 27 const base::string16::const_iterator& end); 28 }; 29 30 // Exposed for tests. 31 class CONTENT_EXPORT HouseNumberParser { 32 public: 33 HouseNumberParser() {} 34 35 bool Parse(const base::string16::const_iterator& begin, 36 const base::string16::const_iterator& end, 37 Word* word); 38 39 private: 40 static inline bool IsPreDelimiter(char16 character); 41 static inline bool IsPostDelimiter(char16 character); 42 inline void RestartOnNextDelimiter(); 43 44 inline bool CheckFinished(Word* word) const; 45 inline void AcceptChars(size_t num_chars); 46 inline void SkipChars(size_t num_chars); 47 inline void ResetState(); 48 49 // Iterators to the beginning, current position and ending of the string 50 // being currently parsed. 51 base::string16::const_iterator begin_; 52 base::string16::const_iterator it_; 53 base::string16::const_iterator end_; 54 55 // Number of digits found in the current result candidate. 56 size_t num_digits_; 57 58 // Number of characters previous to the current iterator that belong 59 // to the current result candidate. 60 size_t result_chars_; 61 62 DISALLOW_COPY_AND_ASSIGN(HouseNumberParser); 63 }; 64 65 typedef std::vector<Word> WordList; 66 typedef base::StringTokenizerT<base::string16, base::string16::const_iterator> 67 String16Tokenizer; 68 69 // These are exposed for tests. 70 CONTENT_EXPORT bool FindStateStartingInWord(WordList* words, 71 size_t state_first_word, 72 size_t* state_last_word, 73 String16Tokenizer* tokenizer, 74 size_t* state_index); 75 76 CONTENT_EXPORT bool IsValidLocationName(const Word& word); 77 CONTENT_EXPORT bool IsZipValid(const Word& word, size_t state_index); 78 CONTENT_EXPORT bool IsZipValidForState(const Word& word, size_t state_index); 79 80 } // namespace internal 81 82 } // namespace address_parser 83 84 } // namespace content 85 86 #endif // CONTENT_COMMON_ADDRESS_PARSER_INTERNAL_H_ 87