From bde57ffc757f10c105fe6a80b60cbb81b9d72426 Mon Sep 17 00:00:00 2001 From: csharptest Date: Sat, 13 Aug 2011 18:22:30 -0500 Subject: [PATCH] Cleanup per review comments. --- protos/extest/unittest_issues.proto | 2 +- src/ProtoGen/ExtensionGenerator.cs | 2 + src/ProtoGen/MessageGenerator.cs | 2 + src/ProtocolBuffers/ICodedInputStream.cs | 2 +- src/ProtocolBuffers/NameHelpers.cs | 69 +++++++++--------------- 5 files changed, 31 insertions(+), 46 deletions(-) diff --git a/protos/extest/unittest_issues.proto b/protos/extest/unittest_issues.proto index e47a3aaea3..459e58f865 100644 --- a/protos/extest/unittest_issues.proto +++ b/protos/extest/unittest_issues.proto @@ -81,7 +81,7 @@ message AB { optional int32 a_b = 1; } -// Similar issue with numberic names +// Similar issue with numeric names message NumberField { optional int32 _01 = 1; } diff --git a/src/ProtoGen/ExtensionGenerator.cs b/src/ProtoGen/ExtensionGenerator.cs index 20ae7412de..a862a7a0ad 100644 --- a/src/ProtoGen/ExtensionGenerator.cs +++ b/src/ProtoGen/ExtensionGenerator.cs @@ -76,7 +76,9 @@ namespace Google.ProtocolBuffers.ProtoGen public void Generate(TextGenerator writer) { if (Descriptor.File.CSharpOptions.ClsCompliance && GetFieldConstantName(Descriptor).StartsWith("_")) + { writer.WriteLine("[global::System.CLSCompliant(false)]"); + } writer.WriteLine("public const int {0} = {1};", GetFieldConstantName(Descriptor), Descriptor.FieldNumber); diff --git a/src/ProtoGen/MessageGenerator.cs b/src/ProtoGen/MessageGenerator.cs index 2b852eb1e6..01d47ce216 100644 --- a/src/ProtoGen/MessageGenerator.cs +++ b/src/ProtoGen/MessageGenerator.cs @@ -247,7 +247,9 @@ namespace Google.ProtocolBuffers.ProtoGen foreach (FieldDescriptor fieldDescriptor in Descriptor.Fields) { if (Descriptor.File.CSharpOptions.ClsCompliance && GetFieldConstantName(fieldDescriptor).StartsWith("_")) + { writer.WriteLine("[global::System.CLSCompliant(false)]"); + } // Rats: we lose the debug comment here :( writer.WriteLine("public const int {0} = {1};", GetFieldConstantName(fieldDescriptor), diff --git a/src/ProtocolBuffers/ICodedInputStream.cs b/src/ProtocolBuffers/ICodedInputStream.cs index f2360021e0..1d9d26e3a7 100644 --- a/src/ProtocolBuffers/ICodedInputStream.cs +++ b/src/ProtocolBuffers/ICodedInputStream.cs @@ -177,7 +177,7 @@ namespace Google.ProtocolBuffers /// /// Reads an array of primitive values into the list, if the wire-type of fieldTag is length-prefixed and the - /// type is numberic, it will read a packed array. + /// type is numeric, it will read a packed array. /// [CLSCompliant(false)] void ReadPrimitiveArray(FieldType fieldType, uint fieldTag, string fieldName, ICollection list); diff --git a/src/ProtocolBuffers/NameHelpers.cs b/src/ProtocolBuffers/NameHelpers.cs index 09621e5583..80a1f9aaeb 100644 --- a/src/ProtocolBuffers/NameHelpers.cs +++ b/src/ProtocolBuffers/NameHelpers.cs @@ -35,8 +35,6 @@ #endregion using System; -using System.Globalization; -using System.Text; using System.Text.RegularExpressions; namespace Google.ProtocolBuffers @@ -46,6 +44,20 @@ namespace Google.ProtocolBuffers /// public class NameHelpers { + /// + /// All characters that are not alpha-numeric + /// + private static readonly Regex NonAlphaNumericCharacters = new Regex(@"[^a-zA-Z0-9]+"); + + /// + /// Matches lower-case character that follow either an underscore, or a number + /// + private static readonly Regex UnderscoreOrNumberWithLowerCase = new Regex(@"[0-9_][a-z]"); + + /// + /// Removes non alpha numeric characters while capitalizing letters that follow + /// a number or underscore. The first letter is always upper case. + /// public static string UnderscoresToPascalCase(string input) { string name = UnderscoresToUpperCase(input); @@ -60,6 +72,10 @@ namespace Google.ProtocolBuffers return name; } + /// + /// Removes non alpha numeric characters while capitalizing letters that follow + /// a number or underscore. The first letter is always lower case. + /// public static string UnderscoresToCamelCase(string input) { string name = UnderscoresToUpperCase(input); @@ -76,20 +92,24 @@ namespace Google.ProtocolBuffers /// /// 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 + /// all non alpha-numeric characters. If the resulting string begins with a number /// an '_' will be prefixed. /// private static string UnderscoresToUpperCase(string input) { - string name = Transform(input, UnderlineCharacter, x => x.Value.ToUpper()); - name = Transform(name, InvalidCharacters, x => String.Empty); + string name = UnderscoreOrNumberWithLowerCase.Replace(input, x => x.Value.ToUpper()); + name = NonAlphaNumericCharacters.Replace(name, String.Empty); if (name.Length == 0) + { throw new ArgumentException(String.Format("The field name '{0}' is invalid.", input)); + } // Fields can not start with a number if (Char.IsNumber(name[0])) + { name = '_' + name; + } return name; } @@ -116,44 +136,5 @@ namespace Google.ProtocolBuffers } return false; } - - /// - /// All characters that are not alpha-numberic - /// - private static Regex InvalidCharacters = new Regex(@"[^a-zA-Z0-9]+"); - - /// - /// Matches lower-case character that follow either an underscore, or a number - /// - private static Regex UnderlineCharacter = new Regex(@"[0-9_][a-z]"); - - /// - /// Used for text-template transformation where a regex match is replaced in the input string. - /// - /// The text to perform the replacement upon - /// The regex used to perform the match - /// A delegate that selects the appropriate replacement text - /// The newly formed text after all replacements are made - /// - /// Originally found at http://csharptest.net/browse/src/Library/Utils/StringUtils.cs#120 - /// Republished here by the original author under this project's licensing. - /// - private static string Transform(string input, Regex pattern, Converter 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