changeset 66:f7e17cfb35f9

minor toString() improvement git-svn-id: https://luan-java.googlecode.com/svn/trunk@67 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 22 Jan 2013 00:00:27 +0000
parents 1ff53a88579a
children 8ca58ab6919b
files src/luan/Luan.java src/luan/LuanState.java src/luan/LuanTable.java
diffstat 3 files changed, 18 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/Luan.java	Thu Jan 17 23:09:50 2013 +0000
+++ b/src/luan/Luan.java	Tue Jan 22 00:00:27 2013 +0000
@@ -1,7 +1,7 @@
 package luan;
 
 
-public class Luan {
+public final class Luan {
 	public static final String version = "Luan 0.0";
 
 	public static String type(Object obj) {
@@ -77,4 +77,18 @@
 		int i = n.intValue();
 		return i==n.doubleValue() ? Integer.valueOf(i) : null;
 	}
+
+	public static String toString(Object obj) {
+		if( obj == null )
+			return "nil";
+		if( obj instanceof Number )
+			return Luan.toString((Number)obj);
+		if( obj instanceof LuanException ) {
+			LuanException le = (LuanException)obj;
+			return le.getMessage();
+		}
+		return obj.toString();
+	}
+
+	private Luan() {}  // never
 }
--- a/src/luan/LuanState.java	Thu Jan 17 23:09:50 2013 +0000
+++ b/src/luan/LuanState.java	Tue Jan 22 00:00:27 2013 +0000
@@ -95,15 +95,7 @@
 		LuanFunction fn = getHandlerFunction(el,"__tostring",obj);
 		if( fn != null )
 			return checkString( el, Luan.first( call(fn,el,"__tostring",obj) ) );
-		if( obj == null )
-			return "nil";
-		if( obj instanceof Number )
-			return Luan.toString((Number)obj);
-		if( obj instanceof LuanException ) {
-			LuanException le = (LuanException)obj;
-			return le.getMessage();
-		}
-		return obj.toString();
+		return Luan.toString(obj);
 	}
 
 	public final LuanFunction getHandlerFunction(LuanElement el,String op,Object obj) throws LuanException {
--- a/src/luan/LuanTable.java	Thu Jan 17 23:09:50 2013 +0000
+++ b/src/luan/LuanTable.java	Tue Jan 22 00:00:27 2013 +0000
@@ -98,7 +98,7 @@
 					}
 					if( gotNull )
 						sb.append(i+1).append('=');
-					sb.append(obj);
+					sb.append(Luan.toString(obj));
 				}
 			}
 		}
@@ -109,7 +109,7 @@
 				} else {
 					sb.append(", ");
 				}
-				sb.append(entry.getKey()).append('=').append(entry.getValue());
+				sb.append(Luan.toString(entry.getKey())).append('=').append(Luan.toString(entry.getValue()));
 			}
 		}
 		sb.append('}');