OLD | NEW |
(Empty) | |
| 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 <libaddressinput/address_metadata.h> |
| 16 |
| 17 #include <libaddressinput/address_field.h> |
| 18 |
| 19 #include <algorithm> |
| 20 #include <string> |
| 21 |
| 22 #include "region_data_constants.h" |
| 23 #include "rule.h" |
| 24 |
| 25 namespace i18n { |
| 26 namespace addressinput { |
| 27 |
| 28 bool IsFieldRequired(AddressField field, const std::string& region_code) { |
| 29 if (field == COUNTRY) { |
| 30 return true; |
| 31 } |
| 32 |
| 33 Rule rule; |
| 34 rule.CopyFrom(Rule::GetDefault()); |
| 35 if (!rule.ParseSerializedRule( |
| 36 RegionDataConstants::GetRegionData(region_code))) { |
| 37 return false; |
| 38 } |
| 39 |
| 40 return std::find(rule.GetRequired().begin(), |
| 41 rule.GetRequired().end(), |
| 42 field) != rule.GetRequired().end(); |
| 43 } |
| 44 |
| 45 bool IsFieldUsed(AddressField field, const std::string& region_code) { |
| 46 if (field == COUNTRY) { |
| 47 return true; |
| 48 } |
| 49 |
| 50 Rule rule; |
| 51 rule.CopyFrom(Rule::GetDefault()); |
| 52 if (!rule.ParseSerializedRule( |
| 53 RegionDataConstants::GetRegionData(region_code))) { |
| 54 return false; |
| 55 } |
| 56 |
| 57 FormatElement to_find(field); |
| 58 for (std::vector<std::vector<FormatElement> >::const_iterator line_it = |
| 59 rule.GetFormat().begin(); |
| 60 line_it != rule.GetFormat().end(); |
| 61 ++line_it) { |
| 62 if (std::find(line_it->begin(), line_it->end(), to_find) != line_it->end()) |
| 63 return true; |
| 64 } |
| 65 |
| 66 return false; |
| 67 } |
| 68 |
| 69 } // namespace addressinput |
| 70 } // namespace i18n |
OLD | NEW |