forked from RPCS3/discord-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNamingStyles.cs
50 lines (43 loc) · 1.27 KB
/
NamingStyles.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
using System;
using System.Text;
namespace CompatApiClient;
public static class NamingStyles
{
public static string CamelCase(string value)
{
if (value == null)
throw new ArgumentNullException(nameof(value));
if (value.Length > 0)
{
if (char.IsUpper(value[0]))
value = char.ToLower(value[0]) + value[1..];
}
return value;
}
public static string Dashed(string value) => Delimitied(value, '-');
public static string Underscore(string value) => Delimitied(value, '_');
private static string Delimitied(string value, char separator)
{
if (value == null)
throw new ArgumentNullException(nameof(value));
if (value.Length == 0)
return value;
var hasPrefix = true;
var builder = new StringBuilder(value.Length + 3);
foreach (var c in value)
{
var ch = c;
if (char.IsUpper(ch))
{
ch = char.ToLower(ch);
if (!hasPrefix)
builder.Append(separator);
hasPrefix = true;
}
else
hasPrefix = false;
builder.Append(ch);
}
return builder.ToString();
}
}