/src/ProtocolBuffers/NameHelpers.cs

https://github.com/jskeet/protobuf-csharp-port · C# · 140 lines · 65 code · 15 blank · 60 comment · 7 complexity · d48446cc1fba797b70a4b395f9cee9c2 MD5 · raw file

  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // http://github.com/jskeet/dotnet-protobufs/
  5. // Original C++/Java/Python code:
  6. // http://code.google.com/p/protobuf/
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. //
  12. // * Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. // * Redistributions in binary form must reproduce the above
  15. // copyright notice, this list of conditions and the following disclaimer
  16. // in the documentation and/or other materials provided with the
  17. // distribution.
  18. // * Neither the name of Google Inc. nor the names of its
  19. // contributors may be used to endorse or promote products derived from
  20. // this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. #endregion
  34. using System;
  35. using System.Text.RegularExpressions;
  36. namespace Google.ProtocolBuffers
  37. {
  38. /// <summary>
  39. /// Helpers for converting names to pascal case etc.
  40. /// </summary>
  41. public class NameHelpers
  42. {
  43. /// <summary>
  44. /// All characters that are not alpha-numeric
  45. /// </summary>
  46. private static readonly Regex NonAlphaNumericCharacters = new Regex(@"[^a-zA-Z0-9]+");
  47. /// <summary>
  48. /// Matches lower-case character that follow either an underscore, or a number
  49. /// </summary>
  50. private static readonly Regex UnderscoreOrNumberWithLowerCase = new Regex(@"[0-9_][a-z]");
  51. /// <summary>
  52. /// Removes non alpha numeric characters while capitalizing letters that follow
  53. /// a number or underscore. The first letter is always upper case.
  54. /// </summary>
  55. public static string UnderscoresToPascalCase(string input)
  56. {
  57. string name = UnderscoresToUpperCase(input);
  58. // Pascal case always begins with upper-case letter
  59. if (Char.IsLower(name[0]))
  60. {
  61. char[] chars = name.ToCharArray();
  62. chars[0] = char.ToUpper(chars[0]);
  63. return new string(chars);
  64. }
  65. return name;
  66. }
  67. /// <summary>
  68. /// Removes non alpha numeric characters while capitalizing letters that follow
  69. /// a number or underscore. The first letter is always lower case.
  70. /// </summary>
  71. public static string UnderscoresToCamelCase(string input)
  72. {
  73. string name = UnderscoresToUpperCase(input);
  74. // Camel case always begins with lower-case letter
  75. if (Char.IsUpper(name[0]))
  76. {
  77. char[] chars = name.ToCharArray();
  78. chars[0] = char.ToLower(chars[0]);
  79. return new string(chars);
  80. }
  81. return name;
  82. }
  83. /// <summary>
  84. /// Capitalizes any characters following an '_' or a number '0' - '9' and removes
  85. /// all non alpha-numeric characters. If the resulting string begins with a number
  86. /// an '_' will be prefixed.
  87. /// </summary>
  88. private static string UnderscoresToUpperCase(string input)
  89. {
  90. string name = UnderscoreOrNumberWithLowerCase.Replace(input, x => x.Value.ToUpper());
  91. name = NonAlphaNumericCharacters.Replace(name, String.Empty);
  92. if (name.Length == 0)
  93. {
  94. throw new ArgumentException(String.Format("The field name '{0}' is invalid.", input));
  95. }
  96. // Fields can not start with a number
  97. if (Char.IsNumber(name[0]))
  98. {
  99. name = '_' + name;
  100. }
  101. return name;
  102. }
  103. internal static string StripProto(string text)
  104. {
  105. if (!StripSuffix(ref text, ".protodevel"))
  106. {
  107. StripSuffix(ref text, ".proto");
  108. }
  109. return text;
  110. }
  111. /// <summary>
  112. /// Attempts to strip a suffix from a string, returning whether
  113. /// or not the suffix was actually present.
  114. /// </summary>
  115. public static bool StripSuffix(ref string text, string suffix)
  116. {
  117. if (text.EndsWith(suffix))
  118. {
  119. text = text.Substring(0, text.Length - suffix.Length);
  120. return true;
  121. }
  122. return false;
  123. }
  124. }
  125. }