Esempio n. 1
0
        /// <summary>
        /// Helper method for saving possible chars used in regexp according of its structure.
        /// </summary>
        /// <param name="sb">stringbuilder used too store data about that alphabet.</param>
        /// <returns></returns>
        private StringBuilder ToStringBuilderForUsedAlphabet(StringBuilder sb)
        {
            switch (kind)
            {
            case Kind.RegexpUnion:
            case Kind.RegexpConcatenation:
                exp1.ToStringBuilderForUsedAlphabet(sb);
                exp2.ToStringBuilderForUsedAlphabet(sb);
                break;

            case Kind.RegexpIntersection:
                var sb1 = new StringBuilder();
                var sb2 = new StringBuilder();
                var st1 = exp1.ToStringBuilderForUsedAlphabet(sb1).ToString().ToCharArray();
                var st2 = exp2.ToStringBuilderForUsedAlphabet(sb2).ToString().ToCharArray();
                sb.Append(st1.Intersect(st2).Distinct());
                break;

            case Kind.RegexpOptional:
            case Kind.RegexpRepeat:
            case Kind.RegexpRepeatMin:
            case Kind.RegexpRepeatMinMax:
                exp1.ToStringBuilderForUsedAlphabet(sb);
                break;

            case Kind.RegexpComplement:
                var sbc = new StringBuilder();
                var stc = exp1.ToStringBuilderForUsedAlphabet(sbc).ToString().ToCharArray();
                if (this.anyCharAlphabet != null)
                {
                    sb.Append(this.anyCharAlphabet.ToCharArray().Where(x => !stc.Contains(x)));
                }
                else
                {
                    var sba = new StringBuilder();
                    var sta = MakeAnyPrintableASCIIChar(this.anyCharAlphabet).ToStringBuilderForUsedAlphabet(sba).ToString().ToCharArray();
                    sb.Append(sta.Where(x => !stc.Contains(x)));
                }
                break;

            case Kind.RegexpChar:
                sb.Append(c);
                break;

            case Kind.RegexpCharRange:
                for (char i = this.@from; i <= this.to; i++)
                {
                    sb.Append(i);
                }
                break;

            case Kind.RegexpAnyChar:
            case Kind.RegexpAnyString:
                if (this.anyCharAlphabet != null)
                {
                    sb.Append(this.anyCharAlphabet);
                }
                else
                {
                    MakeAnyPrintableASCIIChar(this.anyCharAlphabet).ToStringBuilderForUsedAlphabet(sb);
                }

                break;

            case Kind.RegexpEmpty:
                if (this.anyCharAlphabet != null)
                {
                    foreach (char ch in this.anyCharAlphabet)
                    {
                        if (Char.IsWhiteSpace(ch))
                        {
                            sb.Append(ch);
                        }
                    }
                }
                else
                {
                    sb.Append(' ');    // in ' ' to '~' there is only one whitespace, and that is space.
                }
                break;

            case Kind.RegexpString:
            case Kind.RegexpAutomaton:
                sb.Append(s);
                break;

            case Kind.RegexpInterval:
                string s1 = Convert.ToDecimal(min).ToString();
                string s2 = Convert.ToDecimal(max).ToString();
                sb.Append(s1).Append(s2);
                break;
            }

            return(sb);
        }