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 |
|
---|
35 | using System.Globalization;
|
---|
36 | using System.Text;
|
---|
37 |
|
---|
38 | namespace Google.ProtocolBuffers {
|
---|
39 | /// <summary>
|
---|
40 | /// Helpers for converting names to pascal case etc.
|
---|
41 | /// </summary>
|
---|
42 | internal class NameHelpers {
|
---|
43 |
|
---|
44 | internal static string UnderscoresToPascalCase(string input) {
|
---|
45 | return UnderscoresToPascalOrCamelCase(input, true);
|
---|
46 | }
|
---|
47 |
|
---|
48 | internal static string UnderscoresToCamelCase(string input) {
|
---|
49 | return UnderscoresToPascalOrCamelCase(input, false);
|
---|
50 | }
|
---|
51 |
|
---|
52 | /// <summary>
|
---|
53 | /// Converts a string to Pascal or Camel case. The first letter is capitalized or
|
---|
54 | /// lower-cased depending on <paramref name="pascal"/> is true.
|
---|
55 | /// After the first letter, any punctuation is removed but triggers capitalization
|
---|
56 | /// of the next letter. Digits are preserved but trigger capitalization of the next
|
---|
57 | /// letter.
|
---|
58 | /// All capitalisation is done in the invariant culture.
|
---|
59 | /// </summary>
|
---|
60 | private static string UnderscoresToPascalOrCamelCase(string input, bool pascal) {
|
---|
61 | StringBuilder result = new StringBuilder();
|
---|
62 | bool capitaliseNext = pascal;
|
---|
63 | for (int i = 0; i < input.Length; i++) {
|
---|
64 | char c = input[i];
|
---|
65 | if ('a' <= c && c <= 'z') {
|
---|
66 | if (capitaliseNext) {
|
---|
67 | result.Append(char.ToUpper(c, CultureInfo.InvariantCulture));
|
---|
68 | } else {
|
---|
69 | result.Append(c);
|
---|
70 | }
|
---|
71 | capitaliseNext = false;
|
---|
72 | } else if ('A' <= c && c <= 'Z') {
|
---|
73 | if (i == 0 && !pascal) {
|
---|
74 | // Force first letter to lower-case unless explicitly told to
|
---|
75 | // capitalize it.
|
---|
76 | result.Append(char.ToLower(c, CultureInfo.InvariantCulture));
|
---|
77 | } else {
|
---|
78 | // Capital letters after the first are left as-is.
|
---|
79 | result.Append(c);
|
---|
80 | }
|
---|
81 | capitaliseNext = false;
|
---|
82 | } else if ('0' <= c && c <= '9') {
|
---|
83 | result.Append(c);
|
---|
84 | capitaliseNext = true;
|
---|
85 | } else {
|
---|
86 | capitaliseNext = true;
|
---|
87 | }
|
---|
88 | }
|
---|
89 | return result.ToString();
|
---|
90 | }
|
---|
91 |
|
---|
92 | internal static string StripProto(string text) {
|
---|
93 | if (!StripSuffix(ref text, ".protodevel")) {
|
---|
94 | StripSuffix(ref text, ".proto");
|
---|
95 | }
|
---|
96 | return text;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /// <summary>
|
---|
100 | /// Attempts to strip a suffix from a string, returning whether
|
---|
101 | /// or not the suffix was actually present.
|
---|
102 | /// </summary>
|
---|
103 | internal static bool StripSuffix(ref string text, string suffix) {
|
---|
104 | if (text.EndsWith(suffix)) {
|
---|
105 | text = text.Substring(0, text.Length - suffix.Length);
|
---|
106 | return true;
|
---|
107 | }
|
---|
108 | return false;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|