Skip to content

Commit 9f65963

Browse files
Reverted StringBuilderExtensions to classic extension methods as the new style causes an unusual warning. This will need investigating later.
1 parent 51d334e commit 9f65963

File tree

1 file changed

+145
-137
lines changed

1 file changed

+145
-137
lines changed

OnixLabs.Core/Text/Extensions.StringBuilder.cs

Lines changed: 145 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -27,145 +27,153 @@ public static class StringBuilderExtensions
2727
private const char EscapeSequence = '\\';
2828

2929
/// <summary>
30-
/// Provides extension methods for <see cref="StringBuilder"/> instances.
30+
/// Appends the specified values to the current <see cref="StringBuilder"/>.
3131
/// </summary>
32+
/// <param name="values">The values to append.</param>
3233
/// <param name="receiver">The current <see cref="StringBuilder"/> instance.</param>
33-
extension(StringBuilder receiver)
34+
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified values appended.</returns>
35+
public static StringBuilder Append(this StringBuilder receiver, params object[] values) => receiver.AppendJoin(string.Empty, values);
36+
37+
/// <summary>
38+
/// Appends the specified value, prefixed with the escape sequence to the current <see cref="StringBuilder"/>.
39+
/// </summary>
40+
/// <param name="value">The value to append.</param>
41+
/// <param name="receiver">The current <see cref="StringBuilder"/> instance.</param>
42+
/// <returns>Returns the current <see cref="StringBuilder"/> with the escape sequence and specified value appended.</returns>
43+
internal static StringBuilder AppendEscaped(this StringBuilder receiver, char value) => receiver.Append(EscapeSequence).Append(value);
44+
45+
/// <summary>
46+
/// Prepends the specified value to the current <see cref="StringBuilder"/>
47+
/// </summary>
48+
/// <param name="value">The value to prepend.</param>
49+
/// <param name="receiver">The current <see cref="StringBuilder"/> instance.</param>
50+
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified values prepended.</returns>
51+
public static StringBuilder Prepend(this StringBuilder receiver, object value) => receiver.Insert(0, value);
52+
53+
/// <summary>
54+
/// Prepends the specified value to the current <see cref="StringBuilder"/>
55+
/// </summary>
56+
/// <param name="value">The value to prepend.</param>
57+
/// <param name="receiver">The current <see cref="StringBuilder"/> instance.</param>
58+
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified values prepended.</returns>
59+
public static StringBuilder Prepend(this StringBuilder receiver, char value) => receiver.Insert(0, value);
60+
61+
/// <summary>
62+
/// Prepends the specified value to the current <see cref="StringBuilder"/>
63+
/// </summary>
64+
/// <param name="value">The value to prepend.</param>
65+
/// <param name="receiver">The current <see cref="StringBuilder"/> instance.</param>
66+
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified values prepended.</returns>
67+
public static StringBuilder Prepend(this StringBuilder receiver, string value) => receiver.Insert(0, value);
68+
69+
/// <summary>
70+
/// Prepends the specified values to the current <see cref="StringBuilder"/>.
71+
/// </summary>
72+
/// <param name="values">The values to prepend.</param>
73+
/// <param name="receiver">The current <see cref="StringBuilder"/> instance.</param>
74+
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified values prepended.</returns>
75+
public static StringBuilder Prepend(this StringBuilder receiver, params object[] values) => receiver.PrependJoin(string.Empty, values);
76+
77+
/// <summary>
78+
/// Concatenates the string representations of the elements in the provided array of objects, using the specified separator between each member, then prepends the result to the current instance of the string builder.
79+
/// </summary>
80+
/// <param name="separator">The string to use as a separator. <paramref name="separator" /> is included in the joined strings only if <paramref name="values" /> has more than one element.</param>
81+
/// <param name="values">An array that contains the strings to concatenate and append to the current instance of the string builder.</param>
82+
/// <param name="receiver">The current <see cref="StringBuilder"/> instance.</param>
83+
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified values joined and prepended.</returns>
84+
public static StringBuilder PrependJoin(this StringBuilder receiver, string separator, params object?[] values) => receiver.Prepend(string.Join(separator, values));
85+
86+
/// <summary>
87+
/// Trims the specified <see cref="char"/> value from the start and end of the current <see cref="StringBuilder"/>.
88+
/// </summary>
89+
/// <param name="value">The <see cref="char"/> value to trim.</param>
90+
/// <param name="receiver">The current <see cref="StringBuilder"/> instance.</param>
91+
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified <see cref="char"/> value trimmed from the start and end.</returns>
92+
public static StringBuilder Trim(this StringBuilder receiver, char value) => receiver.TrimStart(value).TrimEnd(value);
93+
94+
/// <summary>
95+
/// Trims the specified <see cref="char"/> value from the end of the current <see cref="StringBuilder"/>.
96+
/// </summary>
97+
/// <param name="value">The <see cref="char"/> value to trim.</param>
98+
/// <param name="receiver">The current <see cref="StringBuilder"/> instance.</param>
99+
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified <see cref="char"/> value trimmed from the end.</returns>
100+
public static StringBuilder TrimEnd(this StringBuilder receiver, char value)
101+
{
102+
while (receiver.Length > 0 && receiver[^1] == value)
103+
receiver.Remove(receiver.Length - 1, 1);
104+
105+
return receiver;
106+
}
107+
108+
/// <summary>
109+
/// Trims the specified <see cref="char"/> value from the start of the current <see cref="StringBuilder"/>.
110+
/// </summary>
111+
/// <param name="value">The <see cref="char"/> value to trim.</param>
112+
/// <param name="receiver">The current <see cref="StringBuilder"/> instance.</param>
113+
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified <see cref="char"/> value trimmed from the start.</returns>
114+
public static StringBuilder TrimStart(this StringBuilder receiver, char value)
115+
{
116+
while (receiver.Length > 0 && receiver[0] == value)
117+
receiver.Remove(0, 1);
118+
119+
return receiver;
120+
}
121+
122+
/// <summary>
123+
/// Trims the specified <see cref="string"/> value from the start and end of the current <see cref="StringBuilder"/>.
124+
/// </summary>
125+
/// <param name="value">The <see cref="string"/> value to trim.</param>
126+
/// <param name="receiver">The current <see cref="StringBuilder"/> instance.</param>
127+
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified <see cref="string"/> value trimmed from the start and end.</returns>
128+
public static StringBuilder Trim(this StringBuilder receiver, string value) => receiver.TrimStart(value).TrimEnd(value);
129+
130+
/// <summary>
131+
/// Trims the specified <see cref="string"/> value from the end of the current <see cref="StringBuilder"/>.
132+
/// </summary>
133+
/// <param name="value">The <see cref="string"/> value to trim.</param>
134+
/// <param name="receiver">The current <see cref="StringBuilder"/> instance.</param>
135+
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified <see cref="string"/> value trimmed from the end.</returns>
136+
public static StringBuilder TrimEnd(this StringBuilder receiver, string value)
137+
{
138+
if (string.IsNullOrEmpty(value)) return receiver;
139+
140+
while (receiver.Length >= value.Length && receiver.ToString(receiver.Length - value.Length, value.Length) == value)
141+
receiver.Remove(receiver.Length - value.Length, value.Length);
142+
143+
return receiver;
144+
}
145+
146+
/// <summary>
147+
/// Trims the specified <see cref="string"/> value from the start of the current <see cref="StringBuilder"/>.
148+
/// </summary>
149+
/// <param name="value">The <see cref="string"/> value to trim.</param>
150+
/// <param name="receiver">The current <see cref="StringBuilder"/> instance.</param>
151+
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified <see cref="string"/> value trimmed from the start.</returns>
152+
public static StringBuilder TrimStart(this StringBuilder receiver, string value)
34153
{
35-
/// <summary>
36-
/// Appends the specified values to the current <see cref="StringBuilder"/>.
37-
/// </summary>
38-
/// <param name="values">The values to append.</param>
39-
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified values appended.</returns>
40-
public StringBuilder Append(params object[] values) => receiver.AppendJoin(string.Empty, values);
41-
42-
/// <summary>
43-
/// Appends the specified value, prefixed with the escape sequence to the current <see cref="StringBuilder"/>.
44-
/// </summary>
45-
/// <param name="value">The value to append.</param>
46-
/// <returns>Returns the current <see cref="StringBuilder"/> with the escape sequence and specified value appended.</returns>
47-
internal StringBuilder AppendEscaped(char value) => receiver.Append(EscapeSequence).Append(value);
48-
49-
/// <summary>
50-
/// Prepends the specified value to the current <see cref="StringBuilder"/>
51-
/// </summary>
52-
/// <param name="value">The value to prepend.</param>
53-
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified values prepended.</returns>
54-
public StringBuilder Prepend(object value) => receiver.Insert(0, value);
55-
56-
/// <summary>
57-
/// Prepends the specified value to the current <see cref="StringBuilder"/>
58-
/// </summary>
59-
/// <param name="value">The value to prepend.</param>
60-
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified values prepended.</returns>
61-
public StringBuilder Prepend(char value) => receiver.Insert(0, value);
62-
63-
/// <summary>
64-
/// Prepends the specified value to the current <see cref="StringBuilder"/>
65-
/// </summary>
66-
/// <param name="value">The value to prepend.</param>
67-
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified values prepended.</returns>
68-
public StringBuilder Prepend(string value) => receiver.Insert(0, value);
69-
70-
/// <summary>
71-
/// Prepends the specified values to the current <see cref="StringBuilder"/>.
72-
/// </summary>
73-
/// <param name="values">The values to prepend.</param>
74-
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified values prepended.</returns>
75-
public StringBuilder Prepend(params object[] values) => receiver.PrependJoin(string.Empty, values);
76-
77-
/// <summary>
78-
/// Concatenates the string representations of the elements in the provided array of objects, using the specified separator between each member, then prepends the result to the current instance of the string builder.
79-
/// </summary>
80-
/// <param name="separator">The string to use as a separator. <paramref name="separator" /> is included in the joined strings only if <paramref name="values" /> has more than one element.</param>
81-
/// <param name="values">An array that contains the strings to concatenate and append to the current instance of the string builder.</param>
82-
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified values joined and prepended.</returns>
83-
public StringBuilder PrependJoin(string separator, params object?[] values) => receiver.Prepend(string.Join(separator, values));
84-
85-
/// <summary>
86-
/// Trims the specified <see cref="char"/> value from the start and end of the current <see cref="StringBuilder"/>.
87-
/// </summary>
88-
/// <param name="value">The <see cref="char"/> value to trim.</param>
89-
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified <see cref="char"/> value trimmed from the start and end.</returns>
90-
public StringBuilder Trim(char value) => receiver.TrimStart(value).TrimEnd(value);
91-
92-
/// <summary>
93-
/// Trims the specified <see cref="char"/> value from the end of the current <see cref="StringBuilder"/>.
94-
/// </summary>
95-
/// <param name="value">The <see cref="char"/> value to trim.</param>
96-
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified <see cref="char"/> value trimmed from the end.</returns>
97-
public StringBuilder TrimEnd(char value)
98-
{
99-
while (receiver.Length > 0 && receiver[^1] == value)
100-
receiver.Remove(receiver.Length - 1, 1);
101-
102-
return receiver;
103-
}
104-
105-
/// <summary>
106-
/// Trims the specified <see cref="char"/> value from the start of the current <see cref="StringBuilder"/>.
107-
/// </summary>
108-
/// <param name="value">The <see cref="char"/> value to trim.</param>
109-
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified <see cref="char"/> value trimmed from the start.</returns>
110-
public StringBuilder TrimStart(char value)
111-
{
112-
while (receiver.Length > 0 && receiver[0] == value)
113-
receiver.Remove(0, 1);
114-
115-
return receiver;
116-
}
117-
118-
/// <summary>
119-
/// Trims the specified <see cref="string"/> value from the start and end of the current <see cref="StringBuilder"/>.
120-
/// </summary>
121-
/// <param name="value">The <see cref="string"/> value to trim.</param>
122-
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified <see cref="string"/> value trimmed from the start and end.</returns>
123-
public StringBuilder Trim(string value) => receiver.TrimStart(value).TrimEnd(value);
124-
125-
/// <summary>
126-
/// Trims the specified <see cref="string"/> value from the end of the current <see cref="StringBuilder"/>.
127-
/// </summary>
128-
/// <param name="value">The <see cref="string"/> value to trim.</param>
129-
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified <see cref="string"/> value trimmed from the end.</returns>
130-
public StringBuilder TrimEnd(string value)
131-
{
132-
if (string.IsNullOrEmpty(value)) return receiver;
133-
134-
while (receiver.Length >= value.Length && receiver.ToString(receiver.Length - value.Length, value.Length) == value)
135-
receiver.Remove(receiver.Length - value.Length, value.Length);
136-
137-
return receiver;
138-
}
139-
140-
/// <summary>
141-
/// Trims the specified <see cref="string"/> value from the start of the current <see cref="StringBuilder"/>.
142-
/// </summary>
143-
/// <param name="value">The <see cref="string"/> value to trim.</param>
144-
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified <see cref="string"/> value trimmed from the start.</returns>
145-
public StringBuilder TrimStart(string value)
146-
{
147-
if (string.IsNullOrEmpty(value)) return receiver;
148-
149-
while (receiver.Length >= value.Length && receiver.ToString(0, value.Length) == value)
150-
receiver.Remove(0, value.Length);
151-
152-
return receiver;
153-
}
154-
155-
/// <summary>
156-
/// Wraps the current <see cref="StringBuilder"/> between the specified start and end <see cref="char"/> values.
157-
/// </summary>
158-
/// <param name="start">The <see cref="char"/> value to prepend.</param>
159-
/// <param name="end">The <see cref="char"/> value to append.</param>
160-
/// <returns>Returns the current <see cref="StringBuilder"/> wrapped between the specified start and end <see cref="char"/> values.</returns>
161-
public StringBuilder Wrap(char start, char end) => receiver.Prepend(start).Append(end);
162-
163-
/// <summary>
164-
/// Wraps the current <see cref="StringBuilder"/> between the specified start and end <see cref="string"/> values.
165-
/// </summary>
166-
/// <param name="start">The <see cref="string"/> value to prepend.</param>
167-
/// <param name="end">The <see cref="string"/> value to append.</param>
168-
/// <returns>Returns the current <see cref="StringBuilder"/> wrapped between the specified start and end <see cref="string"/> values.</returns>
169-
public StringBuilder Wrap(string start, string end) => receiver.Prepend(start).Append(end);
154+
if (string.IsNullOrEmpty(value)) return receiver;
155+
156+
while (receiver.Length >= value.Length && receiver.ToString(0, value.Length) == value)
157+
receiver.Remove(0, value.Length);
158+
159+
return receiver;
170160
}
161+
162+
/// <summary>
163+
/// Wraps the current <see cref="StringBuilder"/> between the specified start and end <see cref="char"/> values.
164+
/// </summary>
165+
/// <param name="start">The <see cref="char"/> value to prepend.</param>
166+
/// <param name="end">The <see cref="char"/> value to append.</param>
167+
/// <param name="receiver">The current <see cref="StringBuilder"/> instance.</param>
168+
/// <returns>Returns the current <see cref="StringBuilder"/> wrapped between the specified start and end <see cref="char"/> values.</returns>
169+
public static StringBuilder Wrap(this StringBuilder receiver, char start, char end) => receiver.Prepend(start).Append(end);
170+
171+
/// <summary>
172+
/// Wraps the current <see cref="StringBuilder"/> between the specified start and end <see cref="string"/> values.
173+
/// </summary>
174+
/// <param name="start">The <see cref="string"/> value to prepend.</param>
175+
/// <param name="end">The <see cref="string"/> value to append.</param>
176+
/// <param name="receiver">The current <see cref="StringBuilder"/> instance.</param>
177+
/// <returns>Returns the current <see cref="StringBuilder"/> wrapped between the specified start and end <see cref="string"/> values.</returns>
178+
public static StringBuilder Wrap(this StringBuilder receiver, string start, string end) => receiver.Prepend(start).Append(end);
171179
}

0 commit comments

Comments
 (0)