| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using System.Text;
- namespace OpenVIII.Fields.Scripts
- {
- public sealed class ScriptWriter
- {
- #region Fields
- private readonly StringBuilder _sb;
- private bool _newLine;
- #endregion Fields
- #region Constructors
- public ScriptWriter(int capacity = 8096) => _sb = new StringBuilder(capacity);
- #endregion Constructors
- #region Properties
- public bool HasWhiteLine { get; set; }
- public int Indent { get; set; }
- #endregion Properties
- #region Methods
- public void Append(string text)
- {
- AppendIndent();
- _sb.Append(text);
- }
- public void AppendLine()
- {
- if (_newLine)
- HasWhiteLine = true;
- _sb.AppendLine();
- _newLine = true;
- }
- public void AppendLine(string text)
- {
- AppendIndent();
- _sb.AppendLine(text);
- _newLine = true;
- HasWhiteLine = (text == "{");
- }
- public string Release()
- {
- var result = _sb.ToString();
- _sb.Clear();
- Indent = 0;
- _newLine = true;
- HasWhiteLine = false;
- return result;
- }
- public State RememberState() => new State(this);
- public override string ToString() => _sb.ToString();
- private void AppendIndent()
- {
- if (_newLine)
- {
- for (var i = 0; i < Indent; i++)
- _sb.Append(" ");
- _newLine = false;
- HasWhiteLine = false;
- }
- }
- #endregion Methods
- #region Classes
- public sealed class State
- {
- #region Fields
- private readonly ScriptWriter _sw;
- private bool _hasEmptyLine;
- private int _indent;
- private int _length;
- private bool _newLine;
- #endregion Fields
- #region Constructors
- public State(ScriptWriter sw)
- {
- _sw = sw;
- _indent = _sw.Indent;
- _newLine = _sw._newLine;
- _length = _sw._sb.Length;
- _hasEmptyLine = _sw.HasWhiteLine;
- }
- #endregion Constructors
- #region Properties
- public bool IsChanged => _length != _sw._sb.Length;
- #endregion Properties
- #region Methods
- public void Cancel()
- {
- _sw.Indent = _indent;
- _sw._newLine = _newLine;
- _sw.HasWhiteLine = _hasEmptyLine;
- _sw._sb.Length = _length;
- }
- #endregion Methods
- }
- #endregion Classes
- }
- }
|