Compare two paragraphs

  • I am appending one WPRichText to another in a merge kind of operation, and I was wanting to delete any leading paragraphs of the slave RichText that are already in the Master RichText because they are both initialized with the same leading text. By merging the two as-is, I would end up with duplicate leading text wherever the slave was appended to the master RichText.

    My thought was to loop thru the leading paragraphs of both RichTexts and compare them. If they were the same, I knew I could delete that paragraph from the slave. I was hoping the ComparePar would handle any text objects as well, because I might have a paragraph that just contains an image or linked image, so I want to be sure it compares this as well.

    While looking at the code for TParagraph.ComparePar to see what was being compared, I am slightly confused, well, maybe heavily confused. Why are these two statements doing the exact same thing?

    while (i<CharCount) and ((CharItem[i]=#9) or (CharItem[i]=#32)) do inc(i);

    while (j<CharCount) and ((CharItem[j]=#9) or (CharItem[j]=#32)) do inc(j);


    Instead of the 2nd line, you could effectively just say j := i; So, is the 2nd line supposed to be the following?

    while (j<SecondPar.CharCount) and ((SecondPar.CharItem[j]=#9) or (SecondPar.CharItem[j]=#32)) do inc(j);


    In any event, will ComparePar do what I need in regards to comparing text and text objects in a paragraph? If not, is there another method or do I need to write my own?

    Thanks!

    Eric

    • Offizieller Beitrag

    The code originally was intended to be inside the loop to skip spaces, this is why the lines are duplicated.

    To solve your problem I would create an ID for each paragraph when it is appended. So you can skip the paragraphs which have been appended before.

    As ID you could use the name property of a paragraph or par.ASet(WPAT_USER, id) (which is not saved to RTF, btw).

    Alternatively you could also use hidden text.

  • In the repeat loop you do this:

    if (i = CharCount) and (j = SecondPar.CharCount) then

    which is why I thought j in that 2nd line's while statement should be looping thru SecondPar.CharItem for skipping over SecondPar's white space.

    Anyway, using a paragraph ID is not good enough because users can still edit those paragraphs, and they might even edit them the same way or perform multiple merges, so I really need to look at the paragraph's content. For context, an inspection company might use multiple inspectors to inspect a property, so they will all need to merge their findings into one master report. In our previous version of our app, we received a lot of complaints that leading unmodified "template" text was duplicated, so I was looking at ways to avoid this issue. "Template" text is initial text pre-filled into a comment area, but could later be edited and added to in some cases.

    Using your ComparePar seems to do the job with initial tests, so I'll test other scenarios. If it doesn't quite work like I need it, at least I have something to work with to create my own. Just wanted to point out those two lines of code which seemed questionable at first glance.

  • This is a snipit of my code which might help explain the simple merge process. It seems to work as I mentioned, but more testing needed.

    Einmal editiert, zuletzt von ehimmer (11. März 2020 um 17:03)