kevdoran commented on a change in pull request #5458:
URL: https://github.com/apache/nifi/pull/5458#discussion_r737576297



##########
File path: 
nifi-commons/nifi-properties/src/main/java/org/apache/nifi/util/StringUtils.java
##########
@@ -510,4 +514,27 @@ public static String toTitleCase(String input) {
                 .map(word -> Character.toTitleCase(word.charAt(0)) + 
word.substring(1))
                 .collect(Collectors.joining(" "));
     }
+
+    /**
+     * Escape {@code str} by replacing occurrences of {@code charToEscape} 
with {@code escapeChar+charToEscape}
+     * @param str the input string
+     * @param escapeChar the character used for escaping
+     * @param charToEscape the character that needs to be escaped
+     * @return the escaped string
+     */
+    public static String escapeString(String str, char escapeChar, char 
charToEscape) {
+        if (str == null || str.isEmpty()) {
+            return null;
+        }
+        StringBuilder result = new StringBuilder();
+        for (int i=0; i<str.length(); i++) {
+            char curChar = str.charAt(i);
+            if (curChar == escapeChar || curChar == charToEscape) {
+                // special char
+                result.append(escapeChar);
+            }
+            result.append(curChar);

Review comment:
       Minor, but it would be nice if this utility method were idempotent, 
meaning that if I pass it a string that contains some already escaped 
characters, it has not effect on those. In other words, all of these 
invocations would return the same result:
   
   ```
   StringUtils.escapeString("key=(value0,value1,value2)", '\\', ',');
   StringUtils.escapeString("key=(value0\\,value1,value2)", '\\', ',');
   StringUtils.escapeString("key=(value0\\,value1\\,value2)", '\\', ',');
   ```
   




-- 
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: issues-unsubscr...@nifi.apache.org

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


Reply via email to