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 // An object representing a token in a formatting string. This may be a 16 // placeholder for a field in the address data such as ADMIN_AREA, a literal 17 // string such as " ", or a newline. 18 19 #ifndef I18N_ADDRESSINPUT_FORMAT_ELEMENT_H_ 20 #define I18N_ADDRESSINPUT_FORMAT_ELEMENT_H_ 21 22 #include <libaddressinput/address_field.h> 23 24 #include <iosfwd> 25 #include <string> 26 27 namespace i18n { 28 namespace addressinput { 29 30 class FormatElement { 31 public: 32 // Builds a format element to represent |field|. 33 explicit FormatElement(AddressField field); 34 35 // Builds an element representing a literal string |literal|. 36 explicit FormatElement(const std::string& literal); 37 38 // Builds a newline element. 39 FormatElement(); 40 41 // Returns true if this element represents a field element. IsField()42 bool IsField() const { return literal_.empty(); } 43 44 // Returns true if this element represents a new line. IsNewline()45 bool IsNewline() const { return literal_ == "\n"; } 46 GetField()47 AddressField GetField() const { return field_; } GetLiteral()48 const std::string& GetLiteral() const { return literal_; } 49 50 bool operator==(const FormatElement& other) const; 51 52 private: 53 // The field this element represents. Should only be used if |literal| is an 54 // empty string. 55 AddressField field_; 56 57 // The literal string for this element. This will be "\n" if this is a 58 // newline - but IsNewline() is preferred to determine this. If empty, then 59 // this FormatElement represents an address field. 60 std::string literal_; 61 }; 62 63 } // namespace addressinput 64 } // namespace i18n 65 66 // Produces human-readable output in logging, for example in unit tests. 67 std::ostream& operator<<(std::ostream& o, 68 const i18n::addressinput::FormatElement& field); 69 70 #endif // I18N_ADDRESSINPUT_FORMAT_ELEMENT_H_ 71