/// <summary>Records a replacement to be applied to the inputs
		/// stream.  Whenever <c>singleMatch</c> occurs in
		/// the input, it will be replaced with
		/// <c>replacement</c>.
		/// 
		/// </summary>
		/// <param name="singleMatch">input String to be replaced
		/// </param>
		/// <param name="replacement">output String
		/// </param>
		public virtual void  Add(System.String singleMatch, System.String replacement)
		{
			NormalizeCharMap currMap = this;
			for (var i = 0; i < singleMatch.Length; i++)
			{
				char c = singleMatch[i];
				if (currMap.submap == null)
				{
					currMap.submap = new HashMap<char, NormalizeCharMap>(1);
				}
				var map = currMap.submap[c];
				if (map == null)
				{
					map = new NormalizeCharMap();
					currMap.submap[c] = map;
				}
				currMap = map;
			}
			if (currMap.normStr != null)
			{
				throw new System.SystemException("MappingCharFilter: there is already a mapping for " + singleMatch);
			}
			currMap.normStr = replacement;
			currMap.diff = singleMatch.Length - replacement.Length;
		}
        /// <summary>Records a replacement to be applied to the inputs
        /// stream.  Whenever <c>singleMatch</c> occurs in
        /// the input, it will be replaced with
        /// <c>replacement</c>.
        ///
        /// </summary>
        /// <param name="singleMatch">input String to be replaced
        /// </param>
        /// <param name="replacement">output String
        /// </param>
        public virtual void  Add(System.String singleMatch, System.String replacement)
        {
            NormalizeCharMap currMap = this;

            for (var i = 0; i < singleMatch.Length; i++)
            {
                char c = singleMatch[i];
                if (currMap.submap == null)
                {
                    currMap.submap = new HashMap <char, NormalizeCharMap>(1);
                }
                var map = currMap.submap[c];
                if (map == null)
                {
                    map = new NormalizeCharMap();
                    currMap.submap[c] = map;
                }
                currMap = map;
            }
            if (currMap.normStr != null)
            {
                throw new System.SystemException("MappingCharFilter: there is already a mapping for " + singleMatch);
            }
            currMap.normStr = replacement;
            currMap.diff    = singleMatch.Length - replacement.Length;
        }
        private NormalizeCharMap Match(NormalizeCharMap map)
        {
            NormalizeCharMap result = null;

            if (map.submap != null)
            {
                int chr = NextChar();
                if (chr != -1)
                {
                    NormalizeCharMap subMap = map.submap[(char)chr];
                    if (subMap != null)
                    {
                        result = Match(subMap);
                    }
                    if (result == null)
                    {
                        PushChar(chr);
                    }
                }
            }
            if (result == null && map.normStr != null)
            {
                result = map;
            }
            return(result);
        }
        public override int Read()
        {
            while (true)
            {
                if (replacement != null && charPointer < replacement.Length)
                {
                    return(replacement[charPointer++]);
                }

                int firstChar = NextChar();
                if (firstChar == -1)
                {
                    return(-1);
                }
                NormalizeCharMap nm = normMap.submap != null
                                                      ? normMap.submap[(char)firstChar]
                                                      : null;
                if (nm == null)
                {
                    return(firstChar);
                }
                NormalizeCharMap result = Match(nm);
                if (result == null)
                {
                    return(firstChar);
                }
                replacement = result.normStr;
                charPointer = 0;
                if (result.diff != 0)
                {
                    int prevCumulativeDiff = LastCumulativeDiff;
                    if (result.diff < 0)
                    {
                        for (int i = 0; i < -result.diff; i++)
                        {
                            AddOffCorrectMap(nextCharCounter + i - prevCumulativeDiff, prevCumulativeDiff - 1 - i);
                        }
                    }
                    else
                    {
                        AddOffCorrectMap(nextCharCounter - result.diff - prevCumulativeDiff, prevCumulativeDiff + result.diff);
                    }
                }
            }
        }
		/// Easy-use constructor that takes a <see cref="System.IO.TextReader" />.
		public MappingCharFilter(NormalizeCharMap normMap, System.IO.TextReader @in)
			: base(CharReader.Get(@in))
		{
			this.normMap = normMap;
		}
		/// Default constructor that takes a <see cref="CharStream" />.
		public MappingCharFilter(NormalizeCharMap normMap, CharStream @in)
			: base(@in)
		{
			this.normMap = normMap;
		}
		private NormalizeCharMap Match(NormalizeCharMap map)
		{
			NormalizeCharMap result = null;
			if (map.submap != null)
			{
				int chr = NextChar();
				if (chr != - 1)
				{
					NormalizeCharMap subMap = map.submap[(char)chr];
					if (subMap != null)
					{
						result = Match(subMap);
					}
					if (result == null)
					{
						PushChar(chr);
					}
				}
			}
			if (result == null && map.normStr != null)
			{
				result = map;
			}
			return result;
		}
 /// Easy-use constructor that takes a <see cref="System.IO.TextReader" />.
 public MappingCharFilter(NormalizeCharMap normMap, System.IO.TextReader @in)
     : base(CharReader.Get(@in))
 {
     this.normMap = normMap;
 }
 /// Default constructor that takes a <see cref="CharStream" />.
 public MappingCharFilter(NormalizeCharMap normMap, CharStream @in)
     : base(@in)
 {
     this.normMap = normMap;
 }