[Bind] Improve BindStreamWriter

Allows multiple calls to Write to build up one line of text. Splits on
new line characters even in Write. Only places tabs if line is not
otherwise empty.
This commit is contained in:
Fraser Waters 2014-06-20 20:58:02 +01:00
parent c487bafcd2
commit 52ea9d68ed

View file

@ -43,10 +43,11 @@ namespace Bind
class BindStreamWriter : IDisposable class BindStreamWriter : IDisposable
{ {
static readonly char[] SplitCharacters = new char[] { '\r', '\n' }; static readonly string[] SplitStrings = new string[] { System.Environment.NewLine };
readonly StreamWriter sw; readonly StreamWriter sw;
public readonly string File; public readonly string File;
bool newline = true;
int indent_level = 0; int indent_level = 0;
public BindStreamWriter(string file) public BindStreamWriter(string file)
@ -68,27 +69,25 @@ namespace Bind
public void Write(WriteOptions options, string value) public void Write(WriteOptions options, string value)
{ {
var lines = value.Split(SplitCharacters, var lines = value.Split(SplitStrings, StringSplitOptions.None);
StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < lines.Length; ++i)
bool is_multiline = lines.Length > 1;
if (is_multiline)
{ {
// Write all internal lines if (lines[i].Length != 0)
for (int i = 0; i < lines.Length - 1; i++) {
if (newline)
{ {
var line = lines[i];
WriteIndentations(options); WriteIndentations(options);
sw.Write(line); }
newline = false;
sw.Write(lines[i]);
}
// if we're going to write another line add a line break string
if (i + 1 < lines.Length)
{
sw.Write(System.Environment.NewLine); sw.Write(System.Environment.NewLine);
newline = true;
} }
// Write the last line without appending a newline
WriteIndentations(options);
sw.Write(lines[lines.Length - 1]);
}
else
{
WriteIndentations(options);
sw.Write(value);
} }
} }
@ -109,7 +108,7 @@ namespace Bind
public void WriteLine() public void WriteLine()
{ {
sw.WriteLine(); Write(System.Environment.NewLine);
} }
public void WriteLine(WriteOptions options, string value) public void WriteLine(WriteOptions options, string value)