Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / libaddressinput / src / cpp / src / post_box_matchers.cc
1 // Copyright (C) 2014 Google Inc.
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 #include "post_box_matchers.h"
16
17 #include <cstddef>
18 #include <map>
19 #include <string>
20 #include <utility>
21 #include <vector>
22
23 #include <re2/re2.h>
24
25 #include "language.h"
26 #include "rule.h"
27 #include "util/re2ptr.h"
28
29 namespace i18n {
30 namespace addressinput {
31
32 namespace {
33
34 std::map<std::string, const RE2ptr*> InitMatchers() {
35   static const struct {
36     const char* const language;
37     const RE2ptr ptr;
38   } kMatchers[] = {
39     { "ar",
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") },
43
44     { "cs", new RE2("(?i)p\\.? ?p\\.? \\d") },
45     { "da", new RE2("(?i)Postboks") },
46     { "de", new RE2("(?i)Postfach") },
47
48     { "el",
49       /* "T\\.? ?Θ\\.? \\d{2}" */
50       new RE2("(?i)T\\.? ?\xCE\x98\\.? \\d{2}") },
51
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") },
56
57     { "hu",
58       /* "Postafi(?:[oó]|ó)k|Pf\\.? \\d" */
59       new RE2("(?i)Postafi(?:[o\xC3\xB3]|o\xCC\x81)k|Pf\\.? \\d") },
60
61     { "fr",
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") },
64
65     { "ja",
66       /* "私書箱\\d{1,5}号" */
67       new RE2("(?i)\xE7\xA7\x81\xE6\x9B\xB8\xE7\xAE\xB1\\d{1,5}\xE5\x8F\xB7") },
68
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") },
73
74     { "ru",
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") },
79
80     { "sv", new RE2("(?i)Box \\d") },
81
82     { "zh",
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") },
87
88     { "und", new RE2("P\\.? ?O\\.? Box") }
89   };
90
91   std::map<std::string, const RE2ptr*> matchers;
92
93   for (size_t i = 0; i < sizeof kMatchers / sizeof *kMatchers; ++i) {
94     matchers.insert(std::make_pair(kMatchers[i].language, &kMatchers[i].ptr));
95   }
96
97   return matchers;
98 }
99
100 }  // namespace
101
102 // static
103 std::vector<const RE2ptr*> PostBoxMatchers::GetMatchers(
104     const Rule& country_rule) {
105   static const std::map<std::string, const RE2ptr*> kMatchers(InitMatchers());
106
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);
114   }
115
116   std::vector<const RE2ptr*> result;
117
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);
125     }
126   }
127
128   return result;
129 }
130
131 }  // namespace addressinput
132 }  // namespace i18n