]> source.dussan.org Git - poi.git/commitdiff
Simplify some string operations
authorDominik Stadler <centic@apache.org>
Wed, 26 Dec 2018 13:27:35 +0000 (13:27 +0000)
committerDominik Stadler <centic@apache.org>
Wed, 26 Dec 2018 13:27:35 +0000 (13:27 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1849760 13f79535-47bb-0310-9956-ffa450edef68

15 files changed:
src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java
src/java/org/apache/poi/hssf/record/HyperlinkRecord.java
src/java/org/apache/poi/poifs/macros/VBAMacroExtractor.java
src/java/org/apache/poi/ss/formula/functions/Dec2Bin.java
src/java/org/apache/poi/ss/formula/functions/Substitute.java
src/java/org/apache/poi/ss/usermodel/DataFormatter.java
src/ooxml/java/org/apache/poi/openxml4j/opc/PackagingURIHelper.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
src/scratchpad/src/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java
src/scratchpad/src/org/apache/poi/hpbf/dev/HPBFDumper.java
src/scratchpad/src/org/apache/poi/hpbf/dev/PLCDumper.java
src/scratchpad/src/org/apache/poi/hwpf/extractor/WordExtractor.java
src/testcases/org/apache/poi/hssf/usermodel/TestDataValidation.java
src/testcases/org/apache/poi/ss/formula/function/ExcelFileFormatDocFunctionExtractor.java

index 5f7b973e797ad9e921c0dc6c60ce99c76477533e..cbb7990c5dc0f52943b99b157d8f3921a1776841 100644 (file)
@@ -72,7 +72,7 @@ public class FromHowTo {
                                        InputSource sheetSource = new InputSource(sheet);
                                        parser.parse(sheetSource);
                                }
-                System.out.println("");
+                System.out.println();
             }
         }
     }
index f3999b1bcd4fd60259c719081991a0c9e431f8d9..7f8b345275ff1cffc7a6fe3a6d059f79261244fb 100644 (file)
@@ -130,7 +130,7 @@ public final class HyperlinkRecord extends StandardRecord implements Cloneable {
                        sb.append(HexDump.shortToHex(_d3).substring(PREFIX_LEN));
                        sb.append("-");
                        String d4Chars = HexDump.longToHex(getD4());
-                       sb.append(d4Chars.substring(PREFIX_LEN, PREFIX_LEN+4));
+                       sb.append(d4Chars, PREFIX_LEN, PREFIX_LEN+4);
                        sb.append("-");
                        sb.append(d4Chars.substring(PREFIX_LEN+4));
                        return sb.toString();
index e5ffbb3ada98ded1ca2d096cfa1e400908f86841..57a7b1486fc6f7ca76c1b550f122b14475110f59 100644 (file)
@@ -38,7 +38,7 @@ public class VBAMacroExtractor {
         if (args.length == 0) {
             System.err.println("Use:");
             System.err.println("   VBAMacroExtractor <office.doc> [output]");
-            System.err.println("");
+            System.err.println();
             System.err.println("If an output directory is given, macros are written there");
             System.err.println("Otherwise they are output to the screen");
             System.exit(1);
@@ -90,7 +90,7 @@ public class VBAMacroExtractor {
             if (outputDir == null) {
                 System.out.println(divider);
                 System.out.println(moduleName);
-                System.out.println("");
+                System.out.println();
                 System.out.println(moduleCode);
             } else {
                 File out = new File(outputDir, moduleName + extension);
index 1e0983b86734af7ea0454354d51f2c9857fa8b56..45c387e91525af62e52db8ac2be66f62bc8dc6b9 100644 (file)
@@ -104,7 +104,7 @@ public class Dec2Bin extends Var1or2ArgFunction implements FreeRefFunction {
         String binary = Integer.toBinaryString(number.intValue());
 
         if (binary.length() > DEFAULT_PLACES_VALUE) {
-            binary = binary.substring(binary.length() - DEFAULT_PLACES_VALUE, binary.length());
+            binary = binary.substring(binary.length() - DEFAULT_PLACES_VALUE);
         }
         //If DEC2BIN requires more than places characters, it returns the #NUM! error value.
         if (binary.length() > placesNumber) {
index 421e8888b9c17e389f90dbc8872167e6f36c8c67..e6b55c0d1268df6e51eeab079191c6b148661feb 100644 (file)
@@ -75,7 +75,7 @@ public final class Substitute extends Var3or4ArgFunction {
                                return sb.toString();
                        }
                        // store everything from end of last match to start of this match
-                       sb.append(oldStr.substring(startIndex, nextMatch));
+                       sb.append(oldStr, startIndex, nextMatch);
                        sb.append(newStr);
                        startIndex = nextMatch + searchStr.length();
                }
@@ -97,7 +97,7 @@ public final class Substitute extends Var3or4ArgFunction {
                        count++;
                        if (count == instanceNumber) {
                                StringBuffer sb = new StringBuffer(oldStr.length() + newStr.length());
-                               sb.append(oldStr.substring(0, nextMatch));
+                               sb.append(oldStr, 0, nextMatch);
                                sb.append(newStr);
                                sb.append(oldStr.substring(nextMatch + searchStr.length()));
                                return sb.toString();
index b805d0cc856194fb486d751e0c8a899ba4054818..9c5cb465a59f8740bb68e1fff55b64b52c3b1df9 100644 (file)
@@ -145,20 +145,20 @@ public class DataFormatter implements Observer {
      *  Magenta, Red, White, Yellow, "Color n" (1<=n<=56)
      */
     private static final Pattern colorPattern = 
-       Pattern.compile("(\\[BLACK\\])|(\\[BLUE\\])|(\\[CYAN\\])|(\\[GREEN\\])|" +
-                       "(\\[MAGENTA\\])|(\\[RED\\])|(\\[WHITE\\])|(\\[YELLOW\\])|" +
-                       "(\\[COLOR\\s*\\d\\])|(\\[COLOR\\s*[0-5]\\d\\])", Pattern.CASE_INSENSITIVE);
+       Pattern.compile("(\\[BLACK])|(\\[BLUE])|(\\[CYAN])|(\\[GREEN])|" +
+                       "(\\[MAGENTA])|(\\[RED])|(\\[WHITE])|(\\[YELLOW])|" +
+                       "(\\[COLOR\\s*\\d])|(\\[COLOR\\s*[0-5]\\d])", Pattern.CASE_INSENSITIVE);
 
     /**
      * A regex to identify a fraction pattern.
      * This requires that replaceAll("\\?", "#") has already been called 
      */
-    private static final Pattern fractionPattern = Pattern.compile("(?:([#\\d]+)\\s+)?(#+)\\s*\\/\\s*([#\\d]+)");
+    private static final Pattern fractionPattern = Pattern.compile("(?:([#\\d]+)\\s+)?(#+)\\s*/\\s*([#\\d]+)");
 
     /**
      * A regex to strip junk out of fraction formats
      */
-    private static final Pattern fractionStripper = Pattern.compile("(\"[^\"]*\")|([^ \\?#\\d\\/]+)");
+    private static final Pattern fractionStripper = Pattern.compile("(\"[^\"]*\")|([^ ?#\\d/]+)");
 
     /**
      * A regex to detect if an alternate grouping character is used
@@ -413,7 +413,7 @@ public class DataFormatter implements Observer {
             if (symbol.indexOf('$') > -1) {
                 symbol = symbol.substring(0, symbol.indexOf('$')) +
                         '\\' +
-                        symbol.substring(symbol.indexOf('$'), symbol.length());
+                        symbol.substring(symbol.indexOf('$'));
             }
             formatStr = m.replaceAll(symbol);
             m = localePatternGroup.matcher(formatStr);
index 83f37ef91853a3a05ced4ec857bbda4392024c91..5a8db38ff0a79b1c72ae9829eff74dbc51f20a94 100644 (file)
@@ -349,9 +349,7 @@ public final class PackagingURIHelper {
                 // "/ppt/slides/slide1.xml" and the targetURI is "slide1.xml" then
                 // this it should be relativized as "slide1.xml", i.e. the last segment.
                 retVal.append(segmentsSource[segmentsSource.length - 1]);
-            } else {
-                retVal.append("");
-            }
+                       }
 
                } else {
                        // Matched for so long, but no more
index 53eb543cadbeeab74b2353f016a4f35b1c245a87..fbc06a97093d691760b2be2c66a72884aae67f0b 100644 (file)
@@ -508,7 +508,7 @@ public class XSSFRichTextString implements RichTextString {
         while(m.find()) {
             int pos = m.start();
             if( pos > idx) {
-                buf.append(value.substring(idx, pos));
+                buf.append(value, idx, pos);
             }
 
             String code = m.group(1);
index 41ad4222fb9882037955a69bca66afe5a9a78b69..6f4fb972a9b13c00536470f98a1387e6b488b27d 100644 (file)
@@ -1569,7 +1569,7 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para
                 if ((j == textEnd) && (i == runEnd)) {
                     endChar = charEnd;
                 }
-                out.append(tmpText.substring(startChar, endChar + 1));
+                out.append(tmpText, startChar, endChar + 1);
             }
         }
         return out.toString();
index e945cbae4b003000c3072f334f9773184f68c31c..0abcd771e9c72e5ce45958b2e449f472dad88161 100644 (file)
@@ -45,8 +45,8 @@ public final class HMEFContentsExtractor {
         if(args.length < 2) {
             System.err.println("Use:");
             System.err.println("  HMEFContentsExtractor <filename> <output dir>");
-            System.err.println("");
-            System.err.println("");
+            System.err.println();
+            System.err.println();
             System.err.println("Where <filename> is the winmail.dat file to extract,");
             System.err.println(" and <output dir> is where to place the extracted files");
             System.exit(2);
index 71d34bbbb87cc6dcc74ebc9a075f7d0fdb2df44f..0a96a54843fc3c520695852ce41235a2b3540641 100644 (file)
@@ -124,14 +124,14 @@ public final class HPBFDumper {
        }
        protected void dumpEscherStm(DirectoryNode escherDir) throws IOException {
                byte[] data = getData(escherDir, "EscherStm");
-               System.out.println("");
+               System.out.println();
                System.out.println("EscherStm - " + data.length + " bytes long:");
                if(data.length > 0)
                        dumpEscherStream(data);
        }
        protected void dumpEscherDelayStm(DirectoryNode escherDir) throws IOException {
                byte[] data = getData(escherDir, "EscherDelayStm");
-               System.out.println("");
+               System.out.println();
                System.out.println("EscherDelayStm - " + data.length + " bytes long:");
                if(data.length > 0)
                        dumpEscherStream(data);
@@ -140,14 +140,14 @@ public final class HPBFDumper {
        public void dumpEnvelope() throws IOException {
                byte[] data = getData(fs.getRoot(), "Envelope");
 
-               System.out.println("");
+               System.out.println();
                System.out.println("Envelope - " + data.length + " bytes long:");
        }
 
        public void dumpContents() throws IOException {
                byte[] data = getData(fs.getRoot(), "Contents");
 
-               System.out.println("");
+               System.out.println();
                System.out.println("Contents - " + data.length + " bytes long:");
 
                // 8 bytes, always seems to be
@@ -179,7 +179,7 @@ public final class HPBFDumper {
        public void dumpCONTENTSraw(DirectoryNode dir) throws IOException {
                byte[] data = getData(dir, "CONTENTS");
 
-               System.out.println("");
+               System.out.println();
                System.out.println("CONTENTS - " + data.length + " bytes long:");
 
                // Between the start and 0x200 we have
@@ -236,7 +236,7 @@ public final class HPBFDumper {
                }
                if(textStop > 0) {
                        int len = (textStop - 0x200) / 2;
-                       System.out.println("");
+                       System.out.println();
                        System.out.println(
                                        StringUtil.getFromUnicodeLE(data, 0x200, len)
                        );
@@ -250,7 +250,7 @@ public final class HPBFDumper {
        public void dumpCONTENTSguessed(DirectoryNode dir) throws IOException {
                byte[] data = getData(dir, "CONTENTS");
 
-               System.out.println("");
+               System.out.println();
                System.out.println("CONTENTS - " + data.length + " bytes long:");
 
                String[] startType = new String[20];
@@ -313,10 +313,10 @@ public final class HPBFDumper {
                }
 
                // Text
-               System.out.println("");
+               System.out.println();
                System.out.println("TEXT:");
                System.out.println(text);
-               System.out.println("");
+               System.out.println();
 
                // All the others
                for(int i=0; i<20; i++) {
index c6bfca93b9eadacfe21f7d64bdab31cf07c93b56..0d9d9ae71ddc57d85cc9365ee3ca208d3c51596e 100644 (file)
@@ -73,7 +73,7 @@ public final class PLCDumper {
        }
 
        private void dumpBit(QCBit bit, int index) {
-               System.out.println("");
+               System.out.println();
                System.out.println("Dumping " + bit.getBitType() + " bit at " + index);
                System.out.println("  Is a " + bit.getThingType() + ", number is " + bit.getOptA());
                System.out.println("  Starts at " + bit.getDataOffset() + " (0x" + Integer.toHexString(bit.getDataOffset()) + ")");
index 0fd7a8c24a06b147c1600c3fc44a8d7f67021cfd..eadc454d8b3215f041141ed06bb1116ce742699c 100644 (file)
@@ -90,11 +90,8 @@ public final class WordExtractor extends POIOLE2TextExtractor {
 
         // Process the first argument as a file
         InputStream fin = new FileInputStream( args[0] );
-        WordExtractor extractor = new WordExtractor( fin );
-        try {
-               System.out.println( extractor.getText() );
-        } finally {
-               extractor.close();
+        try (WordExtractor extractor = new WordExtractor(fin)) {
+            System.out.println(extractor.getText());
         }
     }
 
@@ -175,7 +172,7 @@ public final class WordExtractor extends POIOLE2TextExtractor {
         }
         if ( text.endsWith( "\n\n" ))
         {
-            out.append( text.substring( 0, text.length() - 1 ));
+            out.append(text, 0, text.length() - 1);
             return;
         }
         out.append( text );
index a440a39def36ab29d67879a47edf61e6f69c96b7..13b7895ed28e6737b3365ce405683f6bed415d12 100644 (file)
@@ -98,7 +98,7 @@ public final class TestDataValidation extends BaseTestDataValidation {
                                + "make the fix elsewhere (do not modify this test or the proof spreadsheet to get the test working).");
                ps.println("If the changes were wanted, make sure to open the newly generated file in Excel "
                                + "and verify it manually.  The new proof file should be submitted after it is verified to be correct.");
-               ps.println("");
+               ps.println();
                ps.println("One other possible (but less likely) cause of a failed test is a problem in the "
                                + "comparison logic used here. Perhaps some extra file regions need to be ignored.");
                ps.println("The generated file has been saved to '" + generatedFile.getAbsolutePath() + "' for manual inspection.");
index 8da0aded7e1125a3fffc301a23aa2dac7fb0ef29..173ccb7d9cc6288f08b490b585212918ffb58e01 100644 (file)
@@ -514,7 +514,7 @@ public final class ExcelFileFormatDocFunctionExtractor {
                ps.println(" (size=" + effDocFile.length() + ", md5=" + getFileMD5(effDocFile) + ")");
                ps.println("#");
                ps.println("#Columns: (index, name, minParams, maxParams, returnClass, paramClasses, isVolatile, hasFootnote )");
-               ps.println("");
+               ps.println();
                try {
                    // can't use ZipHelper here, because its in a different module
                        ZipFile zf = new ZipFile(effDocFile);