From cc8a41dc6e256bbf23026199fa5bf2cb7b76604b Mon Sep 17 00:00:00 2001 From: alcexhim Date: Fri, 5 Dec 2014 15:28:19 -0500 Subject: [PATCH] Implement ExpandedString to easily construct strings from chains of ExpandedStringSegments --- .../ExpandedString.cs | 93 +++++++++++++++++++ .../UniversalEditor.Essential.csproj | 3 + 2 files changed, 96 insertions(+) create mode 100644 CSharp/Plugins/UniversalEditor.Essential/ExpandedString.cs diff --git a/CSharp/Plugins/UniversalEditor.Essential/ExpandedString.cs b/CSharp/Plugins/UniversalEditor.Essential/ExpandedString.cs new file mode 100644 index 00000000..acf81ed2 --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Essential/ExpandedString.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace UniversalEditor +{ + public class ExpandedString + { + public static readonly ExpandedString Empty = new ExpandedString(); + + public ExpandedString() + { + + } + public ExpandedString(params ExpandedStringSegment[] segments) + { + foreach (ExpandedStringSegment segment in segments) + { + mvarSegments.Add(segment); + } + } + + private ExpandedStringSegment.ExpandedStringSegmentCollection mvarSegments = new ExpandedStringSegment.ExpandedStringSegmentCollection(); + public ExpandedStringSegment.ExpandedStringSegmentCollection Segments { get { return mvarSegments; } } + + public override string ToString() + { + StringBuilder sb = new StringBuilder(); + foreach (ExpandedStringSegment segment in mvarSegments) + { + sb.Append(segment.ToString()); + } + return sb.ToString(); + } + } + public abstract class ExpandedStringSegment + { + public class ExpandedStringSegmentCollection + : System.Collections.ObjectModel.Collection + { + + } + + private static readonly Dictionary _empty = new Dictionary(); + + public abstract string ToString(Dictionary variables); + public override string ToString() + { + return ToString(_empty); + } + } + public class ExpandedStringSegmentLiteral : ExpandedStringSegment + { + private string mvarValue = String.Empty; + public string Value { get { return mvarValue; } set { mvarValue = value; } } + + public override string ToString(Dictionary variables) + { + return mvarValue; + } + } + /// + /// Represents a string segment whose value is populated by a variable. + /// + public class ExpandedStringSegmentVariable : ExpandedStringSegment + { + private string mvarVariableName = String.Empty; + /// + /// The name of the variable in the variables dictionary from which to retrieve the value for this . + /// + public string VariableName { get { return mvarVariableName; } set { mvarVariableName = value; } } + + /// + /// Creates a new instance of with the given variable name. + /// + /// The name of the variable in the variables dictionary from which to retrieve the value for this . + public ExpandedStringSegmentVariable(string variableName) + { + mvarVariableName = variableName; + } + + /// + /// Returns the value of this with the given variables. + /// + /// The variables to pass into the . + /// A that contains the value of this . + public override string ToString(Dictionary variables) + { + return variables[mvarVariableName]; + } + } +} diff --git a/CSharp/Plugins/UniversalEditor.Essential/UniversalEditor.Essential.csproj b/CSharp/Plugins/UniversalEditor.Essential/UniversalEditor.Essential.csproj index f2ef3703..b862bc71 100644 --- a/CSharp/Plugins/UniversalEditor.Essential/UniversalEditor.Essential.csproj +++ b/CSharp/Plugins/UniversalEditor.Essential/UniversalEditor.Essential.csproj @@ -64,6 +64,8 @@ + + @@ -107,6 +109,7 @@ +