diff options
Diffstat (limited to 'cpp')
28 files changed, 264 insertions, 188 deletions
diff --git a/cpp/include/libaddressinput/address_formatter.h b/cpp/include/libaddressinput/address_formatter.h index e267884..3ac94c3 100644 --- a/cpp/include/libaddressinput/address_formatter.h +++ b/cpp/include/libaddressinput/address_formatter.h @@ -45,6 +45,10 @@ void GetFormattedNationalAddressLine( void GetStreetAddressLinesAsSingleLine( const AddressData& address_data, std::string* line); +// Returns the separator used for collapsing multiple parts of an address into a +// single line. +std::string GetLineSeparatorForLanguage(const std::string& language_tag); + } // namespace addressinput } // namespace i18n diff --git a/cpp/include/libaddressinput/address_input_helper.h b/cpp/include/libaddressinput/address_input_helper.h index ce52cf9..043d3fc 100644 --- a/cpp/include/libaddressinput/address_input_helper.h +++ b/cpp/include/libaddressinput/address_input_helper.h @@ -23,9 +23,9 @@ namespace i18n { namespace addressinput { class LookupKey; -class Node; class PreloadSupplier; struct AddressData; +struct Node; class AddressInputHelper { public: diff --git a/cpp/include/libaddressinput/address_metadata.h b/cpp/include/libaddressinput/address_metadata.h index 269f982..cbe72db 100644 --- a/cpp/include/libaddressinput/address_metadata.h +++ b/cpp/include/libaddressinput/address_metadata.h @@ -17,6 +17,8 @@ #include <libaddressinput/address_field.h> +#include <string> + namespace i18n { namespace addressinput { diff --git a/cpp/include/libaddressinput/address_normalizer.h b/cpp/include/libaddressinput/address_normalizer.h index 6bd3366..06b7eb2 100644 --- a/cpp/include/libaddressinput/address_normalizer.h +++ b/cpp/include/libaddressinput/address_normalizer.h @@ -21,9 +21,9 @@ namespace i18n { namespace addressinput { -class AddressData; class PreloadSupplier; class StringCompare; +struct AddressData; class AddressNormalizer { public: diff --git a/cpp/libaddressinput.gypi b/cpp/libaddressinput.gypi index 8d9030f..03ecaf9 100644 --- a/cpp/libaddressinput.gypi +++ b/cpp/libaddressinput.gypi @@ -40,6 +40,7 @@ 'src/retriever.cc', 'src/rule.cc', 'src/rule_retriever.cc', + 'src/util/cctype_tolower_equal.cc', 'src/util/json.cc', 'src/util/md5.cc', 'src/util/string_compare.cc', diff --git a/cpp/res/messages.grdp b/cpp/res/messages.grdp index 1e1bcf2..c04449a 100644 --- a/cpp/res/messages.grdp +++ b/cpp/res/messages.grdp @@ -85,6 +85,12 @@ limitations under the License. Island </message> <message + name="IDS_LIBADDRESSINPUT_OBLAST" + desc="Administrative Area for certain countries (e.g., Russia's + Leningrad)."> + Oblast + </message> + <message name="IDS_LIBADDRESSINPUT_PARISH" desc="Administrative Area for certain countries (e.g., Andorra's Canillo)."> diff --git a/cpp/src/address_formatter.cc b/cpp/src/address_formatter.cc index 29f3486..da5bd07 100644 --- a/cpp/src/address_formatter.cc +++ b/cpp/src/address_formatter.cc @@ -14,20 +14,20 @@ #include <libaddressinput/address_formatter.h> -#include <strings.h> +#include <libaddressinput/address_data.h> +#include <libaddressinput/address_field.h> #include <algorithm> #include <cstddef> +#include <functional> #include <string> #include <vector> -#include <libaddressinput/address_data.h> -#include <libaddressinput/address_field.h> - #include "format_element.h" #include "language.h" #include "region_data_constants.h" #include "rule.h" +#include "util/cctype_tolower_equal.h" namespace i18n { namespace addressinput { @@ -66,53 +66,6 @@ const char* kLanguagesThatUseAnArabicComma[] = { "uz" }; -// Case insensitive matcher for language tags. -struct LanguageMatcher { - LanguageMatcher(const std::string& tag) : tag(tag) {} - std::string tag; - bool operator() (const std::string& s) { - return strcasecmp(tag.c_str(), s.c_str()) == 0; - } -}; - -std::string GetLineSeparatorForLanguage(const std::string& language_tag) { - Language address_language(language_tag); - - // First deal with explicit script tags. - if (address_language.has_latin_script) { - return kCommaSeparator; - } - - // Now guess something appropriate based on the base language. - const std::string& base_language = address_language.base; - if (std::find_if(kLanguagesThatUseSpace, - kLanguagesThatUseSpace + arraysize(kLanguagesThatUseSpace), - LanguageMatcher(base_language)) != - kLanguagesThatUseSpace + arraysize(kLanguagesThatUseSpace)) { - return kSpaceSeparator; - } else if (std::find_if(kLanguagesThatHaveNoSeparator, - kLanguagesThatHaveNoSeparator + - arraysize(kLanguagesThatHaveNoSeparator), - LanguageMatcher(base_language)) != - kLanguagesThatHaveNoSeparator + - arraysize(kLanguagesThatHaveNoSeparator)) { - return ""; - } else if (std::find_if(kLanguagesThatUseAnArabicComma, - kLanguagesThatUseAnArabicComma + - arraysize(kLanguagesThatUseAnArabicComma), - LanguageMatcher(base_language)) != - kLanguagesThatUseAnArabicComma + - arraysize(kLanguagesThatUseAnArabicComma)) { - return kArabicCommaSeparator; - } - // Either the language is a latin-script language, or no language was - // specified. In the latter case we still return ", " as the most common - // separator in use. In countries that don't use this, e.g. Thailand, - // addresses are often written in latin script where this would still be - // appropriate, so this is a reasonable default in the absence of information. - return kCommaSeparator; -} - void CombineLinesForLanguage( const std::vector<std::string>& lines, const std::string& language_tag, std::string *line) { @@ -193,5 +146,45 @@ void GetStreetAddressLinesAsSingleLine( address_data.address_line, address_data.language_code, line); } +std::string GetLineSeparatorForLanguage(const std::string& language_tag) { + Language address_language(language_tag); + + // First deal with explicit script tags. + if (address_language.has_latin_script) { + return kCommaSeparator; + } + + // Now guess something appropriate based on the base language. + const std::string& base_language = address_language.base; + if (std::find_if(kLanguagesThatUseSpace, + kLanguagesThatUseSpace + arraysize(kLanguagesThatUseSpace), + std::bind2nd(EqualToTolowerString(), base_language)) != + kLanguagesThatUseSpace + arraysize(kLanguagesThatUseSpace)) { + return kSpaceSeparator; + } else if (std::find_if( + kLanguagesThatHaveNoSeparator, + kLanguagesThatHaveNoSeparator + + arraysize(kLanguagesThatHaveNoSeparator), + std::bind2nd(EqualToTolowerString(), base_language)) != + kLanguagesThatHaveNoSeparator + + arraysize(kLanguagesThatHaveNoSeparator)) { + return ""; + } else if (std::find_if( + kLanguagesThatUseAnArabicComma, + kLanguagesThatUseAnArabicComma + + arraysize(kLanguagesThatUseAnArabicComma), + std::bind2nd(EqualToTolowerString(), base_language)) != + kLanguagesThatUseAnArabicComma + + arraysize(kLanguagesThatUseAnArabicComma)) { + return kArabicCommaSeparator; + } + // Either the language is a latin-script language, or no language was + // specified. In the latter case we still return ", " as the most common + // separator in use. In countries that don't use this, e.g. Thailand, + // addresses are often written in latin script where this would still be + // appropriate, so this is a reasonable default in the absence of information. + return kCommaSeparator; +} + } // namespace addressinput } // namespace i18n diff --git a/cpp/src/address_metadata.cc b/cpp/src/address_metadata.cc index 59030ef..2310f2e 100644 --- a/cpp/src/address_metadata.cc +++ b/cpp/src/address_metadata.cc @@ -17,6 +17,7 @@ #include <libaddressinput/address_field.h> #include <algorithm> +#include <string> #include "region_data_constants.h" #include "rule.h" diff --git a/cpp/src/lookup_key.cc b/cpp/src/lookup_key.cc index b6f73fc..fbd9a50 100644 --- a/cpp/src/lookup_key.cc +++ b/cpp/src/lookup_key.cc @@ -14,20 +14,22 @@ #include "lookup_key.h" +#include <libaddressinput/address_data.h> +#include <libaddressinput/address_field.h> +#include <libaddressinput/util/basictypes.h> + #include <cassert> #include <cstddef> +#include <functional> #include <map> #include <string> #include <utility> #include <vector> -#include <libaddressinput/address_data.h> -#include <libaddressinput/address_field.h> -#include <libaddressinput/util/basictypes.h> - #include "language.h" #include "region_data_constants.h" #include "rule.h" +#include "util/cctype_tolower_equal.h" namespace i18n { namespace addressinput { @@ -39,15 +41,6 @@ const char kDashDelim[] = "--"; const char kData[] = "data"; const char kUnknown[] = "ZZ"; -// Case insensitive matcher for language tags. -struct LanguageMatcher { - LanguageMatcher(const std::string& tag) : tag(tag) { } - std::string tag; - bool operator() (const std::string& s) { - return strcasecmp(tag.c_str(), s.c_str()) == 0; - } -}; - // Assume the language_tag has had "Latn" script removed when this is called. bool ShouldSetLanguageForKey(const std::string& language_tag, const std::string& region_code) { @@ -69,10 +62,9 @@ bool ShouldSetLanguageForKey(const std::string& language_tag, return false; } // Finally, only return true if the language is one of the remaining ones. - return std::find_if(languages.begin() + 1, - languages.end(), - LanguageMatcher(language_tag)) - != languages.end(); + return std::find_if(languages.begin() + 1, languages.end(), + std::bind2nd(EqualToTolowerString(), language_tag)) != + languages.end(); } } // namespace diff --git a/cpp/src/post_box_matchers.h b/cpp/src/post_box_matchers.h index 8f9f44b..c269f11 100644 --- a/cpp/src/post_box_matchers.h +++ b/cpp/src/post_box_matchers.h @@ -22,8 +22,8 @@ namespace i18n { namespace addressinput { -class RE2ptr; class Rule; +struct RE2ptr; class PostBoxMatchers { public: diff --git a/cpp/src/preload_supplier.cc b/cpp/src/preload_supplier.cc index 31325bd..3f3dc0e 100644 --- a/cpp/src/preload_supplier.cc +++ b/cpp/src/preload_supplier.cc @@ -53,15 +53,11 @@ class IndexLess : public std::binary_function<std::string, std::string, bool> { public: result_type operator()(const first_argument_type& a, const second_argument_type& b) const { + static const StringCompare kStringCompare; return kStringCompare.NaturalLess(a, b); } - - private: - static const StringCompare kStringCompare; }; -const StringCompare IndexLess::kStringCompare; - } // namespace class IndexMap : public std::map<std::string, const Rule*, IndexLess> {}; @@ -105,6 +101,7 @@ class Helper { (void)status; // Prevent unused variable if assert() is optimized away. Json json; + std::string id; std::vector<const Rule*> sub_rules; if (!success) { @@ -124,11 +121,10 @@ class Helper { } const Json& value = json.GetDictionaryValueForKey(*it); - if (!value.HasStringValueForKey("id")) { + if (!value.GetStringValueForKey("id", &id)) { success = false; goto callback; } - const std::string& id = value.GetStringValueForKey("id"); assert(*it == id); // Sanity check. size_t depth = std::count(id.begin(), id.end(), '/') - 1; diff --git a/cpp/src/region_data_constants.cc b/cpp/src/region_data_constants.cc index cabee28..0a1f78b 100644 --- a/cpp/src/region_data_constants.cc +++ b/cpp/src/region_data_constants.cc @@ -43,9 +43,8 @@ std::map<std::string, std::string> InitRegionData() { "\"languages\":\"en\"" "}")); region_data.insert(std::make_pair("AD", "{" - "\"fmt\":\"%N%n%O%n%A%n%Z %S\"," - "\"require\":\"AS\"," - "\"state_name_type\":\"parish\"," + "\"fmt\":\"%N%n%O%n%A%n%Z %C\"," + "\"require\":\"A\"," "\"zipex\":\"AD100,AD501,AD700\"," "\"posturl\":\"http://www.correos.es/comun/CodigosPostales/1010_s-CodPostal.asp\?Provincia=\"," "\"languages\":\"ca\"" @@ -119,7 +118,7 @@ std::map<std::string, std::string> InitRegionData() { "\"languages\":\"nl~pap\"" "}")); region_data.insert(std::make_pair("AX", "{" - "\"fmt\":\"%O%n%N%n%A%nAX-%Z %C%n\u00c5LAND\"," + "\"fmt\":\"%O%n%N%n%A%nAX-%Z %C%n\\u00c5LAND\"," "\"require\":\"ACZ\"," "\"zipex\":\"22150,22550,22240,22710,22270,22730,22430\"," "\"posturl\":\"http://www.posten.ax/department.con\?iPage=123\"," @@ -329,7 +328,7 @@ std::map<std::string, std::string> InitRegionData() { "\"languages\":\"ar~fr\"" "}")); region_data.insert(std::make_pair("DK", "{" - "\"fmt\":\"%O%n%N%n%A%n%Z %C\"," + "\"fmt\":\"%N%n%O%n%A%n%Z %C\"," "\"require\":\"ACZ\"," "\"zipex\":\"8660,1566\"," "\"posturl\":\"http://www.postdanmark.dk/da/Privat/Kundeservice/postnummerkort/Sider/Find-postnummer.aspx\"," @@ -634,7 +633,7 @@ std::map<std::string, std::string> InitRegionData() { "\"languages\":\"ar\"" "}")); region_data.insert(std::make_pair("JP", "{" - "\"fmt\":\"\u3012%Z%n%S%C%n%A%n%O%n%N\"," + "\"fmt\":\"\\u3012%Z%n%S%C%n%A%n%O%n%N\"," "\"lfmt\":\"%N%n%O%n%A%n%C, %S%n%Z\"," "\"require\":\"ACSZ\"," "\"state_name_type\":\"prefecture\"," @@ -1062,8 +1061,9 @@ std::map<std::string, std::string> InitRegionData() { "\"languages\":\"sr-Cyrl~sr-Latn\"" "}")); region_data.insert(std::make_pair("RU", "{" - "\"fmt\":\"%Z %C %n%A%n%O%n%N\"," + "\"fmt\":\"%N%n%O%n%A%n%C%n%S%n%Z\"," "\"require\":\"ACZ\"," + "\"state_name_type\":\"oblast\"," "\"zipex\":\"125075,247112,103375\"," "\"posturl\":\"http://info.russianpost.ru/servlet/department\"," "\"languages\":\"ru\"" diff --git a/cpp/src/rule.cc b/cpp/src/rule.cc index e25142f..4d1cff5 100644 --- a/cpp/src/rule.cc +++ b/cpp/src/rule.cc @@ -38,20 +38,6 @@ namespace { typedef std::map<std::string, int> NameMessageIdMap; -const char kAdminAreaNameTypeKey[] = "state_name_type"; -const char kFormatKey[] = "fmt"; -const char kIdKey[] = "id"; -const char kLanguagesKey[] = "languages"; -const char kLatinFormatKey[] = "lfmt"; -const char kLatinNameKey[] = "lname"; -const char kNameKey[] = "name"; -const char kPostalCodeNameTypeKey[] = "zip_name_type"; -const char kRequireKey[] = "require"; -const char kSubKeysKey[] = "sub_keys"; -const char kZipKey[] = "zip"; -const char kPostalCodeExampleKey[] = "zipex"; -const char kPostServiceUrlKey[] = "posturl"; - // Used as a separator in a list of items. For example, the list of supported // languages can be "de~fr~it". const char kSeparator = '~'; @@ -73,6 +59,8 @@ NameMessageIdMap InitAdminAreaMessageIds() { message_ids.insert(std::make_pair( "island", IDS_LIBADDRESSINPUT_ISLAND)); message_ids.insert(std::make_pair( + "oblast", IDS_LIBADDRESSINPUT_OBLAST)); + message_ids.insert(std::make_pair( "parish", IDS_LIBADDRESSINPUT_PARISH)); message_ids.insert(std::make_pair( "prefecture", IDS_LIBADDRESSINPUT_PREFECTURE)); @@ -180,36 +168,33 @@ bool Rule::ParseSerializedRule(const std::string& serialized_rule) { } void Rule::ParseJsonRule(const Json& json) { - if (json.HasStringValueForKey(kIdKey)) { - id_ = json.GetStringValueForKey(kIdKey); + std::string value; + if (json.GetStringValueForKey("id", &value)) { + id_.swap(value); } - if (json.HasStringValueForKey(kFormatKey)) { - ParseFormatRule(json.GetStringValueForKey(kFormatKey), &format_); + if (json.GetStringValueForKey("fmt", &value)) { + ParseFormatRule(value, &format_); } - if (json.HasStringValueForKey(kLatinFormatKey)) { - ParseFormatRule(json.GetStringValueForKey(kLatinFormatKey), &latin_format_); + if (json.GetStringValueForKey("lfmt", &value)) { + ParseFormatRule(value, &latin_format_); } - if (json.HasStringValueForKey(kRequireKey)) { - ParseAddressFieldsRequired( - json.GetStringValueForKey(kRequireKey), &required_); + if (json.GetStringValueForKey("require", &value)) { + ParseAddressFieldsRequired(value, &required_); } - if (json.HasStringValueForKey(kSubKeysKey)) { - SplitString( - json.GetStringValueForKey(kSubKeysKey), kSeparator, &sub_keys_); + if (json.GetStringValueForKey("sub_keys", &value)) { + SplitString(value, kSeparator, &sub_keys_); } - if (json.HasStringValueForKey(kLanguagesKey)) { - SplitString( - json.GetStringValueForKey(kLanguagesKey), kSeparator, &languages_); + if (json.GetStringValueForKey("languages", &value)) { + SplitString(value, kSeparator, &languages_); } sole_postal_code_.clear(); - if (json.HasStringValueForKey(kZipKey)) { - const std::string& zip = json.GetStringValueForKey(kZipKey); + if (json.GetStringValueForKey("zip", &value)) { // The "zip" field in the JSON data is used in two different ways to // validate the postal code. At the country level, the "zip" field indicates // a Java compatible regular expression corresponding to all postal codes in @@ -223,7 +208,7 @@ void Rule::ParseJsonRule(const Json& json) { // RE2::FullMatch() to perform matching against the entire string. RE2::Options options; options.set_never_capture(true); - RE2* matcher = new RE2("^(" + zip + ")", options); + RE2* matcher = new RE2("^(" + value + ")", options); if (matcher->ok()) { postal_code_matcher_.reset(new RE2ptr(matcher)); } else { @@ -232,37 +217,35 @@ void Rule::ParseJsonRule(const Json& json) { } // If the "zip" field is not a regular expression, then it is the sole // postal code for this rule. - if (!ContainsRegExSpecialCharacters(zip)) { - sole_postal_code_ = zip; + if (!ContainsRegExSpecialCharacters(value)) { + sole_postal_code_.swap(value); } } - if (json.HasStringValueForKey(kAdminAreaNameTypeKey)) { + if (json.GetStringValueForKey("state_name_type", &value)) { admin_area_name_message_id_ = - GetMessageIdFromName(json.GetStringValueForKey(kAdminAreaNameTypeKey), - GetAdminAreaMessageIds()); + GetMessageIdFromName(value, GetAdminAreaMessageIds()); } - if (json.HasStringValueForKey(kPostalCodeNameTypeKey)) { + if (json.GetStringValueForKey("zip_name_type", &value)) { postal_code_name_message_id_ = - GetMessageIdFromName(json.GetStringValueForKey(kPostalCodeNameTypeKey), - GetPostalCodeMessageIds()); + GetMessageIdFromName(value, GetPostalCodeMessageIds()); } - if (json.HasStringValueForKey(kNameKey)) { - name_ = json.GetStringValueForKey(kNameKey); + if (json.GetStringValueForKey("name", &value)) { + name_.swap(value); } - if (json.HasStringValueForKey(kLatinNameKey)) { - latin_name_ = json.GetStringValueForKey(kLatinNameKey); + if (json.GetStringValueForKey("lname", &value)) { + latin_name_.swap(value); } - if (json.HasStringValueForKey(kPostalCodeExampleKey)) { - postal_code_example_ = json.GetStringValueForKey(kPostalCodeExampleKey); + if (json.GetStringValueForKey("zipex", &value)) { + postal_code_example_.swap(value); } - if (json.HasStringValueForKey(kPostServiceUrlKey)) { - post_service_url_ = json.GetStringValueForKey(kPostServiceUrlKey); + if (json.GetStringValueForKey("posturl", &value)) { + post_service_url_.swap(value); } } diff --git a/cpp/src/rule.h b/cpp/src/rule.h index c722932..38f20b3 100644 --- a/cpp/src/rule.h +++ b/cpp/src/rule.h @@ -33,7 +33,7 @@ namespace i18n { namespace addressinput { class Json; -class RE2ptr; +struct RE2ptr; // Stores address metadata addressing rules, to be used for determining the // layout of an address input widget or for address validation. Sample usage: diff --git a/cpp/src/util/cctype_tolower_equal.cc b/cpp/src/util/cctype_tolower_equal.cc new file mode 100644 index 0000000..819138f --- /dev/null +++ b/cpp/src/util/cctype_tolower_equal.cc @@ -0,0 +1,44 @@ +// Copyright (C) 2014 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "cctype_tolower_equal.h" + +#include <algorithm> +#include <cctype> +#include <functional> +#include <string> + +namespace i18n { +namespace addressinput { + +namespace { + +struct EqualToTolowerChar + : public std::binary_function<std::string::value_type, + std::string::value_type, bool> { + result_type operator()(first_argument_type a, second_argument_type b) const { + return std::tolower(a) == std::tolower(b); + } +}; + +} // namespace + +EqualToTolowerString::result_type EqualToTolowerString::operator()( + const first_argument_type& a, const second_argument_type& b) const { + return a.size() == b.size() && + std::equal(a.begin(), a.end(), b.begin(), EqualToTolowerChar()); +} + +} // addressinput +} // i18n diff --git a/cpp/src/util/cctype_tolower_equal.h b/cpp/src/util/cctype_tolower_equal.h new file mode 100644 index 0000000..106087e --- /dev/null +++ b/cpp/src/util/cctype_tolower_equal.h @@ -0,0 +1,35 @@ +// Copyright (C) 2014 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef I18N_ADDRESSINPUT_UTIL_CCTYPE_TOLOWER_EQUAL_H_ +#define I18N_ADDRESSINPUT_UTIL_CCTYPE_TOLOWER_EQUAL_H_ + +#include <functional> +#include <string> + +namespace i18n { +namespace addressinput { + +// Performs case insensitive comparison of |a| and |b| by calling std::tolower() +// from <cctype>. +struct EqualToTolowerString + : public std::binary_function<std::string, std::string, bool> { + result_type operator()(const first_argument_type& a, + const second_argument_type& b) const; +}; + +} // addressinput +} // i18n + +#endif // I18N_ADDRESSINPUT_UTIL_CCTYPE_TOLOWER_EQUAL_H_ diff --git a/cpp/src/util/json.cc b/cpp/src/util/json.cc index d819bb2..352c62b 100644 --- a/cpp/src/util/json.cc +++ b/cpp/src/util/json.cc @@ -139,23 +139,19 @@ const std::vector<std::string>& Json::GetKeys() const { return impl_->GetKeys(); } -bool Json::HasStringValueForKey(const std::string& key) const { - assert(impl_ != NULL); - - // Member is owned by impl_. - const Value::Member* member = impl_->FindMember(key); - return member != NULL && member->value.IsString(); -} - -std::string Json::GetStringValueForKey(const std::string& key) const { +bool Json::GetStringValueForKey(const std::string& key, + std::string* value) const { assert(impl_ != NULL); + assert(value != NULL); // Member is owned by impl_. const Value::Member* member = impl_->FindMember(key.c_str()); - assert(member != NULL); - assert(member->value.IsString()); - return std::string(member->value.GetString(), - member->value.GetStringLength()); + if (member == NULL || !member->value.IsString()) { + return false; + } + + value->assign(member->value.GetString(), member->value.GetStringLength()); + return true; } bool Json::HasDictionaryValueForKey(const std::string& key) const { diff --git a/cpp/src/util/json.h b/cpp/src/util/json.h index 1fbf500..90565e6 100644 --- a/cpp/src/util/json.h +++ b/cpp/src/util/json.h @@ -43,15 +43,11 @@ class Json { // successfully in ParseObject() before invoking this method. const std::vector<std::string>& GetKeys() const; - // Returns true if the parsed JSON contains a string value for |key|. The JSON - // object must be parsed successfully in ParseObject() before invoking this - // method. - bool HasStringValueForKey(const std::string& key) const; - - // Returns the string value for the |key|. The |key| must be present and its - // value must be of string type, i.e., HasStringValueForKey(key) must return - // true before invoking this method. - std::string GetStringValueForKey(const std::string& key) const; + // Returns true if the parsed JSON contains a string value for |key|. Sets + // |value| to the string value of the |key|. The JSON object must be parsed + // successfully in ParseObject() before invoking this method. The |value| + // parameter should not be NULL. + bool GetStringValueForKey(const std::string& key, std::string* value) const; // Returns true if the parsed JSON contains a dictionary value for |key|. The // JSON object must be parsed successfully in ParseObject() before invoking diff --git a/cpp/src/validating_util.cc b/cpp/src/validating_util.cc index 6b52e8b..df3fc06 100644 --- a/cpp/src/validating_util.cc +++ b/cpp/src/validating_util.cc @@ -84,7 +84,10 @@ bool UnwrapHeader(const char* header_prefix, void ValidatingUtil::Wrap(time_t timestamp, std::string* data) { assert(data != NULL); char timestamp_string[2 + 3 * sizeof timestamp]; - snprintf(timestamp_string, sizeof timestamp_string, "%ld", timestamp); + int size = std::sprintf(timestamp_string, "%ld", timestamp); + assert(size > 0); + assert(size < sizeof timestamp_string); + (void)size; std::string header; header.append(kTimestampPrefix, kTimestampPrefixLength); diff --git a/cpp/test/address_input_helper_test.cc b/cpp/test/address_input_helper_test.cc index 84a2286..bb683da 100644 --- a/cpp/test/address_input_helper_test.cc +++ b/cpp/test/address_input_helper_test.cc @@ -326,7 +326,7 @@ TEST_F(AddressInputHelperMockDataTest, "{\"data/KR\": " // The top-level ZIP expression must be present for sub-key matches to be // evaluated. - "{\"id\":\"data/KR\", \"sub_keys\":\"A~B\", \"zip\":\"\\\\d\{5}\"}, " + "{\"id\":\"data/KR\", \"sub_keys\":\"A~B\", \"zip\":\"\\\\d{5}\"}, " "\"data/KR/A\": " "{\"id\":\"data/KR/A\", \"sub_keys\":\"A1~A2\"}, " "\"data/KR/A/A1\": " diff --git a/cpp/test/fake_downloader_test.cc b/cpp/test/fake_downloader_test.cc index 2d88acc..6468277 100644 --- a/cpp/test/fake_downloader_test.cc +++ b/cpp/test/fake_downloader_test.cc @@ -16,6 +16,7 @@ #include <libaddressinput/callback.h> #include <libaddressinput/downloader.h> +#include <libaddressinput/util/basictypes.h> #include <libaddressinput/util/scoped_ptr.h> #include <cstddef> @@ -27,7 +28,6 @@ namespace { -using i18n::addressinput::BuildCallback; using i18n::addressinput::Downloader; using i18n::addressinput::FakeDownloader; using i18n::addressinput::RegionDataConstants; @@ -40,7 +40,8 @@ class FakeDownloaderTest : public testing::TestWithParam<std::string> { virtual ~FakeDownloaderTest() {} Downloader::Callback* BuildCallback() { - return ::BuildCallback(this, &FakeDownloaderTest::OnDownloaded); + return i18n::addressinput::BuildCallback( + this, &FakeDownloaderTest::OnDownloaded); } FakeDownloader downloader_; @@ -58,6 +59,8 @@ class FakeDownloaderTest : public testing::TestWithParam<std::string> { delete data; } } + + DISALLOW_COPY_AND_ASSIGN(FakeDownloaderTest); }; // Returns testing::AssertionSuccess if |data| is valid downloaded data for diff --git a/cpp/test/fake_storage_test.cc b/cpp/test/fake_storage_test.cc index 49663ca..f950ee4 100644 --- a/cpp/test/fake_storage_test.cc +++ b/cpp/test/fake_storage_test.cc @@ -16,6 +16,7 @@ #include <libaddressinput/callback.h> #include <libaddressinput/storage.h> +#include <libaddressinput/util/basictypes.h> #include <libaddressinput/util/scoped_ptr.h> #include <cstddef> @@ -25,7 +26,6 @@ namespace { -using i18n::addressinput::BuildCallback; using i18n::addressinput::FakeStorage; using i18n::addressinput::scoped_ptr; using i18n::addressinput::Storage; @@ -37,7 +37,8 @@ class FakeStorageTest : public testing::Test { virtual ~FakeStorageTest() {} Storage::Callback* BuildCallback() { - return ::BuildCallback(this, &FakeStorageTest::OnDataReady); + return i18n::addressinput::BuildCallback( + this, &FakeStorageTest::OnDataReady); } FakeStorage storage_; @@ -57,6 +58,8 @@ class FakeStorageTest : public testing::Test { delete data; } } + + DISALLOW_COPY_AND_ASSIGN(FakeStorageTest); }; TEST_F(FakeStorageTest, GetWithoutPutReturnsEmptyData) { diff --git a/cpp/test/null_storage_test.cc b/cpp/test/null_storage_test.cc index dca0803..e586568 100644 --- a/cpp/test/null_storage_test.cc +++ b/cpp/test/null_storage_test.cc @@ -25,7 +25,6 @@ namespace { -using i18n::addressinput::BuildCallback; using i18n::addressinput::NullStorage; using i18n::addressinput::scoped_ptr; using i18n::addressinput::Storage; @@ -33,9 +32,11 @@ using i18n::addressinput::Storage; class NullStorageTest : public testing::Test { protected: NullStorageTest() {} + virtual ~NullStorageTest() {} Storage::Callback* BuildCallback() { - return ::BuildCallback(this, &NullStorageTest::OnDataReady); + return i18n::addressinput::BuildCallback( + this, &NullStorageTest::OnDataReady); } NullStorage storage_; diff --git a/cpp/test/post_box_matchers_test.cc b/cpp/test/post_box_matchers_test.cc index d89ee0b..eb9230c 100644 --- a/cpp/test/post_box_matchers_test.cc +++ b/cpp/test/post_box_matchers_test.cc @@ -23,7 +23,7 @@ namespace i18n { namespace addressinput { -class RE2ptr; +struct RE2ptr; } // namespace addressinput } // namespace i18n diff --git a/cpp/test/retriever_test.cc b/cpp/test/retriever_test.cc index aff418c..513e799 100644 --- a/cpp/test/retriever_test.cc +++ b/cpp/test/retriever_test.cc @@ -17,6 +17,7 @@ #include <libaddressinput/callback.h> #include <libaddressinput/null_storage.h> #include <libaddressinput/storage.h> +#include <libaddressinput/util/basictypes.h> #include <libaddressinput/util/scoped_ptr.h> #include <cstddef> @@ -33,7 +34,6 @@ namespace { -using i18n::addressinput::BuildCallback; using i18n::addressinput::FakeDownloader; using i18n::addressinput::MockDownloader; using i18n::addressinput::NullStorage; @@ -68,7 +68,8 @@ class RetrieverTest : public testing::Test { virtual ~RetrieverTest() {} Retriever::Callback* BuildCallback() { - return ::BuildCallback(this, &RetrieverTest::OnDataReady); + return i18n::addressinput::BuildCallback( + this, &RetrieverTest::OnDataReady); } Retriever retriever_; @@ -84,6 +85,8 @@ class RetrieverTest : public testing::Test { key_ = key; data_ = data; } + + DISALLOW_COPY_AND_ASSIGN(RetrieverTest); }; TEST_F(RetrieverTest, RetrieveData) { diff --git a/cpp/test/rule_retriever_test.cc b/cpp/test/rule_retriever_test.cc index ecfb0c8..9a32df3 100644 --- a/cpp/test/rule_retriever_test.cc +++ b/cpp/test/rule_retriever_test.cc @@ -16,6 +16,7 @@ #include <libaddressinput/callback.h> #include <libaddressinput/null_storage.h> +#include <libaddressinput/util/basictypes.h> #include <libaddressinput/util/scoped_ptr.h> #include <string> @@ -28,7 +29,6 @@ namespace { -using i18n::addressinput::BuildCallback; using i18n::addressinput::FakeDownloader; using i18n::addressinput::NullStorage; using i18n::addressinput::Retriever; @@ -50,7 +50,8 @@ class RuleRetrieverTest : public testing::Test { virtual ~RuleRetrieverTest() {} RuleRetriever::Callback* BuildCallback() { - return ::BuildCallback(this, &RuleRetrieverTest::OnRuleReady); + return i18n::addressinput::BuildCallback( + this, &RuleRetrieverTest::OnRuleReady); } RuleRetriever rule_retriever_; @@ -66,6 +67,8 @@ class RuleRetrieverTest : public testing::Test { key_ = key; rule_.CopyFrom(rule); } + + DISALLOW_COPY_AND_ASSIGN(RuleRetrieverTest); }; TEST_F(RuleRetrieverTest, ExistingRule) { diff --git a/cpp/test/util/json_test.cc b/cpp/test/util/json_test.cc index f6eb389..a3b9f80 100644 --- a/cpp/test/util/json_test.cc +++ b/cpp/test/util/json_test.cc @@ -32,8 +32,9 @@ TEST(JsonTest, EmptyStringIsNotValid) { TEST(JsonTest, EmptyDictionaryContainsNoKeys) { Json json; ASSERT_TRUE(json.ParseObject("{}")); - EXPECT_FALSE(json.HasStringValueForKey("key")); - EXPECT_FALSE(json.HasStringValueForKey(std::string())); + std::string not_checked; + EXPECT_FALSE(json.GetStringValueForKey("key", ¬_checked)); + EXPECT_FALSE(json.GetStringValueForKey(std::string(), ¬_checked)); } TEST(JsonTest, InvalidJsonIsNotValid) { @@ -44,35 +45,40 @@ TEST(JsonTest, InvalidJsonIsNotValid) { TEST(JsonTest, OneKeyIsValid) { Json json; ASSERT_TRUE(json.ParseObject("{\"key\": \"value\"}")); - ASSERT_TRUE(json.HasStringValueForKey("key")); - EXPECT_EQ("value", json.GetStringValueForKey("key")); + std::string value; + EXPECT_TRUE(json.GetStringValueForKey("key", &value)); + EXPECT_EQ("value", value); } TEST(JsonTest, EmptyStringKeyIsNotInObject) { Json json; ASSERT_TRUE(json.ParseObject("{\"key\": \"value\"}")); - EXPECT_FALSE(json.HasStringValueForKey(std::string())); + std::string not_checked; + EXPECT_FALSE(json.GetStringValueForKey(std::string(), ¬_checked)); } TEST(JsonTest, EmptyKeyIsValid) { Json json; ASSERT_TRUE(json.ParseObject("{\"\": \"value\"}")); - ASSERT_TRUE(json.HasStringValueForKey(std::string())); - EXPECT_EQ("value", json.GetStringValueForKey(std::string())); + std::string value; + EXPECT_TRUE(json.GetStringValueForKey(std::string(), &value)); + EXPECT_EQ("value", value); } TEST(JsonTest, EmptyValueIsValid) { Json json; ASSERT_TRUE(json.ParseObject("{\"key\": \"\"}")); - ASSERT_TRUE(json.HasStringValueForKey("key")); - EXPECT_TRUE(json.GetStringValueForKey("key").empty()); + std::string value; + EXPECT_TRUE(json.GetStringValueForKey("key", &value)); + EXPECT_TRUE(value.empty()); } TEST(JsonTest, Utf8EncodingIsValid) { Json json; ASSERT_TRUE(json.ParseObject("{\"key\": \"Ü\"}")); - ASSERT_TRUE(json.HasStringValueForKey("key")); - EXPECT_EQ("Ü", json.GetStringValueForKey("key")); + std::string value; + EXPECT_TRUE(json.GetStringValueForKey("key", &value)); + EXPECT_EQ("Ü", value); } TEST(JsonTest, InvalidUtf8IsNotValid) { @@ -89,11 +95,12 @@ TEST(JsonTest, NullInMiddleIsNotValid) { TEST(JsonTest, TwoKeysAreValid) { Json json; ASSERT_TRUE(json.ParseObject("{\"key1\": \"value1\", \"key2\": \"value2\"}")); - ASSERT_TRUE(json.HasStringValueForKey("key1")); - EXPECT_EQ("value1", json.GetStringValueForKey("key1")); + std::string value; + EXPECT_TRUE(json.GetStringValueForKey("key1", &value)); + EXPECT_EQ("value1", value); - ASSERT_TRUE(json.HasStringValueForKey("key2")); - EXPECT_EQ("value2", json.GetStringValueForKey("key2")); + EXPECT_TRUE(json.GetStringValueForKey("key2", &value)); + EXPECT_EQ("value2", value); } TEST(JsonTest, ListIsNotValid) { @@ -122,8 +129,9 @@ TEST(JsonTest, DictionaryFound) { ASSERT_TRUE(json.ParseObject("{\"key\":{\"inner_key\":\"value\"}}")); ASSERT_TRUE(json.HasDictionaryValueForKey("key")); const Json& sub_json = json.GetDictionaryValueForKey("key"); - ASSERT_TRUE(sub_json.HasStringValueForKey("inner_key")); - EXPECT_EQ("value", sub_json.GetStringValueForKey("inner_key")); + std::string value; + EXPECT_TRUE(sub_json.GetStringValueForKey("inner_key", &value)); + EXPECT_EQ("value", value); } TEST(JsonTest, DictionariesHaveKeys) { diff --git a/cpp/test/validating_storage_test.cc b/cpp/test/validating_storage_test.cc index ced22dc..f786117 100644 --- a/cpp/test/validating_storage_test.cc +++ b/cpp/test/validating_storage_test.cc @@ -16,6 +16,7 @@ #include <libaddressinput/callback.h> #include <libaddressinput/storage.h> +#include <libaddressinput/util/basictypes.h> #include <libaddressinput/util/scoped_ptr.h> #include <cstddef> @@ -31,7 +32,6 @@ namespace { -using i18n::addressinput::BuildCallback; using i18n::addressinput::FakeStorage; using i18n::addressinput::scoped_ptr; using i18n::addressinput::Storage; @@ -57,7 +57,8 @@ class ValidatingStorageTest : public testing::Test { virtual ~ValidatingStorageTest() {} ValidatingStorage::Callback* BuildCallback() { - return ::BuildCallback(this, &ValidatingStorageTest::OnDataReady); + return i18n::addressinput::BuildCallback( + this, &ValidatingStorageTest::OnDataReady); } Storage* const wrapped_storage_; // Owned by |storage_|. @@ -76,6 +77,8 @@ class ValidatingStorageTest : public testing::Test { delete data; } } + + DISALLOW_COPY_AND_ASSIGN(ValidatingStorageTest); }; TEST_F(ValidatingStorageTest, GoodData) { |