| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- using System;
- using System.Text;
- namespace FF8
- {
- public sealed class ScriptWriter
- {
- public Int32 Indent { get; set; }
- private readonly StringBuilder _sb;
- private Boolean _newLine;
- public Boolean HasWhiteLine { get; private set; }
- public ScriptWriter(Int32 capacity = 8096)
- {
- _sb = new StringBuilder(capacity);
- }
- 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 void Append(String text)
- {
- AppendIndent();
- _sb.Append(text);
- }
- private void AppendIndent()
- {
- if (_newLine)
- {
- for (Int32 i = 0; i < Indent; i++)
- _sb.Append(" ");
- _newLine = false;
- HasWhiteLine = false;
- }
- }
- public String Release()
- {
- String result = _sb.ToString();
- _sb.Clear();
- Indent = 0;
- _newLine = true;
- HasWhiteLine = false;
- return result;
- }
- public override String ToString()
- {
- return _sb.ToString();
- }
- public State RememberState()
- {
- return new State(this);
- }
- public sealed class State
- {
- private readonly ScriptWriter _sw;
- private Int32 _indent;
- private Boolean _newLine;
- private Boolean _hasEmptyLine;
- private Int32 _length;
- public State(ScriptWriter sw)
- {
- _sw = sw;
- _indent = _sw.Indent;
- _newLine = _sw._newLine;
- _length = _sw._sb.Length;
- _hasEmptyLine = _sw.HasWhiteLine;
- }
- public Boolean IsChanged => _length != _sw._sb.Length;
- public void Cancel()
- {
- _sw.Indent = _indent;
- _sw._newLine = _newLine;
- _sw.HasWhiteLine = _hasEmptyLine;
- _sw._sb.Length = _length;
- }
- }
- }
- }
|