Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 22 additions & 24 deletions Microsoft.Toolkit.Uwp.UI/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@ public static Vector2 ToVector2(this string text)
if (text.Length > 0)
{
// The format <x> or <x, y> 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)
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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(',');

Expand All @@ -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\"");
}

/// <summary>
/// Converts an angle bracketed <see cref="string"/> value to its unbracketed form (e.g. "&lt;float, float&gt;" to "float, float").
/// If the value is already unbracketed, this method will return the value unchanged.
/// </summary>
/// <param name="text">A bracketed <see cref="string"/> value.</param>
/// <returns>The unbracketed <see cref="string"/> value.</returns>
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;
}
}
}