File tree Expand file tree Collapse file tree 2 files changed +31
-1
lines changed
src/main/java/pascal/taie Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change 2424
2525import pascal .taie .World ;
2626import pascal .taie .language .type .ClassType ;
27+ import pascal .taie .util .Strings ;
2728
2829import static pascal .taie .language .classes .ClassNames .STRING ;
2930
@@ -75,6 +76,6 @@ public int hashCode() {
7576
7677 @ Override
7778 public String toString () {
78- return "\" " + value + "\" " ;
79+ return "\" " + Strings . escape ( value ) + "\" " ;
7980 }
8081}
Original file line number Diff line number Diff line change @@ -51,4 +51,33 @@ public static String capitalize(String str) {
5151 return new String (chars );
5252 }
5353
54+ /**
55+ * Escapes special characters in a given string by replacing them with
56+ * their corresponding escape sequences.
57+ * If the input string is null or empty, the method returns it unchanged.
58+ *
59+ * @param str the input string to be escaped
60+ * @return a string with special characters replaced by their corresponding
61+ * escape sequences, or the original string if it is null or empty
62+ */
63+ public static String escape (String str ) {
64+ if (str == null || str .isEmpty ()) {
65+ return str ;
66+ }
67+ StringBuilder buffer = new StringBuilder ();
68+ for (int i = 0 ; i < str .length (); i ++) {
69+ char c = str .charAt (i );
70+ switch (c ) {
71+ case '\\' -> buffer .append ("\\ \\ " );
72+ case '\"' -> buffer .append ("\\ \" " );
73+ case '\b' -> buffer .append ("\\ b" );
74+ case '\t' -> buffer .append ("\\ t" );
75+ case '\n' -> buffer .append ("\\ n" );
76+ case '\f' -> buffer .append ("\\ f" );
77+ case '\r' -> buffer .append ("\\ r" );
78+ default -> buffer .append (c );
79+ }
80+ }
81+ return buffer .toString ();
82+ }
5483}
You can’t perform that action at this time.
0 commit comments