Prechádzať zdrojové kódy

Merge pull request #2906 from ousado/patch-1

Make haxe.macro.Printer use single quotes for format strings
Simon Krajewski 11 rokov pred
rodič
commit
3798b0e30a
1 zmenil súbory, kde vykonal 16 pridanie a 2 odobranie
  1. 16 2
      std/haxe/macro/Printer.hx

+ 16 - 2
std/haxe/macro/Printer.hx

@@ -24,6 +24,7 @@ package haxe.macro;
 
 import haxe.macro.Expr;
 using Lambda;
+using StringTools;
 
 class Printer {
 	var tabs:String;
@@ -69,9 +70,19 @@ class Printer {
 			printBinop(op)
 			+ "=";
 	}
+
+	function escapeString(s:String,delim:String) {
+		return delim + s.replace("\n","\\n").replace("\t","\\t").replace("'","\\'").replace('"',"\\\"") #if sys .replace("\x00","\\x00") #end + delim;
+	}
+
+	public function printFormatString(s:String) {
+		return escapeString(s,"'");
+	}
+
 	public function printString(s:String) {
-		return '"' + s.split("\n").join("\\n").split("\t").join("\\t").split("'").join("\\'").split('"').join("\\\"") #if sys .split("\x00").join("\\x00") #end + '"';
+		return escapeString(s,'"');
 	}
+
 	public function printConstant(c:Constant) return switch(c) {
 		case CString(s): printString(s);
 		case CIdent(s),
@@ -150,6 +161,9 @@ class Printer {
 
 
 	public function printExpr(e:Expr) return e == null ? "#NULL" : switch(e.expr) {
+		#if macro
+		case EConst(CString(s)): haxe.macro.MacroStringTools.isFormatExpr(e) ? printFormatString(s) : printString(s);
+		#end
 		case EConst(c): printConstant(c);
 		case EArray(e1, e2): '${printExpr(e1)}[${printExpr(e2)}]';
 		case EBinop(op, e1, e2): '${printExpr(e1)} ${printBinop(op)} ${printExpr(e2)}';
@@ -292,4 +306,4 @@ class Printer {
 	}
 
 	function opt<T>(v:T, f:T->String, prefix = "") return v == null ? "" : (prefix + f(v));
-}
+}