Skip to content

Commit

Permalink
Restoring CamelCase'd filenames for generated files.
Browse files Browse the repository at this point in the history
Earlier (pre-bf7da0), we used UnderscoresToCapitalizedCamelCase() to transform
filename strings, but that code was too aggressive.  It performed undesirable
transformations like: ABFilename => AbFilename

A new routine -- FilenameToCamelCase() -- has been added that performs a more
appropriate set of filename transformations:

	1. Non-alphanumeric characters are stripped.
	2. Characters following non-letters are uppercased.

This yields the following results:

	ABFilename    =>  ABFilename
	a_filename    =>  AFilename
	myNumber1fan  =>  MyNumber1Fan
  • Loading branch information
Jon Parise committed Nov 12, 2010
1 parent b856753 commit ff33902
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/compiler/objc_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,45 @@ namespace google { namespace protobuf { namespace compiler { namespace objective
}


string FilenameToCamelCase(const string& filename) {
string result;
bool need_uppercase = true;

result.reserve(filename.length());

for (string::const_iterator it(filename.begin()), itEnd(filename.end()); it != itEnd; ++it) {
const char c = *it;

// Ignore non-alphanumeric characters. The next alphanumeric character
// must be uppercased, though.
if (!isalnum(c)) {
need_uppercase = true;
continue;
}

// If an uppercased character has been requested, transform the current
// character, append it to the result, reset the flag, and move on.
// This is safe to do even if the character is already uppercased.
if (need_uppercase && isalpha(c)) {
result += toupper(c);
need_uppercase = false;
continue;
}

// Simply append this character.
result += c;

// If this character was a digit, we want the next character to be an
// uppercased letter.
if (isdigit(c)) {
need_uppercase = true;
}
}

return result;
}


string StripProto(const string& filename) {
if (HasSuffixString(filename, ".protodevel")) {
return StripSuffixString(filename, ".protodevel");
Expand All @@ -149,7 +188,7 @@ namespace google { namespace protobuf { namespace compiler { namespace objective
basename += file->name().substr(last_slash + 1);
}

return StripProto(basename);
return FilenameToCamelCase(StripProto(basename));
}


Expand Down
5 changes: 5 additions & 0 deletions src/compiler/objc_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ string UnderscoresToCapitalizedCamelCase(const string& name);
// of lower-casing the first letter of the name.)
string UnderscoresToCamelCase(const MethodDescriptor* method);

// Apply CamelCase-formatting to the given filename string. Existing
// capitalization is not modified, but non-alphanumeric characters are
// removed and the following legal character is capitalized.
string FilenameToCamelCase(const string& filename);

// Strips ".proto" or ".protodevel" from the end of a filename.
string StripProto(const string& filename);

Expand Down

0 comments on commit ff33902

Please sign in to comment.