Exemple #1
0
        /// <summary>
        ///   Searches if the current buffer matches against one of the
        ///   amongs, starting from the current cursor position and going
        ///   backwards.
        /// </summary>
        ///
        protected int find_among_b(Among[] v)
        {
            int i = 0;
            int j = v.Length;

            int c  = cursor;
            int lb = limit_backward;

            int common_i = 0;
            int common_j = 0;

            bool first_key_inspected = false;

            while (true)
            {
                int   k      = i + ((j - i) >> 1);
                int   diff   = 0;
                int   common = common_i < common_j ? common_i : common_j;
                Among w      = v[k];

                for (int i2 = w.SearchString.Length - 1 - common; i2 >= 0; i2--)
                {
                    if (c - common == lb)
                    {
                        diff = -1;
                        break;
                    }

                    diff = current[c - 1 - common] - w.SearchString[i2];

                    if (diff != 0)
                    {
                        break;
                    }

                    common++;
                }

                if (diff < 0)
                {
                    j        = k;
                    common_j = common;
                }
                else
                {
                    i        = k;
                    common_i = common;
                }

                if (j - i <= 1)
                {
                    if (i > 0)
                    {
                        break;
                    }

                    if (j == i)
                    {
                        break;
                    }

                    if (first_key_inspected)
                    {
                        break;
                    }

                    first_key_inspected = true;
                }
            }

            while (true)
            {
                Among w = v[i];

                if (common_i >= w.SearchString.Length)
                {
                    cursor = c - w.SearchString.Length;

                    if (w.Action == null)
                    {
                        return(w.Result);
                    }

                    bool res = w.Action();
                    cursor = c - w.SearchString.Length;

                    if (res)
                    {
                        return(w.Result);
                    }
                }

                i = w.MatchIndex;

                if (i < 0)
                {
                    return(0);
                }
            }
        }
Exemple #2
0
        /// <summary>
        ///   Searches if the current buffer matches against one of the
        ///   amongs, starting from the current cursor position and going
        ///   forward.
        /// </summary>
        ///
        protected int find_among(Among[] v)
        {
            int i = 0;
            int j = v.Length;

            int c = cursor;
            int l = limit;

            int common_i = 0;
            int common_j = 0;

            bool first_key_inspected = false;

            while (true)
            {
                int k      = i + ((j - i) >> 1);
                int diff   = 0;
                int common = common_i < common_j ? common_i : common_j; // smaller

                Among w = v[k];

                for (int i2 = common; i2 < w.SearchString.Length; i2++)
                {
                    if (c + common == l)
                    {
                        diff = -1;
                        break;
                    }

                    diff = current[c + common] - w.SearchString[i2];

                    if (diff != 0)
                    {
                        break;
                    }

                    common++;
                }

                if (diff < 0)
                {
                    j        = k;
                    common_j = common;
                }
                else
                {
                    i        = k;
                    common_i = common;
                }

                if (j - i <= 1)
                {
                    if (i > 0)
                    {
                        break; // v->s has been inspected
                    }
                    if (j == i)
                    {
                        break; // only one item in v
                    }
                    // - but now we need to go round once more to get
                    // v->s inspected. This looks messy, but is actually
                    // the optimal approach.

                    if (first_key_inspected)
                    {
                        break;
                    }

                    first_key_inspected = true;
                }
            }

            while (true)
            {
                Among w = v[i];

                if (common_i >= w.SearchString.Length)
                {
                    cursor = c + w.SearchString.Length;

                    if (w.Action == null)
                    {
                        return(w.Result);
                    }

                    bool res = w.Action();
                    cursor = c + w.SearchString.Length;

                    if (res)
                    {
                        return(w.Result);
                    }
                }

                i = w.MatchIndex;

                if (i < 0)
                {
                    return(0);
                }
            }
        }