#region Using Directives using System; using System.Collections.Generic; using System.Text; #endregion Using Directive namespace ScintillaNET { /// /// Provides a writer paradigm for building a list and optionally /// the text that is being styled. /// public class StyleRunWriter { #region Fields private List _styleRuns; private StringBuilder _stringBuilder; #endregion Fields #region Methods /// /// Returns the underlying . /// /// The underlying if one was provided; otherwise, null. public StringBuilder GetStringBuilder() { return _stringBuilder; } /// /// Returns a enumerable built by the thus far. /// /// A enumerable representing the style runs written thus far. public IEnumerable GetStyles() { return _styleRuns.ToArray(); } //public IEnumerable GetStyles(Encoding encoding = null) //{ // // If we don't have access to the text or aren't given an // // encoding then the runs are ready to go. // if (_stringBuilder == null || encoding == null) // return _styleRuns.ToArray(); // // Adjust the style runs so that they represent byte lengths // string text = _stringBuilder.ToString(); // List byteRuns = new List(); // int offset = 0; // unsafe // { // fixed (char* cp = text) // { // for (int i = 0; i < _styleRuns.Count; i++) // { // StyleRun sr = _styleRuns[i]; // byteRuns.Add(new StyleRun(encoding.GetByteCount(cp + offset, sr.Length), sr.Style)); // offset += sr.Length; // } // } // } // return byteRuns; //} /// /// Writes a run of the specified string length in the specified style. /// /// /// The string that determines the run length. If a was used to /// create the the string value will also be appended. /// /// The zero-based index of the style for this run. public void Write(string value, int style) { if (string.IsNullOrEmpty(value)) return; _styleRuns.Add(new StyleRun(value.Length, style)); if (_stringBuilder != null) _stringBuilder.Append(value); } #endregion Methods #region Constructors /// /// Initializes a new instance of the class. /// /// The optional to write to. public StyleRunWriter(StringBuilder stringBuilder = null) { _styleRuns = new List(); _stringBuilder = stringBuilder; } #endregion Constructors } }