Bugs found and solved (paragraph start and end index


In this great code errors are unavoidable I'm happy that I can help to detect and solve them.

I found out that the paragraph startindex and endindex are corrupted some how.
This result in a a strange behaviour of repacing text and inserting a table on wrong places.
I found the following

in the file container.cs function internal List<Paragraph> GetParagraphs(bool deepSearch=false)
the line: _index += HelperFunctions.GetText(e).Length; _is on the wrong place.
this must change to
            foreach (XElement e in Xml.Descendants(XName.Get("p", DocX.w.NamespaceName)))
                Paragraph paragraph = new Paragraph(Document, e, index);
                index += HelperFunctions.GetText(e).Length;
the function public virtual List<int> FindAll(string str, RegexOptions options) has a typo error I think
the part: indexes[0] += p.startIndex; should be: indexes[i] += p.startIndex;
public virtual List<int> FindAll(string str, RegexOptions options)
            List<int> list = new List<int>();

            foreach (Paragraph p in Paragraphs)
                List<int> indexes = p.FindAll(str, options);

                for (int i = 0; i < indexes.Count(); i++)
                    indexes[i] += p.startIndex;


            return list;
And I change the function GetFirstParagraphEffectedByInsert(DocX document, int index) in the file HelperFunctions.cs to the following
    internal static Paragraph GetFirstParagraphEffectedByInsert(DocX document, int index)
            // This document contains no Paragraphs and insertion is at index 0

            if (document.Paragraphs.Count() == 0 && index == 0)
                return null;

            //if (document.paragraphLookup.Keys.Count() == 0 && index == 0)
            //    return null;

            foreach (Paragraph p in document.Paragraphs)
                if (p.endIndex >= index)
                    return p;

            //foreach (int paragraphEndIndex in document.paragraphLookup.Keys)
            //    if (paragraphEndIndex >= index)
            //        return document.paragraphLookup[paragraphEndIndex];

            throw new ArgumentOutOfRangeException();
It seems the insert and replace text and tables are working now.


MadBoy wrote Dec 6, 2016 at 5:43 PM

Can you please commit this on github as pull request?