Last active
August 29, 2015 13:56
-
-
Save yzorg/8873971 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Text; | |
namespace Yzorg.Gist { | |
class NoteBuilder : IDisposable { | |
StringBuilder _note; | |
Func<string> _get; | |
Action<string> _set; | |
public NoteBuilder(Func<string> get, Action<string> set) { | |
_get = get; | |
_set = set; | |
} | |
public StringBuilder Note { | |
get { | |
if (_note == null) { | |
_note = new StringBuilder(_get()); | |
if (_note.Length > 0) | |
_note.AppendLine(); | |
} | |
return _note; | |
} | |
} | |
public void Dispose() { | |
if (_note != null && _note.Length >= 2) { | |
if (_note[_note.Length-1] == '\n') | |
_note.Length -= 2; // remove trailing \r\n | |
_set(_note.ToString()); | |
_note = null; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment