-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringExtensions.cs
103 lines (78 loc) · 3.04 KB
/
StringExtensions.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
namespace DynamoDBGenerator.SourceGenerator.Extensions;
public static class StringExtensions
{
public static string ToCamelCaseFromPascal(this string str, [System.Runtime.CompilerServices.CallerMemberName] string? memberName = null)
{
return ToCamelCaseFromPascal(str.AsSpan(), memberName).ToString();
}
public static string ToPrivateFieldFromPascal(this string str, [System.Runtime.CompilerServices.CallerMemberName] string? memberName = null)
{
return ToPrivateFieldFromPascal(str.AsSpan(), memberName).ToString();
}
public static ReadOnlySpan<char> ToPrivateFieldFromPascal(this ReadOnlySpan<char> span, [System.Runtime.CompilerServices.CallerMemberName] string? memberName = null)
{
if (span.Length is 0)
throw new ArgumentException($"Null or Empty string was provided from '{memberName}'");
var array = new char[span.Length + 1];
array[0] = '_';
array[1] = Char.ToLowerInvariant(span[0]);
// Skip first element since we handled it manually.
for (var i = 1; i < span.Length; i++)
array[i + 1] = span[i];
return array;
}
public static ReadOnlySpan<char> ToCamelCaseFromPascal(this ReadOnlySpan<char> span, [System.Runtime.CompilerServices.CallerMemberName] string? memberName = null)
{
if (span.Length is 0)
throw new ArgumentException($"Null or Empty string was provided from '{memberName}'");
if (char.IsLower(span[0]))
return span;
var array = new char[span.Length];
array[0] = Char.ToLowerInvariant(span[0]);
// Skip first element since we handled it manually.
for (var i = 1; i < span.Length; i++)
array[i] = span[i];
return array;
}
public static IEnumerable<string> ScopeTo(this IEnumerable<string> content, string header)
{
return CreateScope(header, content);
}
public static IEnumerable<string> CreateScope(this string header, IEnumerable<string> content)
{
yield return header;
yield return "{";
foreach (var s in content)
yield return $" {s}";
yield return "}";
}
public static IEnumerable<string> CreateScope(this string header, string content)
{
yield return header;
yield return "{";
yield return $" {content}";
yield return "}";
}
public static IEnumerable<string> CreateScope(this string header, string content, string second)
{
yield return header;
yield return "{";
yield return $" {content}";
yield return $" {second}";
yield return "}";
}
public static string ToAlphaNumericMethodName(this string txt)
{
var arr = new char[txt.Length];
var index = 0;
for (var i = 0; i < txt.Length; i++)
{
var c = txt[i];
if (char.IsLetter(c) || index > 0 && char.IsNumber(c))
{
arr[index++] = c;
}
}
return new string(arr, 0, index);
}
}