@@ -32,6 +32,35 @@ public void Trim()
32
32
}
33
33
}
34
34
35
+ /// <summary>
36
+ /// Removes the specified character from the beginning and end of this string.
37
+ /// </summary>
38
+ /// <param name="c">The character to remove.</param>
39
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
40
+ public void Trim ( char c )
41
+ {
42
+ // Remove character from the beginning
43
+ var start = 0 ;
44
+ while ( start < bufferPosition && buffer [ start ] == c )
45
+ {
46
+ start ++ ;
47
+ }
48
+
49
+ // Remove character from the end
50
+ var end = bufferPosition - 1 ;
51
+ while ( end >= start && buffer [ end ] == c )
52
+ {
53
+ end -- ;
54
+ }
55
+
56
+ var newLength = end - start + 1 ;
57
+ if ( newLength < bufferPosition )
58
+ {
59
+ bufferPosition = newLength ;
60
+ buffer . Slice ( start , start + newLength ) . CopyTo ( buffer ) ;
61
+ }
62
+ }
63
+
35
64
/// <summary>
36
65
/// Removes a set of whitespace characters from the beginning of this string.
37
66
/// </summary>
@@ -52,6 +81,27 @@ public void TrimStart()
52
81
}
53
82
}
54
83
84
+ /// <summary>
85
+ /// Removes the specified character from the beginning of this string.
86
+ /// </summary>
87
+ /// <param name="c">The character to remove.</param>
88
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
89
+ public void TrimStart ( char c )
90
+ {
91
+ var start = 0 ;
92
+ while ( start < bufferPosition && buffer [ start ] == c )
93
+ {
94
+ start ++ ;
95
+ }
96
+
97
+ if ( start > 0 )
98
+ {
99
+ var newLength = bufferPosition - start ;
100
+ buffer . Slice ( start , bufferPosition ) . CopyTo ( buffer ) ;
101
+ bufferPosition = newLength ;
102
+ }
103
+ }
104
+
55
105
/// <summary>
56
106
/// Removes a set of whitespace characters from the ending of this string.
57
107
/// </summary>
@@ -66,4 +116,20 @@ public void TrimEnd()
66
116
67
117
bufferPosition = end + 1 ;
68
118
}
119
+
120
+ /// <summary>
121
+ /// Removes the specified character from the end of this string.
122
+ /// </summary>
123
+ /// <param name="c">The character to remove.</param>
124
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
125
+ public void TrimEnd ( char c )
126
+ {
127
+ var end = bufferPosition - 1 ;
128
+ while ( end >= 0 && buffer [ end ] == c )
129
+ {
130
+ end -- ;
131
+ }
132
+
133
+ bufferPosition = end + 1 ;
134
+ }
69
135
}
0 commit comments