Scripts/StringFilter

From Unify Community Wiki
Jump to: navigation, search

Description

This is a simple helper class which allows you to specify a set of certain words / character which should not appear in a string. By default it will only filter control characters (0x00 - 0x1F). You can add custom words / characters to the exclude list.

Usage

The class generally provides two methods:

- CheckForProblems
- FilterString

CheckForProblems fills a List with "Problem" structs which indicate where in the string the problematic item occurred, as well as the item from the exclude list which was found (It just stores the filter reference and the index, no garbage is generated). This can be used to inform the user about illegal characters / phrases in their input string.

FilterString will actually remove all of the exclude items from the string. You can pass a string in which case it returns the filtered string, or pass a StringBuilder. The latter might be useful when you need to do additional processing before / after the filtering. If you cache the StringBuilder you can avoid the extra garbage.


StringFilter.cs

using System;
using System.Collections.Generic;
using System.Text;
 
public class StringFilter
{
    public struct Problem
    {
        private StringFilter m_Filter;
        private int m_ItemIndex;
        public int stringIndex;
        public int ItemIndex { get { return m_ItemIndex; } }
        public string Item
        {
            get
            {
                if (m_Filter == null || m_ItemIndex < 0 || m_ItemIndex >= m_Filter.exclude.Count)
                    return "";
                return m_Filter.exclude[m_ItemIndex];
            }
        }
        public Problem(StringFilter aFilter, int aStringIndex, int aItemIndex)
        {
            m_Filter = aFilter;
            stringIndex = aStringIndex;
            m_ItemIndex = aItemIndex;
        }
    }
    public List<string> exclude = new List<string>();
    public StringFilter(bool aFilterControlChars = true)
    {
        if (aFilterControlChars)
        {
            char[] chars = new char[32];
            for (int i = 0; i < 32; i++)
                chars[i] = (char)i;
            AddExclude(chars);
        }
    }
    public StringFilter(IEnumerable<string> aExcludes)
    {
        exclude.AddRange(aExcludes);
    }
 
    public StringFilter AddExclude(params string[] aExcludeStrings)
    {
        exclude.AddRange(aExcludeStrings);
        return this;
    }
    public StringFilter AddExclude(params char[] aExcludeChars)
    {
        for(int i = 0; i < aExcludeChars.Length; i++)
            exclude.Add(aExcludeChars[i].ToString());
        return this;
    }
 
    public List<Problem> CheckForProblems(string aString)
    {
        List<Problem> tmp = new List<Problem>();
        CheckForProblems(tmp, aString);
        return tmp;
    }
    public bool CheckForProblems(List<Problem> aProblemList, string aString)
    {
        if (aProblemList == null)
            throw new System.ArgumentNullException("aProblemList");
        if (aString == null)
            throw new System.ArgumentNullException("aString");
        bool problem = false;
        for (int i = 0; i < exclude.Count; i++)
        {
            int pos = aString.IndexOf(exclude[i]);
            if (pos >= 0)
            {
                aProblemList.Add(new Problem(this, pos, i));
                problem = true;
            }
        }
        return problem;
    }
 
    public string FilterString(string aString)
    {
        StringBuilder sb = new StringBuilder(aString);
        FilterString(sb);
        return sb.ToString();
    }
 
    public void FilterString(StringBuilder aString)
    {
        for (int i = 0; i < exclude.Count; i++)
        {
            aString.Replace(exclude[i], "");
        }
    }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox