aboutsummaryrefslogtreecommitdiff
path: root/src/ProtocolBuffers
diff options
context:
space:
mode:
authorcsharptest <roger@csharptest.net>2011-08-12 18:28:38 -0500
committerrogerk <devnull@localhost>2011-08-12 18:28:38 -0500
commit5ce95b4b480f6a58a522e1326251859d0b363f10 (patch)
treeeaea1baaf04469b27193d01c29e1fb25440be0de /src/ProtocolBuffers
parent1851676b3ca0798bc7ff871acb960e1df1eac92e (diff)
downloadprotobuf-5ce95b4b480f6a58a522e1326251859d0b363f10.tar.gz
protobuf-5ce95b4b480f6a58a522e1326251859d0b363f10.tar.bz2
protobuf-5ce95b4b480f6a58a522e1326251859d0b363f10.zip
Added tests for the additions to NameHelpers.UnderscoresToXxxxxCase
Diffstat (limited to 'src/ProtocolBuffers')
-rw-r--r--src/ProtocolBuffers/NameHelpers.cs50
1 files changed, 29 insertions, 21 deletions
diff --git a/src/ProtocolBuffers/NameHelpers.cs b/src/ProtocolBuffers/NameHelpers.cs
index 3d2be22d..09621e55 100644
--- a/src/ProtocolBuffers/NameHelpers.cs
+++ b/src/ProtocolBuffers/NameHelpers.cs
@@ -48,37 +48,45 @@ namespace Google.ProtocolBuffers
{
public static string UnderscoresToPascalCase(string input)
{
- return UnderscoresToPascalOrCamelCase(input, true);
+ string name = UnderscoresToUpperCase(input);
+
+ // Pascal case always begins with upper-case letter
+ if (Char.IsLower(name[0]))
+ {
+ char[] chars = name.ToCharArray();
+ chars[0] = char.ToUpper(chars[0]);
+ return new string(chars);
+ }
+ return name;
}
public static string UnderscoresToCamelCase(string input)
{
- return UnderscoresToPascalOrCamelCase(input, false);
+ string name = UnderscoresToUpperCase(input);
+
+ // Camel case always begins with lower-case letter
+ if (Char.IsUpper(name[0]))
+ {
+ char[] chars = name.ToCharArray();
+ chars[0] = char.ToLower(chars[0]);
+ return new string(chars);
+ }
+ return name;
}
/// <summary>
- /// Converts a string to Pascal or Camel case. The first letter is capitalized or
- /// lower-cased depending on <paramref name="pascal"/> is true.
- /// After the first letter, any punctuation is removed but triggers capitalization
- /// of the next letter. Digits are preserved but trigger capitalization of the next
- /// letter.
- /// All capitalisation is done in the invariant culture.
+ /// Capitalizes any characters following an '_' or a number '0' - '9' and removes
+ /// all non alpha-numberic characters. If the resulting string begins with a number
+ /// an '_' will be prefixed.
/// </summary>
- private static string UnderscoresToPascalOrCamelCase(string input, bool pascal)
+ private static string UnderscoresToUpperCase(string input)
{
- string name = Transform(input, pascal ? UnderlineToPascal : UnderlineToCamel, x => x.Value.TrimStart('_').ToUpper());
+ string name = Transform(input, UnderlineCharacter, x => x.Value.ToUpper());
+ name = Transform(name, InvalidCharacters, x => String.Empty);
if (name.Length == 0)
throw new ArgumentException(String.Format("The field name '{0}' is invalid.", input));
- // Pascal case always begins with lower-case letter
- if (!pascal && Char.IsUpper(name[0]))
- {
- char[] chars = name.ToCharArray();
- chars[0] = char.ToLower(chars[0]);
- return new string(chars);
- }
-
// Fields can not start with a number
if (Char.IsNumber(name[0]))
name = '_' + name;
@@ -110,14 +118,14 @@ namespace Google.ProtocolBuffers
}
/// <summary>
- /// Similar to UnderlineToCamel, but also matches the first character if it is lower-case
+ /// All characters that are not alpha-numberic
/// </summary>
- private static Regex UnderlineToPascal = new Regex(@"(?:(?:^|[0-9_])[a-z])|_");
+ private static Regex InvalidCharacters = new Regex(@"[^a-zA-Z0-9]+");
/// <summary>
/// Matches lower-case character that follow either an underscore, or a number
/// </summary>
- private static Regex UnderlineToCamel = new Regex(@"(?:[0-9_][a-z])|_");
+ private static Regex UnderlineCharacter = new Regex(@"[0-9_][a-z]");
/// <summary>
/// Used for text-template transformation where a regex match is replaced in the input string.