From 9d571f645cdb9d52e0d855a52007b4866ab747c3 Mon Sep 17 00:00:00 2001 From: Christiaan Bloemendaal Date: Fri, 23 Aug 2024 17:49:16 +0200 Subject: [PATCH 1/3] Replaced regex and linq search and replace with simpler implementation --- Editor/Extensions/SerializedPropertyExtensions.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Editor/Extensions/SerializedPropertyExtensions.cs b/Editor/Extensions/SerializedPropertyExtensions.cs index 731a7d3..70d4526 100644 --- a/Editor/Extensions/SerializedPropertyExtensions.cs +++ b/Editor/Extensions/SerializedPropertyExtensions.cs @@ -29,8 +29,8 @@ public static T GetValue(this SerializedProperty property) where T : class { if (fieldStructure[i].Contains("[")) { - int index = System.Convert.ToInt32(new string(fieldStructure[i].Where(char.IsDigit).ToArray())); - obj = GetFieldValueWithIndex(rgx.Replace(fieldStructure[i], ""), obj, index); + ParseFieldStructure(fieldStructure[i], out string fieldName, out int index); + obj = GetFieldValueWithIndex(fieldName, obj, index); } else { @@ -41,6 +41,14 @@ public static T GetValue(this SerializedProperty property) where T : class return (T)obj; } + private static void ParseFieldStructure(string input, out string fieldName, out int index) + { + int startIndex = input.IndexOf('['); + int endIndex = input.IndexOf(']'); + index = int.Parse(input.Substring(startIndex + 1, endIndex - startIndex - 1)); + fieldName = input.Substring(0, startIndex) + input.Substring(endIndex + 1); + } + public static bool SetValue(this SerializedProperty property, T value) where T : class { object obj = property.serializedObject.targetObject; From 8de629a6521d61b70401052f6890f0f9934818f8 Mon Sep 17 00:00:00 2001 From: Christiaan Bloemendaal Date: Sat, 24 Aug 2024 11:43:26 +0200 Subject: [PATCH 2/3] Removed creation of Regex --- Editor/Extensions/SerializedPropertyExtensions.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Editor/Extensions/SerializedPropertyExtensions.cs b/Editor/Extensions/SerializedPropertyExtensions.cs index 70d4526..e04822c 100644 --- a/Editor/Extensions/SerializedPropertyExtensions.cs +++ b/Editor/Extensions/SerializedPropertyExtensions.cs @@ -24,7 +24,6 @@ public static T GetValue(this SerializedProperty property) where T : class object obj = property.serializedObject.targetObject; string path = property.propertyPath.Replace(".Array.data", ""); string[] fieldStructure = path.Split('.'); - Regex rgx = new Regex(@"\[\d+\]"); for (int i = 0; i < fieldStructure.Length; i++) { if (fieldStructure[i].Contains("[")) From 88de546bf6235cbf887757a94ed9ce2145b95dd2 Mon Sep 17 00:00:00 2001 From: Christiaan Bloemendaal Date: Sat, 24 Aug 2024 11:49:57 +0200 Subject: [PATCH 3/3] Refactored method to TryParse --- .../Extensions/SerializedPropertyExtensions.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Editor/Extensions/SerializedPropertyExtensions.cs b/Editor/Extensions/SerializedPropertyExtensions.cs index e04822c..fbc75c8 100644 --- a/Editor/Extensions/SerializedPropertyExtensions.cs +++ b/Editor/Extensions/SerializedPropertyExtensions.cs @@ -26,9 +26,8 @@ public static T GetValue(this SerializedProperty property) where T : class string[] fieldStructure = path.Split('.'); for (int i = 0; i < fieldStructure.Length; i++) { - if (fieldStructure[i].Contains("[")) + if (TryParseFieldStructure(fieldStructure[i], out string fieldName, out int index)) { - ParseFieldStructure(fieldStructure[i], out string fieldName, out int index); obj = GetFieldValueWithIndex(fieldName, obj, index); } else @@ -40,12 +39,20 @@ public static T GetValue(this SerializedProperty property) where T : class return (T)obj; } - private static void ParseFieldStructure(string input, out string fieldName, out int index) + private static bool TryParseFieldStructure(string input, out string fieldName, out int index) { - int startIndex = input.IndexOf('['); - int endIndex = input.IndexOf(']'); + int endIndex = input.LastIndexOf(']'); + if (endIndex == -1) + { + fieldName = null; + index = -1; + return false; + } + + int startIndex = input.LastIndexOf('[', endIndex - 1); index = int.Parse(input.Substring(startIndex + 1, endIndex - startIndex - 1)); fieldName = input.Substring(0, startIndex) + input.Substring(endIndex + 1); + return true; } public static bool SetValue(this SerializedProperty property, T value) where T : class