-
Notifications
You must be signed in to change notification settings - Fork 0
/
NameTransforms.cs
123 lines (100 loc) · 3.84 KB
/
NameTransforms.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Collections.Generic;
using System.Text;
namespace DynamicPInvoke
{
public class NameTransforms
{
/// <summary>
/// Returns the input string unchanged
/// </summary>
public static Func<string, string> NoOp => new Func<string, string>(m => m);
/// <summary>
/// Returns a name with the form: funcCall
/// </summary>
public static Func<string, string> Camel => new Func<string, string>(CamelImpl);
/// <summary>
/// Returns a name with the form: func_call
/// </summary>
public static Func<string, string> Snake => new Func<string, string>(SnakeImpl);
/// <summary>
/// Returns a name with the form FuncCall
/// </summary>
public static Func<string, string> Pascal => new Func<string, string>(PascalImpl);
/// <summary>
/// Returns a name with the form FUNC_CALL
/// </summary>
public static Func<string, string> CapSnake => new Func<string, string>(CapSnakeImpl);
private static string CamelImpl(string name)
{
var tokens = Tokenize(name);
var result = new StringBuilder();
result.Append(tokens.Dequeue().ToLower());
while (tokens.Count > 0) {
result.Append(CapCase(tokens.Dequeue()));
}
return result.ToString();
}
private static string SnakeImpl(string name)
{
var tokens = Tokenize(name);
var result = new StringBuilder();
result.Append(tokens.Dequeue().ToLower());
while (tokens.Count > 0) {
result.Append("_");
result.Append(tokens.Dequeue().ToLower());
}
return result.ToString();
}
private static string PascalImpl(string name)
{
var tokens = Tokenize(name);
var result = new StringBuilder();
while (tokens.Count > 0) {
result.Append(CapCase(tokens.Dequeue()));
}
return result.ToString();
}
private static string CapSnakeImpl(string name)
{
var tokens = Tokenize(name);
var result = new StringBuilder();
result.Append(tokens.Dequeue().ToUpper());
while (tokens.Count > 0) {
result.Append("_");
result.Append(tokens.Dequeue().ToUpper());
}
return result.ToString();
}
private static Queue<string> Tokenize(string name)
{
var workingName = name;
// attempt to automatically tokenize it from snake case
char[] separator = { '_' };
var result = new Queue<string>(workingName.Split(separator, StringSplitOptions.RemoveEmptyEntries));
if (result.Count > 1) {
return result;
} else if (result.Count == 1) {
// we've incidentally stripped the name of leading and trailing underscores,
// so use the trimmed version while also clearing the queue
workingName = result.Dequeue();
}
// manually tokenize based on capitalization
var charArray = workingName.ToCharArray();
for (int i = 0, tokenStart = 0; i < charArray.Length; ++i) {
if (charArray.Length == i + 1 || char.IsUpper(charArray[i + 1])) {
result.Enqueue(new string(charArray, tokenStart, i - tokenStart + 1));
tokenStart = i + 1;
}
}
return result;
}
public static string CapCase(string s)
{
if (string.IsNullOrEmpty(s)) {
return string.Empty;
}
return char.ToUpper(s[0]) + s.Substring(1).ToLower();
}
}
}