Example #1
0
        public static StringBuilder Generate([NotNull] Detector detector1, [NotNull] Detector detector2, int length,
                                             [NotNull] IRemoveStrategy removeStrategy,
                                             [NotNull] ICharGenerator generator, [CanBeNull] IGeneratorLogger logger = null)
        {
            var text = detector1.Text;

            text.EnsureCapacity(length);
            while (text.Length < length)
            {
                text.Append(generator.Generate());
                if (logger != null)
                {
                    logger.LogAfterGenerate(text);
                }
                Repetition repetition1, repetition2;
                var        delete1 = 0;
                if (detector1.TryDetect(out repetition1))
                {
                    delete1 = removeStrategy.GetCharsToDelete(text.Length, repetition1);
                    if (logger != null)
                    {
                        logger.LogRepetition(text, repetition1);
                    }
                }
                var delete2 = 0;
                if (detector2.TryDetect(out repetition2))
                {
                    delete2 = removeStrategy.GetCharsToDelete(text.Length, repetition2);
                    if (logger != null)
                    {
                        logger.LogRepetition(text, repetition2);
                    }
                }
                if (delete1 != delete2)
                {
                    throw new Exception("Not matching");
                }
                for (var i = 0; i < delete1; ++i)
                {
                    detector1.Backtrack();
                    detector2.Backtrack();
                    text.Remove(text.Length - 1, 1);
                }
            }
            return(text);
        }
        public static StringBuilder Generate([NotNull] Detector detector, int length, [NotNull] IRemoveStrategy removeStrategy,
                                             [NotNull] ICharGenerator generator, [CanBeNull] IGeneratorLogger logger = null, [CanBeNull] CancellationToken?token = null)
        {
            Statistics.Clear();
            var text = detector.Text;

            text.EnsureCapacity(length);
            var sw = Stopwatch.StartNew();

            while (text.Length < length)
            {
                if (token != null && token.Value.IsCancellationRequested)
                {
                    break;
                }
                text.Append(generator.Generate());
                Statistics.CharsGenerated++;
                if (logger != null)
                {
                    logger.LogAfterGenerate(text);
                }
                Repetition repetition;
                if (detector.TryDetect(out repetition))
                {
                    AddToStats(repetition);
                    var charsToDelete = removeStrategy.GetCharsToDelete(text.Length, repetition);
                    if (logger != null)
                    {
                        logger.LogRepetition(text, repetition);
                    }
                    for (var i = 0; i < charsToDelete; ++i)
                    {
                        detector.Backtrack();
                        text.Remove(text.Length - 1, 1);
                    }
                }
            }
            sw.Stop();
            Statistics.Milliseconds = sw.ElapsedMilliseconds;
            return(text);
        }