From 4de25b13a7e890fe5699d4b282d93b8ef18095f7 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Wed, 28 Jul 2021 13:17:40 +0000 Subject: [PATCH] [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 --- .../org/apache/poi/ss/formula/functions/TextFunction.java | 5 +++-- .../org/apache/poi/ss/formula/functions/TestTrim.java | 8 +++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/TextFunction.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/TextFunction.java index c516ce47e3..449d6d7f35 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/TextFunction.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/TextFunction.java @@ -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(" +", " ")); } }; diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTrim.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTrim.java index 58977577ff..95ed6f2583 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTrim.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTrim.java @@ -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 */