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;
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("<");
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.*;
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;
*/
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('\'')) {
*/
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;
--- /dev/null
+/* ====================================================================
+ 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
+++ /dev/null
-/* ====================================================================
- 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
--- /dev/null
+/* ====================================================================
+ 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");
+ }
+ }
+
+}
+
+++ /dev/null
-/* ====================================================================
- 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);
- }
-
-}
-