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
47 lines (43 loc) · 1.57 KB
/
StringBuilderExtensions.cs
File metadata and controls
47 lines (43 loc) · 1.57 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
namespace JavaScriptEngineSwitcher.Core.Utilities
{
using System.Text;
using System.Text.RegularExpressions;
/// <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 System.Text.StringBuilder object
/// </summary>
/// <param name="sb">Object StringBuilder</param>
/// <returns>Object StringBuilder</returns>
public static StringBuilder AppendFormatLine(this StringBuilder sb)
{
return sb.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="sb">Object StringBuilder</param>
/// <param name="format">A composite format string</param>
/// <param name="args">An array of objects to format</param>
/// <returns>Object StringBuilder</returns>
public static StringBuilder AppendFormatLine(this StringBuilder sb, string format, params object[] args)
{
if (_formatPlaceholderRegExp.IsMatch(format))
{
return sb.AppendFormat(format, args).AppendLine();
}
return sb.AppendLine(format.Replace("{{", "{").Replace("}}", "}"));
}
}
}