]> source.dussan.org Git - poi.git/commitdiff
[bug-65576] replace StringCodepointsIterable
authorPJ Fanning <fanningpj@apache.org>
Thu, 16 Sep 2021 19:26:31 +0000 (19:26 +0000)
committerPJ Fanning <fanningpj@apache.org>
Thu, 16 Sep 2021 19:26:31 +0000 (19:26 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1893385 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SheetDataWriter.java
poi/src/main/java/org/apache/poi/ss/format/CellFormatPart.java
poi/src/main/java/org/apache/poi/util/CodepointsUtil.java [new file with mode: 0644]
poi/src/main/java/org/apache/poi/util/StringCodepointsIterable.java [deleted file]
poi/src/test/java/org/apache/poi/util/TestCodepointsUtil.java [new file with mode: 0644]
poi/src/test/java/org/apache/poi/util/TestStringCodepointsIterable.java [deleted file]

index f72b6a2ff32e1ac6b5f2f6bcd12f192bb78ce5a9..973a93762e30b4c252e261361b1b6f358fcfdd7a 100644 (file)
@@ -39,7 +39,7 @@ import org.apache.poi.ss.usermodel.CellStyle;
 import org.apache.poi.ss.usermodel.CellType;
 import org.apache.poi.ss.usermodel.FormulaError;
 import org.apache.poi.ss.util.CellReference;
-import org.apache.poi.util.StringCodepointsIterable;
+import org.apache.poi.util.CodepointsUtil;
 import org.apache.poi.util.TempFile;
 import org.apache.poi.xssf.model.SharedStringsTable;
 import org.apache.poi.xssf.usermodel.XSSFRichTextString;
@@ -393,7 +393,8 @@ public class SheetDataWriter implements Closeable {
             return;
         }
 
-        for (String codepoint : new StringCodepointsIterable(s)) {
+        for (Iterator<String> iter = CodepointsUtil.iteratorFor(s); iter.hasNext(); ) {
+            String codepoint = iter.next();
             switch (codepoint) {
                 case "<":
                     _out.write("&lt;");
index 9a476d503d38b2b45589e112a0aec5dc9e00967e..abb99782429deba4786b68e7e995007188c93354 100644 (file)
@@ -17,9 +17,8 @@
 package org.apache.poi.ss.format;
 
 import org.apache.poi.hssf.util.HSSFColor;
+import org.apache.poi.util.CodepointsUtil;
 import org.apache.poi.util.LocaleUtil;
-import org.apache.poi.util.StringCodepointsIterable;
-import org.apache.poi.util.StringUtil;
 
 import javax.swing.*;
 
@@ -335,7 +334,7 @@ public class CellFormatPart {
         boolean seenZero = false;
         while (m.find()) {
             String repl = m.group(0);
-            Iterator<String> codePoints = new StringCodepointsIterable(repl).iterator();
+            Iterator<String> codePoints = CodepointsUtil.iteratorFor(repl);
             if (codePoints.hasNext()) {
                 String c1 = codePoints.next();
                 String c2 = null;
@@ -403,7 +402,8 @@ public class CellFormatPart {
      */
     static String quoteSpecial(String repl, CellFormatType type) {
         StringBuilder sb = new StringBuilder();
-        Iterator<String> codePoints = new StringCodepointsIterable(repl).iterator();
+        Iterator<String> codePoints = CodepointsUtil.iteratorFor(repl);
+
         while (codePoints.hasNext()) {
             String ch = codePoints.next();
             if ("\'".equals(ch) && type.isSpecial('\'')) {
@@ -567,7 +567,7 @@ public class CellFormatPart {
      */
     static String expandChar(String part) {
         List<String> codePoints = new ArrayList<>();
-        new StringCodepointsIterable(part).iterator().forEachRemaining(codePoints::add);
+        CodepointsUtil.iteratorFor(part).forEachRemaining(codePoints::add);
         if (codePoints.size() < 2) throw new IllegalArgumentException("Expected part string to have at least 2 chars");
         String ch = codePoints.get(1);
         return ch + ch + ch;
diff --git a/poi/src/main/java/org/apache/poi/util/CodepointsUtil.java b/poi/src/main/java/org/apache/poi/util/CodepointsUtil.java
new file mode 100644 (file)
index 0000000..107e374
--- /dev/null
@@ -0,0 +1,29 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.util;
+
+import java.util.Iterator;
+
+@Internal
+public class CodepointsUtil {
+    public static Iterator<String> iteratorFor(String text) {
+        return text.codePoints()
+                .mapToObj(codePoint -> new StringBuilder().appendCodePoint(codePoint).toString())
+                .iterator();
+    }
+}
\ No newline at end of file
diff --git a/poi/src/main/java/org/apache/poi/util/StringCodepointsIterable.java b/poi/src/main/java/org/apache/poi/util/StringCodepointsIterable.java
deleted file mode 100644 (file)
index 5fb8393..0000000
+++ /dev/null
@@ -1,60 +0,0 @@
-/* ====================================================================
-   Licensed to the Apache Software Foundation (ASF) under one or more
-   contributor license agreements.  See the NOTICE file distributed with
-   this work for additional information regarding copyright ownership.
-   The ASF licenses this file to You under the Apache License, Version 2.0
-   (the "License"); you may not use this file except in compliance with
-   the License.  You may obtain a copy of the License at
-
-       http://www.apache.org/licenses/LICENSE-2.0
-
-   Unless required by applicable law or agreed to in writing, software
-   distributed under the License is distributed on an "AS IS" BASIS,
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-   See the License for the specific language governing permissions and
-   limitations under the License.
-==================================================================== */
-
-package org.apache.poi.util;
-
-import java.util.Iterator;
-import java.util.NoSuchElementException;
-
-// based on https://gist.github.com/EmmanuelOga/48df70b27ead4d80234b
-@Internal
-public class StringCodepointsIterable implements Iterable<String> {
-    private class StringCodepointsIterator implements Iterator<String> {
-        private int index = 0;
-
-        @Override
-        public void remove() {
-            throw new UnsupportedOperationException();
-        }
-
-        @Override
-        public boolean hasNext() {
-            return index < StringCodepointsIterable.this.string.length();
-        }
-
-        @Override
-        public String next() {
-            if (!hasNext()) {
-                throw new NoSuchElementException();
-            }
-            int codePoint = StringCodepointsIterable.this.string.codePointAt(index);
-            index += Character.charCount(codePoint);
-            return new String(Character.toChars(codePoint));
-        }
-    }
-
-    private final String string;
-
-    public StringCodepointsIterable(final String string) {
-        this.string = string;
-    }
-
-    @Override
-    public Iterator<String> iterator() {
-        return new StringCodepointsIterator();
-    }
-}
\ No newline at end of file
diff --git a/poi/src/test/java/org/apache/poi/util/TestCodepointsUtil.java b/poi/src/test/java/org/apache/poi/util/TestCodepointsUtil.java
new file mode 100644 (file)
index 0000000..35a05af
--- /dev/null
@@ -0,0 +1,48 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.util;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Unit test for CodepointsUtil
+ */
+class TestCodepointsUtil {
+
+    @Test
+    void testIterator() {
+        final String unicodeSurrogates = "\uD835\uDF4A\uD835\uDF4B\uD835\uDF4C\uD835\uDF4D\uD835\uDF4E"
+                + "abcdef123456";
+        Iterator<String> sci = CodepointsUtil.iteratorFor(unicodeSurrogates);
+        List<String> codePoints = new ArrayList<>();
+        CodepointsUtil.iteratorFor(unicodeSurrogates).forEachRemaining(codePoints::add);
+        assertEquals(17, codePoints.size());
+        for(String point : codePoints){
+            assertTrue(point.length() >=1 && point.length() <= 2, "codepoint " + point + "is wrong size");
+        }
+    }
+
+}
+
diff --git a/poi/src/test/java/org/apache/poi/util/TestStringCodepointsIterable.java b/poi/src/test/java/org/apache/poi/util/TestStringCodepointsIterable.java
deleted file mode 100644 (file)
index 4630a3c..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-/* ====================================================================
-   Licensed to the Apache Software Foundation (ASF) under one or more
-   contributor license agreements.  See the NOTICE file distributed with
-   this work for additional information regarding copyright ownership.
-   The ASF licenses this file to You under the Apache License, Version 2.0
-   (the "License"); you may not use this file except in compliance with
-   the License.  You may obtain a copy of the License at
-
-       http://www.apache.org/licenses/LICENSE-2.0
-
-   Unless required by applicable law or agreed to in writing, software
-   distributed under the License is distributed on an "AS IS" BASIS,
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-   See the License for the specific language governing permissions and
-   limitations under the License.
-==================================================================== */
-
-package org.apache.poi.util;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.junit.jupiter.api.Test;
-
-/**
- * Unit test for StringCodepointsIterable
- */
-class TestStringCodepointsIterable {
-
-    @Test
-    void testIterable() {
-        final String unicodeSurrogates = "\uD835\uDF4A\uD835\uDF4B\uD835\uDF4C\uD835\uDF4D\uD835\uDF4E"
-                + "abcdef123456";
-        StringCodepointsIterable sci = new StringCodepointsIterable(unicodeSurrogates);
-        List<String> codePoints = new ArrayList<>();
-        List<String> codePoints2 = new ArrayList<>();
-        sci.iterator().forEachRemaining(codePoints::add);
-        sci.iterator().forEachRemaining(codePoints2::add);
-        assertEquals(17, codePoints.size());
-        assertEquals(codePoints, codePoints2);
-    }
-
-}
-