示例#1
0
        //public static ConditionExpression Parse(string str)
        //{
        //	var quoting=0;
        //	var literal=false;
        //	var words=new List<string>();
        //	str=str.Trim();
        //	var startIndex=0;
        //	for(int i=0;i<str.Length;++i){
        //		if(quoting==0&&!literal){
        //			if(!char.IsWhiteSpace(str[i])){
        //				startIndex=i;
        //				literal=true;
        //				if(str[i]=='"'){
        //					++quoting;
        //					++startIndex;
        //				}
        //			}
        //		}else if(literal){
        //			if(quoting>0&&str[i]=='"'&&char.IsWhiteSpace(str[i+1])){

        //				quoting=false;
        //				literal=false;
        //				words.Add(str.Substring(startIndex,i-startIndex-1);
        //			}else if(char.IsWhiteSpace(str[i])){
        //				literal=false;
        //			}
        //		}

        //		if(str[i]=='"'&&((quoting&&char.IsWhiteSpace(str[i+1]))||!quoting)){
        //			quoting=!quoting;
        //		}
        //	}
        //}

        public static ConditionExpression Create(string str)
        {
            var literal = false;
            var words   = new List <string>();

            str = str.Trim();
            if (str == "")
            {
                return(new DummyExpression(true));
            }
            var startIndex = 0;

            for (int i = 0; i < str.Length; ++i)
            {
                if (!literal && !char.IsWhiteSpace(str[i]))
                {
                    literal    = true;
                    startIndex = i;
                }
                else if (literal && char.IsWhiteSpace(str[i]))
                {
                    words.Add(str.Substring(startIndex, i - startIndex));
                    literal = false;
                }
            }
            if (literal)
            {
                words.Add(str.Substring(startIndex, str.Length - startIndex));
            }
            words = words.Select(w => CharWidthNormalizer.Normalize(w)).ToList();
            ConditionExpression root = null;

            if (words.Count == 1)
            {
                root = new LiteralExpression(words.First());
            }
            else
            {
                var last = new AndOperatorExpression(null, null);
                root = last;
                for (int i = 0; i < words.Count; ++i)
                {
                    last.Operand1 = new LiteralExpression(words[i]);
                    if (words.Count == i + 2)
                    {
                        last.Operand2 = new LiteralExpression(words[i + 1]);
                        ++i;
                    }
                    else
                    {
                        var newExpr = new AndOperatorExpression(null, null);
                        last.Operand2 = newExpr;
                        last          = newExpr;
                    }
                }
            }
            return(root);
        }
示例#2
0
 //public static ConditionExpression Parse(string str)
 //{
 //    var quoting=0;
 //    var literal=false;
 //    var words=new List<string>();
 //    str=str.Trim();
 //    var startIndex=0;
 //    for(int i=0;i<str.Length;++i){
 //        if(quoting==0&&!literal){
 //            if(!char.IsWhiteSpace(str[i])){
 //                startIndex=i;
 //                literal=true;
 //                if(str[i]=='"'){
 //                    ++quoting;
 //                    ++startIndex;
 //                }
 //            }
 //        }else if(literal){
 //            if(quoting>0&&str[i]=='"'&&char.IsWhiteSpace(str[i+1])){
 //                quoting=false;
 //                literal=false;
 //                words.Add(str.Substring(startIndex,i-startIndex-1);
 //            }else if(char.IsWhiteSpace(str[i])){
 //                literal=false;
 //            }
 //        }
 //        if(str[i]=='"'&&((quoting&&char.IsWhiteSpace(str[i+1]))||!quoting)){
 //            quoting=!quoting;
 //        }
 //    }
 //}
 public static ConditionExpression Create(string str)
 {
     var literal=false;
     var words=new List<string>();
     str=str.Trim();
     if(str=="") return new DummyExpression(true);
     var startIndex=0;
     for(int i=0;i<str.Length;++i){
         if(!literal&&!char.IsWhiteSpace(str[i])){
             literal=true;
             startIndex=i;
         }else if(literal&&char.IsWhiteSpace(str[i])){
             words.Add(str.Substring(startIndex,i-startIndex));
             literal=false;
         }
     }
     if(literal) words.Add(str.Substring(startIndex,str.Length-startIndex));
     words=words.Select(w=>CharWidthNormalizer.Normalize(w)).ToList();
     ConditionExpression root=null;
     if(words.Count==1) root=new LiteralExpression(words.First());
     else{
         var last=new AndOperatorExpression(null,null);
         root=last;
         for(int i=0;i<words.Count;++i){
             last.Operand1=new LiteralExpression(words[i]);
             if(words.Count==i+2){
                 last.Operand2=new LiteralExpression(words[i+1]);
                 ++i;
             }else{
                 var newExpr=new AndOperatorExpression(null,null);
                 last.Operand2=newExpr;
                 last=newExpr;
             }
         }
     }
     return root;
 }