Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Commit

Permalink
Improve escape() method by removing new-lines and return characters
Browse files Browse the repository at this point in the history
  • Loading branch information
Seyed Mohammad Ghaffarian committed Oct 7, 2019
1 parent 3056e2f commit b2af075
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/main/java/ghaffarian/progex/utils/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,28 @@ public static String escape(String code) {
// Match and replace all string literals
//String escapedCode = code.replaceAll("\".*?\"", "STR");
String escapedCode = code.replaceAll("\"", "'");
escapedCode = escapedCode.replaceAll("\n", " ");
escapedCode = escapedCode.replaceAll("\r", " ");
// Match and replace all numeric literals
//escapedCode = escapedCode.replaceAll("\\b-?\\d+(.\\d+)*\\b", "$NUM");
// Match and escape all backslashes
escapedCode = escapedCode.replaceAll("\\\\", "\\\\\\\\");
return escapedCode;
//
return removeConsecutiveSpaces(escapedCode);
}

/**
* Replaces all occurrences of 3 spaces with a single space.
* The resulting string will only have runs of at most two spaces.
*/
public static String removeConsecutiveSpaces(String str) {
String before;
do {
before = str;
str = str.replace(" ", " ");
} while (!before.equals(str));
return str;
}

/**
* Returns a string which replaces all double-quotes (") chars with the escaped version (\").
Expand Down

0 comments on commit b2af075

Please sign in to comment.