From 18d4136e3a3629c3fc000f5046635ad6b73fff05 Mon Sep 17 00:00:00 2001 From: Par Winzell Date: Thu, 8 May 2008 19:45:39 +0000 Subject: [PATCH] Simplistic support for cyclic references so this method can be used without fear. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5053 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/as/com/threerings/util/StringUtil.as | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/as/com/threerings/util/StringUtil.as b/src/as/com/threerings/util/StringUtil.as index 6935977a5..cf31f06de 100644 --- a/src/as/com/threerings/util/StringUtil.as +++ b/src/as/com/threerings/util/StringUtil.as @@ -22,6 +22,7 @@ package com.threerings.util { import flash.utils.ByteArray; +import flash.utils.Dictionary; import com.threerings.util.env.Environment; @@ -276,13 +277,19 @@ public class StringUtil /** * Nicely format the specified object into a String. */ - public static function toString (obj :*) :String + public static function toString (obj :*, refs :Dictionary = null) :String { if (obj == null) { // checks null or undefined return String(obj); } - // TODO: this should be able to detect circular references + if (refs == null) { + refs = new Dictionary(); + + } else if (refs[obj] !== undefined) { + return "[cyclic reference]"; + } + refs[obj] = true; var s :String; if (obj is Array) { @@ -292,7 +299,7 @@ public class StringUtil if (ii > 0) { s += ", "; } - s += (ii + ": " + toString(arr[ii])); + s += (ii + ": " + toString(arr[ii], refs)); } return "Array(" + s + ")"; @@ -303,7 +310,7 @@ public class StringUtil if (s.length > 0) { s += ", "; } - s += prop + "=>" + toString(obj[prop]); + s += prop + "=>" + toString(obj[prop], refs); } return "Object(" + s + ")";