Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Enhanced String Handling II

, 16 Dec 2010
Overcoming limitations of: Enhanced String Handling
EnhancedStringEvaluate.zip
EnhancedStringEvaluate
bin
Debug
ConfigEvaluate.vshost.exe
ConfigEvaluate.vshost.exe.manifest
EnhancedStringEvaluate.dll
EnhancedStringEvaluate.pdb
ConcordPairEvaluate.csproj.vs10x
EnhancedStringEvaluate.csproj.user
EnhancedStringEvaluate.csproj.vs10x
obj
Debug
Decl
EnhancedStringEvaluate.dll
EnhancedStringEvaluate.pdb
DesignTimeResolveAssemblyReferencesInput.cache
EnhancedStringEvaluate.dll
EnhancedStringEvaluate.pdb
Refactor
ConfigEvaluate.dll
ResolveAssemblyReference.cache
TempPE
Properties
EvaluateTest
bin
Debug
EnhancedStringEvaluate.dll
EnhancedStringEvaluate.pdb
EvaluateSampleTest.dll
EvaluateSampleTest.pdb
EvaluateTest.dll
EvaluateTest.pdb
TestEvaluation.exe
TestEvaluation.pdb
EvaluateSampleTest.csproj.vs10x
EvaluateTest.csproj.vs10x
obj
Debug
Decl
EvaluateSampleTest.dll
EvaluateSampleTest.pdb
DesignTimeResolveAssemblyReferencesInput.cache
EvaluateSampleTest.dll
EvaluateSampleTest.pdb
EvaluateTest.dll
EvaluateTest.pdb
ResolveAssemblyReference.cache
TempPE
Properties
TestEvaluate
bin
Debug
abc.text
EnhancedStringEvaluate.dll
EnhancedStringEvaluate.pdb
TestEvaluate.exe
TestEvaluate.pdb
TestEvaluate.vshost.exe.manifest
TestEvaluation.exe
TestEvaluation.pdb
TestEvaluation.vshost.exe
TestEvaluation.vshost.exe.manifest
TestOnTheFlyConfig.vshost.exe
TestOnTheFlyConfig.vshost.exe.manifest
obj
Debug
Decl
TestEvaluation.exe
TestEvaluation.pdb
DesignTimeResolveAssemblyReferencesInput.cache
Refactor
ResolveAssemblyReference.cache
TempPE
TestEvaluate.exe
TestEvaluate.pdb
TestEvaluation.exe
TestEvaluation.pdb
ProcessEvaluate
Properties
TestEvaluate.csproj.vs10x
TestEvaluation.csproj.user
TestEvaluation.csproj.vs10x
TestOnTheFlyConfig.csproj.vs10x
using System;
using System.Collections.Generic;
using EnhancedStringEvaluate;
using System.Text.RegularExpressions;


namespace TestEvaluation.ProcessEvaluate
{
	/// <summary>
	/// Purpose:
	///		Similar to a calculator memory, a value may be stored and retrieved.
	///	
	///	Format:
	///	(Delimiters are displayed as "{" and "}" but the system will be able to 
	///	handle any delimiter.)
	///		{Memory::Name::Value}	-- Store Value into Name storage
	///		{Memory::Name}			-- Retrieve from Name storage
	/// </summary>
	public class ProcessMemory : IProcessEvaluate
	{
		/// <summary>Expression identifying the memory</summary>
		private Regex _reMemory;

		/// <summary>Identifying the delimiters</summary>
		private readonly IDelimitersAndSeparator _delim;

		/// <summary>Keep track of all the memory stores</summary>
		private Dictionary<string, string> _memories;

		public ProcessMemory() : this(DelimitersAndSeparator.DefaultDelimitedString) {}
		public ProcessMemory(IDelimitersAndSeparator delim)
		{
			_delim = delim;
			_memories = new Dictionary<string, string>();

			RegexOptions reo = RegexOptions.IgnoreCase | RegexOptions.Singleline;
			//string pattern = @"({)Memory::(?<name>:?(([^{}:]+)(:[^{}:]*)?)*)(::(?<value>[^{}]*)?)?(})";
			string pattern = string.Format(@"({0})Memory::(?<name>:?(([^{0}{1}:]+)(:[^{0}{1}:]*)?)*)(::(?<value>[^{0}{1}]*)?)?({1})", 
				_delim.OpenDelimEquivalent, _delim.CloseDelimEquivalent);
			_reMemory = new Regex(pattern, reo);
		}

		#region IProcessEvaluate Members

		public void Evaluate(object src, EnhancedStringEventArgs ea)
		{
			// Initialize return code
			ea.IsHandled = false;

			// Evaluate Task 1
			string text = ea.EhancedPairElem.Value;
			if (string.IsNullOrWhiteSpace(text)) return;

			bool rc = _reMemory.IsMatch(text);
			if (!rc) return;

			string replacement = _reMemory.Replace(text, MemoryReplace);
			if (replacement == text) return;

			// Evaluate Task 2: Announce that expression was successfully handled
			ea.IsHandled = true;

			// Evaluate Task 3: Keep new value
			ea.EhancedPairElem.Value = replacement;
			return;
		}

		#endregion

		private string MemoryReplace(Match m)
		{
			string name = m.Groups["name"].Value;
			if (string.IsNullOrWhiteSpace(name))
				return m.ToString();

			string value = m.Groups["value"].Value;
			if (string.IsNullOrWhiteSpace(value))
			{
				if (_memories.ContainsKey(name))
					return _memories[name];

				_memories.Add(name, string.Empty);
				return string.Empty;
			}

			if (_memories.ContainsKey(name))
				_memories[name] = value;
			else
				_memories.Add(name, value);

			return value;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Avi Farah

United States United States
avifarah@gmail.com

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 16 Dec 2010
Article Copyright 2010 by Avi Farah
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid