From c116c2b2e7301d7ea07a72f9f97331e2c812b69a Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Mon, 22 Dec 2014 14:09:55 +0000 Subject: [PATCH] Remove iterating over the number of runs for certain XSSFRichTextString operations. If I didn't overlook something, they can directly access the array element anyway. Seems this was some leftover copy/paste stuff. git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1647315 13f79535-47bb-0310-9956-ffa450edef68 --- .../xssf/usermodel/XSSFRichTextString.java | 28 +++++++++---------- .../usermodel/TestXSSFRichTextString.java | 23 +++++++++++++-- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java index 89568f0b52..c587950759 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java @@ -279,13 +279,12 @@ public class XSSFRichTextString implements RichTextString { * @return the number of characters this format run covers */ public int getLengthOfFormattingRun(int index) { - if(st.sizeOfRArray() == 0) return length(); - - for(int i = 0; i < st.sizeOfRArray(); i++){ - CTRElt r = st.getRArray(i); - if(i == index) return r.getT().length(); + if(st.sizeOfRArray() == 0 || index >= st.sizeOfRArray()) { + return -1; } - return -1; + + CTRElt r = st.getRArray(index); + return r.getT().length(); } /** @@ -342,16 +341,17 @@ public class XSSFRichTextString implements RichTextString { * @return A copy of the font used or null if no formatting is applied to the specified text run. */ public XSSFFont getFontOfFormattingRun(int index) { - if(st.sizeOfRArray() == 0) return null; + if(st.sizeOfRArray() == 0 || index >= st.sizeOfRArray()) { + return null; + } - for(int i = 0; i < st.sizeOfRArray(); i++){ - CTRElt r = st.getRArray(i); - if(i == index && r.getRPr() != null) { - XSSFFont fnt = new XSSFFont(toCTFont(r.getRPr())); - fnt.setThemesTable(getThemesTable()); - return fnt; - } + CTRElt r = st.getRArray(index); + if(r.getRPr() != null) { + XSSFFont fnt = new XSSFFont(toCTFont(r.getRPr())); + fnt.setThemesTable(getThemesTable()); + return fnt; } + return null; } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java index 6d053bea13..3daa570b92 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java @@ -400,18 +400,35 @@ public final class TestXSSFRichTextString extends TestCase { Row row = sheet.getRow(0); // verify the values to ensure future changes keep the returned information equal - assertEquals(0, row.getCell(0).getRichStringCellValue().numFormattingRuns()); - assertEquals(0, row.getCell(1).getRichStringCellValue().numFormattingRuns()); + XSSFRichTextString rt = (XSSFRichTextString) row.getCell(0).getRichStringCellValue(); + assertEquals(0, rt.numFormattingRuns()); + assertNull(rt.getFontOfFormattingRun(0)); + assertEquals(-1, rt.getLengthOfFormattingRun(0)); - XSSFRichTextString rt = (XSSFRichTextString) row.getCell(2).getRichStringCellValue(); + rt = (XSSFRichTextString) row.getCell(1).getRichStringCellValue(); + assertEquals(0, row.getCell(1).getRichStringCellValue().numFormattingRuns()); + assertNull(rt.getFontOfFormattingRun(1)); + assertEquals(-1, rt.getLengthOfFormattingRun(1)); + + rt = (XSSFRichTextString) row.getCell(2).getRichStringCellValue(); assertEquals(2, rt.numFormattingRuns()); assertNotNull(rt.getFontOfFormattingRun(0)); + assertEquals(4, rt.getLengthOfFormattingRun(0)); + assertNotNull(rt.getFontOfFormattingRun(1)); + assertEquals(9, rt.getLengthOfFormattingRun(1)); + + assertNull(rt.getFontOfFormattingRun(2)); rt = (XSSFRichTextString) row.getCell(3).getRichStringCellValue(); assertEquals(3, rt.numFormattingRuns()); assertNull(rt.getFontOfFormattingRun(0)); + assertEquals(1, rt.getLengthOfFormattingRun(0)); + assertNotNull(rt.getFontOfFormattingRun(1)); + assertEquals(3, rt.getLengthOfFormattingRun(1)); + assertNotNull(rt.getFontOfFormattingRun(2)); + assertEquals(9, rt.getLengthOfFormattingRun(2)); } } -- 2.39.5