-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathValueStringBuilder.Trim.cs
163 lines (145 loc) · 4.85 KB
/
ValueStringBuilder.Trim.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System.Runtime.CompilerServices;
namespace LinkDotNet.StringBuilder;
public ref partial struct ValueStringBuilder
{
/// <summary>
/// Removes all whitespace characters from the start and end of this builder.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Trim()
{
// Hint: We don't want to call TrimStart and TrimEnd because we don't want to copy the buffer twice.
var start = 0;
var end = bufferPosition - 1;
while (start < bufferPosition && char.IsWhiteSpace(buffer[start]))
{
start++;
}
while (end >= start && char.IsWhiteSpace(buffer[end]))
{
end--;
}
var newLength = end - start + 1;
if (newLength < bufferPosition)
{
bufferPosition = newLength;
buffer.Slice(start, start + newLength).CopyTo(buffer);
}
}
/// <summary>
/// Removes all occurrences of the specified character from the start and end of this builder.
/// </summary>
/// <param name="value">The character to remove.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Trim(char value)
{
// Remove character from the beginning
var start = 0;
while (start < bufferPosition && buffer[start] == value)
{
start++;
}
// Remove character from the end
var end = bufferPosition - 1;
while (end >= start && buffer[end] == value)
{
end--;
}
var newLength = end - start + 1;
if (newLength < bufferPosition)
{
bufferPosition = newLength;
buffer.Slice(start, start + newLength).CopyTo(buffer);
}
}
/// <summary>
/// Removes all whitespace characters from the start of this builder.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void TrimStart()
{
var start = 0;
while (start < bufferPosition && char.IsWhiteSpace(buffer[start]))
{
start++;
}
if (start > 0)
{
var newLength = bufferPosition - start;
buffer.Slice(start, bufferPosition).CopyTo(buffer);
bufferPosition = newLength;
}
}
/// <summary>
/// Removes all occurrences of the specified character from the start of this builder.
/// </summary>
/// <param name="value">The character to remove.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void TrimStart(char value)
{
var start = 0;
while (start < bufferPosition && buffer[start] == value)
{
start++;
}
if (start > 0)
{
var newLength = bufferPosition - start;
buffer.Slice(start, bufferPosition).CopyTo(buffer);
bufferPosition = newLength;
}
}
/// <summary>
/// Removes all whitespace characters from the end of this builder.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void TrimEnd()
{
var end = bufferPosition - 1;
while (end >= 0 && char.IsWhiteSpace(buffer[end]))
{
end--;
}
bufferPosition = end + 1;
}
/// <summary>
/// Removes all occurrences of the specified character from the end of this builder.
/// </summary>
/// <param name="value">The character to remove.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void TrimEnd(char value)
{
var end = bufferPosition - 1;
while (end >= 0 && buffer[end] == value)
{
end--;
}
bufferPosition = end + 1;
}
/// <summary>
/// Removes the specified sequence of characters from the start of this builder.
/// </summary>
/// <param name="value">The sequence of characters to remove.</param>
/// <param name="comparisonType">The way to compare the sequences of characters.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void TrimPrefix(ReadOnlySpan<char> value, StringComparison comparisonType = StringComparison.Ordinal)
{
if (AsSpan().StartsWith(value, comparisonType))
{
Remove(0, value.Length);
}
}
/// <summary>
/// Removes the specified sequence of characters from the end of this builder.
/// </summary>
/// <param name="value">The sequence of characters to remove.</param>
/// <param name="comparisonType">The way to compare the sequences of characters.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void TrimSuffix(ReadOnlySpan<char> value, StringComparison comparisonType = StringComparison.Ordinal)
{
if (AsSpan().EndsWith(value, comparisonType))
{
Remove(Length - value.Length, value.Length);
}
}
}