-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFormattedStringBuilder.cs
52 lines (39 loc) Β· 1.39 KB
/
FormattedStringBuilder.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
using System.Collections.Generic;
using System.Text;
namespace Skibitsky.Unity.StringFormatter
{
public class FormattedStringBuilder
{
private readonly StringBuilder _openingTagsBuilder;
private readonly Stack<string> _closingTagsStack;
private string String { get; }
public FormattedStringBuilder() : this(string.Empty)
{
}
public FormattedStringBuilder(string value)
{
String = value;
_openingTagsBuilder = new StringBuilder();
_closingTagsStack = new Stack<string>();
}
public void Append(string value)
{
_openingTagsBuilder.Append(value);
}
public void PushToEnd(string value)
{
_closingTagsStack.Push(value);
}
public string Apply(string value)
{
var builder = new StringBuilder(_openingTagsBuilder.ToString());
builder.Append(value);
foreach (var s in _closingTagsStack)
builder.Append(s);
return builder.ToString();
}
public override string ToString() => Apply(String);
public static implicit operator string(FormattedStringBuilder fsb) => fsb.ToString();
public static implicit operator FormattedStringBuilder(string str) => new FormattedStringBuilder(str);
}
}