changeset 1550:0dc3be25ad20

use_long_strings
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 03 Oct 2020 23:08:36 -0600
parents 41c32da4cbd1
children 9cc4cee39b8b
files src/luan/modules/BasicLuan.java src/luan/modules/parsers/LuanToString.java
diffstat 2 files changed, 24 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
diff -r 41c32da4cbd1 -r 0dc3be25ad20 src/luan/modules/BasicLuan.java
--- a/src/luan/modules/BasicLuan.java	Sat Oct 03 20:55:26 2020 -0600
+++ b/src/luan/modules/BasicLuan.java	Sat Oct 03 23:08:36 2020 -0600
@@ -221,6 +221,9 @@
 			Boolean compressed = Utils.removeBoolean(options,"compressed");
 			if( compressed != null )
 				lts.compressed = compressed;
+			Boolean useLongStrings = Utils.removeBoolean(options,"use_long_strings");
+			if( useLongStrings != null )
+				lts.useLongStrings = useLongStrings;
 			Utils.checkEmpty(options);
 		}
 		return lts.toString(obj);
diff -r 41c32da4cbd1 -r 0dc3be25ad20 src/luan/modules/parsers/LuanToString.java
--- a/src/luan/modules/parsers/LuanToString.java	Sat Oct 03 20:55:26 2020 -0600
+++ b/src/luan/modules/parsers/LuanToString.java	Sat Oct 03 23:08:36 2020 -0600
@@ -12,6 +12,7 @@
 	public boolean strict = false;
 	public boolean numberTypes = false;
 	public boolean compressed = false;
+	public boolean useLongStrings = false;
 
 	public String toString(Object obj) throws LuanException {
 		StringBuilder sb = new StringBuilder();
@@ -33,9 +34,7 @@
 			return;
 		}
 		if( obj instanceof String ) {
-			sb.append( '"' );
-			sb.append( Luan.stringEncode((String)obj) );
-			sb.append( '"' );
+			toString((String)obj,sb);
 			return;
 		}
 		if( obj instanceof LuanTable ) {
@@ -120,4 +119,23 @@
 			throw new LuanRuntimeException(e);
 		}
 	}
+
+	private void toString(String s,StringBuilder sb) {
+		if( useLongStrings && s.indexOf('\n') != -1 ) {
+			StringBuilder start = new StringBuilder("[[\n");
+			StringBuilder end = new StringBuilder("]]");
+			while( s.contains(end) ) {
+				start.insert(1,'=');
+				end.insert(1,'=');
+			}
+			sb.append(start);
+			sb.append(s);
+			sb.append(end);
+			return;
+		}
+		sb.append( '"' );
+		sb.append( Luan.stringEncode(s) );
+		sb.append( '"' );
+	}
+
 }