From 34c8ad364681fbe369db572805c16cf479401476 Mon Sep 17 00:00:00 2001 From: Sinai Date: Tue, 11 May 2021 02:38:24 +1000 Subject: [PATCH] Make PruneString helper --- src/Core/Utility/ToStringUtility.cs | 46 +++++++++++++++++++-------- src/UI/CacheObject/CacheObjectBase.cs | 4 +-- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/Core/Utility/ToStringUtility.cs b/src/Core/Utility/ToStringUtility.cs index f580d75..1559093 100644 --- a/src/Core/Utility/ToStringUtility.cs +++ b/src/Core/Utility/ToStringUtility.cs @@ -23,6 +23,29 @@ namespace UnityExplorer private const string eventSystemNamespace = "UnityEngine.EventSystem"; + public static string PruneString(string s, int chars = 200, int lines = 5) + { + if (string.IsNullOrEmpty(s)) + return s; + + var sb = new StringBuilder(Math.Max(chars, s.Length)); + int newlines = 0; + for (int i = 0; i < s.Length; i++) + { + if (newlines >= lines || i >= chars) + { + sb.Append("..."); + break; + } + char c = s[i]; + if (c == '\r' || c == '\n') + newlines++; + sb.Append(c); + } + + return sb.ToString(); + } + public static string ToStringWithType(object value, Type fallbackType, bool includeNamespace = true) { if (value.IsNullOrDestroyed() && fallbackType == null) @@ -52,13 +75,15 @@ namespace UnityExplorer if (value is UnityEngine.Object obj) { - var name = obj.name; - if (string.IsNullOrEmpty(name)) - name = untitledString; - else if (name.Length > 50) - name = $"{name.Substring(0, 50)}..."; - - sb.Append($"\"{name}\""); + if (string.IsNullOrEmpty(obj.name)) + sb.Append(untitledString); + else + { + sb.Append('"'); + sb.Append(PruneString(obj.name, 50, 1)); + sb.Append('"'); + } + AppendRichType(sb, richType); } else if (type.FullName.StartsWith(eventSystemNamespace)) @@ -79,12 +104,7 @@ namespace UnityExplorer } else // the ToString contains some actual implementation, use that value. { - // prune long strings unless they're unity structs - // (Matrix4x4 and Rect can have some longs ones that we want to display fully) - if (toString.Length > 100 && !(type.IsValueType && type.FullName.StartsWith("UnityEngine"))) - sb.Append(toString.Substring(0, 100)); - else - sb.Append(toString); + sb.Append(PruneString(toString, 200, 5)); AppendRichType(sb, richType); } diff --git a/src/UI/CacheObject/CacheObjectBase.cs b/src/UI/CacheObject/CacheObjectBase.cs index ee71abe..c710c0b 100644 --- a/src/UI/CacheObject/CacheObjectBase.cs +++ b/src/UI/CacheObject/CacheObjectBase.cs @@ -219,9 +219,7 @@ namespace UnityExplorer.UI.CacheObject if (!LastValueWasNull) { string s = Value as string; - if (s.Length > 200) - s = $"{s.Substring(0, 200)}..."; - return $"\"{s}\""; + return $"\"{ToStringUtility.PruneString(s, 200, 5)}\""; } break;