git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1893385 13f79535-47bb-0310-9956-ffa450edef68tags/REL_5_2_0
@@ -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("<"); |
@@ -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; |
@@ -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(); | |||
} | |||
} |
@@ -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(); | |||
} | |||
} |
@@ -18,28 +18,30 @@ | |||
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 StringCodepointsIterable | |||
* Unit test for CodepointsUtil | |||
*/ | |||
class TestStringCodepointsIterable { | |||
class TestCodepointsUtil { | |||
@Test | |||
void testIterable() { | |||
void testIterator() { | |||
final String unicodeSurrogates = "\uD835\uDF4A\uD835\uDF4B\uD835\uDF4C\uD835\uDF4D\uD835\uDF4E" | |||
+ "abcdef123456"; | |||
StringCodepointsIterable sci = new StringCodepointsIterable(unicodeSurrogates); | |||
Iterator<String> sci = CodepointsUtil.iteratorFor(unicodeSurrogates); | |||
List<String> codePoints = new ArrayList<>(); | |||
List<String> codePoints2 = new ArrayList<>(); | |||
sci.iterator().forEachRemaining(codePoints::add); | |||
sci.iterator().forEachRemaining(codePoints2::add); | |||
CodepointsUtil.iteratorFor(unicodeSurrogates).forEachRemaining(codePoints::add); | |||
assertEquals(17, codePoints.size()); | |||
assertEquals(codePoints, codePoints2); | |||
for(String point : codePoints){ | |||
assertTrue(point.length() >=1 && point.length() <= 2, "codepoint " + point + "is wrong size"); | |||
} | |||
} | |||
} |