From 5d5bc234eab4176ea9903bce93c767a83669118c Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Fri, 24 Sep 2021 12:28:23 -0400 Subject: [PATCH] Move some common code into a helper. --- .../objectivec/objectivec_generator.cc | 49 +++++++++++-------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/src/google/protobuf/compiler/objectivec/objectivec_generator.cc b/src/google/protobuf/compiler/objectivec/objectivec_generator.cc index 2168f97b3..a03b8604f 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_generator.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_generator.cc @@ -44,6 +44,28 @@ namespace protobuf { namespace compiler { namespace objectivec { +namespace { + +// Convert a string with "yes"/"no" (case insensitive) to a boolean, returning +// true/false for if the input string was a valid value. If the input string is +// invalid, `result` is unchanged. +bool StringToBool(const std::string& value, bool* result) { + std::string upper_value(value); + UpperString(&upper_value); + if (upper_value == "NO") { + *result = false; + return true; + } + if (upper_value == "YES") { + *result = true; + return true; + } + + return false; +} + +} // namespace + ObjectiveCGenerator::ObjectiveCGenerator() {} ObjectiveCGenerator::~ObjectiveCGenerator() {} @@ -109,13 +131,8 @@ bool ObjectiveCGenerator::GenerateAll( // "yes": They must be registered and an error will be raised if a files // tried to use a prefix that isn't registered. // Default is "no". - std::string upper_value(options[i].second); - UpperString(&upper_value); - if (upper_value == "NO") { - generation_options.prefixes_must_be_registered = false; - } else if (upper_value == "YES") { - generation_options.prefixes_must_be_registered = true; - } else { + if (!StringToBool(options[i].second, + &generation_options.prefixes_must_be_registered)) { *error = "error: Unknown value for prefixes_must_be_registered: " + options[i].second; return false; } @@ -126,13 +143,8 @@ bool ObjectiveCGenerator::GenerateAll( // "yes": Files must have the objc prefix option, and an error will be // raised if a files doesn't have one. // Default is "no". - std::string upper_value(options[i].second); - UpperString(&upper_value); - if (upper_value == "NO") { - generation_options.require_prefixes = false; - } else if (upper_value == "YES") { - generation_options.require_prefixes = true; - } else { + if (!StringToBool(options[i].second, + &generation_options.require_prefixes)) { *error = "error: Unknown value for require_prefixes: " + options[i].second; return false; } @@ -181,12 +193,9 @@ bool ObjectiveCGenerator::GenerateAll( // is just what to do if that isn't set. The available options are: // "no": Not prefixed (the existing mode). // "yes": Make a prefix out of the proto package. - std::string upper_value(options[i].second); - UpperString(&upper_value); - if (upper_value == "NO") { - SetUseProtoPackageAsDefaultPrefix(false); - } else if (upper_value == "YES") { - SetUseProtoPackageAsDefaultPrefix(true); + bool value = false; + if (StringToBool(options[i].second, &value)) { + SetUseProtoPackageAsDefaultPrefix(value); } else { *error = "error: Unknown use_package_as_prefix: " + options[i].second; return false;