Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Unified Diff: third_party/libaddressinput/chromium/cpp/src/rule.cc

Issue 131223004: [rac] Format an address for display. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/libaddressinput/chromium/cpp/src/rule.cc
diff --git a/third_party/libaddressinput/chromium/cpp/src/rule.cc b/third_party/libaddressinput/chromium/cpp/src/rule.cc
index 006ea58714e275971a9a6eb282d7cc2bcfe26293..95c52ef1046798dd6588e6b6ded4a1bc3e361dd4 100644
--- a/third_party/libaddressinput/chromium/cpp/src/rule.cc
+++ b/third_party/libaddressinput/chromium/cpp/src/rule.cc
@@ -71,27 +71,38 @@ bool ParseToken(char c, AddressField* field) {
// Clears |fields|, parses |format|, and adds the format address fields to
// |fields|.
//
-// For example, the address format in Switzerland is "%O%n%N%n%A%nAX-%Z
-// %C%nÅLAND". It includes the allowed fields prefixed with %, newlines denoted
-// %n, and the extra text that should be included on an envelope. This function
-// parses only the tokens denoted % to determine how an address input form
-// should be laid out.
-//
-// The format string "%O%n%N%n%A%nAX-%Z%C%nÅLAND" is parsed into
-// {{ORGANIZATION}, {RECIPIENT}, {STREET_ADDRESS}, {POSTAL_CODE, LOCALITY}, {}}.
-void ParseAddressFieldsFormat(const std::string& format,
- std::vector<std::vector<AddressField> >* fields) {
- assert(fields != NULL);
- fields->clear();
- fields->resize(1);
- std::vector<std::string> format_parts;
- SplitString(format, '%', &format_parts);
- for (size_t i = 1; i < format_parts.size(); ++i) {
+// For example, the address format in Finland is "%O%n%N%n%A%nAX-%Z %C%nÅLAND".
+// It includes the allowed fields prefixed with %, newlines denoted %n, and the
+// extra text that should be included on an envelope. It is parsed into:
+// {
+// {ORGANIZATION},
+// {RECIPIENT},
+// {STREET_ADDRESS},
+// {"AX-", POSTAL_CODE, " ", LOCALITY},
+// {"ÅLAND"}
+// }
+void ParseAddressFieldsFormat(
+ const std::string& data,
+ std::vector<std::vector<FormatElement> >* elements) {
+ assert(elements != NULL);
+ elements->clear();
+ elements->resize(1);
+
+ std::vector<std::string> data_parts;
+ SplitString(data, '%', &data_parts);
+ if (!data_parts.empty() && !data_parts[0].empty()) {
+ elements->back().push_back(FormatElement(data_parts[0]));
Evan Stade 2014/01/14 18:05:22 don't really understand what's going on here, and
please use gerrit instead 2014/01/14 23:21:12 Added a comment.
+ }
+
+ for (size_t i = 1; i < data_parts.size(); ++i) {
AddressField field = COUNTRY;
- if (ParseToken(format_parts[i][0], &field)) {
- fields->back().push_back(field);
- } else if (format_parts[i][0] == 'n') {
- fields->push_back(std::vector<AddressField>());
+ if (ParseToken(data_parts[i][0], &field)) {
+ elements->back().push_back(FormatElement(field));
+ } else if (data_parts[i][0] == 'n') {
+ elements->push_back(std::vector<FormatElement>());
+ }
+ if (data_parts[i].length() > 1) {
+ elements->back().push_back(FormatElement(data_parts[i].substr(1)));
}
}
}
@@ -173,6 +184,20 @@ int GetPostalCodeMessageId(const std::string& postal_code_type, bool error) {
} // namespace
+FormatElement::FormatElement(AddressField field)
+ : type(FIELD), field(field), literal() {}
+
+FormatElement::FormatElement(const std::string& literal)
+ : type(LITERAL), field(COUNTRY), literal(literal) {}
+
+FormatElement::~FormatElement() {}
+
+bool FormatElement::operator==(const FormatElement& other) const {
+ return type == other.type &&
+ field == other.field &&
+ literal == other.literal;
+}
+
Rule::Rule()
: format_(),
required_(),

Powered by Google App Engine
This is Rietveld 408576698