コード例 #1
0
        string Transform(string text, bool isOriginalText)
        {
            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }

            // Get a list of matches by order of the map
            IDictionary <int, int> matches = MatchMap(text, isOriginalText);

            // From the list of matches, rebuild the string
            StringBuilder builder = new StringBuilder();
            int           textIdx = 0;

            while (textIdx < text.Length)
            {
                if (!matches.ContainsKey(textIdx))
                {
                    builder.Append(text[textIdx++]);
                }
                else
                {
                    ReplacerEntry entry    = map[matches[textIdx]];
                    string        original = isOriginalText ? entry.Original : entry.Modified;
                    string        modified = isOriginalText ? entry.Modified : entry.Original;

                    // Append the modified in the new string and skip the original
                    builder.Append(modified);
                    textIdx += original.Length;
                }
            }

            return(builder.ToString());
        }
コード例 #2
0
        /// <summary>
        /// Add or replace an entry in the map.
        /// </summary>
        /// <param name="src">The source field for the entry.</param>
        /// <param name="dst">The destination field for the entry.</param>
        public void Add(string src, string dst)
        {
            if (string.IsNullOrEmpty(src))
            {
                throw new ArgumentNullException(nameof(src));
            }

            if (string.IsNullOrEmpty(dst))
            {
                throw new ArgumentNullException(nameof(dst));
            }

            int index = FindMapEntry(src, true);

            if (index == -1)
            {
                map.Add(new ReplacerEntry(src, dst));
            }
            else
            {
                map[index] = new ReplacerEntry(src, dst);
            }
        }