Mercurial Hosting > luan
diff src/luan/LuanTable.java @ 88:6ca02b188dba
add LuanBit to clean up code;
add repr();
git-svn-id: https://luan-java.googlecode.com/svn/trunk@89 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Wed, 27 Feb 2013 23:50:32 +0000 |
parents | 6db8f286fa6c |
children | 3c404a296995 |
line wrap: on
line diff
--- a/src/luan/LuanTable.java Wed Feb 27 19:42:09 2013 +0000 +++ b/src/luan/LuanTable.java Wed Feb 27 23:50:32 2013 +0000 @@ -11,9 +11,10 @@ import java.util.Set; import java.util.HashSet; import java.util.IdentityHashMap; +import java.util.regex.Pattern; -public final class LuanTable implements DeepCloneable<LuanTable>, Iterable<Map.Entry<Object,Object>> { +public final class LuanTable implements DeepCloneable<LuanTable>, Iterable<Map.Entry<Object,Object>>, LuanRepr { private Map<Object,Object> map = null; private List<Object> list = null; private LuanTable metatable = null; @@ -105,13 +106,13 @@ return "table: " + Integer.toHexString(hashCode()); } - public String show() { - return show( Collections.newSetFromMap(new IdentityHashMap<LuanTable,Boolean>()) ); + public String repr() { + return repr( Collections.newSetFromMap(new IdentityHashMap<LuanTable,Boolean>()) ); } - private String show(Set<LuanTable> set) { + private String repr(Set<LuanTable> set) { if( !set.add(this) ) { - return "..."; + return "\"<circular reference>\""; } StringBuilder sb = new StringBuilder(); sb.append('{'); @@ -130,7 +131,7 @@ } if( gotNull ) sb.append(i+1).append('='); - sb.append(show(set,obj)); + sb.append(repr(set,obj)); } } } @@ -141,19 +142,33 @@ } else { sb.append(", "); } - sb.append(show(set,entry.getKey())).append('=').append(show(set,entry.getValue())); + sb.append(reprKey(set,entry.getKey())).append('=').append(repr(set,entry.getValue())); } } sb.append('}'); return sb.toString(); } - private static String show(Set<LuanTable> set,Object obj) { + private static final Pattern namePtn = Pattern.compile("[a-zA-Z_][a-zA-Z_0-9]*"); + + private static String reprKey(Set<LuanTable> set,Object obj) { + if( obj instanceof String ) { + String s = (String)obj; + if( namePtn.matcher(s).matches() ) + return s; + } + return "[" + repr(set,obj) + "]"; + } + + private static String repr(Set<LuanTable> set,Object obj) { if( obj instanceof LuanTable ) { LuanTable t = (LuanTable)obj; - return t.show(set); + return t.repr(set); } else { - return Luan.toString(obj); + String s = Luan.repr(obj); + if( s == null ) + s = "\"<couldn't repr: " + Luan.stringEncode(Luan.toString(obj)) + ">\""; + return s; } }