コード例 #1
0
        public override StringRebuilder Substring(Span span)
        {
            if (span.End > this.Length)
            {
                throw new ArgumentOutOfRangeException("span");
            }

            if (span.Length == this.Length)
            {
                return(this);
            }
            else if (span.End <= _left.Length)
            {
                return(_left.Substring(span));
            }
            else if (span.Start >= _left.Length)
            {
                return(_right.Substring(new Span(span.Start - _left.Length, span.Length)));
            }
            else
            {
                return(BinaryStringRebuilder.Create(_left.Substring(Span.FromBounds(span.Start, _left.Length)),
                                                    _right.Substring(Span.FromBounds(0, span.End - _left.Length))));
            }
        }
コード例 #2
0
 private StringRebuilder Assemble(Span left, StringRebuilder text, Span right)
 {
     if (text.Length == 0)
     {
         return(Assemble(left, right));
     }
     else if (left.Length == 0)
     {
         return((right.Length == 0) ? text : BinaryStringRebuilder.Create(text, this.GetSubText(right)));
     }
     else if (right.Length == 0)
     {
         return(BinaryStringRebuilder.Create(this.GetSubText(left), text));
     }
     else if (left.Length < right.Length)
     {
         return(BinaryStringRebuilder.Create(BinaryStringRebuilder.Create(this.GetSubText(left), text),
                                             this.GetSubText(right)));
     }
     else
     {
         return(BinaryStringRebuilder.Create(this.GetSubText(left),
                                             BinaryStringRebuilder.Create(text, this.GetSubText(right))));
     }
 }
コード例 #3
0
 private static StringRebuilder ConsolidateOrBalanceTreeNode(StringRebuilder left, StringRebuilder right)
 {
     if ((left.Length + right.Length < TextModelOptions.StringRebuilderMaxCharactersToConsolidate) &&
         (left.LineBreakCount + right.LineBreakCount <= TextModelOptions.StringRebuilderMaxLinesToConsolidate))
     {
         //Consolidate the two rebuilders into a single simple string rebuilder
         return(SimpleStringRebuilder.Create(left, right));
     }
     else
     {
         return(BinaryStringRebuilder.BalanceTreeNode(left, right));
     }
 }
コード例 #4
0
 private static StringRebuilder BalanceTreeNode(StringRebuilder left, StringRebuilder right)
 {
     if (left.Depth > right.Depth + 1)
     {
         return(BinaryStringRebuilder.Pivot(left, right, false));
     }
     else if (right.Depth > left.Depth + 1)
     {
         return(BinaryStringRebuilder.Pivot(right, left, true));
     }
     else
     {
         return(new BinaryStringRebuilder(left, right));
     }
 }
コード例 #5
0
 private StringRebuilder Assemble(Span left, Span right)
 {
     if (left.Length == 0)
     {
         return(this.GetSubText(right));
     }
     else if (right.Length == 0)
     {
         return(this.GetSubText(left));
     }
     else if (left.Length + right.Length == this.Length)
     {
         return(this);
     }
     else
     {
         return(BinaryStringRebuilder.Create(this.GetSubText(left), this.GetSubText(right)));
     }
 }
コード例 #6
0
        public static StringRebuilder Create(StringRebuilder left, StringRebuilder right)
        {
            if (left == null)
            {
                throw new ArgumentNullException("left");
            }
            if (right == null)
            {
                throw new ArgumentNullException("right");
            }

            if (left.Length == 0)
            {
                return(right);
            }
            else if (right.Length == 0)
            {
                return(left);
            }
            else if ((left.Length + right.Length < TextModelOptions.StringRebuilderMaxCharactersToConsolidate) &&
                     (left.LineBreakCount + right.LineBreakCount <= TextModelOptions.StringRebuilderMaxLinesToConsolidate))
            {
                //Consolidate the two rebuilders into a single simple string rebuilder
                return(SimpleStringRebuilder.Create(left, right));
            }
            else if (right.StartsWithNewLine && left.EndsWithReturn)
            {
                //Don't allow a line break to be broken across the seam
                return(BinaryStringRebuilder.Create(BinaryStringRebuilder.Create(left.Substring(new Span(0, left.Length - 1)),
                                                                                 _crlf),
                                                    right.Substring(Span.FromBounds(1, right.Length))));
            }
            else
            {
                return(BinaryStringRebuilder.BalanceStringRebuilder(left, right));
            }
        }
コード例 #7
0
        private static StringRebuilder Pivot(StringRebuilder child, StringRebuilder other, bool deepOnRightSide)
        {
            Debug.Assert(child.Depth > 0);  //child's depth is greater than other's depth.
            StringRebuilder grandchildOutside = child.Child(deepOnRightSide);
            StringRebuilder grandchildInside  = child.Child(!deepOnRightSide);

            if (grandchildOutside.Depth >= grandchildInside.Depth)
            {
                //Simple pivot.
                //From this (case deepOnRightSide)
                //                    this
                //                   /    \
                //                other    child
                //                 ...    /      \
                //                       gcI    gcO
                //                       ...    ...
                //
                //To this:
                //                    child'
                //                   /      \
                //                 this'      gcO
                //                /    \     ...
                //             other   gcI

                StringRebuilder newThis;
                StringRebuilder newChild;
                if (deepOnRightSide)
                {
                    newThis  = BinaryStringRebuilder.ConsolidateOrBalanceTreeNode(other, grandchildInside);
                    newChild = BinaryStringRebuilder.ConsolidateOrBalanceTreeNode(newThis, grandchildOutside);
                }
                else
                {
                    newThis  = BinaryStringRebuilder.ConsolidateOrBalanceTreeNode(grandchildInside, other);
                    newChild = BinaryStringRebuilder.ConsolidateOrBalanceTreeNode(grandchildOutside, newThis);
                }

                return(newChild);
            }
            else
            {
                //Complex pivot.
                //From this (case !deepOnRightSide)
                //                    this
                //                   /    \
                //                other    child
                //                 ...    /      \
                //                       gcI      gcO
                //                     /     \    ...
                //                   ggcI   ggcO
                //                    ...    ...
                //
                //To this:
                //                     gcI'
                //                   /     \
                //                 this'     child'
                //                /    \    /     \
                //              other ggcI ggcO   gcO
                //              ...  ...   ...    ...
                Debug.Assert(grandchildInside.Depth > 0);  //The inside's grandchild depth is > the outside grandchild's.
                StringRebuilder greatgrandchildOutside = grandchildInside.Child(deepOnRightSide);
                StringRebuilder greatgrandchildInside  = grandchildInside.Child(!deepOnRightSide);

                StringRebuilder newThis;
                StringRebuilder newChild;
                StringRebuilder newGcI;

                if (deepOnRightSide)
                {
                    newThis  = BinaryStringRebuilder.ConsolidateOrBalanceTreeNode(other, greatgrandchildInside);
                    newChild = BinaryStringRebuilder.ConsolidateOrBalanceTreeNode(greatgrandchildOutside, grandchildOutside);
                    newGcI   = BinaryStringRebuilder.ConsolidateOrBalanceTreeNode(newThis, newChild);
                }
                else
                {
                    newThis  = BinaryStringRebuilder.ConsolidateOrBalanceTreeNode(greatgrandchildInside, other);
                    newChild = BinaryStringRebuilder.ConsolidateOrBalanceTreeNode(grandchildOutside, greatgrandchildOutside);
                    newGcI   = BinaryStringRebuilder.ConsolidateOrBalanceTreeNode(newChild, newThis);
                }

                return(newGcI);
            }
        }
コード例 #8
0
 private static StringRebuilder BalanceStringRebuilder(StringRebuilder left, StringRebuilder right)
 {
     return(BinaryStringRebuilder.BalanceTreeNode(left, right));
 }