From d406b402dc22b8753a87e637b40ef0e26197ceaa Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Wed, 30 Dec 2020 17:03:27 +0000 Subject: [PATCH] Slightly re-arrange code to only perform computations if we do not exit the loop anyway Also simplify code somewhat git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1884949 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hwpf/model/TextPieceTable.java | 32 +++++++------------ 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hwpf/model/TextPieceTable.java b/src/scratchpad/src/org/apache/poi/hwpf/model/TextPieceTable.java index 8649f06a3f..2da35b30ba 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/model/TextPieceTable.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/model/TextPieceTable.java @@ -241,17 +241,16 @@ public class TextPieceTable implements CharIndexTranslator { for (TextPiece textPiece : _textPiecesFCOrder) { final int tpStart = textPiece.getPieceDescriptor() .getFilePosition(); + if (endBytePosExclusive <= tpStart) + break; + final int tpEnd = textPiece.getPieceDescriptor().getFilePosition() + textPiece.bytesLength(); if (startBytePosInclusive > tpEnd) continue; - if (endBytePosExclusive <= tpStart) - break; - final int rangeStartBytes = Math.max(tpStart, - startBytePosInclusive); + final int rangeStartBytes = Math.max(tpStart, startBytePosInclusive); final int rangeEndBytes = Math.min(tpEnd, endBytePosExclusive); - final int rangeLengthBytes = rangeEndBytes - rangeStartBytes; if (rangeStartBytes > rangeEndBytes) continue; @@ -260,6 +259,7 @@ public class TextPieceTable implements CharIndexTranslator { final int rangeStartCp = textPiece.getStart() + (rangeStartBytes - tpStart) / encodingMultiplier; + final int rangeLengthBytes = rangeEndBytes - rangeStartBytes; final int rangeEndCp = rangeStartCp + rangeLengthBytes / encodingMultiplier; @@ -290,12 +290,11 @@ public class TextPieceTable implements CharIndexTranslator { logger.log( POILogger.WARN, "Text piece has boundaries [", - Integer.valueOf(textPiece.getStart()), + textPiece.getStart(), "; ", - Integer.valueOf(textPiece.getEnd()), + textPiece.getEnd(), ") but length ", - Integer.valueOf(textPiece.getEnd() - - textPiece.getStart())); + textPiece.getEnd() - textPiece.getStart()); } docText.replace(textPiece.getStart(), textPiece.getStart() @@ -303,8 +302,8 @@ public class TextPieceTable implements CharIndexTranslator { } logger.log(POILogger.DEBUG, "Document text were rebuilded in ", - Long.valueOf(System.currentTimeMillis() - start), " ms (", - Integer.valueOf(docText.length()), " chars)"); + System.currentTimeMillis() - start, " ms (", + docText.length(), " chars)"); return docText; } @@ -326,11 +325,7 @@ public class TextPieceTable implements CharIndexTranslator { continue; } - if (pieceStart > bytePos) { - return false; - } - - return true; + return pieceStart <= bytePos; } return false; @@ -347,10 +342,7 @@ public class TextPieceTable implements CharIndexTranslator { int left = Math.max(startBytePos, pieceStart); int right = Math.min(endBytePos, pieceStart + tp.bytesLength()); - if (left >= right) - return false; - - return true; + return left < right; } return false; -- 2.39.5