diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/StringExtensions.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/StringExtensions.cs index 39f641505d0..8905e996c04 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/StringExtensions.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/StringExtensions.cs @@ -29,12 +29,7 @@ public static Vector2 ToVector2(this string text) if (text.Length > 0) { // The format or is supported - if (text.Length >= 2 && - text[0] == '>' && - text[text.Length - 1] == '>') - { - text = text.Substring(1, text.Length - 2); - } + text = Unbracket(text); // Skip allocations when only a component is used if (text.IndexOf(',') == -1) @@ -78,12 +73,7 @@ public static Vector3 ToVector3(this string text) { if (text.Length > 0) { - if (text.Length >= 2 && - text[0] == '>' && - text[text.Length - 1] == '>') - { - text = text.Substring(1, text.Length - 2); - } + text = Unbracket(text); if (text.IndexOf(',') == -1) { @@ -127,12 +117,7 @@ public static Vector4 ToVector4(this string text) { if (text.Length > 0) { - if (text.Length >= 2 && - text[0] == '>' && - text[text.Length - 1] == '>') - { - text = text.Substring(1, text.Length - 2); - } + text = Unbracket(text); if (text.IndexOf(',') == -1) { @@ -176,12 +161,7 @@ public static Quaternion ToQuaternion(this string text) { if (text.Length > 0) { - if (text.Length >= 2 && - text[0] == '>' && - text[text.Length - 1] == '>') - { - text = text.Substring(1, text.Length - 2); - } + text = Unbracket(text); string[] values = text.Split(','); @@ -201,5 +181,23 @@ public static Quaternion ToQuaternion(this string text) static Quaternion Throw(string text) => throw new FormatException($"Cannot convert \"{text}\" to {nameof(Quaternion)}. Use the format \"float, float, float, float\""); } + + /// + /// Converts an angle bracketed value to its unbracketed form (e.g. "<float, float>" to "float, float"). + /// If the value is already unbracketed, this method will return the value unchanged. + /// + /// A bracketed value. + /// The unbracketed value. + private static string Unbracket(string text) + { + if (text.Length >= 2 && + text[0] == '<' && + text[text.Length - 1] == '>') + { + text = text.Substring(1, text.Length - 2); + } + + return text; + } } }