Bug 57727 - XWPFParagraph.searchText may return invalid result when searched string is in more runs
Summary: XWPFParagraph.searchText may return invalid result when searched string is in...
Status: NEW
Alias: None
Product: POI
Classification: Unclassified
Component: XWPF (show other bugs)
Version: 3.12-dev
Hardware: PC Windows NT
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-03-19 13:54 UTC by Vladimir Glasnak
Modified: 2018-05-28 06:48 UTC (History)
1 user (show)



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vladimir Glasnak 2015-03-19 13:54:59 UTC
Run 0
TextPositon 0:

"some text"

Run 0
TextPosition 1:

"SEARCH"

Run 1
TextPosition 0:
"ED"

calling sarchText("SEARCHED", new PositionInParagraph()) returns TextSegment with beginTextPosition == 0. because in every new run it reinitializes to 0 (XWPFParagraph.searchText source code):


        for (int runPos=startRun; runPos<rArray.length; runPos++) {
            int beginTextPos = 0,beginCharPos = 0, textPos = 0,  charPos = 0;    

Similarly for beginCharPos, both should be initialized with beginRunPos above the cycle.
Comment 1 Dominik Stadler 2016-10-20 18:46:08 UTC
Sorry for the late response on this one, but it would make it easier to reproduce this if you can provide both a sample file and a self-contained unit-test, that would help a lot!
Comment 2 Florian Sommer 2018-05-27 10:29:31 UTC
Hi, I created a testcase and a fixed version of the algorithm for this bug:

https://github.com/fsomme2s/apache-poi-bug57727

Go to test class "BeginCharTest".

Note: In this repo, I also demonstrate and fix a second bug in a second test case. The bug is that the searchText() method returns null (= not found) although the searched string is part of the paragraph.
I will create a second bug for this one.
Comment 3 PJ Fanning 2018-05-27 12:41:17 UTC
Could you do a pull request in https://github.com/apache/poi with the TODOs in your tests resolved?
Comment 4 Florian Sommer 2018-05-28 06:48:05 UTC
Sorry I still have a bug in my "fix"... I work on it, when I'm done with everything I'll create the PR!