You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2020/08/10 04:44:30 UTC

[GitHub] [commons-lang] XenoAmess commented on a change in pull request #565: [LANG-1576] refine StringUtils.chomp

XenoAmess commented on a change in pull request #565:
URL: https://github.com/apache/commons-lang/pull/565#discussion_r467685564



##########
File path: src/main/java/org/apache/commons/lang3/StringUtils.java
##########
@@ -697,27 +697,29 @@ public static String center(String str, final int size, String padStr) {
      * @return String without newline, {@code null} if null String input
      */
     public static String chomp(final String str) {
-        if (isEmpty(str)) {
+        final int length = length(str);
+
+        if (length == 0) {
             return str;
         }
 
-        if (str.length() == 1) {
+        if (length == 1) {
             final char ch = str.charAt(0);
             if (ch == CharUtils.CR || ch == CharUtils.LF) {
                 return EMPTY;
             }
             return str;
         }
 
-        int lastIdx = str.length() - 1;
+        int lastIdx = length - 1;
         final char last = str.charAt(lastIdx);
 
-        if (last == CharUtils.LF) {
-            if (str.charAt(lastIdx - 1) == CharUtils.CR) {
-                lastIdx--;
+        if (last != CharUtils.LF) {
+            if (last != CharUtils.CR) {
+                return str;
             }
-        } else if (last != CharUtils.CR) {
-            lastIdx++;
+        } else if (str.charAt(lastIdx - 1) == CharUtils.CR) {
+            --lastIdx;

Review comment:
       > Better to use lastIdx-- here - don't make unnecessary changes
   
   @sebbASF OK




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org