Beispiel #1
0
        protected override string Process(string input, TextWriter output)
        {
            var separators = Separators.Select(s => s.Match.Value);

            var charLimit = Config.GetLimit(1);
            var reversals = Matches.Select(m =>
            {
                var charStack = new Stack <char>();
                m.Replacement.Each((c, i) =>
                {
                    if (charLimit.IsInRange(i, m.Replacement.Length))
                    {
                        charStack.Push(c);
                    }
                });

                if (Config.Random)
                {
                    var charList = charStack.ToList();
                    charList.Shuffle();
                    charStack = new Stack <char>(charList);
                }

                return(new string(m.Replacement.Select(
                                      (c, i) => charLimit.IsInRange(i, m.Replacement.Length) ? charStack.Pop() : c
                                      ).ToArray()));
            });

            if (Config.Reverse)
            {
                reversals = reversals.Reverse();
            }

            return(separators.Riffle(reversals));
        }
Beispiel #2
0
        protected override string Process(string input, TextWriter output)
        {
            IEnumerable <string> sortedMatches;

            if (Config.Random)
            {
                var matchStrings = Matches.Select(m => m.Match.Value).ToList();
                matchStrings.Shuffle();
                sortedMatches = matchStrings;
            }
            else if (Config.SortNumerically)
            {
                sortedMatches = from MatchContext m in Matches
                                let numberMatch = new Regex(@"-?\d+").Match(m.Replacement)
                                                  orderby numberMatch.Success ? int.Parse(numberMatch.Value) : 0
                                                  select m.Match.Value;
            }
            else
            {
                sortedMatches = Matches.OrderBy(m => m.Replacement, StringComparer.Ordinal).Select(m => m.Match.Value);
            }

            var separators = Separators.Select(s => s.Match.Value);

            if (Config.Reverse)
            {
                sortedMatches = sortedMatches.Reverse();
            }

            return(separators.Riffle(sortedMatches));
        }
Beispiel #3
0
        protected override string Process(string input, TextWriter output)
        {
            var separators        = Separators.Select(s => s.Match.Value);
            var matchReplacements = Matches.Select(m => new string(
                                                       m.Replacement.Where((_, i) => Config.GetLimit(1).IsInRange(i, m.Replacement.Length)).ToArray()
                                                       ));

            if (Config.Reverse)
            {
                matchReplacements = matchReplacements.Reverse();
            }

            return(separators.Riffle(matchReplacements));
        }
Beispiel #4
0
        protected override string Process(string input, TextWriter output)
        {
            var separators = Separators.Select(s => s.Match.Value);

            int paddingWidth = Matches.Select(m => m.Replacement.Length).Aggregate(Math.Max);

            string padString = Config.StringParam ?? " ";

            if (!Config.Reverse)
            {
                padString = new string(Enumerable.Range(0, paddingWidth).Select(
                                           i => padString[i % padString.Length]
                                           ).ToArray());
            }
            else
            {
                padString = new string(Enumerable.Range(0, paddingWidth).Reverse().Select(
                                           i => padString[padString.Length - 1 - i % padString.Length]
                                           ).ToArray());
            }

            var paddedMatches = Matches.Select(m =>
            {
                string val = m.Match.Value;
                if (!Config.Reverse)
                {
                    return(val + padString.Remove(0, Math.Min(val.Length, padString.Length)));
                }
                else
                {
                    return(padString.Substring(0, Math.Max(0, padString.Length - val.Length)) + val);
                }
            });

            return(separators.Riffle(paddedMatches));
        }