changeset 1552:46d4baaad64d

add long_string_keys
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 04 Oct 2020 20:26:49 -0600
parents 9cc4cee39b8b
children 26c51acf00f3
files src/luan/modules/BasicLuan.java src/luan/modules/parsers/LuanToString.java
diffstat 2 files changed, 20 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/modules/BasicLuan.java	Sun Oct 04 16:29:54 2020 -0600
+++ b/src/luan/modules/BasicLuan.java	Sun Oct 04 20:26:49 2020 -0600
@@ -8,6 +8,7 @@
 import java.util.List;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashSet;
 import goodjava.json.JsonToString;
 import luan.Luan;
 import luan.LuanTable;
@@ -224,6 +225,9 @@
 			Boolean useLongStrings = Utils.removeBoolean(options,"use_long_strings");
 			if( useLongStrings != null )
 				lts.useLongStrings = useLongStrings;
+			LuanTable longStringKeys = Utils.removeTable(options,"long_string_keys");
+			if( longStringKeys != null )
+				lts.longStringKeys = new HashSet(longStringKeys.asList());
 			Utils.checkEmpty(options);
 		}
 		return lts.toString(obj);
--- a/src/luan/modules/parsers/LuanToString.java	Sun Oct 04 16:29:54 2020 -0600
+++ b/src/luan/modules/parsers/LuanToString.java	Sun Oct 04 20:26:49 2020 -0600
@@ -2,6 +2,8 @@
 
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
+import java.util.Collections;
 import luan.Luan;
 import luan.LuanTable;
 import luan.LuanException;
@@ -13,14 +15,15 @@
 	public boolean numberTypes = false;
 	public boolean compressed = false;
 	public boolean useLongStrings = false;
+	public Set longStringKeys = Collections.emptySet();
 
 	public String toString(Object obj) throws LuanException {
 		StringBuilder sb = new StringBuilder();
-		toString(obj,sb,0);
+		toString(obj,sb,0,useLongStrings);
 		return sb.toString();
 	}
 
-	private void toString(Object obj,StringBuilder sb,int indented) throws LuanException {
+	private void toString(Object obj,StringBuilder sb,int indented,boolean longStrings) throws LuanException {
 		if( obj == null ) {
 			sb.append( "nil" );
 			return;
@@ -34,11 +37,11 @@
 			return;
 		}
 		if( obj instanceof String ) {
-			toString((String)obj,sb);
+			toString((String)obj,sb,longStrings);
 			return;
 		}
 		if( obj instanceof LuanTable ) {
-			toString((LuanTable)obj,sb,indented);
+			toString((LuanTable)obj,sb,indented,longStrings);
 			return;
 		}
 		if( strict )
@@ -48,7 +51,7 @@
 		sb.append( '>' );
 	}
 
-	private void toString(LuanTable tbl,StringBuilder sb,int indented) throws LuanException {
+	private void toString(LuanTable tbl,StringBuilder sb,int indented,boolean longStrings) throws LuanException {
 		List list = tbl.asList();
 		Map map = tbl.rawMap();
 		sb.append( '{' );
@@ -60,7 +63,7 @@
 				first = false;
 			else
 				sb.append( ',' );
-			toString(obj,sb,indented+1);
+			toString(obj,sb,indented+1,longStrings);
 		}
 		for( Object obj : map.entrySet() ) {
 			Map.Entry entry = (Map.Entry)obj;
@@ -84,11 +87,11 @@
 			sb.append( (String)key );
 		} else {
 			sb.append( '[' );
-			toString( key, sb, indented );
+			toString( key, sb, indented, false );
 			sb.append( ']' );
 		}
 		sb.append( compressed ? "=" : " = " );
-		toString( entry.getValue(), sb, indented );
+		toString( entry.getValue(), sb, indented, longStringKeys.contains(key) );
 	}
 
 	private void indent(StringBuilder sb,int indented) {
@@ -120,9 +123,11 @@
 		}
 	}
 
-	private void toString(String s,StringBuilder sb) {
-		if( useLongStrings && s.indexOf('\n') != -1 ) {
-			StringBuilder start = new StringBuilder("[[\n");
+	private void toString(String s,StringBuilder sb,boolean longStrings) {
+		if( longStrings ) {
+			StringBuilder start = new StringBuilder("[[");
+			if( s.indexOf('\n') != -1 )
+				start.append('\n');
 			StringBuilder end = new StringBuilder("]]");
 			while( s.contains(end) ) {
 				start.insert(1,'=');