[bug-65230] TRIM function should trim extra spaces between words

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1891851 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2021-07-28 13:17:40 +00:00
parent 3aa03459a7
commit 4de25b13a7
2 changed files with 10 additions and 3 deletions

View File

@ -148,13 +148,14 @@ public abstract class TextFunction implements Function {
/**
* An implementation of the TRIM function:
* Removes leading and trailing spaces from value if evaluated operand
* value is string.
* value is string. Since POI 5.0.1, this also trims double spaces so that only 1
* is kept (https://bz.apache.org/bugzilla/show_bug.cgi?id=65230).
* Author: Manda Wilson < wilson at c bio dot msk cc dot org >
*/
public static final Function TRIM = new SingleArgTextFunc() {
@Override
protected ValueEval evaluate(String arg) {
return new StringEval(arg.trim());
return new StringEval(arg.trim().replaceAll(" +", " "));
}
};

View File

@ -52,7 +52,6 @@ final class TestTrim {
@Test
void testBasic() {
confirmTrim(new StringEval(" hi "), "hi");
confirmTrim(new StringEval("hi "), "hi");
confirmTrim(new StringEval(" hi"), "hi");
@ -61,6 +60,13 @@ final class TestTrim {
confirmTrim(new StringEval(" "), "");
}
@Test
void testExtraSpaces() {
//https://bz.apache.org/bugzilla/show_bug.cgi?id=65230
confirmTrim(new StringEval(" hi there "), "hi there");
confirmTrim(new StringEval("hi there"), "hi there");
}
/**
* Valid cases where text arg is not exactly a string
*/