Browse Source

Added addSlashes function to StringTools

Tom Bebbington 12 years ago
parent
commit
d078ca33e4
2 changed files with 7 additions and 7 deletions
  1. 6 0
      std/StringTools.hx
  2. 1 7
      std/haxe/macro/Printer.hx

+ 6 - 0
std/StringTools.hx

@@ -30,6 +30,12 @@
 @:keep
 #end
 class StringTools {
+	/**
+		Returns a string with backslashes before characters that need to be quoted.
+	**/
+	public static function addSlashes( s : String ) : String {
+		return s.split("\n").join("\\n").split("\t").join("\\t").split("'").join("\\'").split('"').join("\\\"") #if sys .split("\x00").join("\\x00") #end;
+	}
 
 	/**
 		Encode an URL by using the standard format.

+ 1 - 7
std/haxe/macro/Printer.hx

@@ -69,14 +69,8 @@ class Printer {
 			printBinop(op)
 			+ "=";
 	}
-	public function printString(s:String) {
-		s = StringTools.replace(s, "\n", "\\n");
-		s = StringTools.replace(s, "\t", "\\t");
-		s = StringTools.replace(s, "\r", "\\r");
-		return '"$s"';
-	}
 	public function printConstant(c:Constant) return switch(c) {
-		case CString(s): printString(s);
+		case CString(s): '"${StringTools.addSlashes(s)}"';
 		case CIdent(s),
 			CInt(s),
 			CFloat(s):