Author: maxcom
Date: Tue Jul  6 15:45:36 2010
New Revision: 960922

URL: http://svn.apache.org/viewvc?rev=960922&view=rev
Log:
HWPF: better fix for TextPieceTable.getCharIndex()

Modified:
    poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/TextPieceTable.java

Modified: 
poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/TextPieceTable.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/TextPieceTable.java?rev=960922&r1=960921&r2=960922&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/TextPieceTable.java 
(original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/TextPieceTable.java 
Tue Jul  6 15:45:36 2010
@@ -242,29 +242,50 @@ public final class TextPieceTable implem
                return false;
        }
 
-       public int getCharIndex(int bytePos) {
-               int charCount = 0;
+    public int getCharIndex(int bytePos) {
+        int charCount = 0;
 
         for(TextPiece tp : _textPiecesFCOrder) {
                        int pieceStart = 
tp.getPieceDescriptor().getFilePosition();
-                       if (pieceStart >= bytePos) {
-                               break;
+
+            if (bytePos > pieceStart + tp.bytesLength()) {
+                continue;
+            }
+
+                       if (pieceStart > bytePos) {
+                               bytePos = pieceStart;
                        }
 
-                       int bytesLength = tp.bytesLength();
-                       int pieceEnd = pieceStart + bytesLength;
+            break;
+        }
 
-                       int toAdd = bytePos > pieceEnd ? bytesLength : 
bytesLength - (pieceEnd - bytePos);
+        for(TextPiece tp : _textPieces) {
+            int pieceStart = tp.getPieceDescriptor().getFilePosition();
 
-                       if (tp.isUnicode()) {
-                               charCount += toAdd / 2;
-                       } else {
-                               charCount += toAdd;
-                       }
-               }
+            int bytesLength = tp.bytesLength();
+            int pieceEnd = pieceStart + bytesLength;
 
-               return charCount;
-       }
+            int toAdd;
+
+            if (bytePos< pieceStart || bytePos > pieceEnd) {
+                toAdd = bytesLength;
+            } else {
+                toAdd = bytesLength - (pieceEnd - bytePos);
+            }
+
+            if (tp.isUnicode()) {
+                charCount += toAdd / 2;
+            } else {
+                charCount += toAdd;
+            }
+
+            if (bytePos>=pieceStart && bytePos<=pieceEnd) {
+                break;
+            }
+        }
+
+        return charCount;
+    }
 
     private static class FCComparator implements Comparator<TextPiece> {
         public int compare(TextPiece textPiece, TextPiece textPiece1) {



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to