1 // Copyright (C) 2014 Google Inc.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #include "post_box_matchers.h"
27 #include "util/re2ptr.h"
30 namespace addressinput {
34 std::map<std::string, const RE2ptr*> InitMatchers() {
36 const char* const language;
40 /* "صندوق بريد|ص[-. ]ب" */
41 new RE2("\xD8\xB5\xD9\x86\xD8\xAF\xD9\x88\xD9\x82 "
42 "\xD8\xA8\xD8\xB1\xD9\x8A\xD8\xAF|\xD8\xB5[-. ]\xD8\xA8") },
44 { "cs", new RE2("(?i)p\\.? ?p\\.? \\d") },
45 { "da", new RE2("(?i)Postboks") },
46 { "de", new RE2("(?i)Postfach") },
49 /* "T\\.? ?Θ\\.? \\d{2}" */
50 new RE2("(?i)T\\.? ?\xCE\x98\\.? \\d{2}") },
52 { "en", new RE2("Private Bag|Post(?:al)? Box") },
53 { "es", new RE2("(?i)(?:Apartado|Casillas) de correos?") },
54 { "fi", new RE2("(?i)Postilokero|P\\.?L\\.? \\d") },
55 { "hr", new RE2("(?i)p\\.? ?p\\.? \\d") },
58 /* "Postafi(?:[oó]|ó)k|Pf\\.? \\d" */
59 new RE2("(?i)Postafi(?:[o\xC3\xB3]|o\xCC\x81)k|Pf\\.? \\d") },
62 /* "Bo(?:[iî]|î)te Postale|BP \\d|CEDEX \\d" */
63 new RE2("(?i)Bo(?:[i\xC3\xAE]|i\xCC\x82)te Postale|BP \\d|CEDEX \\d") },
67 new RE2("(?i)\xE7\xA7\x81\xE6\x9B\xB8\xE7\xAE\xB1\\d{1,5}\xE5\x8F\xB7") },
69 { "nl", new RE2("(?i)Postbus") },
70 { "no", new RE2("(?i)Postboks") },
71 { "pl", new RE2("(?i)Skr(?:\\.?|ytka) poczt(?:\\.?|owa)") },
72 { "pt", new RE2("(?i)Apartado") },
75 /* "абонентский ящик|[аa]\\\" */
76 new RE2("(?i)\xD0\xB0\xD0\xB1\xD0\xBE\xD0\xBD\xD0\xB5\xD0\xBD\xD1\x82\xD1"
77 "\x81\xD0\xBA\xD0\xB8\xD0\xB9 \xD1\x8F\xD1\x89\xD0\xB8\xD0\xBA|"
78 "[\xD0\xB0""a]\\\"\xD1\x8F (?:(?:\xE2\x84\x96|#|N) ?)?\\d") },
80 { "sv", new RE2("(?i)Box \\d") },
83 /* "郵政信箱.{1,5}號|郵局第.{1,10}號信箱" */
84 new RE2("(?i)\xE9\x83\xB5\xE6\x94\xBF\xE4\xBF\xA1\xE7\xAE\xB1.{1,5}"
85 "\xE8\x99\x9F|\xE9\x83\xB5\xE5\xB1\x80\xE7\xAC\xAC.{1,10}"
86 "\xE8\x99\x9F\xE4\xBF\xA1\xE7\xAE\xB1") },
88 { "und", new RE2("P\\.? ?O\\.? Box") }
91 std::map<std::string, const RE2ptr*> matchers;
93 for (size_t i = 0; i < sizeof kMatchers / sizeof *kMatchers; ++i) {
94 matchers.insert(std::make_pair(kMatchers[i].language, &kMatchers[i].ptr));
103 std::vector<const RE2ptr*> PostBoxMatchers::GetMatchers(
104 const Rule& country_rule) {
105 static const std::map<std::string, const RE2ptr*> kMatchers(InitMatchers());
107 // Always add any expressions defined for "und" (English-like defaults).
108 std::vector<std::string> languages(1, "und");
109 for (std::vector<std::string>::const_iterator
110 it = country_rule.GetLanguages().begin();
111 it != country_rule.GetLanguages().end(); ++it) {
112 Language language(*it);
113 languages.push_back(language.base);
116 std::vector<const RE2ptr*> result;
118 for (std::vector<std::string>::const_iterator
119 it = languages.begin();
120 it != languages.end(); ++it) {
121 std::map<std::string, const RE2ptr*>::const_iterator
122 jt = kMatchers.find(*it);
123 if (jt != kMatchers.end()) {
124 result.push_back(jt->second);
131 } // namespace addressinput