aboutsummaryrefslogtreecommitdiff
path: root/src/ProtocolBuffers/NameHelpers.cs
diff options
context:
space:
mode:
authorcsharptest <roger@csharptest.net>2011-08-12 16:34:50 -0500
committerrogerk <devnull@localhost>2011-08-12 16:34:50 -0500
commitc7b23c17979e93fb91be6e9a35ec0aef4a73704d (patch)
tree1ee2d6d6883e56f29d2f62c31d64e78aa73b43fc /src/ProtocolBuffers/NameHelpers.cs
parent25981d4007eb5df42a25d8847efe805e05126202 (diff)
downloadprotobuf-c7b23c17979e93fb91be6e9a35ec0aef4a73704d.tar.gz
protobuf-c7b23c17979e93fb91be6e9a35ec0aef4a73704d.tar.bz2
protobuf-c7b23c17979e93fb91be6e9a35ec0aef4a73704d.zip
Rewrite of name capitalization routine 'UnderscoresToPascalOrCamelCase'
Diffstat (limited to 'src/ProtocolBuffers/NameHelpers.cs')
-rw-r--r--src/ProtocolBuffers/NameHelpers.cs88
1 files changed, 47 insertions, 41 deletions
diff --git a/src/ProtocolBuffers/NameHelpers.cs b/src/ProtocolBuffers/NameHelpers.cs
index cd8f2a79..40231147 100644
--- a/src/ProtocolBuffers/NameHelpers.cs
+++ b/src/ProtocolBuffers/NameHelpers.cs
@@ -34,8 +34,10 @@
#endregion
+using System;
using System.Globalization;
using System.Text;
+using System.Text.RegularExpressions;
namespace Google.ProtocolBuffers
{
@@ -64,49 +66,14 @@ namespace Google.ProtocolBuffers
/// </summary>
private static string UnderscoresToPascalOrCamelCase(string input, bool pascal)
{
- StringBuilder result = new StringBuilder();
- bool capitaliseNext = pascal;
- for (int i = 0; i < input.Length; i++)
+ string name = Transform(input, pascal ? UnderlineToPascal : UnderlineToCamel, x => x.Value.TrimStart('_').ToUpper());
+ if (!pascal && name.Length > 0 && Char.IsUpper(name[0]))
{
- char c = input[i];
- if ('a' <= c && c <= 'z')
- {
- if (capitaliseNext)
- {
- result.Append(char.ToUpper(c, CultureInfo.InvariantCulture));
- }
- else
- {
- result.Append(c);
- }
- capitaliseNext = false;
- }
- else if ('A' <= c && c <= 'Z')
- {
- if (i == 0 && !pascal)
- {
- // Force first letter to lower-case unless explicitly told to
- // capitalize it.
- result.Append(char.ToLower(c, CultureInfo.InvariantCulture));
- }
- else
- {
- // Capital letters after the first are left as-is.
- result.Append(c);
- }
- capitaliseNext = false;
- }
- else if ('0' <= c && c <= '9')
- {
- result.Append(c);
- capitaliseNext = true;
- }
- else
- {
- capitaliseNext = true;
- }
+ char[] chars = name.ToCharArray();
+ chars[0] = char.ToLower(chars[0]);
+ return new string(chars);
}
- return result.ToString();
+ return name;
}
internal static string StripProto(string text)
@@ -131,5 +98,44 @@ namespace Google.ProtocolBuffers
}
return false;
}
+
+ /// <summary>
+ /// Similar to UnderlineToCamel, but also matches the first character if it is lower-case
+ /// </summary>
+ private static Regex UnderlineToPascal = new Regex(@"(?:^|[0-9_])[a-z]");
+
+ /// <summary>
+ /// Matches lower-case character that follow either an underscore, or a number
+ /// </summary>
+ private static Regex UnderlineToCamel = new Regex(@"[0-9_][a-z]");
+
+ /// <summary>
+ /// Used for text-template transformation where a regex match is replaced in the input string.
+ /// </summary>
+ /// <param name="input">The text to perform the replacement upon</param>
+ /// <param name="pattern">The regex used to perform the match</param>
+ /// <param name="fnReplace">A delegate that selects the appropriate replacement text</param>
+ /// <returns>The newly formed text after all replacements are made</returns>
+ /// <remarks>
+ /// Originally found at http://csharptest.net/browse/src/Library/Utils/StringUtils.cs#120
+ /// Republished here by the original author under this project's licensing.
+ /// </remarks>
+ private static string Transform(string input, Regex pattern, Converter<Match, string> fnReplace)
+ {
+ int currIx = 0;
+ StringBuilder sb = new StringBuilder();
+
+ foreach (Match match in pattern.Matches(input))
+ {
+ sb.Append(input, currIx, match.Index - currIx);
+ string replace = fnReplace(match);
+ sb.Append(replace);
+
+ currIx = match.Index + match.Length;
+ }
+
+ sb.Append(input, currIx, input.Length - currIx);
+ return sb.ToString();
+ }
}
} \ No newline at end of file