Beispiel #1
0
 public Xeger(string regex, IRandom random)
 {
     _Automaton = new RegExp(RemoveStartEndMarkers(Check.NotNull(nameof(regex), regex)), _AllExceptAnyString).ToAutomaton();
     _Random    = random ?? throw new ArgumentNullException(nameof(random));
 }
        public static Automaton MakeStringMatcher(string s)
        {
            var a      = new Automaton();
            var states = new State[s.Length + 1];

            states[0] = a.Initial;
            for (var i = 0; i < s.Length; i++)
            {
                states[i + 1] = new State();
            }

            var f = states[s.Length];

            f.Accept = true;
            f.Transitions.Add(new Transition(char.MinValue, char.MaxValue, f));
            for (var i = 0; i < s.Length; i++)
            {
                var done = new HashSet <char?>();
                var c    = s[i];
                states[i].Transitions.Add(new Transition(c, states[i + 1]));
                _ = done.Add(c);
                for (var j = i; j >= 1; j--)
                {
                    var d = s[j - 1];
                    if (!done.Contains(d) && s.Substring(0, j - 1).Equals(s.Substring(i - j + 1, i - (i - j + 1))))
                    {
                        states[i].Transitions.Add(new Transition(d, states[j]));
                        _ = done.Add(d);
                    }
                }

                var da = new char[done.Count];
                var h  = 0;
                foreach (char w in done)
                {
                    da[h++] = w;
                }

                Array.Sort(da);
                int from = char.MinValue;
                var k    = 0;
                while (from <= char.MaxValue)
                {
                    while (k < da.Length && da[k] == from)
                    {
                        k++;
                        from++;
                    }

                    if (from <= char.MaxValue)
                    {
                        int to = char.MaxValue;
                        if (k < da.Length)
                        {
                            to = da[k] - 1;
                            k++;
                        }

                        states[i].Transitions.Add(new Transition((char)from, (char)to, states[0]));
                        from = to + 2;
                    }
                }
            }

            a.IsDeterministic = true;
            return(a);
        }
 public static Automaton MakeTotalDigits(int i) => Automaton.Minimize(
     new RegExp("[ \t\n\r]*[-+]?0*([0-9]{0," + i + "}|((([0-9]\\.*){0," + i + "})&@\\.@)0*)[ \t\n\r]*")
     .ToAutomaton());
 public static Automaton MakeFractionDigits(int i) => Automaton.Minimize(
     new RegExp("[ \t\n\r]*[-+]?[0-9]+(\\.[0-9]{0," + i + "}0*)?[ \t\n\r]*")
     .ToAutomaton());
        public static Automaton MakeDecimalValue(string value)
        {
            var minus = false;
            var i     = 0;

            while (i < value.Length)
            {
                var c = value[i];
                if (c == '-')
                {
                    minus = true;
                }

                if ((c >= '1' && c <= '9') || c == '.')
                {
                    break;
                }

                i++;
            }

            var sb1 = new StringBuilder();
            var sb2 = new StringBuilder();
            var p   = value.IndexOf('.', i);

            if (p == -1)
            {
                _ = sb1.Append(value, i, value.Length - i);
            }
            else
            {
                _ = sb1.Append(value, i, p - i);
                i = value.Length - 1;
                while (i > p)
                {
                    var c = value[i];
                    if (c >= '1' && c <= '9')
                    {
                        break;
                    }

                    i--;
                }

                _ = sb2.Append(value, p + 1, i + 1 - (p + 1));
            }

            if (sb1.Length == 0)
            {
                _ = sb1.Append("0");
            }

            var s = minus ? Automaton.MakeChar('-') : Automaton.MakeChar('+').Optional();
            var d = sb2.Length == 0
                ? Automaton.MakeChar('.').Concatenate(Automaton.MakeChar('0').Repeat(1)).Optional()
                : Automaton.MakeChar('.')
                    .Concatenate(Automaton.MakeString(sb2.ToString()))
                    .Concatenate(Automaton.MakeChar('0')
                                 .Repeat());

            return(Automaton.Minimize(WhitespaceAutomaton
                                      .Concatenate(s.Concatenate(Automaton.MakeChar('0').Repeat()).Concatenate(Automaton.MakeString(sb1.ToString())).Concatenate(d))
                                      .Concatenate(WhitespaceAutomaton)));
        }
 public Automaton Intersection(Automaton a) => BasicOperations.Intersection(this, a);
 public Automaton Concatenate(Automaton a) => BasicOperations.Concatenate(this, a);
 public static Automaton Minimize(Automaton a)
 {
     a.Minimize();
     return(a);
 }