forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringBuilderExtensions.cs
More file actions
95 lines (83 loc) · 2.7 KB
/
StringBuilderExtensions.cs
File metadata and controls
95 lines (83 loc) · 2.7 KB
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
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace JavaScriptEngineSwitcher.Core.Extensions
{
/// <summary>
/// Extensions for StringBuilder
/// </summary>
internal static class StringBuilderExtensions
{
/// <summary>
/// Regular expression for format placeholder
/// </summary>
private static readonly Regex _formatPlaceholderRegExp =
new Regex(@"\{[0-9]\}", RegexOptions.Multiline);
/// <summary>
/// Appends the default line terminator to the end of the current <see cref="StringBuilder"/> instance
/// </summary>
/// <param name="source">Instance of <see cref="StringBuilder"/></param>
/// <returns>Instance of <see cref="StringBuilder"/></returns>
public static StringBuilder AppendFormatLine(this StringBuilder source)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
return source.AppendLine();
}
/// <summary>
/// Appends the string returned by processing a composite format string, which
/// contains zero or more format items, with default line terminator to this instance.
/// Each format item is replaced by the string representation of a corresponding
/// argument in a parameter array.
/// </summary>
/// <param name="source">Instance of <see cref="StringBuilder"/></param>
/// <param name="format">A composite format string</param>
/// <param name="args">An array of objects to format</param>
/// <returns>Instance of <see cref="StringBuilder"/></returns>
public static StringBuilder AppendFormatLine(this StringBuilder source, string format, params object[] args)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (_formatPlaceholderRegExp.IsMatch(format))
{
return source.AppendFormat(format, args).AppendLine();
}
return source.AppendLine(format.Replace("{{", "{").Replace("}}", "}"));
}
/// <summary>
/// Removes the all trailing white-space characters from the current <see cref="StringBuilder"/> instance
/// </summary>
/// <param name="source">Instance of <see cref="StringBuilder"/></param>
/// <returns>Instance of <see cref="StringBuilder"/> without trailing white-space characters</returns>
public static StringBuilder TrimEnd(this StringBuilder source)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
int charCount = source.Length;
if (charCount == 0)
{
return source;
}
int charIndex = charCount - 1;
for (; charIndex >= 0; charIndex--)
{
char charValue = source[charIndex];
if (!char.IsWhiteSpace(charValue))
{
break;
}
}
if (charIndex < source.Length - 1)
{
source.Length = charIndex + 1;
}
return source;
}
}
}