public LikeExpression()

in src/main/csharp/Selector/LikeExpression.cs [37:101]


        public LikeExpression(IExpression left, string like, string escape, bool notNot)
            : base(left)
        {
            this.notNot = notNot;

            bool doEscape = false;
            char escapeChar = '%';

            if(escape != null)
            {
                if(escape.Length != 1)
                {
                    throw new ApplicationException("The ESCAPE string litteral is invalid.  It can only be one character.  Litteral used: " + escape);
                }
                doEscape = true;
                escapeChar = escape[0];
            }

            StringBuilder temp = new StringBuilder();
            StringBuilder regexp = new StringBuilder(like.Length * 2);
            regexp.Append("^"); // The beginning of the input
            for(int c = 0; c < like.Length; c++)
            {
                char ch = like[c];
                if(doEscape && (ch == escapeChar))
                {
                    c++;
                    if(c >= like.Length)
                    {
                        // nothing left to escape...
                        break;
                    }
                    temp.Append(like[c]);
                }
                else if(ch == '%')
                {
                    if(temp.Length > 0)
                    {
                        regexp.Append(Regex.Escape(temp.ToString()));
                        temp.Length = 0;
                    }
                    regexp.Append(".*?"); // Do a non-greedy match 
                }
                else if(c == '_')
                {
                    if(temp.Length > 0)
                    {
                        regexp.Append(Regex.Escape(temp.ToString()));
                        temp.Length = 0;
                    }
                    regexp.Append("."); // match one 
                }
                else
                {
                    temp.Append(ch);
                }
            }
            if(temp.Length > 0)
            {
                regexp.Append(Regex.Escape(temp.ToString()));
            }
            regexp.Append("$"); // The end of the input

            pattern = new Regex(regexp.ToString(), RegexOptions.Singleline | RegexOptions.Compiled);
        }