Skip to content

Commit

Permalink
Fixed some lint errors seen in google3. (protocolbuffers#6520)
Browse files Browse the repository at this point in the history
  • Loading branch information
haberman authored and TeBoring committed Aug 16, 2019
1 parent c60aaf7 commit ee4f249
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/csharp/csharp_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ uint GetGroupEndTag(const Descriptor* descriptor) {

std::string ToCSharpName(const std::string& name, const FileDescriptor* file) {
std::string result = GetFileNamespace(file);
if (result != "") {
if (!result.empty()) {
result += '.';
}
string classname;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ void ReflectionClassGenerator::WriteGeneratedCodeInfo(const Descriptor* descript
// Fields
if (descriptor->field_count() > 0) {
std::vector<std::string> fields;
fields.reserve(descriptor->field_count());
for (int i = 0; i < descriptor->field_count(); i++) {
fields.push_back(GetPropertyName(descriptor->field(i)));
}
Expand All @@ -273,6 +274,7 @@ void ReflectionClassGenerator::WriteGeneratedCodeInfo(const Descriptor* descript
// Oneofs
if (descriptor->oneof_decl_count() > 0) {
std::vector<std::string> oneofs;
oneofs.reserve(descriptor->oneof_decl_count());
for (int i = 0; i < descriptor->oneof_decl_count(); i++) {
oneofs.push_back(UnderscoresToCamelCase(descriptor->oneof_decl(i)->name(), true));
}
Expand All @@ -285,6 +287,7 @@ void ReflectionClassGenerator::WriteGeneratedCodeInfo(const Descriptor* descript
// Nested enums
if (descriptor->enum_type_count() > 0) {
std::vector<std::string> enums;
enums.reserve(descriptor->enum_type_count());
for (int i = 0; i < descriptor->enum_type_count(); i++) {
enums.push_back(GetClassName(descriptor->enum_type(i)));
}
Expand Down
51 changes: 23 additions & 28 deletions src/google/protobuf/compiler/php/php_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ std::string GeneratedMetadataFileName(const FileDescriptor* file,
std::string LabelForField(FieldDescriptor* field);
std::string TypeName(FieldDescriptor* field);
std::string UnderscoresToCamelCase(const string& name, bool cap_first_letter);
std::string EscapeDollor(const string& to_escape);
std::string BinaryToHex(const string& binary);
void Indent(io::Printer* printer);
void Outdent(io::Printer* printer);
Expand Down Expand Up @@ -153,7 +152,7 @@ template <typename DescriptorType>
std::string ClassNamePrefix(const string& classname,
const DescriptorType* desc) {
const string& prefix = (desc->file()->options()).php_class_prefix();
if (prefix != "") {
if (!prefix.empty()) {
return prefix;
}

Expand Down Expand Up @@ -244,13 +243,13 @@ template <typename DescriptorType>
std::string RootPhpNamespace(const DescriptorType* desc, bool is_descriptor) {
if (desc->file()->options().has_php_namespace()) {
const string& php_namespace = desc->file()->options().php_namespace();
if (php_namespace != "") {
if (!php_namespace.empty()) {
return php_namespace;
}
return "";
}

if (desc->file()->package() != "") {
if (!desc->file()->package().empty()) {
return PhpName(desc->file()->package(), is_descriptor);
}
return "";
Expand All @@ -260,7 +259,7 @@ template <typename DescriptorType>
std::string FullClassName(const DescriptorType* desc, bool is_descriptor) {
string classname = GeneratedClassNameImpl(desc);
string php_namespace = RootPhpNamespace(desc, is_descriptor);
if (php_namespace != "") {
if (!php_namespace.empty()) {
return php_namespace + "\\" + classname;
}
return classname;
Expand All @@ -270,7 +269,7 @@ template <typename DescriptorType>
std::string LegacyFullClassName(const DescriptorType* desc, bool is_descriptor) {
string classname = LegacyGeneratedClassName(desc);
string php_namespace = RootPhpNamespace(desc, is_descriptor);
if (php_namespace != "") {
if (!php_namespace.empty()) {
return php_namespace + "\\" + classname;
}
return classname;
Expand Down Expand Up @@ -352,7 +351,7 @@ std::string GeneratedMetadataFileName(const FileDescriptor* file,
if (file->options().has_php_metadata_namespace()) {
const string& php_metadata_namespace =
file->options().php_metadata_namespace();
if (php_metadata_namespace != "" && php_metadata_namespace != "\\") {
if (!php_metadata_namespace.empty() && php_metadata_namespace != "\\") {
result += php_metadata_namespace;
std::replace(result.begin(), result.end(), '\\', '/');
if (result.at(result.size() - 1) != '/') {
Expand Down Expand Up @@ -552,45 +551,41 @@ std::string EnumOrMessageSuffix(

// Converts a name to camel-case. If cap_first_letter is true, capitalize the
// first letter.
std::string UnderscoresToCamelCase(const string& input, bool cap_first_letter) {
std::string UnderscoresToCamelCase(const string& name, bool cap_first_letter) {
std::string result;
for (int i = 0; i < input.size(); i++) {
if ('a' <= input[i] && input[i] <= 'z') {
for (int i = 0; i < name.size(); i++) {
if ('a' <= name[i] && name[i] <= 'z') {
if (cap_first_letter) {
result += input[i] + ('A' - 'a');
result += name[i] + ('A' - 'a');
} else {
result += input[i];
result += name[i];
}
cap_first_letter = false;
} else if ('A' <= input[i] && input[i] <= 'Z') {
} else if ('A' <= name[i] && name[i] <= 'Z') {
if (i == 0 && !cap_first_letter) {
// Force first letter to lower-case unless explicitly told to
// capitalize it.
result += input[i] + ('a' - 'A');
result += name[i] + ('a' - 'A');
} else {
// Capital letters after the first are left as-is.
result += input[i];
result += name[i];
}
cap_first_letter = false;
} else if ('0' <= input[i] && input[i] <= '9') {
result += input[i];
} else if ('0' <= name[i] && name[i] <= '9') {
result += name[i];
cap_first_letter = true;
} else {
cap_first_letter = true;
}
}
// Add a trailing "_" if the name should be altered.
if (input[input.size() - 1] == '#') {
if (name[name.size() - 1] == '#') {
result += '_';
}
return result;
}

std::string EscapeDollor(const string& to_escape) {
return StringReplace(to_escape, "$", "\\$", true);
}

std::string BinaryToHex(const string& src) {
std::string BinaryToHex(const string& binary) {
string dest;
size_t i;
unsigned char symbol[16] = {
Expand All @@ -600,12 +595,12 @@ std::string BinaryToHex(const string& src) {
'c', 'd', 'e', 'f',
};

dest.resize(src.size() * 2);
dest.resize(binary.size() * 2);
char* append_ptr = &dest[0];

for (i = 0; i < src.size(); i++) {
*append_ptr++ = symbol[(src[i] & 0xf0) >> 4];
*append_ptr++ = symbol[src[i] & 0x0f];
for (i = 0; i < binary.size(); i++) {
*append_ptr++ = symbol[(binary[i] & 0xf0) >> 4];
*append_ptr++ = symbol[binary[i] & 0x0f];
}

return dest;
Expand Down Expand Up @@ -1103,7 +1098,7 @@ void LegacyGenerateClassFile(const FileDescriptor* file, const DescriptorType* d
GenerateHead(file, &printer);

std::string php_namespace = RootPhpNamespace(desc, is_descriptor);
if (php_namespace != "") {
if (!php_namespace.empty()) {
printer.Print(
"namespace ^name^;\n\n",
"name", php_namespace);
Expand Down

0 comments on commit ee4f249

Please sign in to comment.