ppkarwasz commented on PR #1398:
URL: https://github.com/apache/commons-lang/pull/1398#issuecomment-3018002492

   Hi @trungpnt-byte,
   
   Thank you for providing these examples.
   
   > These are the malformed values I've encountered while working on my 
company products - which allow free-text inputs:
   
   I agree that handling malformed or inconsistent user input is a common 
challenge.
   
   > For general strings
   > 
   > 1. `"This is       a delivery    comment"`
   > 2. `"Please   call  Mr.  John  on arrival"`
   
   In these cases, collapsing multiple consecutive whitespace characters into a 
single space is a sensible and common need, as extra spaces are **difficult to 
notice** and typically unintentional. However, 
[`StringUtils.normalizeSpace`](https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#normalizeSpace%28java.lang.String%29)
 already handles this case effectively.
   
   `normalizeSpace` is more general than the proposed `repeatChars()`, as it 
collapses sequences of **any** ASCII whitespace character—such as spaces, tabs, 
and newlines—into a single space. In contrast, `repeatChars()` would not 
collapse mixed whitespace sequences (e.g., `" \t\n"`).
   
   > 3. `"{prev_     loan} + 10 *      2"`
   >     if we need to sanitize the above string nicely - calling 
`repeatChars("{prev_     loan} + 10 *      2", repeatOn = <space>, reps = 0)` 
will output  `{prev_loan}+10*2`
   
   This is a good illustration of a scenario where whitespace needs to be 
removed entirely. A similar issue I’ve personally encountered was with IBAN 
inputs from banking UIs that included unexpected spaces, which caused 
validation to fail. In this case, however, 
[`StringUtils.deleteWhitespace`](https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#deleteWhitespace%28java.lang.String%29)
 is the most suitable utility.
   
   > For specific values such as longitude / latitude
   > 
   > 4. `"0...323"` , etc // we want this to be `0.323`  => _**repeat the dot 
char only 1, only if dot chars are consecutive.**_
   
   This seems less like a generic string manipulation case and more like 
**domain-specific sanitization**. Unlike whitespace, repeated `.` characters 
are visibl, and I’m not sure it's common enough to justify general-purpose 
support in `StringUtils`.
   
   > To explain a bit regarding the `repeatChars()`, In those malformed values, 
the whitespaces appear consecutive ( at least 2 ) to be resized down to 1, so 
my mind kind of **went to Mars to generalize everything** into the 
`repeatChars()` - it means it will rebuild a new string that includes the 
appointed characters to be repeated [x] times if they are consecutive ( 
configurable ).
   
   I appreciate the idea of a configurable utility like `repeatChars()`, and 
it’s a creative generalization. That said, I believe this approach may be 
**overgeneralizing**. In practice, I can only think of whitespace (and possibly 
a few other niche cases) where collapsing repeated characters is useful. I 
haven't seen a clear use case where collapsing any arbitrary character sequence 
to a configurable count is necessary, or where such logic wouldn't be better 
handled specifically within the consuming application.
   
   For that reason, I’m hesitant to introduce this as part of the public API in 
`StringUtils`.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to