|
/*
* Created by SharpDevelop.
* User: LYCJ
* Date: 18/12/2006
* Time: 21:16
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections;
using System.Collections.Generic;
namespace Cake3
{
internal class tlLookupTag
{
internal tagLookup lookup;
public Int32 foundStart;
public Int32 foundEnd;
public virtual bool Found(Int32 startIdx)
{
return false;
}
public virtual Int32 RequestText(string text)
{
return -1;
}
public virtual tlLookupTag PreviousTag()
{
Int32 idx = lookup.TagList.IndexOf(this);
if (idx > 0)
return lookup.TagList[idx-1];
return null;
}
public tlLookupTag(tagLookup aLookup)
{
lookup = aLookup;
}
}
internal class tlLookupText : tlLookupTag
{
string targetText;
public override bool Found(Int32 startIdx)
{
if (startIdx >= lookup.Text.Length)
foundStart = -1;
else foundStart = lookup.Text.IndexOf(targetText,startIdx+1);
if ((foundStart == -1) && (PreviousTag() != null))
foundStart = PreviousTag().RequestText(targetText);
bool retVal = (foundStart != -1);
if (retVal)
foundEnd = foundStart + targetText.Length;
else
foundEnd = -1;
return retVal;
}
public tlLookupText(tagLookup aLookup, string aTargetText) : base(aLookup)
{
targetText = aTargetText;
}
}
internal class tlLookupMask : tlLookupTag
{
public override bool Found(Int32 startIdx)
{
foundStart = startIdx + 1;
foundEnd = lookup.Text.Length -1;
return true;
}
public override Int32 RequestText(string text)
{
Int32 retVal = lookup.Text.IndexOf(text, foundStart);
if (retVal != -1)
foundEnd = retVal+1;
return retVal;
}
public tlLookupMask(tagLookup aLookup) : base(aLookup)
{
}
}
internal class tagLookup
{
public List<tlLookupTag> TagList = new List<tlLookupTag>();
public string Text;
internal static tlLookupTag CreateLookupTag(tagLookup lookup, string param, bool isMask)
{
if (isMask)
return new tlLookupMask(lookup);
return new tlLookupText(lookup, param);
}
public tagLookup(string aText, string aMask)
{
Text = aText;
string m = aMask;
Int32 idx = m.IndexOf('*');
while (idx != -1)
{
if (idx > 1)
TagList.Add(CreateLookupTag(this, m.Substring(0, idx), false));
TagList.Add(CreateLookupTag(this, "", true));
m = m.Remove(0, idx+1);
idx = m.IndexOf('*');
}
if (m != "") TagList.Add(CreateLookupTag(this, m, false));
}
public bool Found()
{
Int32 idx = -1;
foreach (tlLookupTag tag in TagList)
{
if (tag.Found(idx))
idx = (tag.foundEnd);
else return false;
}
return true;
}
}
/// <summary>
/// Description of StringUtils.
/// </summary>
public static class StringUtils
{
public static bool MatchMask(string aText, string aMask)
{
tagLookup Lookup = new tagLookup(aText, aMask);
return Lookup.Found();
}
}
}
|
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.