This is an automated email from the ASF dual-hosted git repository.

hansva pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/hop.git


The following commit(s) were added to refs/heads/main by this push:
     new c34c3b0f12 replace custom trimming of only spaces by standard trimming 
(#7349)
c34c3b0f12 is described below

commit c34c3b0f127011a5b2896f55b2bcf7adab90ed02
Author: PhilipCubix <[email protected]>
AuthorDate: Mon Jun 29 15:27:21 2026 +0200

    replace custom trimming of only spaces by standard trimming (#7349)
    
    * replace custom trimming of only spaces by standard trimming
    
    standardized trimming function trims also other whitespace characters
    
    * Add a test
    
    ---------
    
    Co-authored-by: Hans Van Akelyen <[email protected]>
---
 .../apache/hop/core/row/value/ValueMetaBase.java   | 34 +-------------
 .../hop/core/row/value/ValueMetaBaseTest.java      | 52 ++++++++++++++++++++++
 2 files changed, 54 insertions(+), 32 deletions(-)

diff --git 
a/core/src/main/java/org/apache/hop/core/row/value/ValueMetaBase.java 
b/core/src/main/java/org/apache/hop/core/row/value/ValueMetaBase.java
index 8ce22730d2..3cdbeef757 100644
--- a/core/src/main/java/org/apache/hop/core/row/value/ValueMetaBase.java
+++ b/core/src/main/java/org/apache/hop/core/row/value/ValueMetaBase.java
@@ -4544,38 +4544,8 @@ public class ValueMetaBase implements IValueMeta {
       }
     }
 
-    // Trimming
-    StringBuilder strpol;
-    switch (trimType) {
-      case IValueMeta.TRIM_TYPE_LEFT:
-        strpol = new StringBuilder(pol);
-        while (!strpol.isEmpty() && strpol.charAt(0) == ' ') {
-          strpol.deleteCharAt(0);
-        }
-        pol = strpol.toString();
-
-        break;
-      case IValueMeta.TRIM_TYPE_RIGHT:
-        strpol = new StringBuilder(pol);
-        while (!strpol.isEmpty() && strpol.charAt(strpol.length() - 1) == ' ') 
{
-          strpol.deleteCharAt(strpol.length() - 1);
-        }
-        pol = strpol.toString();
-
-        break;
-      case IValueMeta.TRIM_TYPE_BOTH:
-        strpol = new StringBuilder(pol);
-        while (!strpol.isEmpty() && strpol.charAt(0) == ' ') {
-          strpol.deleteCharAt(0);
-        }
-        while (!strpol.isEmpty() && strpol.charAt(strpol.length() - 1) == ' ') 
{
-          strpol.deleteCharAt(strpol.length() - 1);
-        }
-        pol = strpol.toString();
-        break;
-      default:
-        break;
-    }
+    // Trim if needed
+    pol = Const.trimToType(pol, trimType);
 
     // On with the regular program...
     // Simply call the ValueMeta routines to do the conversion
diff --git 
a/core/src/test/java/org/apache/hop/core/row/value/ValueMetaBaseTest.java 
b/core/src/test/java/org/apache/hop/core/row/value/ValueMetaBaseTest.java
index eb8cdade3f..b61a2cdf17 100644
--- a/core/src/test/java/org/apache/hop/core/row/value/ValueMetaBaseTest.java
+++ b/core/src/test/java/org/apache/hop/core/row/value/ValueMetaBaseTest.java
@@ -350,6 +350,58 @@ class ValueMetaBaseTest {
             + "Conversion from null string must return empty string");
   }
 
+  @Test
+  void testConvertDataFromStringTrimsWhitespacePerTrimType() throws 
HopValueException {
+    ValueMetaBase inMeta = new ValueMetaString();
+    ValueMetaBase outMeta = new ValueMetaString();
+    // Leading/trailing mix of space and tab around the actual content.
+    String input = " \tHELLO\t ";
+
+    // BOTH strips leading and trailing whitespace.
+    assertEquals(
+        "HELLO",
+        outMeta.convertDataFromString(input, inMeta, null, null, 
IValueMeta.TRIM_TYPE_BOTH));
+
+    // LEFT strips only leading whitespace, trailing is preserved.
+    assertEquals(
+        "HELLO\t ",
+        outMeta.convertDataFromString(input, inMeta, null, null, 
IValueMeta.TRIM_TYPE_LEFT));
+
+    // RIGHT strips only trailing whitespace, leading is preserved.
+    assertEquals(
+        " \tHELLO",
+        outMeta.convertDataFromString(input, inMeta, null, null, 
IValueMeta.TRIM_TYPE_RIGHT));
+
+    // NONE leaves the value untouched.
+    assertEquals(
+        input, outMeta.convertDataFromString(input, inMeta, null, null, 
IValueMeta.TRIM_TYPE_NONE));
+  }
+
+  @Test
+  void testConvertDataFromStringTrimsAllWhitespaceNotJustSpaces() throws 
HopValueException {
+    ValueMetaBase inMeta = new ValueMetaString();
+    ValueMetaBase outMeta = new ValueMetaString();
+
+    // Regression guard: the previous implementation trimmed the ASCII space 
(0x20) only.
+    // Tabs, carriage returns and line feeds must now be trimmed as well, 
consistent with
+    // Const.onlySpaces() which is already used for empty-value detection in 
the same method.
+    assertEquals(
+        "value",
+        outMeta.convertDataFromString(
+            "\t\r\n value \n\r\t", inMeta, null, null, 
IValueMeta.TRIM_TYPE_BOTH));
+  }
+
+  @Test
+  void testConvertDataFromStringTrimsBeforeNumericParsing() throws 
HopValueException {
+    ValueMetaBase inMeta = new ValueMetaString();
+    ValueMetaBase outMeta = new ValueMetaInteger();
+
+    // Surrounding whitespace (including tab/newline) is trimmed before the 
string is parsed.
+    assertEquals(
+        42L,
+        outMeta.convertDataFromString("\t42\n", inMeta, null, null, 
IValueMeta.TRIM_TYPE_BOTH));
+  }
+
   @Test
   void testConvertDataFromStringToDate() throws HopValueException {
     ValueMetaBase inValueMetaString = new ValueMetaString();

Reply via email to